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