forked from antchfx/xmlquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_test.go
115 lines (108 loc) · 2.99 KB
/
query_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package xmlquery
import (
"strings"
"testing"
)
// https://msdn.microsoft.com/en-us/library/ms762271(v=vs.85).aspx
const xmlDoc = `
<?xml version="1.0"?>
<catalog>
<!-- book list-->
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>`
var doc = loadXML(xmlDoc)
func TestXPath(t *testing.T) {
if list := Find(doc, "//book"); len(list) != 3 {
t.Fatal("count(//book) != 3")
}
if node := FindOne(doc, "//book[@id='bk101']"); node == nil {
t.Fatal("//book[@id='bk101] is not found")
}
if node := FindOne(doc, "//book[price>=44.95]"); node == nil {
t.Fatal("//book/price>=44.95 is not found")
}
if list := Find(doc, "//book[genre='Fantasy']"); len(list) != 2 {
t.Fatal("//book[genre='Fantasy'] items count is not equal 2")
}
var c int
FindEach(doc, "//book", func(i int, n *Node) {
c++
})
if c != len(Find(doc, "//book")) {
t.Fatal("count(//book) != 3")
}
node := FindOne(doc, "//book[1]")
if node.SelectAttr("id") != "bk101" {
t.Fatal("//book[1]/@id != bk101")
}
}
func TestXPathCdUp(t *testing.T) {
doc := loadXML(`<a><b attr="1"/></a>`)
node := FindOne(doc, "/a/b/@attr/..")
t.Logf("node = %#v", node)
if node == nil || node.Data != "b" {
t.Fatal("//b/@id/.. != <b/>")
}
}
func TestNavigator(t *testing.T) {
nav := &NodeNavigator{curr: doc, root: doc, attr: -1}
nav.MoveToChild() // New Line
nav.MoveToNext()
nav.MoveToNext() // catalog
if nav.curr.Data != "catalog" {
t.Fatal("current node name != `catalog`")
}
nav.MoveToChild() // New Line
nav.MoveToNext() // comment node
if nav.curr.Type != CommentNode {
t.Fatal("node type not CommentNode")
}
nav.Value()
nav.MoveToNext() // New Line
nav.MoveToNext() //book
nav.MoveToChild()
nav.MoveToNext() // book/author
if nav.LocalName() != "author" {
t.Fatalf("node error")
}
nav.MoveToParent() // book
nav.MoveToNext() // next book
if nav.curr.SelectAttr("id") != "bk102" {
t.Fatal("node error")
}
}
func loadXML(s string) *Node {
node, err := Parse(strings.NewReader(s))
if err != nil {
panic(err)
}
return node
}