diff --git a/README.md b/README.md index 04a74b7..ca62e80 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,6 @@ Coming soon... * Implement NextUntil() and PrevUntil(). * Fix Prev...() functions to return nodes in the same order as jQuery (starting with the immediately preceding node, up until first child of the parent). -* Tests to validate that all methods returning a new `*Selection` "rollback" correctly to the previous Selection when calling `.End()`. Done for array, expand and filter. * Benchmarks so that future changes have a baseline to compare to. * Add jQuery's `Closest()`? Other missing functions? * Support negative indices in `Slice()`, like jQuery. diff --git a/property_test.go b/property_test.go index 17355ba..8257ff6 100644 --- a/property_test.go +++ b/property_test.go @@ -39,16 +39,16 @@ func TestText2(t *testing.T) { } } -/*func TestText3(t *testing.T) { +func TestText3(t *testing.T) { txt := Doc().Root.Find(".pvk-gutter").First().Text() - if ok, e := regexp.MatchString(`^\s+$`, txt); !ok || e != nil { - t.Errorf("Expected spaces, found %v.", txt) + // There's an   character in there... + if ok, e := regexp.MatchString(`^[\s\x{00A0}]+$`, txt); !ok || e != nil { + t.Errorf("Expected spaces, found <%v>.", txt) if e != nil { t.Logf("Error: %s.", e.Error()) } } } -*/ func TestHtml(t *testing.T) { txt, e := Doc().Root.Find("h1").Html() diff --git a/query_test.go b/query_test.go index 0d3822a..ae0c08c 100644 --- a/query_test.go +++ b/query_test.go @@ -35,6 +35,16 @@ func TestIsFunction(t *testing.T) { } } +func TestIsFunctionRollback(t *testing.T) { + ok := Doc().Root.Find("div").IsFunction(func(i int, s *Selection) bool { + return s.HasClass("container-fluid") + }) + + if !ok { + t.Error("Expected some div to have a container-fluid class.") + } +} + func TestIsSelection(t *testing.T) { sel := Doc().Root.Find("div") sel2 := Doc().Root.Find(".pvk-gutter") diff --git a/traversal_test.go b/traversal_test.go index 1d1c69e..d7483eb 100644 --- a/traversal_test.go +++ b/traversal_test.go @@ -9,6 +9,12 @@ func TestFind(t *testing.T) { AssertLength(t, sel.Nodes, 9) } +func TestFindRollback(t *testing.T) { + sel := Doc().Root.Find("div.row-fluid") + sel2 := sel.Find("a").End() + AssertEqual(t, sel, sel2) +} + func TestFindNotSelf(t *testing.T) { sel := Doc().Root.Find("h1").Find("h1") AssertLength(t, sel.Nodes, 0) @@ -29,21 +35,45 @@ func TestChildren(t *testing.T) { AssertLength(t, sel.Nodes, 5) } +func TestChildrenRollback(t *testing.T) { + sel := Doc().Root.Find(".pvk-content") + sel2 := sel.Children().End() + AssertEqual(t, sel, sel2) +} + func TestContents(t *testing.T) { sel := Doc().Root.Find(".pvk-content").Contents() AssertLength(t, sel.Nodes, 13) } +func TestContentsRollback(t *testing.T) { + sel := Doc().Root.Find(".pvk-content") + sel2 := sel.Contents().End() + AssertEqual(t, sel, sel2) +} + func TestChildrenFiltered(t *testing.T) { sel := Doc().Root.Find(".pvk-content").ChildrenFiltered(".hero-unit") AssertLength(t, sel.Nodes, 1) } +func TestChildrenFilteredRollback(t *testing.T) { + sel := Doc().Root.Find(".pvk-content") + sel2 := sel.ChildrenFiltered(".hero-unit").End() + AssertEqual(t, sel, sel2) +} + func TestContentsFiltered(t *testing.T) { sel := Doc().Root.Find(".pvk-content").ContentsFiltered(".hero-unit") AssertLength(t, sel.Nodes, 1) } +func TestContentsFilteredRollback(t *testing.T) { + sel := Doc().Root.Find(".pvk-content") + sel2 := sel.ContentsFiltered(".hero-unit").End() + AssertEqual(t, sel, sel2) +} + func TestChildrenFilteredNone(t *testing.T) { sel := Doc().Root.Find(".pvk-content").ChildrenFiltered("a.btn") AssertLength(t, sel.Nodes, 0) @@ -54,6 +84,12 @@ func TestParent(t *testing.T) { AssertLength(t, sel.Nodes, 3) } +func TestParentRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := sel.Parent().End() + AssertEqual(t, sel, sel2) +} + func TestParentBody(t *testing.T) { sel := Doc().Root.Find("body").Parent() AssertLength(t, sel.Nodes, 1) @@ -65,21 +101,45 @@ func TestParentFiltered(t *testing.T) { AssertClass(t, sel, "hero-unit") } +func TestParentFilteredRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := sel.ParentFiltered(".hero-unit").End() + AssertEqual(t, sel, sel2) +} + func TestParents(t *testing.T) { sel := Doc().Root.Find(".container-fluid").Parents() AssertLength(t, sel.Nodes, 8) } +func TestParentsRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := sel.Parents().End() + AssertEqual(t, sel, sel2) +} + func TestParentsFiltered(t *testing.T) { sel := Doc().Root.Find(".container-fluid").ParentsFiltered("body") AssertLength(t, sel.Nodes, 1) } +func TestParentsFilteredRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := sel.ParentsFiltered("body").End() + AssertEqual(t, sel, sel2) +} + func TestParentsUntil(t *testing.T) { sel := Doc().Root.Find(".container-fluid").ParentsUntil("body") AssertLength(t, sel.Nodes, 6) } +func TestParentsUntilRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := sel.ParentsUntil("body").End() + AssertEqual(t, sel, sel2) +} + func TestParentsUntilSelection(t *testing.T) { sel := Doc().Root.Find(".container-fluid") sel2 := Doc().Root.Find(".pvk-content") @@ -87,6 +147,13 @@ func TestParentsUntilSelection(t *testing.T) { AssertLength(t, sel.Nodes, 3) } +func TestParentsUntilSelectionRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := Doc().Root.Find(".pvk-content") + sel2 = sel.ParentsUntilSelection(sel2).End() + AssertEqual(t, sel, sel2) +} + func TestParentsUntilNodes(t *testing.T) { sel := Doc().Root.Find(".container-fluid") sel2 := Doc().Root.Find(".pvk-content, .hero-unit") @@ -94,11 +161,24 @@ func TestParentsUntilNodes(t *testing.T) { AssertLength(t, sel.Nodes, 2) } +func TestParentsUntilNodesRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := Doc().Root.Find(".pvk-content, .hero-unit") + sel2 = sel.ParentsUntilNodes(sel2.Nodes...).End() + AssertEqual(t, sel, sel2) +} + func TestParentsFilteredUntil(t *testing.T) { sel := Doc().Root.Find(".container-fluid").ParentsFilteredUntil(".pvk-content", "body") AssertLength(t, sel.Nodes, 2) } +func TestParentsFilteredUntilRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := sel.ParentsFilteredUntil(".pvk-content", "body").End() + AssertEqual(t, sel, sel2) +} + func TestParentsFilteredUntilSelection(t *testing.T) { sel := Doc().Root.Find(".container-fluid") sel2 := Doc().Root.Find(".row-fluid") @@ -106,6 +186,13 @@ func TestParentsFilteredUntilSelection(t *testing.T) { AssertLength(t, sel.Nodes, 3) } +func TestParentsFilteredUntilSelectionRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := Doc().Root.Find(".row-fluid") + sel2 = sel.ParentsFilteredUntilSelection("div", sel2).End() + AssertEqual(t, sel, sel2) +} + func TestParentsFilteredUntilNodes(t *testing.T) { sel := Doc().Root.Find(".container-fluid") sel2 := Doc().Root.Find(".row-fluid") @@ -113,11 +200,24 @@ func TestParentsFilteredUntilNodes(t *testing.T) { AssertLength(t, sel.Nodes, 1) } +func TestParentsFilteredUntilNodesRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := Doc().Root.Find(".row-fluid") + sel2 = sel.ParentsFilteredUntilNodes("body", sel2.Nodes...).End() + AssertEqual(t, sel, sel2) +} + func TestSiblings(t *testing.T) { sel := Doc().Root.Find("h1").Siblings() AssertLength(t, sel.Nodes, 1) } +func TestSiblingsRollback(t *testing.T) { + sel := Doc().Root.Find("h1") + sel2 := sel.Siblings().End() + AssertEqual(t, sel, sel2) +} + func TestSiblings2(t *testing.T) { sel := Doc().Root.Find(".pvk-gutter").Siblings() AssertLength(t, sel.Nodes, 9) @@ -133,11 +233,23 @@ func TestSiblingsFiltered(t *testing.T) { AssertLength(t, sel.Nodes, 3) } +func TestSiblingsFilteredRollback(t *testing.T) { + sel := Doc().Root.Find(".pvk-gutter") + sel2 := sel.SiblingsFiltered(".pvk-content").End() + AssertEqual(t, sel, sel2) +} + func TestNext(t *testing.T) { sel := Doc().Root.Find("h1").Next() AssertLength(t, sel.Nodes, 1) } +func TestNextRollback(t *testing.T) { + sel := Doc().Root.Find("h1") + sel2 := sel.Next().End() + AssertEqual(t, sel, sel2) +} + func TestNext2(t *testing.T) { sel := Doc().Root.Find(".close").Next() AssertLength(t, sel.Nodes, 1) @@ -153,6 +265,12 @@ func TestNextFiltered(t *testing.T) { AssertLength(t, sel.Nodes, 2) } +func TestNextFilteredRollback(t *testing.T) { + sel := Doc().Root.Find(".container-fluid") + sel2 := sel.NextFiltered("div").End() + AssertEqual(t, sel, sel2) +} + func TestNextFiltered2(t *testing.T) { sel := Doc().Root.Find(".container-fluid").NextFiltered("[ng-view]") AssertLength(t, sel.Nodes, 1) @@ -164,6 +282,12 @@ func TestPrev(t *testing.T) { AssertClass(t, sel, "green") } +func TestPrevRollback(t *testing.T) { + sel := Doc().Root.Find(".red") + sel2 := sel.Prev().End() + AssertEqual(t, sel, sel2) +} + func TestPrev2(t *testing.T) { sel := Doc().Root.Find(".row-fluid").Prev() AssertLength(t, sel.Nodes, 5) @@ -179,11 +303,23 @@ func TestPrevFiltered(t *testing.T) { AssertLength(t, sel.Nodes, 5) } +func TestPrevFilteredRollback(t *testing.T) { + sel := Doc().Root.Find(".row-fluid") + sel2 := sel.PrevFiltered(".row-fluid").End() + AssertEqual(t, sel, sel2) +} + func TestNextAll(t *testing.T) { sel := Doc().Root.Find("#cf2 div:nth-child(1)").NextAll() AssertLength(t, sel.Nodes, 3) } +func TestNextAllRollback(t *testing.T) { + sel := Doc().Root.Find("#cf2 div:nth-child(1)") + sel2 := sel.NextAll().End() + AssertEqual(t, sel, sel2) +} + func TestNextAll2(t *testing.T) { sel := Doc().Root.Find("div[ng-cloak]").NextAll() AssertLength(t, sel.Nodes, 1) @@ -199,6 +335,12 @@ func TestNextAllFiltered(t *testing.T) { AssertLength(t, sel.Nodes, 2) } +func TestNextAllFilteredRollback(t *testing.T) { + sel := Doc().Root.Find("#cf2 .row-fluid") + sel2 := sel.NextAllFiltered("[ng-cloak]").End() + AssertEqual(t, sel, sel2) +} + func TestNextAllFiltered2(t *testing.T) { sel := Doc().Root.Find(".close").NextAllFiltered("h4") AssertLength(t, sel.Nodes, 1) @@ -209,6 +351,12 @@ func TestPrevAll(t *testing.T) { AssertLength(t, sel.Nodes, 2) } +func TestPrevAllRollback(t *testing.T) { + sel := Doc().Root.Find("[ng-view]") + sel2 := sel.PrevAll().End() + AssertEqual(t, sel, sel2) +} + func TestPrevAll2(t *testing.T) { sel := Doc().Root.Find(".pvk-gutter").PrevAll() AssertLength(t, sel.Nodes, 6) @@ -218,3 +366,9 @@ func TestPrevAllFiltered(t *testing.T) { sel := Doc().Root.Find(".pvk-gutter").PrevAllFiltered(".pvk-content") AssertLength(t, sel.Nodes, 3) } + +func TestPrevAllFilteredRollback(t *testing.T) { + sel := Doc().Root.Find(".pvk-gutter") + sel2 := sel.PrevAllFiltered(".pvk-content").End() + AssertEqual(t, sel, sel2) +}