3
3
* @typedef {import('vfile').VFile } VFile
4
4
* @typedef {import('../lib/index.js').Node } Node
5
5
*
6
- * @typedef Options
6
+ * @typedef Config
7
7
* @property {VFile } file
8
- * @property {string } out
8
+ * @property {URL } out
9
9
*/
10
10
11
- import fs from 'node:fs'
12
- import path from 'node:path'
13
- import assert from 'node:assert'
14
- import test from 'tape'
11
+ import assert from 'node:assert/strict'
12
+ import fs from 'node:fs/promises'
13
+ import test from 'node:test'
15
14
import { isHidden } from 'is-hidden'
16
15
import { parse , parseFragment } from 'parse5'
17
16
import { visit } from 'unist-util-visit'
18
- import { toVFile } from 'to-vfile'
17
+ import { read , toVFile } from 'to-vfile'
19
18
import { fromParse5 } from '../index.js'
20
19
21
- const join = path . join
22
-
23
- test ( 'hast-util-from-parse5' , ( t ) => {
20
+ test ( 'fromParse5' , ( ) => {
24
21
const file = toVFile ( { value : '<title>Hello!</title><h1>World!' } )
25
22
26
- t . deepEqual (
23
+ assert . deepEqual (
27
24
fromParse5 ( parse ( String ( file ) ) ) ,
28
25
{
29
26
type : 'root' ,
@@ -67,7 +64,7 @@ test('hast-util-from-parse5', (t) => {
67
64
'should transform a complete document'
68
65
)
69
66
70
- t . deepEqual (
67
+ assert . deepEqual (
71
68
fromParse5 ( parseFragment ( String ( file ) ) ) ,
72
69
{
73
70
type : 'root' ,
@@ -90,7 +87,7 @@ test('hast-util-from-parse5', (t) => {
90
87
'should transform a fragment'
91
88
)
92
89
93
- t . deepEqual (
90
+ assert . deepEqual (
94
91
fromParse5 ( parse ( String ( file ) , { sourceCodeLocationInfo : true } ) , file ) ,
95
92
{
96
93
type : 'root' ,
@@ -164,7 +161,7 @@ test('hast-util-from-parse5', (t) => {
164
161
'should accept a file as options'
165
162
)
166
163
167
- t . deepEqual (
164
+ assert . deepEqual (
168
165
fromParse5 ( parse ( String ( file ) ) , file ) ,
169
166
{
170
167
type : 'root' ,
@@ -218,7 +215,7 @@ test('hast-util-from-parse5', (t) => {
218
215
'should accept a file as options (without location info)'
219
216
)
220
217
221
- t . deepEqual (
218
+ assert . deepEqual (
222
219
fromParse5 (
223
220
{
224
221
nodeName : 'title' ,
@@ -258,7 +255,7 @@ test('hast-util-from-parse5', (t) => {
258
255
'should support synthetic locations'
259
256
)
260
257
261
- t . deepEqual (
258
+ assert . deepEqual (
262
259
fromParse5 (
263
260
{
264
261
nodeName : 'p' ,
@@ -312,7 +309,7 @@ test('hast-util-from-parse5', (t) => {
312
309
'should support synthetic locations on unclosed elements'
313
310
)
314
311
315
- t . deepEqual (
312
+ assert . deepEqual (
316
313
fromParse5 (
317
314
parseFragment (
318
315
[
@@ -352,47 +349,33 @@ test('hast-util-from-parse5', (t) => {
352
349
} ,
353
350
'should transform svg'
354
351
)
355
-
356
- t . end ( )
357
352
} )
358
353
359
- test ( 'fixtures' , ( t ) => {
360
- const base = join ( 'test ', 'fixtures' )
361
- const files = fs . readdirSync ( base )
354
+ test ( 'fixtures' , async ( ) => {
355
+ const base = new URL ( 'fixtures/ ', import . meta . url )
356
+ const folders = await fs . readdir ( base )
362
357
let index = - 1
363
358
364
- while ( ++ index < files . length ) {
365
- if ( ! isHidden ( files [ index ] ) ) {
366
- each ( files [ index ] )
367
- }
368
- }
359
+ while ( ++ index < folders . length ) {
360
+ const folder = folders [ index ]
369
361
370
- t . end ( )
371
-
372
- /**
373
- * @param {string } fixture
374
- */
375
- function each ( fixture ) {
376
- t . test ( fixture , ( st ) => {
377
- const options = {
378
- file : toVFile . readSync ( join ( base , fixture , 'index.html' ) ) ,
379
- out : join ( base , fixture , 'index.json' )
380
- }
381
-
382
- st . plan ( 4 )
362
+ if ( isHidden ( folder ) ) {
363
+ continue
364
+ }
383
365
384
- checkYesYes ( st , options )
385
- checkNoYes ( st , options )
386
- checkYesNo ( st , options )
387
- checkNoNo ( st , options )
388
- } )
366
+ const file = await read ( new URL ( folder + '/index.html' , base ) )
367
+ const out = new URL ( folder + '/index.json' , base )
368
+ const config = { file, out}
369
+ await checkYesYes ( config )
370
+ await checkNoYes ( config )
371
+ await checkYesNo ( config )
372
+ await checkNoNo ( config )
389
373
}
390
374
391
375
/**
392
- * @param {Test } t
393
- * @param {Options } options
376
+ * @param {Config } options
394
377
*/
395
- function checkYesYes ( t , options ) {
378
+ async function checkYesYes ( options ) {
396
379
const input = parse ( String ( options . file ) , {
397
380
sourceCodeLocationInfo : true
398
381
} )
@@ -401,65 +384,70 @@ test('fixtures', (t) => {
401
384
let expected
402
385
403
386
try {
404
- expected = JSON . parse ( String ( fs . readFileSync ( options . out ) ) )
387
+ expected = JSON . parse ( String ( await fs . readFile ( options . out ) ) )
405
388
} catch {
406
389
// New fixture.
407
- fs . writeFileSync ( options . out , JSON . stringify ( actual , null , 2 ) + '\n' )
390
+ await fs . writeFile ( options . out , JSON . stringify ( actual , null , 2 ) + '\n' )
408
391
return
409
392
}
410
393
411
- log ( 'yesyes' , actual , expected )
412
- t . deepEqual ( actual , expected , 'p5 w/ position, hast w/ intent of position' )
394
+ assert . deepEqual (
395
+ actual ,
396
+ expected ,
397
+ 'p5 w/ position, hast w/ intent of position'
398
+ )
413
399
}
414
400
415
401
/**
416
- * @param {Test } t
417
- * @param {Options } options
402
+ * @param {Config } options
418
403
*/
419
- function checkNoYes ( t , options ) {
404
+ async function checkNoYes ( options ) {
420
405
const input = parse ( String ( options . file ) )
421
406
const actual = fromParse5 ( input , { file : options . file , verbose : true } )
422
407
/** @type {Node } */
423
- const expected = JSON . parse ( String ( fs . readFileSync ( options . out ) ) )
408
+ const expected = JSON . parse ( String ( await fs . readFile ( options . out ) ) )
424
409
425
410
clean ( expected )
426
411
427
- log ( 'noyes' , actual , expected )
428
- t . deepEqual ( actual , expected , 'p5 w/o position, hast w/ intent of position' )
412
+ assert . deepEqual (
413
+ actual ,
414
+ expected ,
415
+ 'p5 w/o position, hast w/ intent of position'
416
+ )
429
417
}
430
418
431
419
/**
432
- * @param {Test } t
433
- * @param {Options } options
420
+ * @param {Config } options
434
421
*/
435
- function checkYesNo ( t , options ) {
422
+ async function checkYesNo ( options ) {
436
423
const input = parse ( String ( options . file ) , {
437
424
sourceCodeLocationInfo : true
438
425
} )
439
426
const actual = fromParse5 ( input )
440
427
/** @type {Node } */
441
- const expected = JSON . parse ( String ( fs . readFileSync ( options . out ) ) )
428
+ const expected = JSON . parse ( String ( await fs . readFile ( options . out ) ) )
442
429
443
430
clean ( expected )
444
431
445
- log ( 'yesno' , actual , expected )
446
- t . deepEqual ( actual , expected , 'p5 w/ position, hast w/o intent of position' )
432
+ assert . deepEqual (
433
+ actual ,
434
+ expected ,
435
+ 'p5 w/ position, hast w/o intent of position'
436
+ )
447
437
}
448
438
449
439
/**
450
- * @param {Test } t
451
- * @param {Options } options
440
+ * @param {Config } options
452
441
*/
453
- function checkNoNo ( t , options ) {
442
+ async function checkNoNo ( options ) {
454
443
const input = parse ( String ( options . file ) )
455
444
const actual = fromParse5 ( input )
456
445
/** @type {Node } */
457
- const expected = JSON . parse ( String ( fs . readFileSync ( options . out ) ) )
446
+ const expected = JSON . parse ( String ( await fs . readFile ( options . out ) ) )
458
447
459
448
clean ( expected )
460
449
461
- log ( 'nono' , actual , expected )
462
- t . deepEqual (
450
+ assert . deepEqual (
463
451
actual ,
464
452
expected ,
465
453
'p5 w/o position, hast w/o intent of position'
@@ -484,19 +472,3 @@ function clean(tree) {
484
472
}
485
473
} )
486
474
}
487
-
488
- /**
489
- * @param {string } label
490
- * @param {Node } actual
491
- * @param {Node } expected
492
- */
493
- function log ( label , actual , expected ) {
494
- try {
495
- assert . deepStrictEqual ( actual , expected , label )
496
- } catch {
497
- console . log ( 'actual:%s:' , label )
498
- console . dir ( actual , { depth : null } )
499
- console . log ( 'expected:%s:' , label )
500
- console . dir ( expected , { depth : null } )
501
- }
502
- }
0 commit comments