33 * @typedef {import('vfile').VFile } VFile
44 * @typedef {import('../lib/index.js').Node } Node
55 *
6- * @typedef Options
6+ * @typedef Config
77 * @property {VFile } file
8- * @property {string } out
8+ * @property {URL } out
99 */
1010
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'
1514import { isHidden } from 'is-hidden'
1615import { parse , parseFragment } from 'parse5'
1716import { visit } from 'unist-util-visit'
18- import { toVFile } from 'to-vfile'
17+ import { read , toVFile } from 'to-vfile'
1918import { fromParse5 } from '../index.js'
2019
21- const join = path . join
22-
23- test ( 'hast-util-from-parse5' , ( t ) => {
20+ test ( 'fromParse5' , ( ) => {
2421 const file = toVFile ( { value : '<title>Hello!</title><h1>World!' } )
2522
26- t . deepEqual (
23+ assert . deepEqual (
2724 fromParse5 ( parse ( String ( file ) ) ) ,
2825 {
2926 type : 'root' ,
@@ -67,7 +64,7 @@ test('hast-util-from-parse5', (t) => {
6764 'should transform a complete document'
6865 )
6966
70- t . deepEqual (
67+ assert . deepEqual (
7168 fromParse5 ( parseFragment ( String ( file ) ) ) ,
7269 {
7370 type : 'root' ,
@@ -90,7 +87,7 @@ test('hast-util-from-parse5', (t) => {
9087 'should transform a fragment'
9188 )
9289
93- t . deepEqual (
90+ assert . deepEqual (
9491 fromParse5 ( parse ( String ( file ) , { sourceCodeLocationInfo : true } ) , file ) ,
9592 {
9693 type : 'root' ,
@@ -164,7 +161,7 @@ test('hast-util-from-parse5', (t) => {
164161 'should accept a file as options'
165162 )
166163
167- t . deepEqual (
164+ assert . deepEqual (
168165 fromParse5 ( parse ( String ( file ) ) , file ) ,
169166 {
170167 type : 'root' ,
@@ -218,7 +215,7 @@ test('hast-util-from-parse5', (t) => {
218215 'should accept a file as options (without location info)'
219216 )
220217
221- t . deepEqual (
218+ assert . deepEqual (
222219 fromParse5 (
223220 {
224221 nodeName : 'title' ,
@@ -258,7 +255,7 @@ test('hast-util-from-parse5', (t) => {
258255 'should support synthetic locations'
259256 )
260257
261- t . deepEqual (
258+ assert . deepEqual (
262259 fromParse5 (
263260 {
264261 nodeName : 'p' ,
@@ -312,7 +309,7 @@ test('hast-util-from-parse5', (t) => {
312309 'should support synthetic locations on unclosed elements'
313310 )
314311
315- t . deepEqual (
312+ assert . deepEqual (
316313 fromParse5 (
317314 parseFragment (
318315 [
@@ -352,47 +349,33 @@ test('hast-util-from-parse5', (t) => {
352349 } ,
353350 'should transform svg'
354351 )
355-
356- t . end ( )
357352} )
358353
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 )
362357 let index = - 1
363358
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 ]
369361
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+ }
383365
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 )
389373 }
390374
391375 /**
392- * @param {Test } t
393- * @param {Options } options
376+ * @param {Config } options
394377 */
395- function checkYesYes ( t , options ) {
378+ async function checkYesYes ( options ) {
396379 const input = parse ( String ( options . file ) , {
397380 sourceCodeLocationInfo : true
398381 } )
@@ -401,65 +384,70 @@ test('fixtures', (t) => {
401384 let expected
402385
403386 try {
404- expected = JSON . parse ( String ( fs . readFileSync ( options . out ) ) )
387+ expected = JSON . parse ( String ( await fs . readFile ( options . out ) ) )
405388 } catch {
406389 // 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' )
408391 return
409392 }
410393
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+ )
413399 }
414400
415401 /**
416- * @param {Test } t
417- * @param {Options } options
402+ * @param {Config } options
418403 */
419- function checkNoYes ( t , options ) {
404+ async function checkNoYes ( options ) {
420405 const input = parse ( String ( options . file ) )
421406 const actual = fromParse5 ( input , { file : options . file , verbose : true } )
422407 /** @type {Node } */
423- const expected = JSON . parse ( String ( fs . readFileSync ( options . out ) ) )
408+ const expected = JSON . parse ( String ( await fs . readFile ( options . out ) ) )
424409
425410 clean ( expected )
426411
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+ )
429417 }
430418
431419 /**
432- * @param {Test } t
433- * @param {Options } options
420+ * @param {Config } options
434421 */
435- function checkYesNo ( t , options ) {
422+ async function checkYesNo ( options ) {
436423 const input = parse ( String ( options . file ) , {
437424 sourceCodeLocationInfo : true
438425 } )
439426 const actual = fromParse5 ( input )
440427 /** @type {Node } */
441- const expected = JSON . parse ( String ( fs . readFileSync ( options . out ) ) )
428+ const expected = JSON . parse ( String ( await fs . readFile ( options . out ) ) )
442429
443430 clean ( expected )
444431
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+ )
447437 }
448438
449439 /**
450- * @param {Test } t
451- * @param {Options } options
440+ * @param {Config } options
452441 */
453- function checkNoNo ( t , options ) {
442+ async function checkNoNo ( options ) {
454443 const input = parse ( String ( options . file ) )
455444 const actual = fromParse5 ( input )
456445 /** @type {Node } */
457- const expected = JSON . parse ( String ( fs . readFileSync ( options . out ) ) )
446+ const expected = JSON . parse ( String ( await fs . readFile ( options . out ) ) )
458447
459448 clean ( expected )
460449
461- log ( 'nono' , actual , expected )
462- t . deepEqual (
450+ assert . deepEqual (
463451 actual ,
464452 expected ,
465453 'p5 w/o position, hast w/o intent of position'
@@ -484,19 +472,3 @@ function clean(tree) {
484472 }
485473 } )
486474}
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