add Parent() and ParentFiltered(), refactor tests using Asserts...()

This commit is contained in:
Martin Angers
2012-09-04 09:52:19 -04:00
parent 81b024325c
commit 01f04bca8b
7 changed files with 148 additions and 186 deletions

View File

@@ -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")
}