/
test-common.js
1189 lines (1129 loc) · 34.5 KB
/
test-common.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2011-2019 Digital Bazaar, Inc. All rights reserved.
*/
/* eslint-disable indent */
const EarlReport = require('./earl-report');
const benchmark = require('benchmark');
const join = require('join-path-js');
const rdfCanonize = require('rdf-canonize');
module.exports = function(options) {
'use strict';
const assert = options.assert;
const jsonld = options.jsonld;
const manifest = options.manifest || {
'@context': 'https://json-ld.org/test-suite/context.jsonld',
'@id': '',
'@type': 'mf:Manifest',
description: 'Top level jsonld.js manifest',
name: 'jsonld.js',
sequence: options.entries || [],
filename: '/'
};
const TEST_TYPES = {
'jld:CompactTest': {
skip: {
// skip tests where behavior changed for a 1.1 processor
// see JSON-LD 1.0 Errata
specVersion: ['json-ld-1.0'],
// FIXME
idRegex: [
// @type: @none
/compact-manifest.jsonld#ttn01$/,
/compact-manifest.jsonld#ttn02$/,
/compact-manifest.jsonld#ttn03$/,
// included
/compact-manifest.jsonld#tin01$/,
/compact-manifest.jsonld#tin02$/,
/compact-manifest.jsonld#tin03$/,
/compact-manifest.jsonld#tin04$/,
/compact-manifest.jsonld#tin05$/,
// @json null
/compact-manifest.jsonld#tjs11$/,
// direction
/compact-manifest.jsonld#tdi01$/,
/compact-manifest.jsonld#tdi02$/,
/compact-manifest.jsonld#tdi03$/,
/compact-manifest.jsonld#tdi04$/,
/compact-manifest.jsonld#tdi05$/,
/compact-manifest.jsonld#tdi06$/,
/compact-manifest.jsonld#tdi07$/,
// html
/html-manifest.jsonld#tc001$/,
/html-manifest.jsonld#tc002$/,
/html-manifest.jsonld#tc003$/,
/html-manifest.jsonld#tc004$/,
]
},
fn: 'compact',
params: [
readTestUrl('input'),
readTestJson('context'),
createTestOptions()
],
compare: compareExpectedJson
},
'jld:ExpandTest': {
skip: {
// skip tests where behavior changed for a 1.1 processor
// see JSON-LD 1.0 Errata
specVersion: ['json-ld-1.0'],
// FIXME
idRegex: [
// terms having form of keyword
/expand-manifest.jsonld#t0119$/,
/expand-manifest.jsonld#t0120$/,
/expand-manifest.jsonld#t0122$/,
// html
/html-manifest.jsonld#te001$/,
/html-manifest.jsonld#te002$/,
/html-manifest.jsonld#te003$/,
/html-manifest.jsonld#te004$/,
/html-manifest.jsonld#te005$/,
/html-manifest.jsonld#te006$/,
/html-manifest.jsonld#te007$/,
/html-manifest.jsonld#te010$/,
/html-manifest.jsonld#te011$/,
/html-manifest.jsonld#te012$/,
/html-manifest.jsonld#te013$/,
/html-manifest.jsonld#te014$/,
/html-manifest.jsonld#te015$/,
/html-manifest.jsonld#te016$/,
/html-manifest.jsonld#te017$/,
/html-manifest.jsonld#te018$/,
/html-manifest.jsonld#te019$/,
/html-manifest.jsonld#te020$/,
/html-manifest.jsonld#te021$/,
/html-manifest.jsonld#te022$/,
/html-manifest.jsonld#tex01$/,
// HTML extraction
/expand-manifest.jsonld#thc01$/,
/expand-manifest.jsonld#thc02$/,
/expand-manifest.jsonld#thc03$/,
/expand-manifest.jsonld#thc04$/,
/expand-manifest.jsonld#thc05$/,
// @type: @none
/expand-manifest.jsonld#ttn02$/,
// misc
/expand-manifest.jsonld#te043$/,
/expand-manifest.jsonld#te044$/,
/expand-manifest.jsonld#te048$/,
/expand-manifest.jsonld#te049$/,
// protected null IRI mapping
/expand-manifest.jsonld#tpr28$/,
// remote
/remote-doc-manifest.jsonld#t0005$/,
/remote-doc-manifest.jsonld#t0006$/,
/remote-doc-manifest.jsonld#t0007$/,
/remote-doc-manifest.jsonld#t0010$/,
/remote-doc-manifest.jsonld#t0011$/,
/remote-doc-manifest.jsonld#t0012$/,
/remote-doc-manifest.jsonld#t0013$/,
/remote-doc-manifest.jsonld#tla01$/,
/remote-doc-manifest.jsonld#tla05$/,
// @import
/expand-manifest.jsonld#tso01$/,
/expand-manifest.jsonld#tso02$/,
/expand-manifest.jsonld#tso03$/,
/expand-manifest.jsonld#tso05$/,
/expand-manifest.jsonld#tso06$/,
// @import and protected
/expand-manifest.jsonld#tso07$/,
// context merging
/expand-manifest.jsonld#tso08$/,
/expand-manifest.jsonld#tso10$/,
/expand-manifest.jsonld#tso11$/,
// colliding keywords
/expand-manifest.jsonld#t0114$/,
// included
/expand-manifest.jsonld#tin01$/,
/expand-manifest.jsonld#tin02$/,
/expand-manifest.jsonld#tin03$/,
/expand-manifest.jsonld#tin04$/,
/expand-manifest.jsonld#tin05$/,
/expand-manifest.jsonld#tin06$/,
/expand-manifest.jsonld#tin07$/,
/expand-manifest.jsonld#tin08$/,
/expand-manifest.jsonld#tin09$/,
// @nest
/expand-manifest.jsonld#tn008$/,
// keywords
/expand-manifest.jsonld#tpr30$/,
/expand-manifest.jsonld#tpr31$/,
/expand-manifest.jsonld#tpr32$/,
/expand-manifest.jsonld#tpr33$/,
/expand-manifest.jsonld#tpr34$/,
/expand-manifest.jsonld#tpr35$/,
/expand-manifest.jsonld#tpr36$/,
/expand-manifest.jsonld#tpr37$/,
/expand-manifest.jsonld#tpr39$/,
// direction
/expand-manifest.jsonld#tdi01$/,
/expand-manifest.jsonld#tdi02$/,
/expand-manifest.jsonld#tdi03$/,
/expand-manifest.jsonld#tdi04$/,
/expand-manifest.jsonld#tdi05$/,
/expand-manifest.jsonld#tdi06$/,
/expand-manifest.jsonld#tdi07$/,
/expand-manifest.jsonld#tdi08$/,
/expand-manifest.jsonld#tdi09$/,
]
},
fn: 'expand',
params: [
readTestUrl('input'),
createTestOptions()
],
compare: compareExpectedJson
},
'jld:FlattenTest': {
skip: {
// skip tests where behavior changed for a 1.1 processor
// see JSON-LD 1.0 Errata
specVersion: ['json-ld-1.0'],
// FIXME
idRegex: [
// html
/html-manifest.jsonld#tf001$/,
/html-manifest.jsonld#tf002$/,
/html-manifest.jsonld#tf003$/,
/html-manifest.jsonld#tf004$/,
// included
/flatten-manifest.jsonld#tin01$/,
/flatten-manifest.jsonld#tin02$/,
/flatten-manifest.jsonld#tin03$/,
/flatten-manifest.jsonld#tin04$/,
/flatten-manifest.jsonld#tin05$/,
/flatten-manifest.jsonld#tin06$/,
]
},
fn: 'flatten',
params: [
readTestUrl('input'),
readTestJson('context'),
createTestOptions()
],
compare: compareExpectedJson
},
'jld:FrameTest': {
skip: {
// skip tests where behavior changed for a 1.1 processor
// see JSON-LD 1.0 Errata
specVersion: ['json-ld-1.0'],
// FIXME
idRegex: [
// ex
/frame-manifest.jsonld#tg001$/,
// graphs
/frame-manifest.jsonld#t0011$/,
/frame-manifest.jsonld#t0010$/,
/frame-manifest.jsonld#t0020$/,
/frame-manifest.jsonld#t0023$/,
/frame-manifest.jsonld#t0026$/,
/frame-manifest.jsonld#t0027$/,
/frame-manifest.jsonld#t0028$/,
/frame-manifest.jsonld#t0029$/,
/frame-manifest.jsonld#t0030$/,
/frame-manifest.jsonld#t0031$/,
/frame-manifest.jsonld#t0032$/,
/frame-manifest.jsonld#t0034$/,
/frame-manifest.jsonld#t0035$/,
/frame-manifest.jsonld#t0036$/,
/frame-manifest.jsonld#t0037$/,
/frame-manifest.jsonld#t0038$/,
/frame-manifest.jsonld#t0039$/,
/frame-manifest.jsonld#t0040$/,
/frame-manifest.jsonld#t0041$/,
/frame-manifest.jsonld#t0042$/,
/frame-manifest.jsonld#t0043$/,
/frame-manifest.jsonld#t0044$/,
/frame-manifest.jsonld#t0045$/,
/frame-manifest.jsonld#t0046$/,
/frame-manifest.jsonld#t0047$/,
/frame-manifest.jsonld#t0048$/,
/frame-manifest.jsonld#t0049$/,
/frame-manifest.jsonld#t0050$/,
/frame-manifest.jsonld#t0051$/,
/frame-manifest.jsonld#t0064$/,
/frame-manifest.jsonld#tg002$/,
/frame-manifest.jsonld#tg003$/,
/frame-manifest.jsonld#tg004$/,
/frame-manifest.jsonld#tg006$/,
/frame-manifest.jsonld#tg007$/,
/frame-manifest.jsonld#tg008$/,
/frame-manifest.jsonld#tg009$/,
/frame-manifest.jsonld#tg010$/,
/frame-manifest.jsonld#tp046$/,
/frame-manifest.jsonld#tp049$/,
// blank nodes
/frame-manifest.jsonld#t0052$/,
/frame-manifest.jsonld#t0053$/,
// embed
/frame-manifest.jsonld#t0054$/,
// lists
/frame-manifest.jsonld#t0055$/,
/frame-manifest.jsonld#t0058$/,
// misc
/frame-manifest.jsonld#tp010$/,
/frame-manifest.jsonld#tp050$/,
/frame-manifest.jsonld#teo01$/,
/frame-manifest.jsonld#t0062$/,
/frame-manifest.jsonld#t0063$/,
// @embed:@first
/frame-manifest.jsonld#t0060$/,
// requireAll
/frame-manifest.jsonld#tra01$/,
/frame-manifest.jsonld#tra02$/,
/frame-manifest.jsonld#tra03$/,
// wildcard
/frame-manifest.jsonld#t0061$/,
// included
/frame-manifest.jsonld#tin01$/,
/frame-manifest.jsonld#tin02$/,
/frame-manifest.jsonld#tin03$/,
]
},
fn: 'frame',
params: [
readTestUrl('input'),
readTestJson('frame'),
createTestOptions()
],
compare: compareExpectedJson
},
'jld:FromRDFTest': {
skip: {
// skip tests where behavior changed for a 1.1 processor
// see JSON-LD 1.0 Errata
specVersion: ['json-ld-1.0'],
// FIXME
idRegex: [
// direction
/fromRdf-manifest.jsonld#tdi05$/,
/fromRdf-manifest.jsonld#tdi06$/,
/fromRdf-manifest.jsonld#tdi11$/,
/fromRdf-manifest.jsonld#tdi12$/,
]
},
fn: 'fromRDF',
params: [
readTestNQuads('input'),
createTestOptions({format: 'application/n-quads'})
],
compare: compareExpectedJson
},
'jld:NormalizeTest': {
fn: 'normalize',
params: [
readTestUrl('input'),
createTestOptions({format: 'application/n-quads'})
],
compare: compareExpectedNQuads
},
'jld:ToRDFTest': {
skip: {
// skip tests where behavior changed for a 1.1 processor
// see JSON-LD 1.0 Errata
specVersion: ['json-ld-1.0'],
// FIXME
idRegex: [
// blank node properties
/toRdf-manifest.jsonld#t0118$/,
// terms having form of keyword
/toRdf-manifest.jsonld#te119$/,
/toRdf-manifest.jsonld#te120$/,
/toRdf-manifest.jsonld#te122$/,
// well formed
/toRdf-manifest.jsonld#twf01$/,
/toRdf-manifest.jsonld#twf02$/,
/toRdf-manifest.jsonld#twf03$/,
/toRdf-manifest.jsonld#twf04$/,
/toRdf-manifest.jsonld#twf05$/,
/toRdf-manifest.jsonld#twf06$/,
/toRdf-manifest.jsonld#twf07$/,
// html
/html-manifest.jsonld#tr001$/,
/html-manifest.jsonld#tr002$/,
/html-manifest.jsonld#tr003$/,
/html-manifest.jsonld#tr004$/,
/html-manifest.jsonld#tr005$/,
/html-manifest.jsonld#tr006$/,
/html-manifest.jsonld#tr007$/,
/html-manifest.jsonld#tr010$/,
/html-manifest.jsonld#tr011$/,
/html-manifest.jsonld#tr012$/,
/html-manifest.jsonld#tr013$/,
/html-manifest.jsonld#tr014$/,
/html-manifest.jsonld#tr015$/,
/html-manifest.jsonld#tr016$/,
/html-manifest.jsonld#tr017$/,
/html-manifest.jsonld#tr018$/,
/html-manifest.jsonld#tr019$/,
/html-manifest.jsonld#tr020$/,
/html-manifest.jsonld#tr021$/,
/html-manifest.jsonld#tr022$/,
// number fixes
/toRdf-manifest.jsonld#trt01$/,
// IRI resolution
/toRdf-manifest.jsonld#t0130$/,
/toRdf-manifest.jsonld#t0131$/,
/toRdf-manifest.jsonld#t0132$/,
// @vocab mapping
/toRdf-manifest.jsonld#te075$/,
/toRdf-manifest.jsonld#te109$/,
// Invalid Statement
/toRdf-manifest.jsonld#te111$/,
/toRdf-manifest.jsonld#te112$/,
// index maps
/toRdf-manifest.jsonld#tpi05$/,
/toRdf-manifest.jsonld#tpi06$/,
/toRdf-manifest.jsonld#tpi07$/,
/toRdf-manifest.jsonld#tpi08$/,
/toRdf-manifest.jsonld#tpi09$/,
/toRdf-manifest.jsonld#tpi10$/,
/toRdf-manifest.jsonld#tpi11$/,
// protected null IRI mapping
/toRdf-manifest.jsonld#tpr28$/,
// prefix
/toRdf-manifest.jsonld#tpr29$/,
// @import
/toRdf-manifest.jsonld#tso01$/,
/toRdf-manifest.jsonld#tso02$/,
/toRdf-manifest.jsonld#tso03$/,
/toRdf-manifest.jsonld#tso05$/,
/toRdf-manifest.jsonld#tso06$/,
// @import and protected
/toRdf-manifest.jsonld#tso07$/,
// context merging
/toRdf-manifest.jsonld#tso08$/,
/toRdf-manifest.jsonld#tso10$/,
/toRdf-manifest.jsonld#tso11$/,
// type:none
/toRdf-manifest.jsonld#ttn02$/,
// colliding keyword
/toRdf-manifest.jsonld#te114$/,
// included
/toRdf-manifest.jsonld#tin01$/,
/toRdf-manifest.jsonld#tin02$/,
/toRdf-manifest.jsonld#tin03$/,
/toRdf-manifest.jsonld#tin04$/,
/toRdf-manifest.jsonld#tin05$/,
/toRdf-manifest.jsonld#tin06$/,
// @next
/toRdf-manifest.jsonld#tn008$/,
// keywords
/toRdf-manifest.jsonld#tpr30$/,
/toRdf-manifest.jsonld#tpr31$/,
/toRdf-manifest.jsonld#tpr32$/,
/toRdf-manifest.jsonld#tpr33$/,
/toRdf-manifest.jsonld#tpr34$/,
/toRdf-manifest.jsonld#tpr35$/,
/toRdf-manifest.jsonld#tpr36$/,
/toRdf-manifest.jsonld#tpr37$/,
/toRdf-manifest.jsonld#tpr39$/,
// direction
/toRdf-manifest.jsonld#tdi01$/,
/toRdf-manifest.jsonld#tdi02$/,
/toRdf-manifest.jsonld#tdi03$/,
/toRdf-manifest.jsonld#tdi04$/,
/toRdf-manifest.jsonld#tdi05$/,
/toRdf-manifest.jsonld#tdi06$/,
/toRdf-manifest.jsonld#tdi07$/,
/toRdf-manifest.jsonld#tdi08$/,
/toRdf-manifest.jsonld#tdi09$/,
/toRdf-manifest.jsonld#tdi10$/,
/toRdf-manifest.jsonld#tdi11$/,
/toRdf-manifest.jsonld#tdi12$/,
]
},
fn: 'toRDF',
params: [
readTestUrl('input'),
createTestOptions({format: 'application/n-quads'})
],
compare: compareCanonizedExpectedNQuads
},
'rdfn:Urgna2012EvalTest': {
fn: 'normalize',
params: [
readTestNQuads('action'),
createTestOptions({
algorithm: 'URGNA2012',
inputFormat: 'application/n-quads',
format: 'application/n-quads'
})
],
compare: compareExpectedNQuads
},
'rdfn:Urdna2015EvalTest': {
fn: 'normalize',
params: [
readTestNQuads('action'),
createTestOptions({
algorithm: 'URDNA2015',
inputFormat: 'application/n-quads',
format: 'application/n-quads'
})
],
compare: compareExpectedNQuads
}
};
const SKIP_TESTS = [];
// create earl report
if(options.earl && options.earl.filename) {
options.earl.report = new EarlReport({id: options.earl.id});
}
return new Promise(resolve => {
// async generated tests
// _tests => [{suite}, ...]
// suite => {
// title: ...,
// tests: [test, ...],
// suites: [suite, ...]
// }
const _tests = [];
return addManifest(manifest, _tests)
.then(() => {
_testsToMocha(_tests);
}).then(() => {
if(options.earl.report) {
describe('Writing EARL report to: ' + options.earl.filename, function() {
it('should print the earl report', function() {
return options.writeFile(
options.earl.filename, options.earl.report.reportJson());
});
});
}
}).then(() => resolve());
// build mocha tests from local test structure
function _testsToMocha(tests) {
tests.forEach(suite => {
if(suite.skip) {
describe.skip(suite.title);
return;
}
describe(suite.title, () => {
suite.tests.forEach(test => {
if(test.only) {
it.only(test.title, test.f);
return;
}
it(test.title, test.f);
});
_testsToMocha(suite.suites);
});
suite.imports.forEach(f => {
options.import(f);
});
});
}
});
/**
* Adds the tests for all entries in the given manifest.
*
* @param manifest {Object} the manifest.
* @param parent {Object} the parent test structure
* @return {Promise}
*/
function addManifest(manifest, parent) {
return new Promise((resolve, reject) => {
// create test structure
const suite = {
title: manifest.name || manifest.label,
tests: [],
suites: [],
imports: []
};
parent.push(suite);
// get entries and sequence (alias for entries)
const entries = [].concat(
getJsonLdValues(manifest, 'entries'),
getJsonLdValues(manifest, 'sequence')
);
const includes = getJsonLdValues(manifest, 'include');
// add includes to sequence as jsonld files
for(let i = 0; i < includes.length; ++i) {
entries.push(includes[i] + '.jsonld');
}
// resolve all entry promises and process
Promise.all(entries).then(entries => {
let p = Promise.resolve();
entries.forEach(entry => {
if(typeof entry === 'string' && entry.endsWith('js')) {
// process later as a plain JavaScript file
suite.imports.push(entry);
return;
} else if(typeof entry === 'function') {
// process as a function that returns a promise
p = p.then(() => {
return entry(options);
}).then(childSuite => {
if(suite) {
suite.suites.push(childSuite);
}
});
return;
}
p = p.then(() => {
return readManifestEntry(manifest, entry);
}).then(entry => {
if(isJsonLdType(entry, '__SKIP__')) {
// special local skip logic
suite.tests.push(entry);
} else if(isJsonLdType(entry, 'mf:Manifest')) {
// entry is another manifest
return addManifest(entry, suite.suites);
} else {
// assume entry is a test
return addTest(manifest, entry, suite.tests);
}
});
});
return p;
}).then(() => {
resolve();
}).catch(err => {
console.error(err);
reject(err);
});
});
}
/**
* Adds a test.
*
* @param manifest {Object} the manifest.
* @param parent {Object} the test.
* @param tests {Array} the list of tests to add to.
* @return {Promise}
*/
function addTest(manifest, test, tests) {
// expand @id and input base
const test_id = test['@id'] || test['id'];
//var number = test_id.substr(2);
test['@id'] = manifest.baseIri + basename(manifest.filename) + test_id;
test.base = manifest.baseIri + test.input;
test.manifest = manifest;
const description = test_id + ' ' + (test.purpose || test.name);
const _test = {
title: description,
f: makeFn()
};
// only based on test manifest
// skip handled via skip()
if('only' in test) {
_test.only = test.only;
}
tests.push(_test);
function makeFn() {
return async function() {
const self = this;
self.timeout(5000);
const testInfo = TEST_TYPES[getJsonLdTestType(test)];
// skip based on test manifest
if('skip' in test && test.skip) {
if(options.verboseSkip) {
console.log('Skipping test due to manifest:',
{id: test['@id'], name: test.name});
}
self.skip();
}
// skip based on unknown test type
const testTypes = Object.keys(TEST_TYPES);
if(!isJsonLdType(test, testTypes)) {
if(options.verboseSkip) {
const type = [].concat(
getJsonLdValues(test, '@type'),
getJsonLdValues(test, 'type')
);
console.log('Skipping test due to unknown type:',
{id: test['@id'], name: test.name, type});
}
self.skip();
}
// skip based on test type
if(isJsonLdType(test, SKIP_TESTS)) {
if(options.verboseSkip) {
const type = [].concat(
getJsonLdValues(test, '@type'),
getJsonLdValues(test, 'type')
);
console.log('Skipping test due to test type:',
{id: test['@id'], name: test.name, type});
}
self.skip();
}
// skip based on type info
if(testInfo.skip && testInfo.skip.type) {
if(options.verboseSkip) {
console.log('Skipping test due to type info:',
{id: test['@id'], name: test.name});
}
self.skip();
}
// skip based on id regex
if(testInfo.skip && testInfo.skip.idRegex) {
testInfo.skip.idRegex.forEach(function(re) {
if(re.test(test['@id'])) {
if(options.verboseSkip) {
console.log('Skipping test due to id:',
{id: test['@id']});
}
self.skip();
}
});
}
// skip based on description regex
if(testInfo.skip && testInfo.skip.descriptionRegex) {
testInfo.skip.descriptionRegex.forEach(function(re) {
if(re.test(description)) {
if(options.verboseSkip) {
console.log('Skipping test due to description:',
{id: test['@id'], name: test.name, description});
}
self.skip();
}
});
}
const testOptions = getJsonLdValues(test, 'option');
testOptions.forEach(function(opt) {
const processingModes = getJsonLdValues(opt, 'processingMode');
processingModes.forEach(function(pm) {
let skipModes = [];
if(testInfo.skip && testInfo.skip.processingMode) {
skipModes = testInfo.skip.processingMode;
}
if(skipModes.indexOf(pm) !== -1) {
if(options.verboseSkip) {
console.log('Skipping test due to processingMode:',
{id: test['@id'], name: test.name, processingMode: pm});
}
self.skip();
}
});
});
testOptions.forEach(function(opt) {
const specVersions = getJsonLdValues(opt, 'specVersion');
specVersions.forEach(function(sv) {
let skipVersions = [];
if(testInfo.skip && testInfo.skip.specVersion) {
skipVersions = testInfo.skip.specVersion;
}
if(skipVersions.indexOf(sv) !== -1) {
if(options.verboseSkip) {
console.log('Skipping test due to specVersion:',
{id: test['@id'], name: test.name, specVersion: sv});
}
self.skip();
}
});
});
const fn = testInfo.fn;
const params = testInfo.params.map(param => param(test));
// resolve test data
const values = await Promise.all(params);
let err;
let result;
// run and capture errors and results
try {
result = await jsonld[fn].apply(null, values);
} catch(e) {
err = e;
}
try {
if(isJsonLdType(test, 'jld:NegativeEvaluationTest')) {
await compareExpectedError(test, err);
} else if(isJsonLdType(test, 'jld:PositiveEvaluationTest') ||
isJsonLdType(test, 'rdfn:Urgna2012EvalTest') ||
isJsonLdType(test, 'rdfn:Urdna2015EvalTest')) {
if(err) {
throw err;
}
await testInfo.compare(test, result);
} else if(isJsonLdType(test, 'jld:PositiveSyntaxTest')) {
// no checks
} else {
throw Error('Unknown test type: ' + test.type);
}
if(options.benchmark) {
// pre-load params to avoid doc loader and parser timing
const benchParams = testInfo.params.map(param => param(test, {
load: true
}));
const benchValues = await Promise.all(benchParams);
await new Promise((resolve, reject) => {
const suite = new benchmark.Suite();
suite.add({
name: test.name,
defer: true,
fn: deferred => {
jsonld[fn].apply(null, benchValues).then(() => {
deferred.resolve();
});
}
});
suite
.on('start', e => {
self.timeout((e.target.maxTime + 2) * 1000);
})
.on('cycle', e => {
console.log(String(e.target));
})
.on('error', err => {
reject(new Error(err));
})
.on('complete', () => {
resolve();
})
.run({async: true});
});
}
if(options.earl.report) {
options.earl.report.addAssertion(test, true);
}
} catch(err) {
if(options.bailOnError) {
if(err.name !== 'AssertionError') {
console.error('\nError: ', JSON.stringify(err, null, 2));
}
options.exit();
}
if(options.earl.report) {
options.earl.report.addAssertion(test, false);
}
console.error('Error: ', JSON.stringify(err, null, 2));
throw err;
}
};
}
}
function getJsonLdTestType(test) {
const types = Object.keys(TEST_TYPES);
for(let i = 0; i < types.length; ++i) {
if(isJsonLdType(test, types[i])) {
return types[i];
}
}
return null;
}
function readManifestEntry(manifest, entry) {
let p = Promise.resolve();
let _entry = entry;
if(typeof entry === 'string') {
let _filename;
p = p.then(() => {
if(entry.endsWith('json') || entry.endsWith('jsonld')) {
// load as file
return entry;
}
// load as dir with manifest.jsonld
return joinPath(entry, 'manifest.jsonld');
}).then(entry => {
const dir = dirname(manifest.filename);
return joinPath(dir, entry);
}).then(filename => {
_filename = filename;
return readJson(filename);
}).then(entry => {
_entry = entry;
_entry.filename = _filename;
return _entry;
}).catch(err => {
if(err.code === 'ENOENT') {
//console.log('File does not exist, skipping: ' + _filename);
// return a "skip" entry
_entry = {
type: '__SKIP__',
title: 'Not found, skipping: ' + _filename,
filename: _filename,
skip: true
};
return;
}
throw err;
});
}
return p.then(() => {
_entry.dirname = dirname(_entry.filename || manifest.filename);
return _entry;
});
}
function readTestUrl(property) {
return async function(test, options) {
if(!test[property]) {
return null;
}
if(options && options.load) {
// always load
const filename = await joinPath(test.dirname, test[property]);
return readJson(filename);
}
return test.manifest.baseIri + test[property];
};
}
function readTestJson(property) {
return async function(test) {
if(!test[property]) {
return null;
}
const filename = await joinPath(test.dirname, test[property]);
return readJson(filename);
};
}
function readTestNQuads(property) {
return async function(test) {
if(!test[property]) {
return null;
}
const filename = await joinPath(test.dirname, test[property]);
return readFile(filename);
};
}
function createTestOptions(opts) {
return function(test) {
const options = {
documentLoader: createDocumentLoader(test)
};
const httpOptions = ['contentType', 'httpLink', 'httpStatus', 'redirectTo'];
const testOptions = test.option || {};
for(const key in testOptions) {
if(httpOptions.indexOf(key) === -1) {
options[key] = testOptions[key];
}
}
if(opts) {
// extend options
for(const key in opts) {
options[key] = opts[key];
}
}
return options;
};
}
// find the expected output property or throw error
function _getExpectProperty(test) {
if('expectErrorCode' in test) {
return 'expectErrorCode';
} else if('expect' in test) {
return 'expect';
} else if('result' in test) {
return 'result';
} else {
throw Error('No expected output property found');
}
}
async function compareExpectedJson(test, result) {
let expect;
try {
expect = await readTestJson(_getExpectProperty(test))(test);
assert.deepStrictEqual(result, expect);
} catch(err) {
if(options.bailOnError) {
console.log('\nTEST FAILED\n');
console.log('EXPECTED: ' + JSON.stringify(expect, null, 2));
console.log('ACTUAL: ' + JSON.stringify(result, null, 2));
}
throw err;
}
}
async function compareExpectedNQuads(test, result) {
let expect;
try {
expect = await readTestNQuads(_getExpectProperty(test))(test);
assert.strictEqual(result, expect);
} catch(ex) {
if(options.bailOnError) {
console.log('\nTEST FAILED\n');
console.log('EXPECTED:\n' + expect);
console.log('ACTUAL:\n' + result);
}
throw ex;
}
}
async function compareCanonizedExpectedNQuads(test, result) {
let expect;
try {
expect = await readTestNQuads(_getExpectProperty(test))(test);
const opts = {algorithm: 'URDNA2015'};
const expectDataset = rdfCanonize.NQuads.parse(expect);
const expectCmp = await rdfCanonize.canonize(expectDataset, opts);
const resultDataset = rdfCanonize.NQuads.parse(result);
const resultCmp = await rdfCanonize.canonize(resultDataset, opts);
assert.strictEqual(resultCmp, expectCmp);
} catch(err) {
if(options.bailOnError) {
console.log('\nTEST FAILED\n');
console.log('EXPECTED:\n' + expect);
console.log('ACTUAL:\n' + result);
}
throw err;