-
Notifications
You must be signed in to change notification settings - Fork 17
/
FunctionCall.tests.ts
244 lines (218 loc) · 6.14 KB
/
FunctionCall.tests.ts
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
import * as chai from 'chai';
import jsonMlMapper from 'test-helpers/jsonMlMapper';
import * as slimdom from 'slimdom';
import {
evaluateXPath,
evaluateXPathToBoolean,
evaluateXPathToString,
evaluateXPathToStrings,
evaluateXPathToNumbers,
evaluateXPathToNumber,
evaluateXPathToAsyncIterator
} from 'fontoxpath';
let documentNode;
beforeEach(() => {
documentNode = new slimdom.Document();
});
describe('Dynamic function call', () => {
it('parses with a function with any number of arguments', () =>
chai.assert.equal(
evaluateXPathToString(
'let $fn := concat#3 return $fn("abc", "def", "ghi")',
documentNode
),
'abcdefghi'
));
it('parses a function with a function to be executed as one of its arguments', () =>
chai.assert.equal(
evaluateXPathToString(
'let $fn := concat#2 return $fn(avg((10, 20)), max((2, 50)))',
documentNode
),
'1550'
));
it('allows recursion', () =>
chai.assert.equal(
evaluateXPathToNumber(
'let $fn := function ($recurse, $i) {if ($i < 100) then $recurse($recurse, $i + 1) else $i } return $fn($fn, 0)',
documentNode
),
100
));
it('allows async recursion', async () => {
const it = evaluateXPathToAsyncIterator(
'let $fn := function ($recurse, $i) {if ($i < 100) then fontoxpath:sleep($recurse($recurse, $i + 1), 1) else $i } return $fn($fn, 0)',
documentNode
);
chai.assert.equal((await it.next()).value, 100);
chai.assert.equal((await it.next()).done, true);
});
it('Fibonacci', async () => {
const it = evaluateXPathToAsyncIterator(
`
let $fib := function ($recurse, $a, $b) {
($a, $recurse($recurse, $b, $a + $b))
},
$fib-entries := $fib($fib, 1, 1)
return $fib-entries
`,
documentNode
);
chai.assert.equal((await it.next()).value, 1);
chai.assert.equal((await it.next()).value, 1);
chai.assert.equal((await it.next()).value, 2);
chai.assert.equal((await it.next()).value, 3);
chai.assert.equal((await it.next()).value, 5);
chai.assert.equal((await it.next()).value, 8);
chai.assert.equal((await it.next()).value, 13);
chai.assert.equal((await it.next()).value, 21);
chai.assert.equal((await it.next()).value, 34);
});
it('parses a function with a fixed number of arguments', () =>
chai.assert.isFalse(
evaluateXPathToBoolean('let $fn := not#1 return $fn(true())', documentNode)
));
it('parses a function with a node lookup', () => {
jsonMlMapper.parse(
[
'someElement',
{
someAttribute1: 'someValue1',
someAttribute2: 'someValue2',
someAttribute3: 'someValue3'
}
],
documentNode
);
chai.assert.equal(
evaluateXPathToString(
'let $fn := string-join#2 return $fn(@node()/name(), ",")',
documentNode.firstChild
),
'someAttribute1,someAttribute2,someAttribute3'
);
});
it('parses a function with a no arguments', () =>
chai.assert.isFalse(
evaluateXPathToBoolean('let $fn := false#0 return $fn()', documentNode)
));
it('parses a dynamic functionc all with the arrow syntax', () =>
chai.assert.isFalse(
evaluateXPathToBoolean('let $fn := not#1 return true() => $fn()', documentNode)
));
it('throws when the base expression does not evaluate to a function', () =>
chai.assert.throws(
() =>
evaluateXPathToString(
'let $notfn := 123 return $notfn("someArgument")',
documentNode
),
'XPTY0004'
));
it('throws when the base expression evaluates to a non-singleton sequence of functions', () =>
chai.assert.throws(
() => evaluateXPathToString('(false#0, false#0)()', documentNode),
'XPTY0004'
));
it('throws when a function is called with the wrong type of arguments', () =>
chai.assert.throws(
() => evaluateXPathToString('let $fn := ends-with#2 return $fn(0, 0)', documentNode),
'XPTY0004'
));
it('throws when a function is called with the wrong multiplicity of arguments', () =>
chai.assert.throws(
() => evaluateXPathToString('let $fn := ends-with#2 return $fn(0, 0)', documentNode),
'XPTY0004'
));
it('throws when a function is called with the wrong number of arguments', () =>
chai.assert.throws(
() => evaluateXPathToString('let $fn := false#0 return $fn(1, 2)', documentNode),
'XPTY0004'
));
it('throws when a function with the wrong arity cannot be found', () =>
chai.assert.throws(
() => evaluateXPathToString('let $fn := false#2 return $fn(1, 2)', documentNode),
'XPST0017'
));
it('allows a spaces around the arguments', () =>
chai.assert.equal(evaluateXPathToString('concat( (), () )', documentNode), ''));
});
describe('function argument transformation', () => {
it('atomizes attributes to anyUntypedAtomic, then transforming it to a string', () => {
jsonMlMapper.parse(
[
'someElement',
{
attr: 'a b c'
}
],
documentNode
);
chai.assert.deepEqual(
evaluateXPathToStrings('@attr => tokenize()', documentNode.firstChild),
['a', 'b', 'c']
);
});
it('atomizes attributes to anyUntypedAtomic, then transforming it to a numerical value', () => {
jsonMlMapper.parse(
[
'someElement',
{
attr: '1'
}
],
documentNode
);
chai.assert.deepEqual(evaluateXPathToNumbers('@attr to 2', documentNode.firstChild), [
1,
2
]);
});
it('also transforms the return value', () => {
chai.assert.deepEqual(
evaluateXPathToBoolean(
`
declare namespace test = "test";
declare function test:test () as xs:string {
<element>XXX</element>
};
let $ret := test:test()
return $ret instance of xs:string and $ret = "XXX"
`,
null,
null,
null,
{
language: evaluateXPath.XQUERY_3_1_LANGUAGE,
nodesFactory: documentNode
}
),
true
);
});
it('fails if an argument is not convertable', () => {
jsonMlMapper.parse(
[
'someElement',
{
attr: 'not something numerical'
}
],
documentNode
);
chai.assert.throws(
() => evaluateXPathToNumbers('@attr to 2', documentNode.firstChild),
'FORG0001'
);
});
it('Mentions the erroneous function if argument conversion fails', () => {
chai.assert.throws(
() => evaluateXPathToNumbers('abs((1,2,3))', documentNode.firstChild),
'abs'
);
chai.assert.throws(
() => evaluateXPathToNumbers('array:size(())', documentNode.firstChild),
'size'
);
});
});