add PrevUntilSelection() and PrevUntilNodes() with tests

This commit is contained in:
Martin Angers
2012-09-06 10:57:54 -04:00
parent 7dcaa4213b
commit eea8ab69ed
7 changed files with 86 additions and 8 deletions
+1 -1
View File
@@ -72,7 +72,7 @@ Taken from example_test.go:
// remove the leading "x" before Output on the next line. This will cause the
// example to fail (all the "real" tests should pass).
// xOutput: volutarily fail the Example output.
// xOutput: voluntarily fail the Example output.
}
+1 -1
View File
@@ -38,5 +38,5 @@ func ExampleScrape_MetalReview() {
// remove the leading "x" before Output on the next line. This will cause the
// example to fail (all the "real" tests should pass).
// xOutput: volutarily fail the Example output.
// xOutput: voluntarily fail the Example output.
}
-2
View File
@@ -7,8 +7,6 @@ import (
)
func TestAttrExists(t *testing.T) {
EnsureDocLoaded()
if val, ok := Doc().Root.Find("a").Attr("href"); !ok {
t.Error("Expected a value for the href attribute.")
} else {
+16
View File
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Tests for siblings</title>
</head>
<body>
<div id="main">
<div id="n1"></div>
<div id="n2"></div>
<div id="n3"></div>
<div id="n4"></div>
<div id="n5"></div>
<div id="n6"></div>
</div>
</body>
</html>
+18
View File
@@ -228,6 +228,24 @@ func (this *Selection) PrevUntil(selector string) *Selection {
selector, nil))
}
// PrevUntilSelection() gets all preceding siblings of each element up to but not
// including the element matched by the Selection. It returns a new Selection
// object containing the matched elements.
func (this *Selection) PrevUntilSelection(sel *Selection) *Selection {
if sel == nil {
return this.PrevAll()
}
return this.PrevUntilNodes(sel.Nodes...)
}
// PrevUntilNodes() gets all preceding siblings of each element up to but not
// including the element matched by the nodes. It returns a new Selection
// object containing the matched elements.
func (this *Selection) PrevUntilNodes(nodes ...*html.Node) *Selection {
return pushStack(this, getSiblingNodes(this.Nodes, siblingPrevUntil,
"", nodes))
}
// Filter and push filters the nodes based on a selector, and pushes the results
// on the stack, with the srcSel as previous selection.
func filterAndPush(srcSel *Selection, nodes []*html.Node, selector string) *Selection {
+37
View File
@@ -439,3 +439,40 @@ func TestPrevUntilRollback(t *testing.T) {
sel2 := sel.PrevUntil("#cf2-1").End()
AssertEqual(t, sel, sel2)
}
func TestPrevUntilSelection(t *testing.T) {
sel := Doc2().Root.Find("#n4")
sel2 := Doc2().Root.Find("#n2")
sel2 = sel.PrevUntilSelection(sel2)
AssertLength(t, sel2.Nodes, 1)
if !sel2.Eq(0).Is("#n3") {
t.Errorf("Expected node 0 to be n3, found %+v.", sel2.Get(0))
}
}
func TestPrevUntilSelectionRollback(t *testing.T) {
sel := Doc2().Root.Find("#n4")
sel2 := Doc2().Root.Find("#n2")
sel2 = sel.PrevUntilSelection(sel2).End()
AssertEqual(t, sel, sel2)
}
func TestPrevUntilNodes(t *testing.T) {
sel := Doc2().Root.Find("#n5")
sel2 := Doc2().Root.Find("#n2")
sel2 = sel.PrevUntilNodes(sel2.Nodes...)
AssertLength(t, sel2.Nodes, 2)
if !sel2.Eq(0).Is("#n4") {
t.Errorf("Expected node 0 to be n4, found %+v.", sel2.Get(0))
}
if !sel2.Eq(1).Is("#n3") {
t.Errorf("Expected node 1 to be n3, found %+v.", sel2.Get(1))
}
}
func TestPrevUntilNodesRollback(t *testing.T) {
sel := Doc2().Root.Find("#n5")
sel2 := Doc2().Root.Find("#n2")
sel2 = sel.PrevUntilNodes(sel2.Nodes...).End()
AssertEqual(t, sel, sel2)
}
+13 -4
View File
@@ -2,19 +2,27 @@ package goquery
import (
"exp/html"
"fmt"
"os"
"testing"
)
// Test helper functions and members
var doc *Document
var doc2 *Document
func Doc() *Document {
if doc == nil {
EnsureDocLoaded()
doc = LoadDoc("page.html")
}
return doc
}
func Doc2() *Document {
if doc2 == nil {
doc2 = LoadDoc("page2.html")
}
return doc2
}
func AssertLength(t *testing.T, nodes []*html.Node, length int) {
if len(nodes) != length {
@@ -43,17 +51,18 @@ func AssertEqual(t *testing.T, s1 *Selection, s2 *Selection) {
}
}
func EnsureDocLoaded() {
if f, e := os.Open("./testdata/page.html"); e != nil {
func LoadDoc(page string) *Document {
if f, e := os.Open(fmt.Sprintf("./testdata/%s", page)); e != nil {
panic(e.Error())
} else {
defer f.Close()
if node, e := html.Parse(f); e != nil {
panic(e.Error())
} else {
doc = NewDocumentFromNode(node)
return NewDocumentFromNode(node)
}
}
return nil
}
func TestNewDocument(t *testing.T) {