Debian Codegraph @

xpath_function_test.go

raw
1 package xpath
2
3 import (
4 "math"
5 "testing"
6 )
7
8 // Some test examples from http://zvon.org/comp/r/ref-XPath_2.html
9
10 func Test_func_boolean(t *testing.T) {
11 test_xpath_eval(t, empty_example, `true()`, true)
12 test_xpath_eval(t, empty_example, `false()`, false)
13 test_xpath_eval(t, empty_example, `boolean(0)`, false)
14 test_xpath_eval(t, empty_example, `boolean(null)`, false)
15 test_xpath_eval(t, empty_example, `boolean(1)`, true)
16 test_xpath_eval(t, empty_example, `boolean(2)`, true)
17 test_xpath_eval(t, empty_example, `boolean(true)`, false)
18 test_xpath_eval(t, empty_example, `boolean(1 > 2)`, false)
19 test_xpath_eval(t, book_example, `boolean(//*[@lang])`, true)
20 test_xpath_eval(t, book_example, `boolean(//*[@x])`, false)
21 test_xpath_eval(t, empty_example, `'14' > 0`, true)
22 test_xpath_eval(t, empty_example, `'abc' = 1`, false) // NaN
23 test_xpath_eval(t, empty_example, `'2' < '10'`, true)
24 }
25
26 func Test_func_name(t *testing.T) {
27 test_xpath_eval(t, html_example, `name(//html/@lang)`, "lang")
28 test_xpath_eval(t, html_example, `name(html/head/title)`, "title")
29 test_xpath_count(t, html_example, `//*[name() = "li"]`, 3)
30 }
31
32 func Test_func_not(t *testing.T) {
33 //test_xpath_eval(t, empty_example, `not(0)`, true)
34 //test_xpath_eval(t, empty_example, `not(1)`, false)
35 test_xpath_elements(t, employee_example, `//employee[not(@id = "1")]`, 8, 13)
36 test_xpath_elements(t, book_example, `//book[not(year = 2005)]`, 15, 25)
37 test_xpath_count(t, book_example, `//book[not(title)]`, 0)
38 }
39
40 func Test_func_ceiling_floor(t *testing.T) {
41 test_xpath_eval(t, empty_example, "ceiling(5.2)", float64(6))
42 test_xpath_eval(t, empty_example, "floor(5.2)", float64(5))
43 }
44
45 func Test_func_concat(t *testing.T) {
46 test_xpath_eval(t, empty_example, `concat("1", "2", "3")`, "123")
47 //test_xpath_eval(t, empty_example, `concat("Ciao!", ())`, "Ciao!")
48 test_xpath_eval(t, book_example, `concat(//book[1]/title, ", ", //book[1]/year)`, "Everyday Italian, 2005")
49 result := concatFunc(testQuery("a"), testQuery("b"))(nil, nil).(string)
50 assertEqual(t, result, "ab")
51 // concat converts each argument with string() (REC 4.2); number and boolean
52 // arguments must be stringified, not silently dropped.
53 test_xpath_eval(t, empty_example, `concat("id-", 5, "-x")`, "id-5-x")
54 test_xpath_eval(t, empty_example, `concat(1, 2, 3)`, "123")
55 test_xpath_eval(t, empty_example, `concat("n=", 1 div 4)`, "n=0.25")
56 test_xpath_eval(t, empty_example, `concat("a", true(), "b")`, "atrueb")
57 test_xpath_eval(t, empty_example, `concat("a", 1 div 0, "z")`, "aInfinityz")
58 }
59
60 func Test_func_contains(t *testing.T) {
61 test_xpath_eval(t, empty_example, `contains("tattoo", "t")`, true)
62 test_xpath_eval(t, empty_example, `contains("tattoo", "T")`, false)
63 test_xpath_eval(t, empty_example, `contains("tattoo", "ttt")`, false)
64 //test_xpath_eval(t, empty_example, `contains("", ())`, true)
65 test_xpath_elements(t, book_example, `//book[contains(title, "Potter")]`, 9)
66 test_xpath_elements(t, book_example, `//book[contains(year, "2005")]`, 3, 9)
67 assertPanic(t, func() { selectNode(html_example, "//*[contains(0, 0)]") })
68 }
69
70 func Test_func_count(t *testing.T) {
71 test_xpath_eval(t, book_example, `count(//book)`, float64(4))
72 test_xpath_eval(t, book_example, `count(//book[3]/author)`, float64(5))
73 }
74
75 func Test_func_ends_with(t *testing.T) {
76 test_xpath_eval(t, empty_example, `ends-with("tattoo", "tattoo")`, true)
77 test_xpath_eval(t, empty_example, `ends-with("tattoo", "atto")`, false)
78 test_xpath_elements(t, book_example, `//book[ends-with(@category,'ing')]`, 3)
79 test_xpath_elements(t, book_example, `//book[ends-with(./price,'.99')]`, 9, 15)
80 assertPanic(t, func() { selectNode(html_example, `//*[ends-with(0, 0)]`) }) // arg must be start with string
81 assertPanic(t, func() { selectNode(html_example, `//*[ends-with(name(), 0)]`) })
82 }
83
84 func Test_func_last(t *testing.T) {
85 test_xpath_elements(t, book_example, `//bookstore[last()]`, 2)
86 test_xpath_elements(t, book_example, `//bookstore/book[last()]`, 25)
87 test_xpath_elements(t, book_example, `(//bookstore/book)[last()]`, 25)
88 //https: //github.com/antchfx/xpath/issues/76
89 test_xpath_elements(t, book_example, `(//bookstore/book[year = 2005])[last()]`, 9)
90 test_xpath_elements(t, book_example, `//bookstore/book[year = 2005][last()]`, 9)
91 test_xpath_elements(t, html_example, `//ul/li[last()]`, 15)
92 test_xpath_elements(t, html_example, `(//ul/li)[last()]`, 15)
93 }
94
95 func Test_func_local_name(t *testing.T) {
96 test_xpath_eval(t, book_example, `local-name(bookstore)`, "bookstore")
97 test_xpath_eval(t, mybook_example, `local-name(//mybook:book)`, "book")
98 }
99
100 func Test_func_starts_with(t *testing.T) {
101 test_xpath_eval(t, employee_example, `starts-with("tattoo", "tat")`, true)
102 test_xpath_eval(t, employee_example, `starts-with("tattoo", "att")`, false)
103 test_xpath_elements(t, book_example, `//book[starts-with(title,'Everyday')]`, 3)
104 assertPanic(t, func() { selectNode(html_example, `//*[starts-with(0, 0)]`) })
105 assertPanic(t, func() { selectNode(html_example, `//*[starts-with(name(), 0)]`) })
106 }
107
108 func Test_func_string(t *testing.T) {
109 test_xpath_eval(t, empty_example, `string(1.23)`, "1.23")
110 test_xpath_eval(t, empty_example, `string(3)`, "3")
111 test_xpath_eval(t, book_example, `string(//book/@category)`, "cooking")
112 // number->string per XPath 1.0 REC 4.2: decimal form, never an exponent.
113 test_xpath_eval(t, empty_example, `string(1000000)`, "1000000")
114 test_xpath_eval(t, empty_example, `string(1234567)`, "1234567")
115 test_xpath_eval(t, empty_example, `string(123456789)`, "123456789")
116 test_xpath_eval(t, empty_example, `string(0.00001)`, "0.00001")
117 test_xpath_eval(t, empty_example, `string(10000000000)`, "10000000000")
118 test_xpath_eval(t, empty_example, `string(100000000000000000000)`, "100000000000000000000")
119 // infinity, NaN and negative zero.
120 test_xpath_eval(t, empty_example, `string(1 div 0)`, "Infinity")
121 test_xpath_eval(t, empty_example, `string(-1 div 0)`, "-Infinity")
122 test_xpath_eval(t, empty_example, `string(0 div 0)`, "NaN")
123 test_xpath_eval(t, empty_example, `string(-0)`, "0")
124 test_xpath_eval(t, empty_example, `string(0 div -1)`, "0")
125 // unchanged finite cases (regression guard).
126 test_xpath_eval(t, empty_example, `string(0.5)`, "0.5")
127 test_xpath_eval(t, empty_example, `string(-3)`, "-3")
128 test_xpath_eval(t, empty_example, `string(0.25)`, "0.25")
129 }
130 func Test_func_string_empty(t *testing.T) {
131 // https://github.com/antchfx/xpath/issues/113
132 // Create the equivalent of <div>hi</div> using TNode
133 doc := createNode("", RootNode)
134 div := doc.createChildNode("div", ElementNode)
135 div.lines = 1 // Assign a line number for the test helper
136 div.createChildNode("hi", TextNode)
137
138 // Use the existing test helper to evaluate the XPath
139 // The expression selects the div if its string value ("hi") is true in a boolean context.
140 test_xpath_values(t, doc, `//div[string()]`, "hi")
141 }
142
143 func Test_func_string_join(t *testing.T) {
144 //(t, empty_example, `string-join(('Now', 'is', 'the', 'time', '...'), '')`, "Now is the time ...")
145 test_xpath_eval(t, empty_example, `string-join("some text", ";")`, "some text")
146 test_xpath_eval(t, book_example, `string-join(//book/@category, ";")`, "cooking;children;web;web")
147 }
148
149 func Test_func_string_length(t *testing.T) {
150 test_xpath_eval(t, empty_example, `string-length("Harp not on that string, madam; that is past.")`, float64(45))
151 test_xpath_eval(t, empty_example, `string-length(normalize-space(' abc '))`, float64(3))
152 test_xpath_eval(t, html_example, `string-length(//title/text())`, float64(len("My page")))
153 test_xpath_eval(t, html_example, `string-length(//html/@lang)`, float64(len("en")))
154 test_xpath_count(t, employee_example, `//employee[string-length(@id) > 0]`, 3) // = //employee[@id]
155 }
156
157 func Test_func_substring(t *testing.T) {
158 test_xpath_eval(t, empty_example, `substring("motor car", 6)`, " car")
159 test_xpath_eval(t, empty_example, `substring("metadata", 4, 3)`, "ada")
160 test_xpath_eval(t, empty_example, `substring("12345", 5, -3)`, "")
161 test_xpath_eval(t, empty_example, `substring("12345", 1.5, 2.6)`, "234")
162 test_xpath_eval(t, empty_example, `substring("12345", 0, 3)`, "12")
163 test_xpath_eval(t, empty_example, `substring("12345", 5, -3)`, "")
164 test_xpath_eval(t, empty_example, `substring("12345", 0, 5)`, "1234")
165 test_xpath_eval(t, empty_example, `substring("12345", 1, 5)`, "12345")
166 test_xpath_eval(t, empty_example, `substring("12345", 1, 6)`, "12345")
167 test_xpath_eval(t, html_example, `substring(//title/child::node(), 1)`, "My page")
168 //assertPanic(t, func() { selectNode(empty_example, `substring("12345", 5, "")`) })
169 }
170
171 func Test_func_substring_after(t *testing.T) {
172 test_xpath_eval(t, empty_example, `substring-after("tattoo", "tat")`, "too")
173 test_xpath_eval(t, empty_example, `substring-after("tattoo", "tattoo")`, "")
174 }
175
176 func Test_func_substring_before(t *testing.T) {
177 test_xpath_eval(t, empty_example, `substring-before("tattoo", "attoo")`, "t")
178 test_xpath_eval(t, empty_example, `substring-before("tattoo", "tatto")`, "")
179 }
180
181 func Test_func_sum(t *testing.T) {
182 test_xpath_eval(t, empty_example, `sum(1 + 2)`, float64(3))
183 test_xpath_eval(t, empty_example, `sum(1.1 + 2)`, float64(3.1))
184 test_xpath_eval(t, book_example, `sum(//book/price)`, float64(149.93))
185 test_xpath_elements(t, book_example, `//book[sum(./price) > 40]`, 15)
186 assertPanic(t, func() { selectNode(html_example, `//title[sum('Hello') = 0]`) })
187 }
188
189 func Test_func_translate(t *testing.T) {
190 test_xpath_eval(t, empty_example, `translate("bar","abc","ABC")`, "BAr")
191 test_xpath_eval(t, empty_example, `translate("--aaa--","abc-","ABC")`, "AAA")
192 test_xpath_eval(t, empty_example, `translate("abcdabc", "abc", "AB")`, "ABdAB")
193 test_xpath_eval(t, empty_example, `translate('The quick brown fox', 'brown', 'red')`, "The quick red fdx")
194 }
195
196 func Test_func_matches(t *testing.T) {
197 test_xpath_eval(t, empty_example, `matches("abracadabra", "bra")`, true)
198 test_xpath_eval(t, empty_example, `matches("abracadabra", "(?i)^A.*A$")`, true)
199 test_xpath_eval(t, empty_example, `matches("abracadabra", "^a.*a$")`, true)
200 test_xpath_eval(t, empty_example, `matches("abracadabra", "^bra")`, false)
201 assertPanic(t, func() { selectNode(html_example, `//*[matches()]`) }) // arg len check failure
202 assertPanic(t, func() { selectNode(html_example, "//*[matches(substring(), 0)]") }) // first arg processing failure
203 assertPanic(t, func() { selectNode(html_example, "//*[matches(@href, substring())]") }) // second arg processing failure
204 assertPanic(t, func() { selectNode(html_example, "//*[matches(@href, 0)]") }) // second arg not string
205 assertPanic(t, func() { selectNode(html_example, "//*[matches(@href, '[invalid')]") }) // second arg invalid regexp
206 // testing unexpected the regular expression.
207 _, err := Compile(`//*[matches(., '^[\u0621-\u064AA-Za-z\-]+')]`)
208 assertErr(t, err)
209 _, err = Compile(`//*[matches(., '//*[matches(., '\w+`)
210 assertErr(t, err)
211 }
212
213 func Test_func_number(t *testing.T) {
214 test_xpath_eval(t, empty_example, `number(10)`, float64(10))
215 test_xpath_eval(t, empty_example, `number(1.11)`, float64(1.11))
216 test_xpath_eval(t, empty_example, `number("10") > 10`, false)
217 test_xpath_eval(t, empty_example, `number("10") = 10`, true)
218 test_xpath_eval(t, empty_example, `number("123") < 1000`, true)
219 test_xpath_eval(t, empty_example, `number(//non-existent-node) = 0`, false)
220 assertTrue(t, math.IsNaN(MustCompile(`number(//non-existent-node)`).Evaluate(createNavigator(empty_example)).(float64)))
221 assertTrue(t, math.IsNaN(MustCompile(`number("123a")`).Evaluate(createNavigator(empty_example)).(float64)))
222 }
223
224 func Test_func_position(t *testing.T) {
225 test_xpath_elements(t, book_example, `//book[position() = 1]`, 3)
226 test_xpath_elements(t, book_example, `//book[(position() mod 2) = 0]`, 9, 25)
227 test_xpath_elements(t, book_example, `//book[position() = last()]`, 25)
228 test_xpath_elements(t, book_example, `//book/*[position() = 1]`, 4, 10, 16, 26)
229 // Test Failed
230 //test_xpath_elements(t, book_example, `(//book/title)[position() = 1]`, 3)
231 }
232
233 func Test_func_replace(t *testing.T) {
234 test_xpath_eval(t, empty_example, `replace('aa-bb-cc','bb','ee')`, "aa-ee-cc")
235 test_xpath_eval(t, empty_example, `replace("abracadabra", "bra", "*")`, "a*cada*")
236 test_xpath_eval(t, empty_example, `replace("abracadabra", "a", "")`, "brcdbr")
237 // The below xpath expressions is not supported yet
238 //
239 test_xpath_eval(t, empty_example, `replace("abracadabra", "a.*a", "*")`, "*")
240 test_xpath_eval(t, empty_example, `replace("abracadabra", "a.*?a", "*")`, "*c*bra")
241 // test_xpath_eval(t, empty_example, `replace("abracadabra", ".*?", "$1")`, "*c*bra") // error, because the pattern matches the zero-length string
242 test_xpath_eval(t, empty_example, `replace("AAAA", "A+", "b")`, "b")
243 test_xpath_eval(t, empty_example, `replace("AAAA", "A+?", "b")`, "bbbb")
244 test_xpath_eval(t, empty_example, `replace("darted", "^(.*?)d(.*)$", "$1c$2")`, "carted")
245 test_xpath_eval(t, empty_example, `replace("abracadabra", "a(.)", "a$1$1")`, "abbraccaddabbra")
246 test_xpath_eval(t, empty_example, `replace("abcd", "(ab)|(a)", "[1=$1][2=$2]")`, "[1=ab][2=]cd")
247 test_xpath_eval(t, empty_example, `replace("1/1/c11/1", "(.*)/[^/]+$", "$1")`, "1/1/c11")
248 test_xpath_eval(t, empty_example, `replace("A/B/C/D/E/F/G/H/I/J/K/L", "([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/(.*)", "$1-$2-$3-$4-$5-$6-$7-$8-$9-$10")`, "A-B-C-D-E-F-G-H-I-J/K/L")
249 }
250
251 func Test_func_reverse(t *testing.T) {
252 //test_xpath_eval(t, employee_example, `reverse(("hello"))`, "hello") // Not passed
253 test_xpath_elements(t, employee_example, `reverse(//employee)`, 13, 8, 3)
254 test_xpath_elements(t, employee_example, `//employee[reverse(.) = reverse(.)]`, 3, 8, 13)
255 assertPanic(t, func() { selectNode(html_example, "reverse(concat())") }) // invalid node-sets argument.
256 assertPanic(t, func() { selectNode(html_example, "reverse()") }) // missing node-sets argument.
257 }
258
259 func Test_func_round(t *testing.T) {
260 // round() returns a number (float64), consistent with ceiling()/floor().
261 test_xpath_eval(t, empty_example, `round(2.5)`, float64(3))
262 test_xpath_eval(t, empty_example, `round(2.4999)`, float64(2))
263 test_xpath_eval(t, empty_example, `round(0.5)`, float64(1))
264 test_xpath_eval(t, empty_example, `round(3.5)`, float64(4))
265 // REC §4.4: halves round toward +Infinity, not away from zero.
266 test_xpath_eval(t, empty_example, `round(-0.5)`, float64(0))
267 test_xpath_eval(t, empty_example, `round(-1.5)`, float64(-1))
268 test_xpath_eval(t, empty_example, `round(-2.5)`, float64(-2))
269 test_xpath_eval(t, empty_example, `round(-0.6)`, float64(-1))
270 test_xpath_eval(t, empty_example, `round(-0.4)`, float64(0))
271 // ±Infinity pass through instead of overflowing an int conversion.
272 test_xpath_eval(t, empty_example, `round(1 div 0)`, math.Inf(1))
273 test_xpath_eval(t, empty_example, `round(-1 div 0)`, math.Inf(-1))
274 assertTrue(t, math.IsNaN(MustCompile(`round(0 div 0)`).Evaluate(createNavigator(empty_example)).(float64)))
275 // A number result composes in further arithmetic (was NaN when round returned int).
276 test_xpath_eval(t, empty_example, `round(2.5) + 0.5`, float64(3.5))
277 }
278
279 func Test_func_mod(t *testing.T) {
280 // REC §3.5: truncating remainder; sign follows the dividend.
281 test_xpath_eval(t, empty_example, `5 mod 3`, float64(2))
282 test_xpath_eval(t, empty_example, `5 mod -3`, float64(2))
283 test_xpath_eval(t, empty_example, `-5 mod 3`, float64(-2))
284 test_xpath_eval(t, empty_example, `6 mod 3`, float64(0))
285 // operands keep their fractional part (were truncated to int before).
286 test_xpath_eval(t, empty_example, `5.5 mod 2`, float64(1.5))
287 test_xpath_eval(t, empty_example, `7.5 mod 3`, float64(1.5))
288 // mod by zero is NaN, not a panic.
289 assertTrue(t, math.IsNaN(MustCompile(`5 mod 0`).Evaluate(createNavigator(empty_example)).(float64)))
290 }
291
292 func Test_func_namespace_uri(t *testing.T) {
293 test_xpath_eval(t, mybook_example, `namespace-uri(//mybook:book)`, "http://www.contoso.com/books")
294 test_xpath_elements(t, mybook_example, `//*[namespace-uri()='http://www.contoso.com/books']`, 3, 9)
295 }
296
297 func Test_func_normalize_space(t *testing.T) {
298 const testStr = "\t \rloooooooonnnnnnngggggggg \r \n tes \u00a0 t strin \n\n \r g "
299 const expectedStr = `loooooooonnnnnnngggggggg tes t strin g`
300 test_xpath_eval(t, empty_example, `normalize-space("`+testStr+`")`, expectedStr)
301 test_xpath_eval(t, empty_example, `normalize-space(' abc ')`, "abc")
302 n := selectNode(employee_example, `//employee[@id="1"]/name`)
303 test_xpath_eval(t, n, `normalize-space()`, "Opal Kole")
304 test_xpath_eval(t, n, `normalize-space(.)`, "Opal Kole")
305 test_xpath_eval(t, book_example, `normalize-space(//book/title)`, "Everyday Italian")
306 test_xpath_eval(t, book_example, `normalize-space(//book[1]/title)`, "Everyday Italian")
307
308 }
309
310 func Test_func_lower_case(t *testing.T) {
311 test_xpath_eval(t, empty_example, `lower-case("ABc!D")`, "abc!d")
312 test_xpath_count(t, employee_example, `//name[@from="ca"]`, 0)
313 test_xpath_elements(t, employee_example, `//name[lower-case(@from) = "ca"]`, 9)
314 //test_xpath_eval(t, employee_example, `//employee/name/lower-case(text())`, "opal kole", "max miller", "beccaa moss")
315 }
316
317 func Benchmark_NormalizeSpaceFunc(b *testing.B) {
318 b.ReportAllocs()
319 const strForNormalization = "\t \rloooooooonnnnnnngggggggg \r \n tes \u00a0 t strin \n\n \r g "
320 for i := 0; i < b.N; i++ {
321 _ = normalizespaceFunc(testQuery(strForNormalization))(nil, nil)
322 }
323 }
324
325 func Benchmark_ConcatFunc(b *testing.B) {
326 b.ReportAllocs()
327 for i := 0; i < b.N; i++ {
328 _ = concatFunc(testQuery("a"), testQuery("b"))(nil, nil)
329 }
330 }
331
332 func Benchmark_GetHashCode(b *testing.B) {
333 // Create a more complex test node that will actually go through the full getHashCode paths
334 doc := createNavigator(book_example)
335 doc.MoveToRoot()
336 doc.MoveToChild() // Move to the first element
337 doc.MoveToChild() // Deeper
338 // Find a node with attributes
339 var node NodeNavigator
340 for {
341 if doc.NodeType() == AttributeNode || doc.NodeType() == TextNode || doc.NodeType() == CommentNode {
342 node = doc.Copy()
343 break
344 }
345 if !doc.MoveToNext() {
346 if !doc.MoveToChild() {
347 // If we can't find a suitable node, default to using the first element
348 doc.MoveToRoot()
349 doc.MoveToChild()
350 node = doc.Copy()
351 break
352 }
353 }
354 }
355
356 b.Run("getHashCode", func(b *testing.B) {
357 b.ReportAllocs()
358 for i := 0; i < b.N; i++ {
359 n := node.Copy()
360 _ = getHashCode(n)
361 }
362 })
363 }