finalize iterators, with tests and comments.

This commit is contained in:
Martin Angers
2012-09-01 10:55:01 -04:00
parent e6faf847cc
commit 8ddd8c6655
3 changed files with 23 additions and 2 deletions
+1 -1
View File
@@ -102,7 +102,7 @@ package goquery
// DONE query.go : Reflect (query) node: Is(), Contains(), HasClass()
// property.go : Inspect node: Contents(), Html(), Text(), Attr(), ** Does it make sense in a static HTML tree? Val()**, Length(), Size()
// traversal.go : Traversal: Find(), Children(), Parents...(), Next...(), Prev...(), Closest(), Siblings()
// iteration.go : Iteration: Each(), Map()
// DONE iteration.go : Iteration: Each(), Map()
// type.go : Selection and Document
// TODO : Benchmarks
+2 -1
View File
@@ -1,6 +1,7 @@
package goquery
// Returns this (same Selection object)
// Each() iterates over a Selection object, executing a function for each
// matched element.
func (this *Selection) Each(f func(int, *Selection)) *Selection {
for i, n := range this.Nodes {
f(i, newSingleSelection(n, this.document))
+20
View File
@@ -1,6 +1,7 @@
package goquery
import (
"exp/html"
"testing"
)
@@ -19,3 +20,22 @@ func TestEach(t *testing.T) {
t.Errorf("Expected 6 matching nodes, found %v.", len(sel.Nodes))
}
}
func TestMap(t *testing.T) {
sel := Doc().Find(".pvk-content")
vals := sel.Map(func(i int, s *Selection) string {
n := s.Get(0)
if n.Type == html.ElementNode {
return n.Data
}
return ""
})
for _, v := range vals {
if v != "div" {
t.Error("Expected Map array result to be all 'div's.")
}
}
if len(vals) != 3 {
t.Errorf("Expected Map array result to have a length of 3, found %v.", len(vals))
}
}