property_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package goquery
  2. import (
  3. "regexp"
  4. "strings"
  5. "testing"
  6. )
  7. func TestAttrExists(t *testing.T) {
  8. if val, ok := Doc().Find("a").Attr("href"); !ok {
  9. t.Error("Expected a value for the href attribute.")
  10. } else {
  11. t.Logf("Href of first anchor: %v.", val)
  12. }
  13. }
  14. func TestAttrOr(t *testing.T) {
  15. if val := Doc().Find("a").AttrOr("fake-attribute", "alternative"); val != "alternative" {
  16. t.Error("Expected an alternative value for 'fake-attribute' attribute.")
  17. } else {
  18. t.Logf("Value returned for not existing attribute: %v.", val)
  19. }
  20. if val := Doc().Find("zz").AttrOr("fake-attribute", "alternative"); val != "alternative" {
  21. t.Error("Expected an alternative value for 'fake-attribute' on an empty selection.")
  22. } else {
  23. t.Logf("Value returned for empty selection: %v.", val)
  24. }
  25. }
  26. func TestAttrNotExist(t *testing.T) {
  27. if val, ok := Doc().Find("div.row-fluid").Attr("href"); ok {
  28. t.Errorf("Expected no value for the href attribute, got %v.", val)
  29. }
  30. }
  31. func TestRemoveAttr(t *testing.T) {
  32. sel := Doc2Clone().Find("div")
  33. sel.RemoveAttr("id")
  34. _, ok := sel.Attr("id")
  35. if ok {
  36. t.Error("Expected there to be no id attributes set")
  37. }
  38. }
  39. func TestSetAttr(t *testing.T) {
  40. sel := Doc2Clone().Find("#main")
  41. sel.SetAttr("id", "not-main")
  42. val, ok := sel.Attr("id")
  43. if !ok {
  44. t.Error("Expected an id attribute on main")
  45. }
  46. if val != "not-main" {
  47. t.Errorf("Expected an attribute id to be not-main, got %s", val)
  48. }
  49. }
  50. func TestSetAttr2(t *testing.T) {
  51. sel := Doc2Clone().Find("#main")
  52. sel.SetAttr("foo", "bar")
  53. val, ok := sel.Attr("foo")
  54. if !ok {
  55. t.Error("Expected an 'foo' attribute on main")
  56. }
  57. if val != "bar" {
  58. t.Errorf("Expected an attribute 'foo' to be 'bar', got '%s'", val)
  59. }
  60. }
  61. func TestText(t *testing.T) {
  62. txt := Doc().Find("h1").Text()
  63. if strings.Trim(txt, " \n\r\t") != "Provok.in" {
  64. t.Errorf("Expected text to be Provok.in, found %s.", txt)
  65. }
  66. }
  67. func TestText2(t *testing.T) {
  68. txt := Doc().Find(".hero-unit .container-fluid .row-fluid:nth-child(1)").Text()
  69. if ok, e := regexp.MatchString(`^\s+Provok\.in\s+Prove your point.\s+$`, txt); !ok || e != nil {
  70. t.Errorf("Expected text to be Provok.in Prove your point., found %s.", txt)
  71. if e != nil {
  72. t.Logf("Error: %s.", e.Error())
  73. }
  74. }
  75. }
  76. func TestText3(t *testing.T) {
  77. txt := Doc().Find(".pvk-gutter").First().Text()
  78. // There's an   character in there...
  79. if ok, e := regexp.MatchString(`^[\s\x{00A0}]+$`, txt); !ok || e != nil {
  80. t.Errorf("Expected spaces, found <%v>.", txt)
  81. if e != nil {
  82. t.Logf("Error: %s.", e.Error())
  83. }
  84. }
  85. }
  86. func TestHtml(t *testing.T) {
  87. txt, e := Doc().Find("h1").Html()
  88. if e != nil {
  89. t.Errorf("Error: %s.", e)
  90. }
  91. if ok, e := regexp.MatchString(`^\s*<a href="/">Provok<span class="green">\.</span><span class="red">i</span>n</a>\s*$`, txt); !ok || e != nil {
  92. t.Errorf("Unexpected HTML content, found %s.", txt)
  93. if e != nil {
  94. t.Logf("Error: %s.", e.Error())
  95. }
  96. }
  97. }
  98. func TestNbsp(t *testing.T) {
  99. src := `<p>Some&nbsp;text</p>`
  100. d, err := NewDocumentFromReader(strings.NewReader(src))
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. txt := d.Find("p").Text()
  105. ix := strings.Index(txt, "\u00a0")
  106. if ix != 4 {
  107. t.Errorf("Text: expected a non-breaking space at index 4, got %d", ix)
  108. }
  109. h, err := d.Find("p").Html()
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. ix = strings.Index(h, "\u00a0")
  114. if ix != 4 {
  115. t.Errorf("Html: expected a non-breaking space at index 4, got %d", ix)
  116. }
  117. }
  118. func TestAddClass(t *testing.T) {
  119. sel := Doc2Clone().Find("#main")
  120. sel.AddClass("main main main")
  121. // Make sure that class was only added once
  122. if a, ok := sel.Attr("class"); !ok || a != "main" {
  123. t.Error("Expected #main to have class main")
  124. }
  125. }
  126. func TestAddClassSimilar(t *testing.T) {
  127. sel := Doc2Clone().Find("#nf5")
  128. sel.AddClass("odd")
  129. assertClass(t, sel, "odd")
  130. assertClass(t, sel, "odder")
  131. printSel(t, sel.Parent())
  132. }
  133. func TestAddEmptyClass(t *testing.T) {
  134. sel := Doc2Clone().Find("#main")
  135. sel.AddClass("")
  136. // Make sure that class was only added once
  137. if a, ok := sel.Attr("class"); ok {
  138. t.Errorf("Expected #main to not to have a class, have: %s", a)
  139. }
  140. }
  141. func TestAddClasses(t *testing.T) {
  142. sel := Doc2Clone().Find("#main")
  143. sel.AddClass("a b")
  144. // Make sure that class was only added once
  145. if !sel.HasClass("a") || !sel.HasClass("b") {
  146. t.Errorf("#main does not have classes")
  147. }
  148. }
  149. func TestHasClass(t *testing.T) {
  150. sel := Doc().Find("div")
  151. if !sel.HasClass("span12") {
  152. t.Error("Expected at least one div to have class span12.")
  153. }
  154. }
  155. func TestHasClassNone(t *testing.T) {
  156. sel := Doc().Find("h2")
  157. if sel.HasClass("toto") {
  158. t.Error("Expected h1 to have no class.")
  159. }
  160. }
  161. func TestHasClassNotFirst(t *testing.T) {
  162. sel := Doc().Find(".alert")
  163. if !sel.HasClass("alert-error") {
  164. t.Error("Expected .alert to also have class .alert-error.")
  165. }
  166. }
  167. func TestRemoveClass(t *testing.T) {
  168. sel := Doc2Clone().Find("#nf1")
  169. sel.RemoveClass("one row")
  170. if !sel.HasClass("even") || sel.HasClass("one") || sel.HasClass("row") {
  171. classes, _ := sel.Attr("class")
  172. t.Error("Expected #nf1 to have class even, has ", classes)
  173. }
  174. }
  175. func TestRemoveClassSimilar(t *testing.T) {
  176. sel := Doc2Clone().Find("#nf5, #nf6")
  177. assertLength(t, sel.Nodes, 2)
  178. sel.RemoveClass("odd")
  179. assertClass(t, sel.Eq(0), "odder")
  180. printSel(t, sel)
  181. }
  182. func TestRemoveAllClasses(t *testing.T) {
  183. sel := Doc2Clone().Find("#nf1")
  184. sel.RemoveClass()
  185. if a, ok := sel.Attr("class"); ok {
  186. t.Error("All classes were not removed, has ", a)
  187. }
  188. sel = Doc2Clone().Find("#main")
  189. sel.RemoveClass()
  190. if a, ok := sel.Attr("class"); ok {
  191. t.Error("All classes were not removed, has ", a)
  192. }
  193. }
  194. func TestToggleClass(t *testing.T) {
  195. sel := Doc2Clone().Find("#nf1")
  196. sel.ToggleClass("one")
  197. if sel.HasClass("one") {
  198. t.Error("Expected #nf1 to not have class one")
  199. }
  200. sel.ToggleClass("one")
  201. if !sel.HasClass("one") {
  202. t.Error("Expected #nf1 to have class one")
  203. }
  204. sel.ToggleClass("one even row")
  205. if a, ok := sel.Attr("class"); ok {
  206. t.Errorf("Expected #nf1 to have no classes, have %q", a)
  207. }
  208. }