@@ -276,7 +276,7 @@ function addTest(manifest, test, tests) {
276
276
} ) ;
277
277
278
278
function makeFn ( ) {
279
- return function ( done ) {
279
+ return async function ( ) {
280
280
const self = this ;
281
281
self . timeout ( 5000 ) ;
282
282
const testInfo = TEST_TYPES [ getJsonLdTestType ( test ) ] ;
@@ -338,82 +338,79 @@ function addTest(manifest, test, tests) {
338
338
339
339
const fn = testInfo . fn ;
340
340
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 ;
359
359
}
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
+ }
387
380
} ) ;
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
+ }
408
397
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 ) ) ;
415
412
throw err ;
416
- } ) ;
413
+ } ;
417
414
} ;
418
415
}
419
416
}
@@ -480,36 +477,36 @@ function readManifestEntry(manifest, entry) {
480
477
}
481
478
482
479
function readTestUrl ( property ) {
483
- return function ( test , options ) {
480
+ return async function ( test , options ) {
484
481
if ( ! test [ property ] ) {
485
482
return null ;
486
483
}
487
484
if ( options && options . load ) {
488
485
// 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 ) ;
491
488
}
492
489
return test . manifest . baseIri + test [ property ] ;
493
490
} ;
494
491
}
495
492
496
493
function readTestJson ( property ) {
497
- return function ( test ) {
494
+ return async function ( test ) {
498
495
if ( ! test [ property ] ) {
499
496
return null ;
500
497
}
501
- return joinPath ( test . dirname , test [ property ] )
502
- . then ( readJson ) ;
498
+ const filename = await joinPath ( test . dirname , test [ property ] ) ;
499
+ return readJson ( filename ) ;
503
500
} ;
504
501
}
505
502
506
503
function readTestNQuads ( property ) {
507
- return function ( test ) {
504
+ return async function ( test ) {
508
505
if ( ! test [ property ] ) {
509
506
return null ;
510
507
}
511
- return joinPath ( test . dirname , test [ property ] )
512
- . then ( readFile ) ;
508
+ const filename = await joinPath ( test . dirname , test [ property ] ) ;
509
+ return readFile ( filename ) ;
513
510
} ;
514
511
}
515
512
@@ -546,52 +543,52 @@ function _getExpectProperty(test) {
546
543
}
547
544
}
548
545
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 ) ;
553
550
assert . deepEqual ( result , expect ) ;
554
- } ) . catch ( err => {
551
+ } catch ( err ) {
555
552
if ( options . bailOnError ) {
556
553
console . log ( '\nTEST FAILED\n' ) ;
557
- console . log ( 'EXPECTED: ' + JSON . stringify ( _expect , null , 2 ) ) ;
554
+ console . log ( 'EXPECTED: ' + JSON . stringify ( expect , null , 2 ) ) ;
558
555
console . log ( 'ACTUAL: ' + JSON . stringify ( result , null , 2 ) ) ;
559
556
}
560
557
throw err ;
561
- } ) ;
558
+ }
562
559
}
563
560
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 ) ;
568
565
assert . equal ( result , expect ) ;
569
- } ) . catch ( err => {
566
+ } catch ( err ) {
570
567
if ( options . bailOnError ) {
571
568
console . log ( '\nTEST FAILED\n' ) ;
572
- console . log ( 'EXPECTED:\n' + _expect ) ;
569
+ console . log ( 'EXPECTED:\n' + expect ) ;
573
570
console . log ( 'ACTUAL:\n' + result ) ;
574
571
}
575
572
throw err ;
576
- } ) ;
573
+ }
577
574
}
578
575
579
- function compareExpectedError ( test , err ) {
576
+ async function compareExpectedError ( test , err ) {
580
577
let expect ;
581
578
let result ;
582
- return Promise . resolve ( ) . then ( ( ) => {
579
+ try {
583
580
expect = test [ _getExpectProperty ( test ) ] ;
584
581
result = getJsonLdErrorCode ( err ) ;
585
582
assert . ok ( err ) ;
586
583
assert . equal ( result , expect ) ;
587
- } ) . catch ( err => {
584
+ } catch ( err ) {
588
585
if ( options . bailOnError ) {
589
586
console . log ( '\nTEST FAILED\n' ) ;
590
587
console . log ( 'EXPECTED: ' + expect ) ;
591
588
console . log ( 'ACTUAL: ' + result ) ;
592
589
}
593
590
throw err ;
594
- } ) ;
591
+ }
595
592
}
596
593
597
594
function isJsonLdType ( node , type ) {
@@ -634,19 +631,17 @@ function getJsonLdErrorCode(err) {
634
631
return err . name ;
635
632
}
636
633
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 ) ;
641
637
}
642
638
643
- function readFile ( filename ) {
639
+ async function readFile ( filename ) {
644
640
return options . readFile ( filename ) ;
645
641
}
646
642
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 ) ) ;
650
645
}
651
646
652
647
function dirname ( filename ) {
0 commit comments