Skip to content

Commit 562156d

Browse files
committed
Partial testing async/await conversion.
1 parent 174c72a commit 562156d

File tree

1 file changed

+103
-108
lines changed

1 file changed

+103
-108
lines changed

tests/test-common.js

Lines changed: 103 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function addTest(manifest, test, tests) {
276276
});
277277

278278
function makeFn() {
279-
return function(done) {
279+
return async function() {
280280
const self = this;
281281
self.timeout(5000);
282282
const testInfo = TEST_TYPES[getJsonLdTestType(test)];
@@ -338,82 +338,79 @@ function addTest(manifest, test, tests) {
338338

339339
const fn = testInfo.fn;
340340
const params = testInfo.params.map(param => param(test));
341-
const callback = function(err, result) {
342-
Promise.resolve().then(() => {
343-
if(isNegativeTest(test)) {
344-
return compareExpectedError(test, err);
345-
} else {
346-
// default is to assume positive and skip isPositiveTest(test) check
347-
if(err) {
348-
throw err;
349-
}
350-
return testInfo.compare(test, result);
351-
}
352-
}).then(() => {
353-
if(options.benchmark) {
354-
// pre-load params to avoid doc loader and parser timing
355-
const benchParams = testInfo.params.map(param => param(test, {
356-
load: true
357-
}));
358-
return Promise.all(benchParams);
341+
// resolve test data
342+
const values = await Promise.all(params);
343+
let err;
344+
let result;
345+
// run and capture errors and results
346+
try {
347+
result = await jsonld[fn].apply(null, values);
348+
} catch(e) {
349+
err = e;
350+
}
351+
352+
try {
353+
if(isNegativeTest(test)) {
354+
await compareExpectedError(test, err);
355+
} else {
356+
// default is to assume positive and skip isPositiveTest(test) check
357+
if(err) {
358+
throw err;
359359
}
360-
}).then(values => {
361-
if(options.benchmark) {
362-
return new Promise((resolve, reject) => {
363-
const suite = new benchmark.Suite();
364-
suite.add({
365-
name: test.name,
366-
defer: true,
367-
fn: deferred => {
368-
jsonld[fn].apply(null, values).then(() => {
369-
deferred.resolve();
370-
});
371-
}
372-
});
373-
suite
374-
.on('start', e => {
375-
self.timeout((e.target.maxTime + 2) * 1000);
376-
})
377-
.on('cycle', e => {
378-
console.log(String(e.target));
379-
})
380-
.on('error', err => {
381-
reject(new Error(err));
382-
})
383-
.on('complete', e => {
384-
resolve();
385-
})
386-
.run({async: true});
360+
await testInfo.compare(test, result);
361+
}
362+
363+
if(options.benchmark) {
364+
// pre-load params to avoid doc loader and parser timing
365+
const benchParams = testInfo.params.map(param => param(test, {
366+
load: true
367+
}));
368+
const benchValues = await Promise.all(benchParams);
369+
370+
await new Promise((resolve, reject) => {
371+
const suite = new benchmark.Suite();
372+
suite.add({
373+
name: test.name,
374+
defer: true,
375+
fn: deferred => {
376+
jsonld[fn].apply(null, values).then(() => {
377+
deferred.resolve();
378+
});
379+
}
387380
});
388-
}
389-
}).then(() => {
390-
if(options.earl.report) {
391-
options.earl.report.addAssertion(test, true);
392-
}
393-
done();
394-
}).catch(err => {
395-
if(options.bailOnError) {
396-
if(err.name !== 'AssertionError') {
397-
console.error('\nError: ', JSON.stringify(err, null, 2));
398-
}
399-
options.exit();
400-
}
401-
if(options.earl.report) {
402-
options.earl.report.addAssertion(test, false);
403-
}
404-
console.error('Error: ', JSON.stringify(err, null, 2));
405-
done(err);
406-
});
407-
};
381+
suite
382+
.on('start', e => {
383+
self.timeout((e.target.maxTime + 2) * 1000);
384+
})
385+
.on('cycle', e => {
386+
console.log(String(e.target));
387+
})
388+
.on('error', err => {
389+
reject(new Error(err));
390+
})
391+
.on('complete', e => {
392+
resolve();
393+
})
394+
.run({async: true});
395+
});
396+
}
408397

409-
// resolve test data run
410-
Promise.all(params).then(values => {
411-
const promise = jsonld[fn].apply(null, values);
412-
return promise.then(callback.bind(null, null), callback);
413-
}).catch(err => {
414-
console.error(err);
398+
if(options.earl.report) {
399+
options.earl.report.addAssertion(test, true);
400+
}
401+
} catch(err) {
402+
if(options.bailOnError) {
403+
if(err.name !== 'AssertionError') {
404+
console.error('\nError: ', JSON.stringify(err, null, 2));
405+
}
406+
options.exit();
407+
}
408+
if(options.earl.report) {
409+
options.earl.report.addAssertion(test, false);
410+
}
411+
console.error('Error: ', JSON.stringify(err, null, 2));
415412
throw err;
416-
});
413+
};
417414
};
418415
}
419416
}
@@ -480,36 +477,36 @@ function readManifestEntry(manifest, entry) {
480477
}
481478

482479
function readTestUrl(property) {
483-
return function(test, options) {
480+
return async function(test, options) {
484481
if(!test[property]) {
485482
return null;
486483
}
487484
if(options && options.load) {
488485
// always load
489-
return joinPath(test.dirname, test[property])
490-
.then(readJson);
486+
const filename = await joinPath(test.dirname, test[property]);
487+
return readJson(filename);
491488
}
492489
return test.manifest.baseIri + test[property];
493490
};
494491
}
495492

496493
function readTestJson(property) {
497-
return function(test) {
494+
return async function(test) {
498495
if(!test[property]) {
499496
return null;
500497
}
501-
return joinPath(test.dirname, test[property])
502-
.then(readJson);
498+
const filename = await joinPath(test.dirname, test[property]);
499+
return readJson(filename);
503500
};
504501
}
505502

506503
function readTestNQuads(property) {
507-
return function(test) {
504+
return async function(test) {
508505
if(!test[property]) {
509506
return null;
510507
}
511-
return joinPath(test.dirname, test[property])
512-
.then(readFile);
508+
const filename = await joinPath(test.dirname, test[property]);
509+
return readFile(filename);
513510
};
514511
}
515512

@@ -546,52 +543,52 @@ function _getExpectProperty(test) {
546543
}
547544
}
548545

549-
function compareExpectedJson(test, result) {
550-
let _expect;
551-
return readTestJson(_getExpectProperty(test))(test).then(expect => {
552-
_expect = expect;
546+
async function compareExpectedJson(test, result) {
547+
let expect;
548+
try {
549+
expect = await readTestJson(_getExpectProperty(test))(test);
553550
assert.deepEqual(result, expect);
554-
}).catch(err => {
551+
} catch(err) {
555552
if(options.bailOnError) {
556553
console.log('\nTEST FAILED\n');
557-
console.log('EXPECTED: ' + JSON.stringify(_expect, null, 2));
554+
console.log('EXPECTED: ' + JSON.stringify(expect, null, 2));
558555
console.log('ACTUAL: ' + JSON.stringify(result, null, 2));
559556
}
560557
throw err;
561-
});
558+
}
562559
}
563560

564-
function compareExpectedNQuads(test, result) {
565-
let _expect;
566-
return readTestNQuads(_getExpectProperty(test))(test).then(expect => {
567-
_expect = expect;
561+
async function compareExpectedNQuads(test, result) {
562+
let expect;
563+
try {
564+
expect = await readTestNQuads(_getExpectProperty(test))(test);
568565
assert.equal(result, expect);
569-
}).catch(err => {
566+
} catch(err) {
570567
if(options.bailOnError) {
571568
console.log('\nTEST FAILED\n');
572-
console.log('EXPECTED:\n' + _expect);
569+
console.log('EXPECTED:\n' + expect);
573570
console.log('ACTUAL:\n' + result);
574571
}
575572
throw err;
576-
});
573+
}
577574
}
578575

579-
function compareExpectedError(test, err) {
576+
async function compareExpectedError(test, err) {
580577
let expect;
581578
let result;
582-
return Promise.resolve().then(() => {
579+
try {
583580
expect = test[_getExpectProperty(test)];
584581
result = getJsonLdErrorCode(err);
585582
assert.ok(err);
586583
assert.equal(result, expect);
587-
}).catch(err => {
584+
} catch(err) {
588585
if(options.bailOnError) {
589586
console.log('\nTEST FAILED\n');
590587
console.log('EXPECTED: ' + expect);
591588
console.log('ACTUAL: ' + result);
592589
}
593590
throw err;
594-
});
591+
}
595592
}
596593

597594
function isJsonLdType(node, type) {
@@ -634,19 +631,17 @@ function getJsonLdErrorCode(err) {
634631
return err.name;
635632
}
636633

637-
function readJson(filename) {
638-
return readFile(filename).then((data) => {
639-
return JSON.parse(data);
640-
});
634+
async function readJson(filename) {
635+
const data = await readFile(filename);
636+
return JSON.parse(data);
641637
}
642638

643-
function readFile(filename) {
639+
async function readFile(filename) {
644640
return options.readFile(filename);
645641
}
646642

647-
function joinPath() {
648-
return Promise.resolve(
649-
join.apply(null, Array.prototype.slice.call(arguments)));
643+
async function joinPath() {
644+
return join.apply(null, Array.prototype.slice.call(arguments));
650645
}
651646

652647
function dirname(filename) {

0 commit comments

Comments
 (0)