diff --git a/array_test.go b/array_test.go index e1f7d42..d10937a 100644 --- a/array_test.go +++ b/array_test.go @@ -5,7 +5,7 @@ import ( ) func TestFirst(t *testing.T) { - sel := doc.Find(".pvk-content").First() + sel := Doc().Find(".pvk-content").First() if len(sel.Nodes) != 1 { 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.") } }() - doc.Find(".pvk-zzcontentzz").First() + Doc().Find(".pvk-zzcontentzz").First() } func TestLast(t *testing.T) { - sel := doc.Find(".pvk-content").Last() + sel := Doc().Find(".pvk-content").Last() if len(sel.Nodes) != 1 { t.Errorf("Expected 1 node, found %v.", len(sel.Nodes)) } // Should contain Footer - foot := doc.Find(".footer") + foot := Doc().Find(".footer") if !sel.Contains(foot.Nodes[0]) { t.Error("Last .pvk-content should contain .footer.") } } func TestEq(t *testing.T) { - sel := doc.Find(".pvk-content").Eq(1) + sel := Doc().Find(".pvk-content").Eq(1) if len(sel.Nodes) != 1 { t.Errorf("Expected 1 node, found %v.", len(sel.Nodes)) } } func TestEqNegative(t *testing.T) { - sel := doc.Find(".pvk-content").Eq(-1) + sel := Doc().Find(".pvk-content").Eq(-1) if len(sel.Nodes) != 1 { t.Errorf("Expected 1 node, found %v.", len(sel.Nodes)) } // Should contain Footer - foot := doc.Find(".footer") + foot := Doc().Find(".footer") if !sel.Contains(foot.Nodes[0]) { t.Error("Index -1 of .pvk-content should contain .footer.") } } 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 { 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.") } }() - doc.Find(".pvk-content").Slice(2, 12) + Doc().Find(".pvk-content").Slice(2, 12) } func TestGet(t *testing.T) { - sel := doc.Find(".pvk-content") + sel := Doc().Find(".pvk-content") node := sel.Get(1) if sel.Nodes[1] != node { 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) { - sel := doc.Find(".pvk-content") + sel := Doc().Find(".pvk-content") node := sel.Get(-3) if sel.Nodes[0] != node { 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) } func TestIndex(t *testing.T) { - sel := doc.Find(".pvk-content") + sel := Doc().Find(".pvk-content") if i := sel.Index(); i != 1 { t.Errorf("Expected index of 1, got %v.", i) } } func TestIndexSelector(t *testing.T) { - sel := doc.Find(".hero-unit") + sel := Doc().Find(".hero-unit") if i := sel.IndexSelector("div"); i != 4 { t.Errorf("Expected index of 4, got %v.", i) } } 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 { t.Errorf("Expected index of 1, got %v.", i) } } func TestIndexOfNilNode(t *testing.T) { - sel := doc.Find("div.pvk-gutter") + sel := Doc().Find("div.pvk-gutter") if i := sel.IndexOfNode(nil); i != -1 { t.Errorf("Expected index of -1, got %v.", i) } } func TestIndexOfSelection(t *testing.T) { - sel := doc.Find("div") - sel2 := doc.Find(".hero-unit") + sel := Doc().Find("div") + sel2 := Doc().Find(".hero-unit") if i := sel.IndexOfSelection(sel2); i != 4 { t.Errorf("Expected index of 4, got %v.", i) } diff --git a/expand_test.go b/expand_test.go index b78367c..333d5cc 100644 --- a/expand_test.go +++ b/expand_test.go @@ -9,7 +9,7 @@ func TestAdd(t *testing.T) { EnsureDocLoaded() - sel := doc.Find("div.row-fluid") + sel := Doc().Find("div.row-fluid") cnt = len(sel.Nodes) sel2 := sel.Add("a") if sel != sel2 { diff --git a/filter_test.go b/filter_test.go index 688fba7..b16f1ce 100644 --- a/filter_test.go +++ b/filter_test.go @@ -5,21 +5,21 @@ import ( ) func TestFilter(t *testing.T) { - sel := doc.Find(".span12").Filter(".alert") + sel := Doc().Find(".span12").Filter(".alert") if len(sel.Nodes) != 1 { t.Errorf("Expected 1 node, found %v.", len(sel.Nodes)) } } func TestFilterNone(t *testing.T) { - sel := doc.Find(".span12").Filter(".zzalert") + sel := Doc().Find(".span12").Filter(".zzalert") if sel.Nodes != nil { t.Error("Expected no node (nil), found some.") } } 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 }) if len(sel.Nodes) != 2 { @@ -28,7 +28,7 @@ func TestFilterFunction(t *testing.T) { } func TestFilterNode(t *testing.T) { - sel := doc.Find(".pvk-content") + sel := Doc().Find(".pvk-content") sel2 := sel.FilterNodes(sel.Nodes[2]) if len(sel2.Nodes) != 1 { t.Errorf("Expected 1 node, found %v.", len(sel2.Nodes)) @@ -36,8 +36,8 @@ func TestFilterNode(t *testing.T) { } func TestFilterSelection(t *testing.T) { - sel := doc.Find(".link") - sel2 := doc.Find("a[ng-click]") + sel := Doc().Find(".link") + sel2 := Doc().Find("a[ng-click]") sel3 := sel.FilterSelection(sel2) if len(sel3.Nodes) != 1 { t.Errorf("Expected 1 node, found %v.", len(sel3.Nodes)) diff --git a/iteration_test.go b/iteration_test.go index 94cf1f3..f1753e4 100644 --- a/iteration_test.go +++ b/iteration_test.go @@ -7,7 +7,7 @@ import ( func TestEach(t *testing.T) { 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++ t.Logf("At index %v, node %v", i, n.Nodes[0].Data) }).Find("a") diff --git a/property_test.go b/property_test.go index 088a293..d15c704 100644 --- a/property_test.go +++ b/property_test.go @@ -7,7 +7,7 @@ import ( func TestAttrExists(t *testing.T) { 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.") } else { t.Logf("Href of first anchor: %v.", val) @@ -15,7 +15,7 @@ func TestAttrExists(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) } } diff --git a/query_test.go b/query_test.go index efd9e4c..e0aeaaa 100644 --- a/query_test.go +++ b/query_test.go @@ -5,7 +5,7 @@ import ( ) func TestHasClass(t *testing.T) { - sel := doc.Find("div") + sel := Doc().Find("div") if !sel.HasClass("span12") { t.Error("Expected at least one div to have class span12.") } diff --git a/traversal_test.go b/traversal_test.go index a2defa2..13f794e 100644 --- a/traversal_test.go +++ b/traversal_test.go @@ -5,7 +5,7 @@ import ( ) 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 { 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) { var cnt int - sel := doc.Find("zzzz") + sel := Doc().Find("zzzz") sel.Each(func(i int, n *Selection) { cnt++ }) @@ -38,14 +38,14 @@ func TestEachEmptySelection(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 { t.Errorf("Expected 4 matching nodes, found %v.", len(sel.Nodes)) } } func TestChildren(t *testing.T) { - sel := doc.Find(".pvk-content").Children() + sel := Doc().Find(".pvk-content").Children() if len(sel.Nodes) != 13 { t.Errorf("Expected 13 child nodes, got %v.", len(sel.Nodes)) for _, n := range sel.Nodes { @@ -55,7 +55,7 @@ func TestChildren(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 { t.Errorf("Expected 1 child nodes, got %v.", len(sel.Nodes)) for _, n := range sel.Nodes { @@ -65,7 +65,7 @@ func TestChildrenFiltered(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 { t.Errorf("Expected 0 child node, got %v.", len(sel.Nodes)) for _, n := range sel.Nodes { diff --git a/type_test.go b/type_test.go index b95a7a3..507954e 100644 --- a/type_test.go +++ b/type_test.go @@ -8,6 +8,13 @@ import ( var doc *Document +func Doc() *Document { + if doc == nil { + EnsureDocLoaded() + } + return doc +} + func EnsureDocLoaded() { if f, e := os.Open("./testdata/page.html"); e != nil { panic(e.Error())