mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-31 03:36:31 +08:00
fix tests after reorganization
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFirst(t *testing.T) {
|
func TestFirst(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content").First()
|
sel := Doc().Find(".pvk-content").First()
|
||||||
if len(sel.Nodes) != 1 {
|
if len(sel.Nodes) != 1 {
|
||||||
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
||||||
}
|
}
|
||||||
@@ -17,42 +17,42 @@ func TestFirstEmpty(t *testing.T) {
|
|||||||
t.Error("Expected a panic, First() called on empty Selection.")
|
t.Error("Expected a panic, First() called on empty Selection.")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
doc.Find(".pvk-zzcontentzz").First()
|
Doc().Find(".pvk-zzcontentzz").First()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLast(t *testing.T) {
|
func TestLast(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content").Last()
|
sel := Doc().Find(".pvk-content").Last()
|
||||||
if len(sel.Nodes) != 1 {
|
if len(sel.Nodes) != 1 {
|
||||||
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
||||||
}
|
}
|
||||||
// Should contain Footer
|
// Should contain Footer
|
||||||
foot := doc.Find(".footer")
|
foot := Doc().Find(".footer")
|
||||||
if !sel.Contains(foot.Nodes[0]) {
|
if !sel.Contains(foot.Nodes[0]) {
|
||||||
t.Error("Last .pvk-content should contain .footer.")
|
t.Error("Last .pvk-content should contain .footer.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEq(t *testing.T) {
|
func TestEq(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content").Eq(1)
|
sel := Doc().Find(".pvk-content").Eq(1)
|
||||||
if len(sel.Nodes) != 1 {
|
if len(sel.Nodes) != 1 {
|
||||||
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEqNegative(t *testing.T) {
|
func TestEqNegative(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content").Eq(-1)
|
sel := Doc().Find(".pvk-content").Eq(-1)
|
||||||
if len(sel.Nodes) != 1 {
|
if len(sel.Nodes) != 1 {
|
||||||
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
||||||
}
|
}
|
||||||
// Should contain Footer
|
// Should contain Footer
|
||||||
foot := doc.Find(".footer")
|
foot := Doc().Find(".footer")
|
||||||
if !sel.Contains(foot.Nodes[0]) {
|
if !sel.Contains(foot.Nodes[0]) {
|
||||||
t.Error("Index -1 of .pvk-content should contain .footer.")
|
t.Error("Index -1 of .pvk-content should contain .footer.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSlice(t *testing.T) {
|
func TestSlice(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content").Slice(0, 2)
|
sel := Doc().Find(".pvk-content").Slice(0, 2)
|
||||||
if len(sel.Nodes) != 2 {
|
if len(sel.Nodes) != 2 {
|
||||||
t.Errorf("Expected 2 nodes, found %v.", len(sel.Nodes))
|
t.Errorf("Expected 2 nodes, found %v.", len(sel.Nodes))
|
||||||
}
|
}
|
||||||
@@ -64,11 +64,11 @@ func TestSliceOutOfBounds(t *testing.T) {
|
|||||||
t.Error("Expected a panic, Slice() called with out of bounds indices.")
|
t.Error("Expected a panic, Slice() called with out of bounds indices.")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
doc.Find(".pvk-content").Slice(2, 12)
|
Doc().Find(".pvk-content").Slice(2, 12)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content")
|
sel := Doc().Find(".pvk-content")
|
||||||
node := sel.Get(1)
|
node := sel.Get(1)
|
||||||
if sel.Nodes[1] != node {
|
if sel.Nodes[1] != node {
|
||||||
t.Errorf("Expected node %v to be %v.", node, sel.Nodes[1])
|
t.Errorf("Expected node %v to be %v.", node, sel.Nodes[1])
|
||||||
@@ -76,7 +76,7 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetNegative(t *testing.T) {
|
func TestGetNegative(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content")
|
sel := Doc().Find(".pvk-content")
|
||||||
node := sel.Get(-3)
|
node := sel.Get(-3)
|
||||||
if sel.Nodes[0] != node {
|
if sel.Nodes[0] != node {
|
||||||
t.Errorf("Expected node %v to be %v.", node, sel.Nodes[0])
|
t.Errorf("Expected node %v to be %v.", node, sel.Nodes[0])
|
||||||
@@ -90,41 +90,41 @@ func TestGetInvalid(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
sel := doc.Find(".pvk-content")
|
sel := Doc().Find(".pvk-content")
|
||||||
sel.Get(129)
|
sel.Get(129)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIndex(t *testing.T) {
|
func TestIndex(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content")
|
sel := Doc().Find(".pvk-content")
|
||||||
if i := sel.Index(); i != 1 {
|
if i := sel.Index(); i != 1 {
|
||||||
t.Errorf("Expected index of 1, got %v.", i)
|
t.Errorf("Expected index of 1, got %v.", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIndexSelector(t *testing.T) {
|
func TestIndexSelector(t *testing.T) {
|
||||||
sel := doc.Find(".hero-unit")
|
sel := Doc().Find(".hero-unit")
|
||||||
if i := sel.IndexSelector("div"); i != 4 {
|
if i := sel.IndexSelector("div"); i != 4 {
|
||||||
t.Errorf("Expected index of 4, got %v.", i)
|
t.Errorf("Expected index of 4, got %v.", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIndexOfNode(t *testing.T) {
|
func TestIndexOfNode(t *testing.T) {
|
||||||
sel := doc.Find("div.pvk-gutter")
|
sel := Doc().Find("div.pvk-gutter")
|
||||||
if i := sel.IndexOfNode(sel.Nodes[1]); i != 1 {
|
if i := sel.IndexOfNode(sel.Nodes[1]); i != 1 {
|
||||||
t.Errorf("Expected index of 1, got %v.", i)
|
t.Errorf("Expected index of 1, got %v.", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIndexOfNilNode(t *testing.T) {
|
func TestIndexOfNilNode(t *testing.T) {
|
||||||
sel := doc.Find("div.pvk-gutter")
|
sel := Doc().Find("div.pvk-gutter")
|
||||||
if i := sel.IndexOfNode(nil); i != -1 {
|
if i := sel.IndexOfNode(nil); i != -1 {
|
||||||
t.Errorf("Expected index of -1, got %v.", i)
|
t.Errorf("Expected index of -1, got %v.", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIndexOfSelection(t *testing.T) {
|
func TestIndexOfSelection(t *testing.T) {
|
||||||
sel := doc.Find("div")
|
sel := Doc().Find("div")
|
||||||
sel2 := doc.Find(".hero-unit")
|
sel2 := Doc().Find(".hero-unit")
|
||||||
if i := sel.IndexOfSelection(sel2); i != 4 {
|
if i := sel.IndexOfSelection(sel2); i != 4 {
|
||||||
t.Errorf("Expected index of 4, got %v.", i)
|
t.Errorf("Expected index of 4, got %v.", i)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ func TestAdd(t *testing.T) {
|
|||||||
|
|
||||||
EnsureDocLoaded()
|
EnsureDocLoaded()
|
||||||
|
|
||||||
sel := doc.Find("div.row-fluid")
|
sel := Doc().Find("div.row-fluid")
|
||||||
cnt = len(sel.Nodes)
|
cnt = len(sel.Nodes)
|
||||||
sel2 := sel.Add("a")
|
sel2 := sel.Add("a")
|
||||||
if sel != sel2 {
|
if sel != sel2 {
|
||||||
|
|||||||
@@ -5,21 +5,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFilter(t *testing.T) {
|
func TestFilter(t *testing.T) {
|
||||||
sel := doc.Find(".span12").Filter(".alert")
|
sel := Doc().Find(".span12").Filter(".alert")
|
||||||
if len(sel.Nodes) != 1 {
|
if len(sel.Nodes) != 1 {
|
||||||
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFilterNone(t *testing.T) {
|
func TestFilterNone(t *testing.T) {
|
||||||
sel := doc.Find(".span12").Filter(".zzalert")
|
sel := Doc().Find(".span12").Filter(".zzalert")
|
||||||
if sel.Nodes != nil {
|
if sel.Nodes != nil {
|
||||||
t.Error("Expected no node (nil), found some.")
|
t.Error("Expected no node (nil), found some.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFilterFunction(t *testing.T) {
|
func TestFilterFunction(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content").FilterFunction(func(i int, s *Selection) bool {
|
sel := Doc().Find(".pvk-content").FilterFunction(func(i int, s *Selection) bool {
|
||||||
return i > 0
|
return i > 0
|
||||||
})
|
})
|
||||||
if len(sel.Nodes) != 2 {
|
if len(sel.Nodes) != 2 {
|
||||||
@@ -28,7 +28,7 @@ func TestFilterFunction(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFilterNode(t *testing.T) {
|
func TestFilterNode(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content")
|
sel := Doc().Find(".pvk-content")
|
||||||
sel2 := sel.FilterNodes(sel.Nodes[2])
|
sel2 := sel.FilterNodes(sel.Nodes[2])
|
||||||
if len(sel2.Nodes) != 1 {
|
if len(sel2.Nodes) != 1 {
|
||||||
t.Errorf("Expected 1 node, found %v.", len(sel2.Nodes))
|
t.Errorf("Expected 1 node, found %v.", len(sel2.Nodes))
|
||||||
@@ -36,8 +36,8 @@ func TestFilterNode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFilterSelection(t *testing.T) {
|
func TestFilterSelection(t *testing.T) {
|
||||||
sel := doc.Find(".link")
|
sel := Doc().Find(".link")
|
||||||
sel2 := doc.Find("a[ng-click]")
|
sel2 := Doc().Find("a[ng-click]")
|
||||||
sel3 := sel.FilterSelection(sel2)
|
sel3 := sel.FilterSelection(sel2)
|
||||||
if len(sel3.Nodes) != 1 {
|
if len(sel3.Nodes) != 1 {
|
||||||
t.Errorf("Expected 1 node, found %v.", len(sel3.Nodes))
|
t.Errorf("Expected 1 node, found %v.", len(sel3.Nodes))
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
func TestEach(t *testing.T) {
|
func TestEach(t *testing.T) {
|
||||||
var cnt int
|
var cnt int
|
||||||
|
|
||||||
sel := doc.Find(".hero-unit .row-fluid").Each(func(i int, n *Selection) {
|
sel := Doc().Find(".hero-unit .row-fluid").Each(func(i int, n *Selection) {
|
||||||
cnt++
|
cnt++
|
||||||
t.Logf("At index %v, node %v", i, n.Nodes[0].Data)
|
t.Logf("At index %v, node %v", i, n.Nodes[0].Data)
|
||||||
}).Find("a")
|
}).Find("a")
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
func TestAttrExists(t *testing.T) {
|
func TestAttrExists(t *testing.T) {
|
||||||
EnsureDocLoaded()
|
EnsureDocLoaded()
|
||||||
|
|
||||||
if val, ok := doc.Find("a").Attr("href"); !ok {
|
if val, ok := Doc().Find("a").Attr("href"); !ok {
|
||||||
t.Error("Expected a value for the href attribute.")
|
t.Error("Expected a value for the href attribute.")
|
||||||
} else {
|
} else {
|
||||||
t.Logf("Href of first anchor: %v.", val)
|
t.Logf("Href of first anchor: %v.", val)
|
||||||
@@ -15,7 +15,7 @@ func TestAttrExists(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAttrNotExist(t *testing.T) {
|
func TestAttrNotExist(t *testing.T) {
|
||||||
if val, ok := doc.Find("div.row-fluid").Attr("href"); ok {
|
if val, ok := Doc().Find("div.row-fluid").Attr("href"); ok {
|
||||||
t.Errorf("Expected no value for the href attribute, got %v.", val)
|
t.Errorf("Expected no value for the href attribute, got %v.", val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestHasClass(t *testing.T) {
|
func TestHasClass(t *testing.T) {
|
||||||
sel := doc.Find("div")
|
sel := Doc().Find("div")
|
||||||
if !sel.HasClass("span12") {
|
if !sel.HasClass("span12") {
|
||||||
t.Error("Expected at least one div to have class span12.")
|
t.Error("Expected at least one div to have class span12.")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFind(t *testing.T) {
|
func TestFind(t *testing.T) {
|
||||||
sel := doc.Find("div.row-fluid")
|
sel := Doc().Find("div.row-fluid")
|
||||||
if sel.Nodes == nil || len(sel.Nodes) != 9 {
|
if sel.Nodes == nil || len(sel.Nodes) != 9 {
|
||||||
t.Errorf("Expected 9 matching nodes, found %v.", len(sel.Nodes))
|
t.Errorf("Expected 9 matching nodes, found %v.", len(sel.Nodes))
|
||||||
}
|
}
|
||||||
@@ -18,13 +18,13 @@ func TestFindInvalidSelector(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
doc.Find(":+ ^")
|
Doc().Find(":+ ^")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEachEmptySelection(t *testing.T) {
|
func TestEachEmptySelection(t *testing.T) {
|
||||||
var cnt int
|
var cnt int
|
||||||
|
|
||||||
sel := doc.Find("zzzz")
|
sel := Doc().Find("zzzz")
|
||||||
sel.Each(func(i int, n *Selection) {
|
sel.Each(func(i int, n *Selection) {
|
||||||
cnt++
|
cnt++
|
||||||
})
|
})
|
||||||
@@ -38,14 +38,14 @@ func TestEachEmptySelection(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestChainedFind(t *testing.T) {
|
func TestChainedFind(t *testing.T) {
|
||||||
sel := doc.Find("div.hero-unit").Find(".row-fluid")
|
sel := Doc().Find("div.hero-unit").Find(".row-fluid")
|
||||||
if sel.Nodes == nil || len(sel.Nodes) != 4 {
|
if sel.Nodes == nil || len(sel.Nodes) != 4 {
|
||||||
t.Errorf("Expected 4 matching nodes, found %v.", len(sel.Nodes))
|
t.Errorf("Expected 4 matching nodes, found %v.", len(sel.Nodes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChildren(t *testing.T) {
|
func TestChildren(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content").Children()
|
sel := Doc().Find(".pvk-content").Children()
|
||||||
if len(sel.Nodes) != 13 {
|
if len(sel.Nodes) != 13 {
|
||||||
t.Errorf("Expected 13 child nodes, got %v.", len(sel.Nodes))
|
t.Errorf("Expected 13 child nodes, got %v.", len(sel.Nodes))
|
||||||
for _, n := range sel.Nodes {
|
for _, n := range sel.Nodes {
|
||||||
@@ -55,7 +55,7 @@ func TestChildren(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestChildrenFiltered(t *testing.T) {
|
func TestChildrenFiltered(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content").ChildrenFiltered(".hero-unit")
|
sel := Doc().Find(".pvk-content").ChildrenFiltered(".hero-unit")
|
||||||
if len(sel.Nodes) != 1 {
|
if len(sel.Nodes) != 1 {
|
||||||
t.Errorf("Expected 1 child nodes, got %v.", len(sel.Nodes))
|
t.Errorf("Expected 1 child nodes, got %v.", len(sel.Nodes))
|
||||||
for _, n := range sel.Nodes {
|
for _, n := range sel.Nodes {
|
||||||
@@ -65,7 +65,7 @@ func TestChildrenFiltered(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestChildrenFilteredNone(t *testing.T) {
|
func TestChildrenFilteredNone(t *testing.T) {
|
||||||
sel := doc.Find(".pvk-content").ChildrenFiltered("a.btn")
|
sel := Doc().Find(".pvk-content").ChildrenFiltered("a.btn")
|
||||||
if len(sel.Nodes) != 0 {
|
if len(sel.Nodes) != 0 {
|
||||||
t.Errorf("Expected 0 child node, got %v.", len(sel.Nodes))
|
t.Errorf("Expected 0 child node, got %v.", len(sel.Nodes))
|
||||||
for _, n := range sel.Nodes {
|
for _, n := range sel.Nodes {
|
||||||
|
|||||||
@@ -8,6 +8,13 @@ import (
|
|||||||
|
|
||||||
var doc *Document
|
var doc *Document
|
||||||
|
|
||||||
|
func Doc() *Document {
|
||||||
|
if doc == nil {
|
||||||
|
EnsureDocLoaded()
|
||||||
|
}
|
||||||
|
return doc
|
||||||
|
}
|
||||||
|
|
||||||
func EnsureDocLoaded() {
|
func EnsureDocLoaded() {
|
||||||
if f, e := os.Open("./testdata/page.html"); e != nil {
|
if f, e := os.Open("./testdata/page.html"); e != nil {
|
||||||
panic(e.Error())
|
panic(e.Error())
|
||||||
|
|||||||
Reference in New Issue
Block a user