You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
4.7 KiB

  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. }
  82. func TestSliceEmpty(t *testing.T) {
  83. defer assertPanic(t)
  84. Doc().Find("x").Slice(0, 2)
  85. }
  86. func TestSliceInvalid(t *testing.T) {
  87. defer assertPanic(t)
  88. Doc().Find("").Slice(0, 2)
  89. }
  90. func TestSliceOutOfBounds(t *testing.T) {
  91. defer assertPanic(t)
  92. Doc().Find(".pvk-content").Slice(2, 12)
  93. }
  94. func TestNegativeSliceStart(t *testing.T) {
  95. sel := Doc().Find(".container-fluid").Slice(-2, 3)
  96. assertLength(t, sel.Nodes, 1)
  97. assertSelectionIs(t, sel.Eq(0), "#cf3")
  98. }
  99. func TestNegativeSliceEnd(t *testing.T) {
  100. sel := Doc().Find(".container-fluid").Slice(1, -1)
  101. assertLength(t, sel.Nodes, 2)
  102. assertSelectionIs(t, sel.Eq(0), "#cf2")
  103. assertSelectionIs(t, sel.Eq(1), "#cf3")
  104. }
  105. func TestNegativeSliceBoth(t *testing.T) {
  106. sel := Doc().Find(".container-fluid").Slice(-3, -1)
  107. assertLength(t, sel.Nodes, 2)
  108. assertSelectionIs(t, sel.Eq(0), "#cf2")
  109. assertSelectionIs(t, sel.Eq(1), "#cf3")
  110. }
  111. func TestNegativeSliceOutOfBounds(t *testing.T) {
  112. defer assertPanic(t)
  113. Doc().Find(".container-fluid").Slice(-12, -7)
  114. }
  115. func TestSliceRollback(t *testing.T) {
  116. sel := Doc().Find(".pvk-content")
  117. sel2 := sel.Slice(0, 2).End()
  118. assertEqual(t, sel, sel2)
  119. }
  120. func TestGet(t *testing.T) {
  121. sel := Doc().Find(".pvk-content")
  122. node := sel.Get(1)
  123. if sel.Nodes[1] != node {
  124. t.Errorf("Expected node %v to be %v.", node, sel.Nodes[1])
  125. }
  126. }
  127. func TestGetNegative(t *testing.T) {
  128. sel := Doc().Find(".pvk-content")
  129. node := sel.Get(-3)
  130. if sel.Nodes[0] != node {
  131. t.Errorf("Expected node %v to be %v.", node, sel.Nodes[0])
  132. }
  133. }
  134. func TestGetInvalid(t *testing.T) {
  135. defer assertPanic(t)
  136. sel := Doc().Find(".pvk-content")
  137. sel.Get(129)
  138. }
  139. func TestIndex(t *testing.T) {
  140. sel := Doc().Find(".pvk-content")
  141. if i := sel.Index(); i != 1 {
  142. t.Errorf("Expected index of 1, got %v.", i)
  143. }
  144. }
  145. func TestIndexSelector(t *testing.T) {
  146. sel := Doc().Find(".hero-unit")
  147. if i := sel.IndexSelector("div"); i != 4 {
  148. t.Errorf("Expected index of 4, got %v.", i)
  149. }
  150. }
  151. func TestIndexSelectorInvalid(t *testing.T) {
  152. sel := Doc().Find(".hero-unit")
  153. if i := sel.IndexSelector(""); i != -1 {
  154. t.Errorf("Expected index of -1, got %v.", i)
  155. }
  156. }
  157. func TestIndexOfNode(t *testing.T) {
  158. sel := Doc().Find("div.pvk-gutter")
  159. if i := sel.IndexOfNode(sel.Nodes[1]); i != 1 {
  160. t.Errorf("Expected index of 1, got %v.", i)
  161. }
  162. }
  163. func TestIndexOfNilNode(t *testing.T) {
  164. sel := Doc().Find("div.pvk-gutter")
  165. if i := sel.IndexOfNode(nil); i != -1 {
  166. t.Errorf("Expected index of -1, got %v.", i)
  167. }
  168. }
  169. func TestIndexOfSelection(t *testing.T) {
  170. sel := Doc().Find("div")
  171. sel2 := Doc().Find(".hero-unit")
  172. if i := sel.IndexOfSelection(sel2); i != 4 {
  173. t.Errorf("Expected index of 4, got %v.", i)
  174. }
  175. }