Eq now returns an empty Selector when an invalid index is given

This commit is contained in:
tnt
2014-02-28 21:54:48 +01:00
parent e273eed13e
commit 48de68c2b7
2 changed files with 22 additions and 2 deletions
+5
View File
@@ -24,6 +24,11 @@ func (this *Selection) Eq(index int) *Selection {
if index < 0 {
index += len(this.Nodes)
}
if index >= len(this.Nodes) || index < 0 {
return newEmptySelection(this.document)
}
return this.Slice(index, index+1)
}
+17 -2
View File
@@ -10,8 +10,8 @@ func TestFirst(t *testing.T) {
}
func TestFirstEmpty(t *testing.T) {
defer AssertPanic(t)
Doc().Find(".pvk-zzcontentzz").First()
sel := Doc().Find(".pvk-zzcontentzz").First()
AssertLength(t, sel.Nodes, 0)
}
func TestFirstRollback(t *testing.T) {
@@ -53,6 +53,21 @@ func TestEqNegative(t *testing.T) {
}
}
func TestEqEmpty(t *testing.T) {
sel := Doc().Find("something_random_that_does_not_exists").Eq(0)
AssertLength(t, sel.Nodes, 0)
}
func TestEqInvalidPositive(t *testing.T) {
sel := Doc().Find(".pvk-content").Eq(3)
AssertLength(t, sel.Nodes, 0)
}
func TestEqInvalidNegative(t *testing.T) {
sel := Doc().Find(".pvk-content").Eq(-4)
AssertLength(t, sel.Nodes, 0)
}
func TestEqRollback(t *testing.T) {
sel := Doc().Find(".pvk-content")
sel2 := sel.Eq(1).End()