-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfunction.test.js
564 lines (506 loc) · 16.7 KB
/
function.test.js
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import { html, oneEvent, fixtureSync, expect } from '@open-wc/testing';
import '../index.js';
import { evaluateXPathToNodes, evaluateXPath } from 'fontoxpath';
import registerFunction from '../src/functions/registerFunction.js';
describe('functions', () => {
describe('Functions in JavaScript', () => {
it('can define a simple function', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-function
signature="local:theanswer() as xs:decimal"
override="no"
type="text/javascript"
>
return 21*2;
</fx-function>
</fx-model>
<label>{local:theanswer()}</label>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
const label = el.querySelector('label');
expect(label.innerText).to.equal('42');
});
it('can define a simple function with an argument', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-function
signature="local:pow2($arg as xs:decimal) as xs:decimal"
override="no"
type="text/javascript"
>
return $arg * $arg;
</fx-function>
</fx-model>
<label>{local:pow2(10)}</label>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
const label = el.querySelector('label');
expect(label.innerText).to.equal('100');
});
});
describe('functions in XPath', () => {
it('can define a simple function', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-function
signature="local:hello-world() as xs:string"
override="no"
type="text/xpath"
>
("Hello", "World") => string-join(" ")
</fx-function>
</fx-model>
<label>{local:hello-world()}</label>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
const label = el.querySelector('label');
expect(label.innerText).to.equal('Hello World');
});
it('can define a simple function without any prefix', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-function signature="hello-world() as xs:string" override="no" type="text/xpath">
("Hello", "World") => string-join(" ")
</fx-function>
</fx-model>
<label>{hello-world()}</label>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
const label = el.querySelector('label');
expect(label.innerText).to.equal('Hello World');
});
it('can define a simple function with an argument', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-function
signature="local:hello($who as xs:string) as xs:string"
override="no"
type="text/xpath"
>
"Hello " || $who
</fx-function>
</fx-model>
<label>{local:hello("World")}</label>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
const label = el.querySelector('label');
expect(label.innerText).to.equal('Hello World');
});
});
// The following two tests are failing.
it('can define a simple function', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-function
signature="local:theanswer() as xs:decimal"
override="no"
type="text/javascript"
>
return 21*2;
</fx-function>
</fx-model>
<fx-output ref="theanswer">{local:theanswer()}</fx-output>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
const model = el.querySelector('fx-model');
console.log('modelitems ', model.modelItems);
expect(model.modelItems.length).to.equal(1);
const output = el.querySelector('fx-output');
expect(output.textContent).to.equal('42');
});
it('can define a simple function using explicit models', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-instance>
<data>
<theanswer></theanswer>
</data>
</fx-instance>
<fx-bind ref="theanswer" required="true()" calculate="local:theans"></fx-bind>
<fx-function
signature="local:theanswer() as xs:decimal"
override="no"
type="text/javascript"
>
return 21*2;
</fx-function>
</fx-model>
<div id="output">{local:theanswer()}</div>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
const model = el.querySelector('fx-model');
console.log('modelitems ', model.modelItems);
expect(model.modelItems.length).to.equal(1);
// following assumptions were wrong - output is never setting the value
const output = el.querySelector('#output');
expect(output.textContent).to.equal('42');
});
it('returns 1 for repeat index() by default', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-instance>
<data>
<theanswer></theanswer>
<theanswer></theanswer>
</data>
</fx-instance>
</fx-model>
<fx-repeat id="repeat">
<template>
<fx-output ref="theanswer"></fx-output>
</template>
</fx-repeat>
<span id="index">{index('repeat')}</span>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
const indexVal = document.getElementById('index').innerText;
expect(Number(indexVal)).to.equal(1);
});
it('returns correct index after insert for repeat index()', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-instance>
<data>
<theanswer></theanswer>
<theanswer></theanswer>
</data>
</fx-instance>
</fx-model>
<fx-repeat id="repeat">
<template>
<fx-output ref="theanswer"></fx-output>
</template>
</fx-repeat>
<span id="index">{index('repeat')}</span>
<fx-trigger>
<button>insert at end</button>
<fx-insert ref="theanswer"></fx-insert>
</fx-trigger>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
const trigger = el.querySelector('fx-trigger');
await trigger.performActions();
const indexVal = document.getElementById('index').innerText;
expect(Number(indexVal)).to.equal(3);
});
it.skip('returns correct index after insert for nested repeat index()', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-instance>
<data>
<item>1</item>
<item>2</item>
<item>3</item>
</data>
</fx-instance>
</fx-model>
<pre id="indices">
<fx-repeat id="repeat" ref="/data/item">
<template>
<fx-repeat id="nested-repeat" ref="/data/item">
<template>
<span>({index("repeat") || ";" || index("nested-repeat")})</span>
<input type="text"/>
</template>
</fx-repeat>
</template>
</fx-repeat>
</pre>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
el.style.display = 'block';
// Second row, second item
const span = el.querySelectorAll('input')[3 + 1];
span.focus();
el.refresh();
const indices = document.getElementById('indices');
expect(indices.innerText.replace(/\s/g, '')).to.equal(
'(2;1)(2;1)(2;1)(2;2)(2;2)(2;2)(2;1)(2;1)(2;1)',
'The outer repeat is indexed at 2, the first and last inner ones at 1, the middle inner one at 2!',
);
});
it('context() in repeats returns the correct item', async () => {
const el = await fixtureSync(
html` <fx-fore>
<fx-model>
<fx-instance id="mapping">
<data>
<df tag="245" scope="bf:Instance" scope-rel="bf:title" domain="bf:Title">
<sf code="a">bf:mainTitle</sf>
<sf code="b">bf:subtitle</sf>
<sf code="c">bf:responsibilityStatement</sf>
</df>
</data>
</fx-instance>
<fx-instance id="desc">
<data>
<df>
<tag>245</tag>
<ind1></ind1>
<ind2></ind2>
<sfs>
<sf>
<code>a</code>
<value>value-of-a</value>
</sf>
<sf>
<code>b</code>
<value>value-of-b</value>
</sf>
</sfs>
</df>
</data>
</fx-instance>
</fx-model>
<fx-repeat ref="instance('desc')/df">
<template>
<fx-control ref="tag" update-event="input">
<label>Datafield</label>
</fx-control>
<fx-control ref="sfs/sf/code" update-event="input">
<label>Subfield</label>
</fx-control>
<fx-control ref="sfs/sf/value" update-event="input">
<label>Content</label>
</fx-control>
</template>
</fx-repeat>
<fx-repeat ref="instance('mapping')/df[@tag = instance('desc')/df/tag]" id="outer-repeat">
<template>
<h2>{@scope || " ➙ " || @scope-rel || " ➙ " || @domain}</h2>
<fx-repeat ref="sf[@code = instance('desc')/df/sfs/sf/code]" id="inner-repeat">
<template>
<fx-var name="current" value="."></fx-var>
<h3 style="display: inline;">{.}</h3>
<!-- context() does not work here, but $current does -->
<span id="context-item-span">{@code}</span>
<span id="context-function-span">{context()/@code}</span>
<span id="current-span">{$current/@code}</span>
<p id="result-p-with-context" style="display: inline;">
{instance('desc')/df/sfs/sf[code = context()/@code]/value}
</p>
<p id="result-p-with-current" style="display: inline;">
{instance('desc')/df/sfs/sf[code = $current/@code]/value}
</p>
</template>
</fx-repeat>
</template>
</fx-repeat>
</fx-fore>`,
);
await oneEvent(el, 'refresh-done');
const contextItemSpans = el.querySelectorAll('#context-item-span');
const contextFunctionSpans = el.querySelectorAll('#context-function-span');
const firstContextItemSpan = contextItemSpans[0];
expect(firstContextItemSpan.innerText).to.equal(
'a',
'firstContextItemSpan.innerText should be OK',
);
const firstContextFunctionSpan = contextFunctionSpans[0];
expect(firstContextFunctionSpan.innerText).to.equal(
'a',
'firstContextFunctionSpan.innerText should be OK',
);
const secondContextItemSpan = contextItemSpans[1];
expect(secondContextItemSpan.innerText).to.equal(
'b',
'secondContextItemSpan.innerText should be OK',
);
const secondContextFunctionSpan = contextFunctionSpans[1];
expect(secondContextFunctionSpan.innerText).to.equal(
'b',
'secondContextFunctionSpan.innerText should be OK',
);
const contextPs = el.querySelectorAll('#result-p-with-context');
const currentPs = el.querySelectorAll('#result-p-with-current');
expect(contextPs[0].innerText).to.equal(
currentPs[0].innerText,
'The second result in the P should be correct',
);
expect(contextPs[1].innerText).to.equal(
currentPs[1].innerText,
'The second result in the P should be correct',
);
});
describe('Edge cases', () => {
/**
* @type {HTMLElement}
*/
let el;
beforeEach(async () => {
const doc = await fixtureSync(html`
<fx-fore>
<fx-model> </fx-model>
<span>Hello World!</span>
</fx-fore>
`);
await oneEvent(doc, 'refresh-done');
el = doc.querySelector('span');
});
it('should parse simple function with prefix', () => {
expect(() =>
registerFunction({
signature: 'fn:concat($arg1 as xs:string, $arg2 as xs:string) as xs:string',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse local function with multiple parameters', () => {
expect(() =>
registerFunction({
signature:
'local:my-function($param1 as xs:integer, $param2 as element()*) as element()*',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse local function with multiple element parameters', () => {
expect(() =>
registerFunction({
signature:
'local:my-function($param1 as element()?, $param2 as element()*) as element()*',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse function with no parameters', () => {
expect(() =>
registerFunction({
signature: 'myFunction() as xs:boolean',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse function with no return type', () => {
expect(() =>
registerFunction({
signature: 'fn:sum($seq as xs:integer*)',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse function with item parameter', () => {
expect(() =>
registerFunction({
signature: 'prefix:function($param as item())',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse function with double return type', () => {
expect(() =>
registerFunction({
signature: 'compute-average($values as xs:double*) as xs:double',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse function with element parameter', () => {
expect(() =>
registerFunction({
signature: 'generate-id($element as element()) as xs:string',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse function with multiple xs:integer parameters', () => {
expect(() =>
registerFunction({
signature: 'calculate($a as xs:integer, $b as xs:integer) as xs:integer',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse function with multiple xs:string parameters', () => {
expect(() =>
registerFunction({
signature:
'translate($text as xs:string, $fromLang as xs:string, $toLang as xs:string) as xs:string',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse function with no return type and multiple parameters', () => {
expect(() =>
registerFunction({
signature: 'prefix:noReturnType($param1 as xs:string, $param2 as xs:integer)',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
it('should parse function with single xs:string parameter', () => {
expect(() =>
registerFunction({
signature: 'xml:parse($xmlString as xs:string)',
type: null,
functionBody: '()',
}),
).to.not.throw();
});
});
});
/*
it.only('context() function returns correct nodesets', async () => {
const el = await fixtureSync(html`
<fx-fore>
<fx-model>
<fx-instance>
<data>
<group>
<item attr="foo">bar</item>
</group>
<copy></copy>
</data>
</fx-instance>
</fx-model>
<fx-group ref="group">
<fx-control ref="item/@attr">
<fx-setvalue event="value-changed" ref="copy" value="context()"></fx-setvalue>
</fx-control>
<div id="result">{copy}</div>
</fx-group>
</fx-fore>
`);
await oneEvent(el, 'refresh-done');
const control = el.querySelector('fx-control');
control.setValue("fooo");
const action = el.querySelector('fx-setvalue');
action.perform();
// const result = evaluateXPath('context()',action);
// await oneEvent(action, 'action-performed');
const copy = el.querySelector('#result');
expect(copy.innerHTML).to.equal('foo');
});
*/