mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-04 08:26:37 +08:00
remove Document.Root, Document is itself a Selection (using embedded struct)
This commit is contained in:
@@ -5,86 +5,86 @@ import (
|
||||
)
|
||||
|
||||
func TestFirst(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content").First()
|
||||
sel := Doc().Find(".pvk-content").First()
|
||||
AssertLength(t, sel.Nodes, 1)
|
||||
}
|
||||
|
||||
func TestFirstEmpty(t *testing.T) {
|
||||
defer AssertPanic(t)
|
||||
Doc().Root.Find(".pvk-zzcontentzz").First()
|
||||
Doc().Find(".pvk-zzcontentzz").First()
|
||||
}
|
||||
|
||||
func TestFirstRollback(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content")
|
||||
sel := Doc().Find(".pvk-content")
|
||||
sel2 := sel.First().End()
|
||||
AssertEqual(t, sel, sel2)
|
||||
}
|
||||
|
||||
func TestLast(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content").Last()
|
||||
sel := Doc().Find(".pvk-content").Last()
|
||||
AssertLength(t, sel.Nodes, 1)
|
||||
|
||||
// Should contain Footer
|
||||
foot := Doc().Root.Find(".footer")
|
||||
foot := Doc().Find(".footer")
|
||||
if !sel.Contains(foot.Nodes[0]) {
|
||||
t.Error("Last .pvk-content should contain .footer.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLastRollback(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content")
|
||||
sel := Doc().Find(".pvk-content")
|
||||
sel2 := sel.Last().End()
|
||||
AssertEqual(t, sel, sel2)
|
||||
}
|
||||
|
||||
func TestEq(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content").Eq(1)
|
||||
sel := Doc().Find(".pvk-content").Eq(1)
|
||||
AssertLength(t, sel.Nodes, 1)
|
||||
}
|
||||
|
||||
func TestEqNegative(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content").Eq(-1)
|
||||
sel := Doc().Find(".pvk-content").Eq(-1)
|
||||
AssertLength(t, sel.Nodes, 1)
|
||||
|
||||
// Should contain Footer
|
||||
foot := Doc().Root.Find(".footer")
|
||||
foot := Doc().Find(".footer")
|
||||
if !sel.Contains(foot.Nodes[0]) {
|
||||
t.Error("Index -1 of .pvk-content should contain .footer.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEqRollback(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content")
|
||||
sel := Doc().Find(".pvk-content")
|
||||
sel2 := sel.Eq(1).End()
|
||||
AssertEqual(t, sel, sel2)
|
||||
}
|
||||
|
||||
func TestSlice(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content").Slice(0, 2)
|
||||
sel := Doc().Find(".pvk-content").Slice(0, 2)
|
||||
|
||||
AssertLength(t, sel.Nodes, 2)
|
||||
}
|
||||
|
||||
func TestSliceOutOfBounds(t *testing.T) {
|
||||
defer AssertPanic(t)
|
||||
Doc().Root.Find(".pvk-content").Slice(2, 12)
|
||||
Doc().Find(".pvk-content").Slice(2, 12)
|
||||
}
|
||||
|
||||
func TestNegativeSliceStart(t *testing.T) {
|
||||
sel := Doc().Root.Find(".container-fluid").Slice(-2, 3)
|
||||
sel := Doc().Find(".container-fluid").Slice(-2, 3)
|
||||
AssertLength(t, sel.Nodes, 1)
|
||||
AssertSelectionIs(t, sel.Eq(0), "#cf3")
|
||||
}
|
||||
|
||||
func TestNegativeSliceEnd(t *testing.T) {
|
||||
sel := Doc().Root.Find(".container-fluid").Slice(1, -1)
|
||||
sel := Doc().Find(".container-fluid").Slice(1, -1)
|
||||
AssertLength(t, sel.Nodes, 2)
|
||||
AssertSelectionIs(t, sel.Eq(0), "#cf2")
|
||||
AssertSelectionIs(t, sel.Eq(1), "#cf3")
|
||||
}
|
||||
|
||||
func TestNegativeSliceBoth(t *testing.T) {
|
||||
sel := Doc().Root.Find(".container-fluid").Slice(-3, -1)
|
||||
sel := Doc().Find(".container-fluid").Slice(-3, -1)
|
||||
AssertLength(t, sel.Nodes, 2)
|
||||
AssertSelectionIs(t, sel.Eq(0), "#cf2")
|
||||
AssertSelectionIs(t, sel.Eq(1), "#cf3")
|
||||
@@ -92,17 +92,17 @@ func TestNegativeSliceBoth(t *testing.T) {
|
||||
|
||||
func TestNegativeSliceOutOfBounds(t *testing.T) {
|
||||
defer AssertPanic(t)
|
||||
Doc().Root.Find(".container-fluid").Slice(-12, -7)
|
||||
Doc().Find(".container-fluid").Slice(-12, -7)
|
||||
}
|
||||
|
||||
func TestSliceRollback(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content")
|
||||
sel := Doc().Find(".pvk-content")
|
||||
sel2 := sel.Slice(0, 2).End()
|
||||
AssertEqual(t, sel, sel2)
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content")
|
||||
sel := Doc().Find(".pvk-content")
|
||||
node := sel.Get(1)
|
||||
if sel.Nodes[1] != node {
|
||||
t.Errorf("Expected node %v to be %v.", node, sel.Nodes[1])
|
||||
@@ -110,7 +110,7 @@ func TestGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetNegative(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content")
|
||||
sel := Doc().Find(".pvk-content")
|
||||
node := sel.Get(-3)
|
||||
if sel.Nodes[0] != node {
|
||||
t.Errorf("Expected node %v to be %v.", node, sel.Nodes[0])
|
||||
@@ -119,41 +119,41 @@ func TestGetNegative(t *testing.T) {
|
||||
|
||||
func TestGetInvalid(t *testing.T) {
|
||||
defer AssertPanic(t)
|
||||
sel := Doc().Root.Find(".pvk-content")
|
||||
sel := Doc().Find(".pvk-content")
|
||||
sel.Get(129)
|
||||
}
|
||||
|
||||
func TestIndex(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content")
|
||||
sel := Doc().Find(".pvk-content")
|
||||
if i := sel.Index(); i != 1 {
|
||||
t.Errorf("Expected index of 1, got %v.", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexSelector(t *testing.T) {
|
||||
sel := Doc().Root.Find(".hero-unit")
|
||||
sel := Doc().Find(".hero-unit")
|
||||
if i := sel.IndexSelector("div"); i != 4 {
|
||||
t.Errorf("Expected index of 4, got %v.", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexOfNode(t *testing.T) {
|
||||
sel := Doc().Root.Find("div.pvk-gutter")
|
||||
sel := Doc().Find("div.pvk-gutter")
|
||||
if i := sel.IndexOfNode(sel.Nodes[1]); i != 1 {
|
||||
t.Errorf("Expected index of 1, got %v.", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexOfNilNode(t *testing.T) {
|
||||
sel := Doc().Root.Find("div.pvk-gutter")
|
||||
sel := Doc().Find("div.pvk-gutter")
|
||||
if i := sel.IndexOfNode(nil); i != -1 {
|
||||
t.Errorf("Expected index of -1, got %v.", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexOfSelection(t *testing.T) {
|
||||
sel := Doc().Root.Find("div")
|
||||
sel2 := Doc().Root.Find(".hero-unit")
|
||||
sel := Doc().Find("div")
|
||||
sel2 := Doc().Find(".hero-unit")
|
||||
if i := sel.IndexOfSelection(sel2); i != 4 {
|
||||
t.Errorf("Expected index of 4, got %v.", i)
|
||||
}
|
||||
|
Reference in New Issue
Block a user