mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-05 08:46:53 +08:00
add Parent() and ParentFiltered(), refactor tests using Asserts...()
This commit is contained in:
@@ -6,81 +6,61 @@ import (
|
||||
|
||||
func TestFind(t *testing.T) {
|
||||
sel := Doc().Root.Find("div.row-fluid")
|
||||
if sel.Nodes == nil || len(sel.Nodes) != 9 {
|
||||
t.Errorf("Expected 9 matching nodes, found %v.", len(sel.Nodes))
|
||||
}
|
||||
AssertLength(t, sel.Nodes, 9)
|
||||
}
|
||||
|
||||
func TestFindNotSelf(t *testing.T) {
|
||||
sel := Doc().Root.Find("h1").Find("h1")
|
||||
if len(sel.Nodes) > 0 {
|
||||
t.Errorf("Expected no node, found %v.", len(sel.Nodes))
|
||||
}
|
||||
AssertLength(t, sel.Nodes, 0)
|
||||
}
|
||||
|
||||
func TestFindInvalidSelector(t *testing.T) {
|
||||
defer func() {
|
||||
if e := recover(); e == nil {
|
||||
t.Error("Expected panic due to invalid selector.")
|
||||
}
|
||||
}()
|
||||
|
||||
defer AssertPanic(t)
|
||||
Doc().Root.Find(":+ ^")
|
||||
}
|
||||
|
||||
func TestChainedFind(t *testing.T) {
|
||||
sel := Doc().Root.Find("div.hero-unit").Find(".row-fluid")
|
||||
if sel.Nodes == nil || len(sel.Nodes) != 4 {
|
||||
t.Errorf("Expected 4 matching nodes, found %v.", len(sel.Nodes))
|
||||
}
|
||||
AssertLength(t, sel.Nodes, 4)
|
||||
}
|
||||
|
||||
func TestChildren(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content").Children()
|
||||
if len(sel.Nodes) != 5 {
|
||||
t.Errorf("Expected 5 child nodes, got %v.", len(sel.Nodes))
|
||||
for _, n := range sel.Nodes {
|
||||
t.Logf("%+v", n)
|
||||
}
|
||||
}
|
||||
AssertLength(t, sel.Nodes, 5)
|
||||
}
|
||||
|
||||
func TestContents(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content").Contents()
|
||||
if len(sel.Nodes) != 13 {
|
||||
t.Errorf("Expected 13 child nodes, got %v.", len(sel.Nodes))
|
||||
for _, n := range sel.Nodes {
|
||||
t.Logf("%+v", n)
|
||||
}
|
||||
}
|
||||
AssertLength(t, sel.Nodes, 13)
|
||||
}
|
||||
|
||||
func TestChildrenFiltered(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content").ChildrenFiltered(".hero-unit")
|
||||
if len(sel.Nodes) != 1 {
|
||||
t.Errorf("Expected 1 child nodes, got %v.", len(sel.Nodes))
|
||||
for _, n := range sel.Nodes {
|
||||
t.Logf("%+v", n)
|
||||
}
|
||||
}
|
||||
AssertLength(t, sel.Nodes, 1)
|
||||
}
|
||||
|
||||
func TestContentsFiltered(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content").ContentsFiltered(".hero-unit")
|
||||
if len(sel.Nodes) != 1 {
|
||||
t.Errorf("Expected 1 child nodes, got %v.", len(sel.Nodes))
|
||||
for _, n := range sel.Nodes {
|
||||
t.Logf("%+v", n)
|
||||
}
|
||||
}
|
||||
AssertLength(t, sel.Nodes, 1)
|
||||
}
|
||||
|
||||
func TestChildrenFilteredNone(t *testing.T) {
|
||||
sel := Doc().Root.Find(".pvk-content").ChildrenFiltered("a.btn")
|
||||
if len(sel.Nodes) != 0 {
|
||||
t.Errorf("Expected 0 child node, got %v.", len(sel.Nodes))
|
||||
for _, n := range sel.Nodes {
|
||||
t.Logf("%+v", n)
|
||||
}
|
||||
}
|
||||
AssertLength(t, sel.Nodes, 0)
|
||||
}
|
||||
|
||||
func TestParent(t *testing.T) {
|
||||
sel := Doc().Root.Find(".container-fluid").Parent()
|
||||
AssertLength(t, sel.Nodes, 3)
|
||||
}
|
||||
|
||||
func TestParentBody(t *testing.T) {
|
||||
sel := Doc().Root.Find("body").Parent()
|
||||
AssertLength(t, sel.Nodes, 1)
|
||||
}
|
||||
|
||||
func TestParentFiltered(t *testing.T) {
|
||||
sel := Doc().Root.Find(".container-fluid").ParentFiltered(".hero-unit")
|
||||
AssertLength(t, sel.Nodes, 1)
|
||||
AssertClass(t, sel, "hero-unit")
|
||||
}
|
||||
|
Reference in New Issue
Block a user