Fixed inadvertent recursion in SelectNodes. Updated/add tests for this behavior
This commit is contained in:
parent
d5a758279d
commit
ecf8264ac7
14
node.go
14
node.go
|
@ -383,15 +383,13 @@ func (this *Node) SelectNodesRecursive(namespace, name string) []*Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
func rec_SelectNodes(cn *Node, namespace, name string, list *[]*Node, recurse bool) {
|
func rec_SelectNodes(cn *Node, namespace, name string, list *[]*Node, recurse bool) {
|
||||||
if (namespace == "*" || cn.Name.Space == namespace) && (name == "*" || cn.Name.Local == name) {
|
|
||||||
*list = append(*list, cn)
|
|
||||||
if !recurse {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range cn.Children {
|
for _, v := range cn.Children {
|
||||||
rec_SelectNodes(v, namespace, name, list, recurse)
|
if (namespace == "*" || v.Name.Space == namespace) && (name == "*" || v.Name.Local == name) {
|
||||||
|
*list = append(*list, v)
|
||||||
|
}
|
||||||
|
if recurse {
|
||||||
|
rec_SelectNodes(v, namespace, name, list, recurse)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
31
xmlx_test.go
31
xmlx_test.go
|
@ -28,10 +28,10 @@ func TestWildcard(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
list := doc.SelectNodes("ns", "*")
|
list := doc.SelectNodesRecursive("ns", "*")
|
||||||
|
|
||||||
if len(list) != 1 {
|
if len(list) != 7 {
|
||||||
t.Errorf("Wrong number of child elements. Expected 1, got %d.", len(list))
|
t.Errorf("Wrong number of child elements. Expected 7, got %d.", len(list))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,13 +94,36 @@ func TestNodeSearch(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes := doc.SelectNodes("", "item")
|
nodes := doc.SelectNodesRecursive("", "item")
|
||||||
if len(nodes) == 0 {
|
if len(nodes) == 0 {
|
||||||
t.Errorf("SelectNodes(): no nodes found.")
|
t.Errorf("SelectNodes(): no nodes found.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSelectNodes(t *testing.T) {
|
||||||
|
doc := New()
|
||||||
|
|
||||||
|
if err := doc.LoadFile("test1.xml", nil); err != nil {
|
||||||
|
t.Errorf("LoadFile(): %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := doc.SelectNode("", "channel")
|
||||||
|
|
||||||
|
topLevelLinks := ch.SelectNodes("", "link")
|
||||||
|
if len(topLevelLinks) != 1 {
|
||||||
|
t.Errorf("SelectNodes(): Expected 1, Got %d", len(topLevelLinks))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
allLinks := ch.SelectNodesRecursive("", "link")
|
||||||
|
if len(allLinks) != 8 {
|
||||||
|
t.Errorf("SelectNodes(): Expected 8, Got %d", len(allLinks))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Image struct {
|
type Image struct {
|
||||||
Title string `xml:"title"`
|
Title string `xml:"title"`
|
||||||
Url string `xml:"url"`
|
Url string `xml:"url"`
|
||||||
|
|
Loading…
Reference in New Issue