type_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package goquery
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "testing"
  8. "golang.org/x/net/html"
  9. )
  10. // Test helper functions and members
  11. var doc *Document
  12. var doc2 *Document
  13. var doc3 *Document
  14. var docB *Document
  15. var docW *Document
  16. func Doc() *Document {
  17. if doc == nil {
  18. doc = loadDoc("page.html")
  19. }
  20. return doc
  21. }
  22. func Doc2() *Document {
  23. if doc2 == nil {
  24. doc2 = loadDoc("page2.html")
  25. }
  26. return doc2
  27. }
  28. func Doc2Clone() *Document {
  29. return CloneDocument(Doc2())
  30. }
  31. func Doc3() *Document {
  32. if doc3 == nil {
  33. doc3 = loadDoc("page3.html")
  34. }
  35. return doc3
  36. }
  37. func Doc3Clone() *Document {
  38. return CloneDocument(Doc3())
  39. }
  40. func DocB() *Document {
  41. if docB == nil {
  42. docB = loadDoc("gotesting.html")
  43. }
  44. return docB
  45. }
  46. func DocW() *Document {
  47. if docW == nil {
  48. docW = loadDoc("gowiki.html")
  49. }
  50. return docW
  51. }
  52. func assertLength(t *testing.T, nodes []*html.Node, length int) {
  53. if len(nodes) != length {
  54. t.Errorf("Expected %d nodes, found %d.", length, len(nodes))
  55. for i, n := range nodes {
  56. t.Logf("Node %d: %+v.", i, n)
  57. }
  58. }
  59. }
  60. func assertClass(t *testing.T, sel *Selection, class string) {
  61. if !sel.HasClass(class) {
  62. t.Errorf("Expected node to have class %s, found %+v.", class, sel.Get(0))
  63. }
  64. }
  65. func assertPanic(t *testing.T) {
  66. if e := recover(); e == nil {
  67. t.Error("Expected a panic.")
  68. }
  69. }
  70. func assertEqual(t *testing.T, s1 *Selection, s2 *Selection) {
  71. if s1 != s2 {
  72. t.Error("Expected selection objects to be the same.")
  73. }
  74. }
  75. func assertSelectionIs(t *testing.T, sel *Selection, is ...string) {
  76. for i := 0; i < sel.Length(); i++ {
  77. if !sel.Eq(i).Is(is[i]) {
  78. t.Errorf("Expected node %d to be %s, found %+v", i, is[i], sel.Get(i))
  79. }
  80. }
  81. }
  82. func printSel(t *testing.T, sel *Selection) {
  83. if testing.Verbose() {
  84. h, err := sel.Html()
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. t.Log(h)
  89. }
  90. }
  91. func loadDoc(page string) *Document {
  92. var f *os.File
  93. var e error
  94. if f, e = os.Open(fmt.Sprintf("./testdata/%s", page)); e != nil {
  95. panic(e.Error())
  96. }
  97. defer f.Close()
  98. var node *html.Node
  99. if node, e = html.Parse(f); e != nil {
  100. panic(e.Error())
  101. }
  102. return NewDocumentFromNode(node)
  103. }
  104. func TestNewDocument(t *testing.T) {
  105. if f, e := os.Open("./testdata/page.html"); e != nil {
  106. t.Error(e.Error())
  107. } else {
  108. defer f.Close()
  109. if node, e := html.Parse(f); e != nil {
  110. t.Error(e.Error())
  111. } else {
  112. doc = NewDocumentFromNode(node)
  113. }
  114. }
  115. }
  116. func TestNewDocumentFromReader(t *testing.T) {
  117. cases := []struct {
  118. src string
  119. err bool
  120. sel string
  121. cnt int
  122. }{
  123. 0: {
  124. src: `
  125. <html>
  126. <head>
  127. <title>Test</title>
  128. <body>
  129. <h1>Hi</h1>
  130. </body>
  131. </html>`,
  132. sel: "h1",
  133. cnt: 1,
  134. },
  135. 1: {
  136. // Actually pretty hard to make html.Parse return an error
  137. // based on content...
  138. src: `<html><body><aef<eqf>>>qq></body></ht>`,
  139. },
  140. }
  141. buf := bytes.NewBuffer(nil)
  142. for i, c := range cases {
  143. buf.Reset()
  144. buf.WriteString(c.src)
  145. d, e := NewDocumentFromReader(buf)
  146. if (e != nil) != c.err {
  147. if c.err {
  148. t.Errorf("[%d] - expected error, got none", i)
  149. } else {
  150. t.Errorf("[%d] - expected no error, got %s", i, e)
  151. }
  152. }
  153. if c.sel != "" {
  154. s := d.Find(c.sel)
  155. if s.Length() != c.cnt {
  156. t.Errorf("[%d] - expected %d nodes, found %d", i, c.cnt, s.Length())
  157. }
  158. }
  159. }
  160. }
  161. func TestNewDocumentFromResponseNil(t *testing.T) {
  162. _, e := NewDocumentFromResponse(nil)
  163. if e == nil {
  164. t.Error("Expected error, got none")
  165. }
  166. }
  167. func TestIssue103(t *testing.T) {
  168. d, err := NewDocumentFromReader(strings.NewReader("<html><title>Scientists Stored These Images in DNA—Then Flawlessly Retrieved Them</title></html>"))
  169. if err != nil {
  170. t.Error(err)
  171. }
  172. text := d.Find("title").Text()
  173. for i, r := range text {
  174. t.Logf("%d: %d - %q\n", i, r, string(r))
  175. }
  176. t.Log(text)
  177. }