-
Notifications
You must be signed in to change notification settings - Fork 30
/
query_test.go
247 lines (220 loc) · 5.57 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package jsonquery
import (
"strings"
"testing"
"github.com/antchfx/xpath"
"github.com/stretchr/testify/require"
)
func BenchmarkSelectorCache(b *testing.B) {
DisableSelectorCache = false
for i := 0; i < b.N; i++ {
getQuery("/AAA/BBB/DDD/CCC/EEE/ancestor::*")
}
}
func BenchmarkDisableSelectorCache(b *testing.B) {
DisableSelectorCache = true
for i := 0; i < b.N; i++ {
getQuery("/AAA/BBB/DDD/CCC/EEE/ancestor::*")
}
}
func TestNavigator(t *testing.T) {
s := `{
"name":"John",
"age":30,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}`
doc, err := parseString(s)
require.NoError(t, err)
nav := CreateXPathNavigator(doc)
nav.MoveToRoot()
require.Equal(t, xpath.RootNode, nav.NodeType(), "node type is not RootNode")
// Move to first child(age).
require.True(t, nav.MoveToChild())
require.Equal(t, "age", nav.Current().Data)
require.Equal(t, float64(30), nav.GetValue())
// Move to next sibling node(cars).
require.True(t, nav.MoveToNext())
require.Equal(t, "cars", nav.Current().Data)
m := make(map[string][]string)
// Move to cars child node.
cur := nav.Copy()
for ok := nav.MoveToChild(); ok; ok = nav.MoveToNext() {
// Move to <element> node.
// <element><models>...</models><name>Ford</name></element>
cur1 := nav.Copy()
var name string
var models []string
// name || models
for ok := nav.MoveToChild(); ok; ok = nav.MoveToNext() {
cur2 := nav.Copy()
n := nav.Current()
require.NotNil(t, n)
if n.Data == "name" {
name = n.InnerText()
} else {
for ok := nav.MoveToChild(); ok; ok = nav.MoveToNext() {
cur3 := nav.Copy()
models = append(models, nav.Value())
nav.MoveTo(cur3)
}
}
nav.MoveTo(cur2)
}
nav.MoveTo(cur1)
m[name] = models
}
nav.MoveTo(cur)
// move to name.
require.True(t, nav.MoveToNext())
// move to cars
require.True(t, nav.MoveToPrevious())
require.Equal(t, "cars", nav.Current().Data)
// move to age.
require.True(t, nav.MoveToFirst())
require.Equal(t, "age", nav.Current().Data)
nav.MoveToParent()
require.Equal(t, DocumentNode, nav.Current().Type)
}
func TestToXML(t *testing.T) {
s := `{
"name":"John",
"age":31,
"female":false
}`
doc, err := Parse(strings.NewReader(s))
require.NoError(t, err)
expected := `<?xml version="1.0" encoding="utf-8"?><root><age>31</age><female>false</female><name>John</name></root>`
require.Equal(t, expected, doc.OutputXML())
}
func TestArrayToXML(t *testing.T) {
s := `[1,2,3,4]`
doc, err := Parse(strings.NewReader(s))
require.NoError(t, err)
expected := `<?xml version="1.0" encoding="utf-8"?><root><1>1</1><2>2</2><3>3</3><4>4</4></root>`
require.Equal(t, expected, doc.OutputXML())
}
func TestNestToArray(t *testing.T) {
s := `{
"address": {
"city": "Nara",
"postalCode": "630-0192",
"streetAddress": "naist street"
},
"age": 26,
"name": "John",
"phoneNumbers": [
{
"number": "0123-4567-8888",
"type": "iPhone"
},
{
"number": "0123-4567-8910",
"type": "home"
}
]
}`
doc, err := Parse(strings.NewReader(s))
require.NoError(t, err)
expected := `<?xml version="1.0" encoding="utf-8"?><root><address><city>Nara</city><postalCode>630-0192</postalCode><streetAddress>naist street</streetAddress></address><age>26</age><name>John</name><phoneNumbers><number>0123-4567-8888</number><type>iPhone</type></phoneNumbers><phoneNumbers><number>0123-4567-8910</number><type>home</type></phoneNumbers></root>`
require.Equal(t, expected, doc.OutputXML())
}
func TestQuery(t *testing.T) {
doc, err := Parse(strings.NewReader(BooksExample))
require.NoError(t, err)
q := "/store/bicycle"
n := FindOne(doc, q)
require.NotNil(t, n)
q = "/store/bicycle/color"
n = FindOne(doc, q)
require.NotNil(t, n)
require.Equal(t, "color", n.Data)
}
func TestQueryWhere(t *testing.T) {
doc, err := Parse(strings.NewReader(BooksExample))
require.NoError(t, err)
// for number
q := "//*[price<=12.99]"
list := Find(doc, q)
require.Len(t, list, 3)
// for string
q = "//*/isbn[text()='0-553-21311-3']"
n := FindOne(doc, q)
require.NotNil(t, n)
require.Equal(t, "isbn", n.Data)
}
func TestStringRepresentation(t *testing.T) {
s := `{
"a": "a string",
"b": 3.1415,
"c": true,
"d": {
"d1": 1,
"d2": "foo",
"d3": true,
"d4": null
},
"e": ["master", 42, true],
"f": 1690193829
}`
doc, err := Parse(strings.NewReader(s))
require.NoError(t, err)
expected := map[string]string{
"a": "a string",
"b": "3.1415",
"c": "true",
"d": `{"d1":1,"d2":"foo","d3":true,"d4":null}`,
"e": `["master",42,true]`,
"f": "1690193829",
}
nn := CreateXPathNavigator(doc)
hasData := nn.MoveToChild()
require.True(t, hasData)
for hasData {
require.NotNil(t, nn.Current())
name := nn.Current().Data
require.Equalf(t, expected[name], nn.Value(), "mismatch for node %q", name)
hasData = nn.MoveToNext()
}
}
var BooksExample string = `{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
`