From 9cc531274b7c8c1ade2840626830dad17434933c Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Wed, 15 Jun 2016 08:42:27 -0400 Subject: [PATCH] implement tests for selection methods with invalid selector strings --- array_test.go | 32 +++++++++++++++++ expand_test.go | 10 ++++++ filter.go | 4 +-- filter_test.go | 15 ++++++++ i114_test.go | 15 -------- query_test.go | 7 ++++ traversal_test.go | 92 +++++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 155 insertions(+), 20 deletions(-) delete mode 100644 i114_test.go diff --git a/array_test.go b/array_test.go index 1dda525..3a421cf 100644 --- a/array_test.go +++ b/array_test.go @@ -14,6 +14,11 @@ func TestFirstEmpty(t *testing.T) { assertLength(t, sel.Nodes, 0) } +func TestFirstInvalid(t *testing.T) { + sel := Doc().Find("").First() + assertLength(t, sel.Nodes, 0) +} + func TestFirstRollback(t *testing.T) { sel := Doc().Find(".pvk-content") sel2 := sel.First().End() @@ -36,6 +41,11 @@ func TestLastEmpty(t *testing.T) { assertLength(t, sel.Nodes, 0) } +func TestLastInvalid(t *testing.T) { + sel := Doc().Find("").Last() + assertLength(t, sel.Nodes, 0) +} + func TestLastRollback(t *testing.T) { sel := Doc().Find(".pvk-content") sel2 := sel.Last().End() @@ -63,6 +73,11 @@ func TestEqEmpty(t *testing.T) { assertLength(t, sel.Nodes, 0) } +func TestEqInvalid(t *testing.T) { + sel := Doc().Find("").Eq(0) + assertLength(t, sel.Nodes, 0) +} + func TestEqInvalidPositive(t *testing.T) { sel := Doc().Find(".pvk-content").Eq(3) assertLength(t, sel.Nodes, 0) @@ -85,6 +100,16 @@ func TestSlice(t *testing.T) { assertLength(t, sel.Nodes, 2) } +func TestSliceEmpty(t *testing.T) { + sel := Doc().Find("x").Slice(0, 2) + assertLength(t, sel.Nodes, 0) +} + +func TestSliceInvalid(t *testing.T) { + sel := Doc().Find("").Slice(0, 2) + assertLength(t, sel.Nodes, 0) +} + func TestSliceOutOfBounds(t *testing.T) { defer assertPanic(t) Doc().Find(".pvk-content").Slice(2, 12) @@ -157,6 +182,13 @@ func TestIndexSelector(t *testing.T) { } } +func TestIndexSelectorInvalid(t *testing.T) { + sel := Doc().Find(".hero-unit") + if i := sel.IndexSelector(""); i != -1 { + t.Errorf("Expected index of -1, got %v.", i) + } +} + func TestIndexOfNode(t *testing.T) { sel := Doc().Find("div.pvk-gutter") if i := sel.IndexOfNode(sel.Nodes[1]); i != 1 { diff --git a/expand_test.go b/expand_test.go index cff6996..03f35bb 100644 --- a/expand_test.go +++ b/expand_test.go @@ -9,6 +9,16 @@ func TestAdd(t *testing.T) { assertLength(t, sel.Nodes, 19) } +func TestAddInvalid(t *testing.T) { + sel1 := Doc().Find("div.row-fluid") + sel2 := sel1.Add("") + assertLength(t, sel1.Nodes, 19) + assertLength(t, sel2.Nodes, 19) + if sel1 == sel2 { + t.Errorf("selections should not be the same") + } +} + func TestAddRollback(t *testing.T) { sel := Doc().Find(".pvk-content") sel2 := sel.Add("a").End() diff --git a/filter.go b/filter.go index f7e0704..c8d9825 100644 --- a/filter.go +++ b/filter.go @@ -12,8 +12,8 @@ func (s *Selection) Filter(selector string) *Selection { } // FilterMatcher reduces the set of matched elements to those that match -// the given matcher. -// It returns a new Selection object for this subset of matching elements. +// the given matcher. It returns a new Selection object for this subset +// of matching elements. func (s *Selection) FilterMatcher(m Matcher) *Selection { return pushStack(s, winnow(s, m, true)) } diff --git a/filter_test.go b/filter_test.go index d5978b5..f663c08 100644 --- a/filter_test.go +++ b/filter_test.go @@ -14,6 +14,11 @@ func TestFilterNone(t *testing.T) { assertLength(t, sel.Nodes, 0) } +func TestFilterInvalid(t *testing.T) { + sel := Doc().Find(".span12").Filter("") + assertLength(t, sel.Nodes, 0) +} + func TestFilterRollback(t *testing.T) { sel := Doc().Find(".pvk-content") sel2 := sel.Filter(".alert").End() @@ -74,6 +79,11 @@ func TestNot(t *testing.T) { assertLength(t, sel.Nodes, 1) } +func TestNotInvalid(t *testing.T) { + sel := Doc().Find(".span12").Not("") + assertLength(t, sel.Nodes, 2) +} + func TestNotRollback(t *testing.T) { sel := Doc().Find(".span12") sel2 := sel.Not(".alert").End() @@ -145,6 +155,11 @@ func TestHas(t *testing.T) { // Has() returns the high-level .container-fluid div, and the one that is the immediate parent of center-content } +func TestHasInvalid(t *testing.T) { + sel := Doc().Find(".container-fluid").Has("") + assertLength(t, sel.Nodes, 0) +} + func TestHasRollback(t *testing.T) { sel := Doc().Find(".container-fluid") sel2 := sel.Has(".center-content").End() diff --git a/i114_test.go b/i114_test.go deleted file mode 100644 index 038eff1..0000000 --- a/i114_test.go +++ /dev/null @@ -1,15 +0,0 @@ -package goquery - -import ( - "strings" - "testing" -) - -func TestIssue114(t *testing.T) { - d, err := NewDocumentFromReader(strings.NewReader("some title

H1

")) - if err != nil { - t.Error(err) - } - sel := d.Find("~") - t.Log(sel) -} diff --git a/query_test.go b/query_test.go index 2f40f42..54b2a2e 100644 --- a/query_test.go +++ b/query_test.go @@ -11,6 +11,13 @@ func TestIs(t *testing.T) { } } +func TestIsInvalid(t *testing.T) { + sel := Doc().Find(".footer p:nth-child(1)") + if sel.Is("") { + t.Error("Is should not succeed with invalid selector string") + } +} + func TestIsPositional(t *testing.T) { sel := Doc().Find(".footer p:nth-child(2)") if !sel.Is("p:nth-child(2)") { diff --git a/traversal_test.go b/traversal_test.go index 00fb5cd..59b17a8 100644 --- a/traversal_test.go +++ b/traversal_test.go @@ -21,9 +21,9 @@ func TestFindNotSelf(t *testing.T) { assertLength(t, sel.Nodes, 0) } -func TestFindInvalidSelector(t *testing.T) { - defer assertPanic(t) - Doc().Find(":+ ^") +func TestFindInvalid(t *testing.T) { + sel := Doc().Find(":+ ^") + assertLength(t, sel.Nodes, 0) } func TestChainedFind(t *testing.T) { @@ -31,6 +31,11 @@ func TestChainedFind(t *testing.T) { assertLength(t, sel.Nodes, 4) } +func TestChainedFindInvalid(t *testing.T) { + sel := Doc().Find("div.hero-unit").Find("") + assertLength(t, sel.Nodes, 0) +} + func TestChildren(t *testing.T) { sel := Doc().Find(".pvk-content").Children() assertLength(t, sel.Nodes, 5) @@ -58,6 +63,11 @@ func TestChildrenFiltered(t *testing.T) { assertLength(t, sel.Nodes, 1) } +func TestChildrenFilteredInvalid(t *testing.T) { + sel := Doc().Find(".pvk-content").ChildrenFiltered("") + assertLength(t, sel.Nodes, 0) +} + func TestChildrenFilteredRollback(t *testing.T) { sel := Doc().Find(".pvk-content") sel2 := sel.ChildrenFiltered(".hero-unit").End() @@ -69,6 +79,11 @@ func TestContentsFiltered(t *testing.T) { assertLength(t, sel.Nodes, 1) } +func TestContentsFilteredInvalid(t *testing.T) { + sel := Doc().Find(".pvk-content").ContentsFiltered("~") + assertLength(t, sel.Nodes, 0) +} + func TestContentsFilteredRollback(t *testing.T) { sel := Doc().Find(".pvk-content") sel2 := sel.ContentsFiltered(".hero-unit").End() @@ -102,6 +117,11 @@ func TestParentFiltered(t *testing.T) { assertClass(t, sel, "hero-unit") } +func TestParentFilteredInvalid(t *testing.T) { + sel := Doc().Find(".container-fluid").ParentFiltered("") + assertLength(t, sel.Nodes, 0) +} + func TestParentFilteredRollback(t *testing.T) { sel := Doc().Find(".container-fluid") sel2 := sel.ParentFiltered(".hero-unit").End() @@ -130,6 +150,11 @@ func TestParentsFiltered(t *testing.T) { assertLength(t, sel.Nodes, 1) } +func TestParentsFilteredInvalid(t *testing.T) { + sel := Doc().Find(".container-fluid").ParentsFiltered("") + assertLength(t, sel.Nodes, 0) +} + func TestParentsFilteredRollback(t *testing.T) { sel := Doc().Find(".container-fluid") sel2 := sel.ParentsFiltered("body").End() @@ -141,6 +166,11 @@ func TestParentsUntil(t *testing.T) { assertLength(t, sel.Nodes, 6) } +func TestParentsUntilInvalid(t *testing.T) { + sel := Doc().Find(".container-fluid").ParentsUntil("") + assertLength(t, sel.Nodes, 666) // TODO : ? +} + func TestParentsUntilRollback(t *testing.T) { sel := Doc().Find(".container-fluid") sel2 := sel.ParentsUntil("body").End() @@ -180,6 +210,11 @@ func TestParentsFilteredUntil(t *testing.T) { assertLength(t, sel.Nodes, 2) } +func TestParentsFilteredUntilInvalid(t *testing.T) { + sel := Doc().Find(".container-fluid").ParentsFilteredUntil("", "") + assertLength(t, sel.Nodes, 0) +} + func TestParentsFilteredUntilRollback(t *testing.T) { sel := Doc().Find(".container-fluid") sel2 := sel.ParentsFilteredUntil(".pvk-content", "body").End() @@ -240,6 +275,11 @@ func TestSiblingsFiltered(t *testing.T) { assertLength(t, sel.Nodes, 3) } +func TestSiblingsFilteredInvalid(t *testing.T) { + sel := Doc().Find(".pvk-gutter").SiblingsFiltered("") + assertLength(t, sel.Nodes, 0) +} + func TestSiblingsFilteredRollback(t *testing.T) { sel := Doc().Find(".pvk-gutter") sel2 := sel.SiblingsFiltered(".pvk-content").End() @@ -272,6 +312,11 @@ func TestNextFiltered(t *testing.T) { assertLength(t, sel.Nodes, 2) } +func TestNextFilteredInvalid(t *testing.T) { + sel := Doc().Find(".container-fluid").NextFiltered("") + assertLength(t, sel.Nodes, 0) +} + func TestNextFilteredRollback(t *testing.T) { sel := Doc().Find(".container-fluid") sel2 := sel.NextFiltered("div").End() @@ -310,6 +355,11 @@ func TestPrevFiltered(t *testing.T) { assertLength(t, sel.Nodes, 5) } +func TestPrevFilteredInvalid(t *testing.T) { + sel := Doc().Find(".row-fluid").PrevFiltered("") + assertLength(t, sel.Nodes, 0) +} + func TestPrevFilteredRollback(t *testing.T) { sel := Doc().Find(".row-fluid") sel2 := sel.PrevFiltered(".row-fluid").End() @@ -342,6 +392,11 @@ func TestNextAllFiltered(t *testing.T) { assertLength(t, sel.Nodes, 2) } +func TestNextAllFilteredInvalid(t *testing.T) { + sel := Doc().Find("#cf2 .row-fluid").NextAllFiltered("") + assertLength(t, sel.Nodes, 0) +} + func TestNextAllFilteredRollback(t *testing.T) { sel := Doc().Find("#cf2 .row-fluid") sel2 := sel.NextAllFiltered("[ng-cloak]").End() @@ -380,6 +435,11 @@ func TestPrevAllFiltered(t *testing.T) { assertLength(t, sel.Nodes, 3) } +func TestPrevAllFilteredInvalid(t *testing.T) { + sel := Doc().Find(".pvk-gutter").PrevAllFiltered("") + assertLength(t, sel.Nodes, 0) +} + func TestPrevAllFilteredRollback(t *testing.T) { sel := Doc().Find(".pvk-gutter") sel2 := sel.PrevAllFiltered(".pvk-content").End() @@ -392,6 +452,11 @@ func TestNextUntil(t *testing.T) { assertSelectionIs(t, sel, "h4") } +func TestNextUntilInvalid(t *testing.T) { + sel := Doc().Find(".alert a").NextUntil("") + assertLength(t, sel.Nodes, 100) // TODO : ? +} + func TestNextUntil2(t *testing.T) { sel := Doc().Find("#cf2-1").NextUntil("[ng-cloak]") assertLength(t, sel.Nodes, 1) @@ -446,6 +511,11 @@ func TestPrevUntil(t *testing.T) { assertSelectionIs(t, sel, "h4") } +func TestPrevUntilInvalid(t *testing.T) { + sel := Doc().Find(".alert p").PrevUntil("") + assertLength(t, sel.Nodes, 100) // TODO : ? +} + func TestPrevUntil2(t *testing.T) { sel := Doc().Find("[ng-cloak]").PrevUntil(":not([ng-cloak])") assertLength(t, sel.Nodes, 1) @@ -500,6 +570,11 @@ func TestNextFilteredUntil(t *testing.T) { assertSelectionIs(t, sel, "#n3", "#n5", "#nf3", "#nf5") } +func TestNextFilteredUntilInvalid(t *testing.T) { + sel := Doc2().Find(".two").NextFilteredUntil("", "") + assertLength(t, sel.Nodes, 0) +} + func TestNextFilteredUntilRollback(t *testing.T) { sel := Doc2().Find(".two") sel2 := sel.NextFilteredUntil(".even", ".six").End() @@ -542,6 +617,11 @@ func TestPrevFilteredUntil(t *testing.T) { assertSelectionIs(t, sel, "#n4", "#n2", "#nf4", "#nf2") } +func TestPrevFilteredUntilInvalid(t *testing.T) { + sel := Doc2().Find(".five").PrevFilteredUntil("", "") + assertLength(t, sel.Nodes, 0) +} + func TestPrevFilteredUntilRollback(t *testing.T) { sel := Doc2().Find(".four") sel2 := sel.PrevFilteredUntil(".odd", ".one").End() @@ -598,6 +678,12 @@ func TestClosestNone(t *testing.T) { assertLength(t, sel2.Nodes, 0) } +func TestClosestInvalid(t *testing.T) { + sel := Doc().Find("h4") + sel2 := sel.Closest("") + assertLength(t, sel2.Nodes, 0) +} + func TestClosestMany(t *testing.T) { sel := Doc().Find(".container-fluid") sel2 := sel.Closest(".pvk-content")