Skip to content

Commit a6b5e4f

Browse files
committed
Support test loader url rewrite map option.
- Experimental option used for benchmarking.
1 parent 5dcc0d2 commit a6b5e4f

File tree

1 file changed

+66
-31
lines changed

1 file changed

+66
-31
lines changed

tests/test-common.js

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -570,42 +570,21 @@ function addTest(manifest, test, tests) {
570570
throw Error('Unknown test type: ' + test.type);
571571
}
572572

573+
let benchResults = null;
573574
if(options.benchmark) {
574-
// pre-load params to avoid doc loader and parser timing
575-
const benchParams = testInfo.params.map(param => param(test, {
576-
load: true
577-
}));
578-
const benchValues = await Promise.all(benchParams);
579-
580-
await new Promise((resolve, reject) => {
581-
const suite = new benchmark.Suite();
582-
suite.add({
583-
name: test.name,
584-
defer: true,
585-
fn: deferred => {
586-
jsonld[fn].apply(null, benchValues).then(() => {
587-
deferred.resolve();
588-
});
589-
}
590-
});
591-
suite
592-
.on('start', e => {
593-
self.timeout((e.target.maxTime + 2) * 1000);
594-
})
595-
.on('cycle', e => {
596-
console.log(String(e.target));
597-
})
598-
.on('error', err => {
599-
reject(new Error(err));
600-
})
601-
.on('complete', () => {
602-
resolve();
603-
})
604-
.run({async: true});
575+
benchResults = await runBenchmark({
576+
test,
577+
fn,
578+
params: testInfo.params.map(param => param(test, {
579+
// pre-load params to avoid doc loader and parser timing
580+
load: true
581+
})),
582+
mochaTest: self
605583
});
606584
}
607585

608586
if(options.earl.report) {
587+
// TODO: add benchmark info
609588
options.earl.report.addAssertion(test, true);
610589
}
611590
} catch(err) {
@@ -625,6 +604,38 @@ function addTest(manifest, test, tests) {
625604
}
626605
}
627606

607+
async function runBenchmark({test, fn, params, mochaTest}) {
608+
const values = await Promise.all(params);
609+
610+
return new Promise((resolve, reject) => {
611+
const suite = new benchmark.Suite();
612+
suite.add({
613+
name: test.name,
614+
defer: true,
615+
fn: deferred => {
616+
jsonld[fn].apply(null, values).then(() => {
617+
deferred.resolve();
618+
});
619+
}
620+
});
621+
suite
622+
.on('start', e => {
623+
// set timeout to a bit more than max benchmark time
624+
mochaTest.timeout((e.target.maxTime + 2) * 1000);
625+
})
626+
.on('cycle', e => {
627+
console.log(String(e.target));
628+
})
629+
.on('error', err => {
630+
reject(new Error(err));
631+
})
632+
.on('complete', e => {
633+
resolve();
634+
})
635+
.run({async: true});
636+
});
637+
}
638+
628639
function getJsonLdTestType(test) {
629640
const types = Object.keys(TEST_TYPES);
630641
for(let i = 0; i < types.length; ++i) {
@@ -892,6 +903,26 @@ function basename(filename) {
892903
return filename.substr(idx + 1);
893904
}
894905

906+
// check test.option.loader.rewrite map for url,
907+
// if no test rewrite, check manifest,
908+
// else no rewrite
909+
function rewrite(test, url) {
910+
if(test.option &&
911+
test.option.loader &&
912+
test.option.loader.rewrite &&
913+
url in test.option.loader.rewrite) {
914+
return test.option.loader.rewrite[url];
915+
}
916+
const manifest = test.manifest;
917+
if(manifest.option &&
918+
manifest.option.loader &&
919+
manifest.option.loader.rewrite &&
920+
url in manifest.option.loader.rewrite) {
921+
return manifest.option.loader.rewrite[url];
922+
}
923+
return url;
924+
}
925+
895926
/**
896927
* Creates a test remote document loader.
897928
*
@@ -906,13 +937,17 @@ function createDocumentLoader(test) {
906937
'https://w3c.github.io/json-ld-api/tests',
907938
'https://w3c.github.io/json-ld-framing/tests'
908939
];
940+
909941
const localLoader = function(url) {
910942
// always load remote-doc tests remotely in node
911943
// NOTE: disabled due to github pages issues.
912944
//if(options.nodejs && test.manifest.name === 'Remote document') {
913945
// return jsonld.documentLoader(url);
914946
//}
915947

948+
// handle loader rewrite options for test or manifest
949+
url = rewrite(test, url);
950+
916951
// FIXME: this check only works for main test suite and will not work if:
917952
// - running other tests and main test suite not installed
918953
// - use other absolute URIs but want to load local files

0 commit comments

Comments
 (0)