filter_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package goquery
  2. import (
  3. "testing"
  4. )
  5. func TestFilter(t *testing.T) {
  6. sel := Doc().Find(".span12").Filter(".alert")
  7. assertLength(t, sel.Nodes, 1)
  8. }
  9. func TestFilterNone(t *testing.T) {
  10. sel := Doc().Find(".span12").Filter(".zzalert")
  11. assertLength(t, sel.Nodes, 0)
  12. }
  13. func TestFilterInvalid(t *testing.T) {
  14. sel := Doc().Find(".span12").Filter("")
  15. assertLength(t, sel.Nodes, 0)
  16. }
  17. func TestFilterRollback(t *testing.T) {
  18. sel := Doc().Find(".pvk-content")
  19. sel2 := sel.Filter(".alert").End()
  20. assertEqual(t, sel, sel2)
  21. }
  22. func TestFilterFunction(t *testing.T) {
  23. sel := Doc().Find(".pvk-content").FilterFunction(func(i int, s *Selection) bool {
  24. return i > 0
  25. })
  26. assertLength(t, sel.Nodes, 2)
  27. }
  28. func TestFilterFunctionRollback(t *testing.T) {
  29. sel := Doc().Find(".pvk-content")
  30. sel2 := sel.FilterFunction(func(i int, s *Selection) bool {
  31. return i > 0
  32. }).End()
  33. assertEqual(t, sel, sel2)
  34. }
  35. func TestFilterNode(t *testing.T) {
  36. sel := Doc().Find(".pvk-content")
  37. sel2 := sel.FilterNodes(sel.Nodes[2])
  38. assertLength(t, sel2.Nodes, 1)
  39. }
  40. func TestFilterNodeRollback(t *testing.T) {
  41. sel := Doc().Find(".pvk-content")
  42. sel2 := sel.FilterNodes(sel.Nodes[2]).End()
  43. assertEqual(t, sel, sel2)
  44. }
  45. func TestFilterSelection(t *testing.T) {
  46. sel := Doc().Find(".link")
  47. sel2 := Doc().Find("a[ng-click]")
  48. sel3 := sel.FilterSelection(sel2)
  49. assertLength(t, sel3.Nodes, 1)
  50. }
  51. func TestFilterSelectionRollback(t *testing.T) {
  52. sel := Doc().Find(".link")
  53. sel2 := Doc().Find("a[ng-click]")
  54. sel2 = sel.FilterSelection(sel2).End()
  55. assertEqual(t, sel, sel2)
  56. }
  57. func TestFilterSelectionNil(t *testing.T) {
  58. var sel2 *Selection
  59. sel := Doc().Find(".link")
  60. sel3 := sel.FilterSelection(sel2)
  61. assertLength(t, sel3.Nodes, 0)
  62. }
  63. func TestNot(t *testing.T) {
  64. sel := Doc().Find(".span12").Not(".alert")
  65. assertLength(t, sel.Nodes, 1)
  66. }
  67. func TestNotInvalid(t *testing.T) {
  68. sel := Doc().Find(".span12").Not("")
  69. assertLength(t, sel.Nodes, 2)
  70. }
  71. func TestNotRollback(t *testing.T) {
  72. sel := Doc().Find(".span12")
  73. sel2 := sel.Not(".alert").End()
  74. assertEqual(t, sel, sel2)
  75. }
  76. func TestNotNone(t *testing.T) {
  77. sel := Doc().Find(".span12").Not(".zzalert")
  78. assertLength(t, sel.Nodes, 2)
  79. }
  80. func TestNotFunction(t *testing.T) {
  81. sel := Doc().Find(".pvk-content").NotFunction(func(i int, s *Selection) bool {
  82. return i > 0
  83. })
  84. assertLength(t, sel.Nodes, 1)
  85. }
  86. func TestNotFunctionRollback(t *testing.T) {
  87. sel := Doc().Find(".pvk-content")
  88. sel2 := sel.NotFunction(func(i int, s *Selection) bool {
  89. return i > 0
  90. }).End()
  91. assertEqual(t, sel, sel2)
  92. }
  93. func TestNotNode(t *testing.T) {
  94. sel := Doc().Find(".pvk-content")
  95. sel2 := sel.NotNodes(sel.Nodes[2])
  96. assertLength(t, sel2.Nodes, 2)
  97. }
  98. func TestNotNodeRollback(t *testing.T) {
  99. sel := Doc().Find(".pvk-content")
  100. sel2 := sel.NotNodes(sel.Nodes[2]).End()
  101. assertEqual(t, sel, sel2)
  102. }
  103. func TestNotSelection(t *testing.T) {
  104. sel := Doc().Find(".link")
  105. sel2 := Doc().Find("a[ng-click]")
  106. sel3 := sel.NotSelection(sel2)
  107. assertLength(t, sel3.Nodes, 6)
  108. }
  109. func TestNotSelectionRollback(t *testing.T) {
  110. sel := Doc().Find(".link")
  111. sel2 := Doc().Find("a[ng-click]")
  112. sel2 = sel.NotSelection(sel2).End()
  113. assertEqual(t, sel, sel2)
  114. }
  115. func TestIntersection(t *testing.T) {
  116. sel := Doc().Find(".pvk-gutter")
  117. sel2 := Doc().Find("div").Intersection(sel)
  118. assertLength(t, sel2.Nodes, 6)
  119. }
  120. func TestIntersectionRollback(t *testing.T) {
  121. sel := Doc().Find(".pvk-gutter")
  122. sel2 := Doc().Find("div")
  123. sel2 = sel.Intersection(sel2).End()
  124. assertEqual(t, sel, sel2)
  125. }
  126. func TestHas(t *testing.T) {
  127. sel := Doc().Find(".container-fluid").Has(".center-content")
  128. assertLength(t, sel.Nodes, 2)
  129. // Has() returns the high-level .container-fluid div, and the one that is the immediate parent of center-content
  130. }
  131. func TestHasInvalid(t *testing.T) {
  132. sel := Doc().Find(".container-fluid").Has("")
  133. assertLength(t, sel.Nodes, 0)
  134. }
  135. func TestHasRollback(t *testing.T) {
  136. sel := Doc().Find(".container-fluid")
  137. sel2 := sel.Has(".center-content").End()
  138. assertEqual(t, sel, sel2)
  139. }
  140. func TestHasNodes(t *testing.T) {
  141. sel := Doc().Find(".container-fluid")
  142. sel2 := Doc().Find(".center-content")
  143. sel = sel.HasNodes(sel2.Nodes...)
  144. assertLength(t, sel.Nodes, 2)
  145. // Has() returns the high-level .container-fluid div, and the one that is the immediate parent of center-content
  146. }
  147. func TestHasNodesRollback(t *testing.T) {
  148. sel := Doc().Find(".container-fluid")
  149. sel2 := Doc().Find(".center-content")
  150. sel2 = sel.HasNodes(sel2.Nodes...).End()
  151. assertEqual(t, sel, sel2)
  152. }
  153. func TestHasSelection(t *testing.T) {
  154. sel := Doc().Find("p")
  155. sel2 := Doc().Find("small")
  156. sel = sel.HasSelection(sel2)
  157. assertLength(t, sel.Nodes, 1)
  158. }
  159. func TestHasSelectionRollback(t *testing.T) {
  160. sel := Doc().Find("p")
  161. sel2 := Doc().Find("small")
  162. sel2 = sel.HasSelection(sel2).End()
  163. assertEqual(t, sel, sel2)
  164. }
  165. func TestEnd(t *testing.T) {
  166. sel := Doc().Find("p").Has("small").End()
  167. assertLength(t, sel.Nodes, 4)
  168. }
  169. func TestEndToTop(t *testing.T) {
  170. sel := Doc().Find("p").Has("small").End().End().End()
  171. assertLength(t, sel.Nodes, 0)
  172. }