This repository has been archived by the owner on Apr 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
fns_test.go
279 lines (247 loc) · 8.07 KB
/
fns_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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package goxpath
import (
"bytes"
"encoding/xml"
"fmt"
"testing"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
)
func TestStrLit(t *testing.T) {
p := `'strlit'`
x := `<?xml version="1.0" encoding="UTF-8"?><test><path/></test>`
exp := "strlit"
execVal(p, x, exp, nil, t)
}
func TestNumLit(t *testing.T) {
p := `123`
x := `<?xml version="1.0" encoding="UTF-8"?><test><path/></test>`
exp := "123"
execVal(p, x, exp, nil, t)
p = `123.456`
exp = "123.456"
execVal(p, x, exp, nil, t)
}
func TestLast(t *testing.T) {
p := `/p1/*[last()]`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2/><p3/><p4/></p1>`
exp := []string{"<p4></p4>"}
execPath(p, x, exp, nil, t)
p = `/p1/p5[last()]`
exp = []string{}
execPath(p, x, exp, nil, t)
p = `/p1[last()]`
exp = []string{"<p1><p2></p2><p3></p3><p4></p4></p1>"}
execPath(p, x, exp, nil, t)
}
func TestCount(t *testing.T) {
p := `count(//p1)`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2><?test?></p2><p3/><p1/></p1>`
exp := "2"
execVal(p, x, exp, nil, t)
}
func TestCount2(t *testing.T) {
x := `<?xml version="1.0"?>
<test>
<x a="1">
<x a="2">
<x>
<y>y31</y>
<y>y32</y>
</x>
</x>
</x>
<x a="1">
<x a="2">
<y>y21</y>
<y>y22</y>
</x>
</x>
<x a="1">
<y>y11</y>
<y>y12</y>
</x>
<x>
<y>y03</y>
<y>y04</y>
</x>
</test>
`
execVal(`count(//x)`, x, "7", nil, t)
execVal(`count(//x[1])`, x, "4", nil, t)
execVal(`count(//x/y)`, x, "8", nil, t)
execVal(`count(//x/y[1])`, x, "4", nil, t)
execVal(`count(//x[1]/y[1])`, x, "2", nil, t)
}
func TestNames(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><test xmlns="http://foo.com" xmlns:bar="http://bar.com" bar:attr="val"><?pi pival?><!--comment--></test>`
testMap := make(map[string]map[string]string)
testMap["/*"] = make(map[string]string)
testMap["/*"]["local-name"] = "test"
testMap["/*"]["namespace-uri"] = "http://foo.com"
testMap["/*"]["name"] = "{http://foo.com}test"
testMap["/none"] = make(map[string]string)
testMap["/none"]["local-name"] = ""
testMap["/none"]["namespace-uri"] = ""
testMap["/none"]["name"] = ""
testMap["/*/@*:attr"] = make(map[string]string)
testMap["/*/@*:attr"]["local-name"] = "attr"
testMap["/*/@*:attr"]["namespace-uri"] = "http://bar.com"
testMap["/*/@*:attr"]["name"] = "{http://bar.com}attr"
testMap["//processing-instruction()"] = make(map[string]string)
testMap["//processing-instruction()"]["local-name"] = "pi"
testMap["//processing-instruction()"]["namespace-uri"] = ""
testMap["//processing-instruction()"]["name"] = "pi"
testMap["//comment()"] = make(map[string]string)
testMap["//comment()"]["local-name"] = ""
testMap["//comment()"]["namespace-uri"] = ""
testMap["//comment()"]["name"] = ""
for path, i := range testMap {
for nt, res := range i {
p := fmt.Sprintf("%s(%s)", nt, path)
exp := res
execVal(p, x, exp, nil, t)
}
}
x = `<?xml version="1.0" encoding="UTF-8"?><test xmlns="http://foo.com" />`
execPath("/*[local-name() = 'test']", x, []string{`<test xmlns="http://foo.com"></test>`}, nil, t)
execPath("/*[namespace-uri() = 'http://foo.com']", x, []string{`<test xmlns="http://foo.com"></test>`}, nil, t)
execPath("/*[name() = '{http://foo.com}test']", x, []string{`<test xmlns="http://foo.com"></test>`}, nil, t)
}
func TestBoolean(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2/><p3/><p4/></p1>`
execVal(`true()`, x, "true", nil, t)
execVal(`false()`, x, "false", nil, t)
p := `boolean(/p1/p2)`
exp := "true"
execVal(p, x, exp, nil, t)
p = `boolean(/p1/p5)`
exp = "false"
execVal(p, x, exp, nil, t)
p = `boolean('123')`
exp = "true"
execVal(p, x, exp, nil, t)
p = `boolean(123)`
exp = "true"
execVal(p, x, exp, nil, t)
p = `boolean('')`
exp = "false"
execVal(p, x, exp, nil, t)
p = `boolean(0)`
exp = "false"
execVal(p, x, exp, nil, t)
}
func TestNot(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2/><p3/><p4/></p1>`
execVal(`not(false())`, x, "true", nil, t)
execVal(`not(true())`, x, "false", nil, t)
}
func TestConversions(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1>foo</p1>`
execVal(`number(true())`, x, "1", nil, t)
execVal(`number(false())`, x, "0", nil, t)
execVal(`string(/p2)`, x, "", nil, t)
execVal(`/p1[string() = 'foo']`, x, "foo", nil, t)
execVal(`number('abc')`, x, "NaN", nil, t)
}
func TestLang(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?>
<p1>
<p xml:lang="en">I went up a floor.</p>
<p xml:lang="en-GB">I took the lift.</p>
<p xml:lang="en-US">I rode the elevator.</p>
</p1>`
execVal(`count(//p[lang('en')])`, x, "3", nil, t)
execVal(`count(//text()[lang('en-GB')])`, x, "1", nil, t)
execVal(`count(//p[lang('en-US')])`, x, "1", nil, t)
execVal(`count(//p[lang('de')])`, x, "0", nil, t)
execVal(`count(/p1[lang('en')])`, x, "0", nil, t)
}
func TestString(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1>text</p1>`
execVal(`string(2 + 2)`, x, "4", nil, t)
execVal(`string(/p1)`, x, "text", nil, t)
}
func TestConcat(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1/>`
execVal(`concat('abc', 'def', 'hij', '123')`, x, "abcdefhij123", nil, t)
}
func TestStartsWith(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1/>`
execVal(`starts-with('abcd', 'ab')`, x, "true", nil, t)
execVal(`starts-with('abcd', 'abd')`, x, "false", nil, t)
}
func TestContains(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1/>`
execVal(`contains('abcd', 'bcd')`, x, "true", nil, t)
execVal(`contains('abcd', 'bd')`, x, "false", nil, t)
}
func TestSubstrBefore(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1/>`
execVal(`substring-before("1999/04/01","/")`, x, "1999", nil, t)
execVal(`substring-before("1999/04/01","2")`, x, "", nil, t)
}
func TestSubstrAfter(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1/>`
execVal(`substring-after("1999/04/01","/")`, x, "04/01", nil, t)
execVal(`substring-after("1999/04/01","19")`, x, "99/04/01", nil, t)
execVal(`substring-after("1999/04/01","a")`, x, "", nil, t)
}
func TestSubstring(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1/>`
execVal(`substring("12345", 2, 3)`, x, "234", nil, t)
execVal(`substring("12345", 2)`, x, "2345", nil, t)
execVal(`substring('abcd', -2, 5)`, x, "ab", nil, t)
execVal(`substring('abcd', 0)`, x, "abcd", nil, t)
execVal(`substring('abcd', 1, 4)`, x, "abcd", nil, t)
execVal(`substring("12345", 1.5, 2.6)`, x, "234", nil, t)
execVal(`substring("12345", 0 div 0, 3)`, x, "", nil, t)
execVal(`substring("12345", 1, 0 div 0)`, x, "", nil, t)
execVal(`substring("12345", -42, 1 div 0)`, x, "12345", nil, t)
execVal(`substring("12345", -1 div 0, 1 div 0)`, x, "", nil, t)
}
func TestStrLength(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1>abc</p1>`
execVal(`string-length('def')`, x, "3", nil, t)
execVal(`/p1[string-length() = 3]`, x, "abc", nil, t)
}
func TestNormalizeSpace(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1>
a b
</p1>`
execVal(`normalize-space(/p1)`, x, "a b", nil, t)
execVal(`/p1[normalize-space(/p1) = 'a b']`, x, `
a b
`, nil, t)
execVal(`/p1[normalize-space() = 'a b']`, x, `
a b
`, nil, t)
}
func TestTranslate(t *testing.T) {
x := `<?xml version="1.0" encoding="UTF-8"?><p1/>`
execVal(`translate("bar","abc","ABC")`, x, "BAr", nil, t)
execVal(`translate("--aaa--","abc-","ABC")`, x, "AAA", nil, t)
}
func TestPathAfterFunction(t *testing.T) {
x := xmltree.MustParseXML(bytes.NewBufferString(`
<root>
<a>aa<b>bb</b></a>
<c a="bb">cc</c>
</root>
`))
current, err := MustParse(`/root/a`).ExecNode(x)
if err != nil {
t.Error("err not nil")
}
currentFn := func(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
return current, nil
}
custFns := map[xml.Name]tree.Wrap{
{Local: "current"}: {Fn: currentFn},
}
opts := func(o *Opts) { o.Funcs = custFns }
result := MustParse(`/root/c/@a[. = current()/b]`).MustExec(x, opts)
if result.String() != `bb` {
t.Error("result not foo:", result.String())
}
}