array_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package goquery
  2. import (
  3. "testing"
  4. )
  5. func TestFirst(t *testing.T) {
  6. sel := Doc().Find(".pvk-content").First()
  7. assertLength(t, sel.Nodes, 1)
  8. }
  9. func TestFirstEmpty(t *testing.T) {
  10. sel := Doc().Find(".pvk-zzcontentzz").First()
  11. assertLength(t, sel.Nodes, 0)
  12. }
  13. func TestFirstInvalid(t *testing.T) {
  14. sel := Doc().Find("").First()
  15. assertLength(t, sel.Nodes, 0)
  16. }
  17. func TestFirstRollback(t *testing.T) {
  18. sel := Doc().Find(".pvk-content")
  19. sel2 := sel.First().End()
  20. assertEqual(t, sel, sel2)
  21. }
  22. func TestLast(t *testing.T) {
  23. sel := Doc().Find(".pvk-content").Last()
  24. assertLength(t, sel.Nodes, 1)
  25. // Should contain Footer
  26. foot := Doc().Find(".footer")
  27. if !sel.Contains(foot.Nodes[0]) {
  28. t.Error("Last .pvk-content should contain .footer.")
  29. }
  30. }
  31. func TestLastEmpty(t *testing.T) {
  32. sel := Doc().Find(".pvk-zzcontentzz").Last()
  33. assertLength(t, sel.Nodes, 0)
  34. }
  35. func TestLastInvalid(t *testing.T) {
  36. sel := Doc().Find("").Last()
  37. assertLength(t, sel.Nodes, 0)
  38. }
  39. func TestLastRollback(t *testing.T) {
  40. sel := Doc().Find(".pvk-content")
  41. sel2 := sel.Last().End()
  42. assertEqual(t, sel, sel2)
  43. }
  44. func TestEq(t *testing.T) {
  45. sel := Doc().Find(".pvk-content").Eq(1)
  46. assertLength(t, sel.Nodes, 1)
  47. }
  48. func TestEqNegative(t *testing.T) {
  49. sel := Doc().Find(".pvk-content").Eq(-1)
  50. assertLength(t, sel.Nodes, 1)
  51. // Should contain Footer
  52. foot := Doc().Find(".footer")
  53. if !sel.Contains(foot.Nodes[0]) {
  54. t.Error("Index -1 of .pvk-content should contain .footer.")
  55. }
  56. }
  57. func TestEqEmpty(t *testing.T) {
  58. sel := Doc().Find("something_random_that_does_not_exists").Eq(0)
  59. assertLength(t, sel.Nodes, 0)
  60. }
  61. func TestEqInvalid(t *testing.T) {
  62. sel := Doc().Find("").Eq(0)
  63. assertLength(t, sel.Nodes, 0)
  64. }
  65. func TestEqInvalidPositive(t *testing.T) {
  66. sel := Doc().Find(".pvk-content").Eq(3)
  67. assertLength(t, sel.Nodes, 0)
  68. }
  69. func TestEqInvalidNegative(t *testing.T) {
  70. sel := Doc().Find(".pvk-content").Eq(-4)
  71. assertLength(t, sel.Nodes, 0)
  72. }
  73. func TestEqRollback(t *testing.T) {
  74. sel := Doc().Find(".pvk-content")
  75. sel2 := sel.Eq(1).End()
  76. assertEqual(t, sel, sel2)
  77. }
  78. func TestSlice(t *testing.T) {
  79. sel := Doc().Find(".pvk-content").Slice(0, 2)
  80. assertLength(t, sel.Nodes, 2)
  81. assertSelectionIs(t, sel, "#pc1", "#pc2")
  82. }
  83. func TestSliceToEnd(t *testing.T) {
  84. sel := Doc().Find(".pvk-content").Slice(1, ToEnd)
  85. assertLength(t, sel.Nodes, 2)
  86. assertSelectionIs(t, sel.Eq(0), "#pc2")
  87. if _, ok := sel.Eq(1).Attr("id"); ok {
  88. t.Error("Want no attribute ID, got one")
  89. }
  90. }
  91. func TestSliceEmpty(t *testing.T) {
  92. defer assertPanic(t)
  93. Doc().Find("x").Slice(0, 2)
  94. }
  95. func TestSliceInvalid(t *testing.T) {
  96. defer assertPanic(t)
  97. Doc().Find("").Slice(0, 2)
  98. }
  99. func TestSliceInvalidToEnd(t *testing.T) {
  100. defer assertPanic(t)
  101. Doc().Find("").Slice(2, ToEnd)
  102. }
  103. func TestSliceOutOfBounds(t *testing.T) {
  104. defer assertPanic(t)
  105. Doc().Find(".pvk-content").Slice(2, 12)
  106. }
  107. func TestNegativeSliceStart(t *testing.T) {
  108. sel := Doc().Find(".container-fluid").Slice(-2, 3)
  109. assertLength(t, sel.Nodes, 1)
  110. assertSelectionIs(t, sel.Eq(0), "#cf3")
  111. }
  112. func TestNegativeSliceEnd(t *testing.T) {
  113. sel := Doc().Find(".container-fluid").Slice(1, -1)
  114. assertLength(t, sel.Nodes, 2)
  115. assertSelectionIs(t, sel.Eq(0), "#cf2")
  116. assertSelectionIs(t, sel.Eq(1), "#cf3")
  117. }
  118. func TestNegativeSliceBoth(t *testing.T) {
  119. sel := Doc().Find(".container-fluid").Slice(-3, -1)
  120. assertLength(t, sel.Nodes, 2)
  121. assertSelectionIs(t, sel.Eq(0), "#cf2")
  122. assertSelectionIs(t, sel.Eq(1), "#cf3")
  123. }
  124. func TestNegativeSliceToEnd(t *testing.T) {
  125. sel := Doc().Find(".container-fluid").Slice(-3, ToEnd)
  126. assertLength(t, sel.Nodes, 3)
  127. assertSelectionIs(t, sel, "#cf2", "#cf3", "#cf4")
  128. }
  129. func TestNegativeSliceOutOfBounds(t *testing.T) {
  130. defer assertPanic(t)
  131. Doc().Find(".container-fluid").Slice(-12, -7)
  132. }
  133. func TestSliceRollback(t *testing.T) {
  134. sel := Doc().Find(".pvk-content")
  135. sel2 := sel.Slice(0, 2).End()
  136. assertEqual(t, sel, sel2)
  137. }
  138. func TestGet(t *testing.T) {
  139. sel := Doc().Find(".pvk-content")
  140. node := sel.Get(1)
  141. if sel.Nodes[1] != node {
  142. t.Errorf("Expected node %v to be %v.", node, sel.Nodes[1])
  143. }
  144. }
  145. func TestGetNegative(t *testing.T) {
  146. sel := Doc().Find(".pvk-content")
  147. node := sel.Get(-3)
  148. if sel.Nodes[0] != node {
  149. t.Errorf("Expected node %v to be %v.", node, sel.Nodes[0])
  150. }
  151. }
  152. func TestGetInvalid(t *testing.T) {
  153. defer assertPanic(t)
  154. sel := Doc().Find(".pvk-content")
  155. sel.Get(129)
  156. }
  157. func TestIndex(t *testing.T) {
  158. sel := Doc().Find(".pvk-content")
  159. if i := sel.Index(); i != 1 {
  160. t.Errorf("Expected index of 1, got %v.", i)
  161. }
  162. }
  163. func TestIndexSelector(t *testing.T) {
  164. sel := Doc().Find(".hero-unit")
  165. if i := sel.IndexSelector("div"); i != 4 {
  166. t.Errorf("Expected index of 4, got %v.", i)
  167. }
  168. }
  169. func TestIndexSelectorInvalid(t *testing.T) {
  170. sel := Doc().Find(".hero-unit")
  171. if i := sel.IndexSelector(""); i != -1 {
  172. t.Errorf("Expected index of -1, got %v.", i)
  173. }
  174. }
  175. func TestIndexOfNode(t *testing.T) {
  176. sel := Doc().Find("div.pvk-gutter")
  177. if i := sel.IndexOfNode(sel.Nodes[1]); i != 1 {
  178. t.Errorf("Expected index of 1, got %v.", i)
  179. }
  180. }
  181. func TestIndexOfNilNode(t *testing.T) {
  182. sel := Doc().Find("div.pvk-gutter")
  183. if i := sel.IndexOfNode(nil); i != -1 {
  184. t.Errorf("Expected index of -1, got %v.", i)
  185. }
  186. }
  187. func TestIndexOfSelection(t *testing.T) {
  188. sel := Doc().Find("div")
  189. sel2 := Doc().Find(".hero-unit")
  190. if i := sel.IndexOfSelection(sel2); i != 4 {
  191. t.Errorf("Expected index of 4, got %v.", i)
  192. }
  193. }