@@ -8,72 +8,61 @@ import process from 'node:process'
88import test from 'node:test'
99import { isHidden } from 'is-hidden'
1010import { fromXml } from '../index.js'
11- import * as mod from '../index.js'
1211
13- test ( 'fromXml' , ( ) => {
14- assert . deepEqual (
15- Object . keys ( mod ) . sort ( ) ,
16- [ 'fromXml' ] ,
17- 'should expose the public api'
18- )
12+ test ( 'fromXml' , async function ( t ) {
13+ await t . test ( 'should expose the public api' , async function ( ) {
14+ assert . deepEqual ( Object . keys ( await import ( '../index.js' ) ) . sort ( ) , [
15+ 'fromXml'
16+ ] )
17+ } )
1918
20- try {
21- fromXml ( '<root unquoted=attribute>' )
22- assert . fail ( 'should fail (1)' )
23- } catch ( error ) {
24- assert . match (
25- String ( error ) ,
26- / ^ 1 : 1 6 : A t t r i b u t e v a l u e e x p e c t e d / ,
27- 'should throw messages'
28- )
29- }
19+ await t . test ( 'should throw messages' , async function ( ) {
20+ try {
21+ fromXml ( '<root unquoted=attribute>' )
22+ assert . fail ( )
23+ } catch ( error ) {
24+ assert . match ( String ( error ) , / ^ 1 : 1 6 : A t t r i b u t e v a l u e e x p e c t e d / )
25+ }
26+ } )
3027
31- try {
32- fromXml ( '<!ENTITY>' )
33- assert . fail ( 'should fail (2)' )
34- } catch ( error ) {
35- assert . match (
36- String ( error ) ,
37- / ^ 1 : 1 : R o o t e l e m e n t i s m i s s i n g o r i n v a l i d / ,
38- 'should throw for SGML directives'
39- )
40- }
28+ await t . test ( 'should throw for SGML directives' , async function ( ) {
29+ try {
30+ fromXml ( '<!ENTITY>' )
31+ assert . fail ( )
32+ } catch ( error ) {
33+ assert . match ( String ( error ) , / ^ 1 : 1 : R o o t e l e m e n t i s m i s s i n g o r i n v a l i d / )
34+ }
35+ } )
4136
42- try {
43- fromXml ( '<root>&foo;</root>' )
44- assert . fail ( 'should fail (3)' )
45- } catch ( error ) {
46- assert . match (
47- String ( error ) ,
48- / ^ 1 : 7 : N a m e d e n t i t y i s n ' t d e f i n e d / ,
49- 'should throw for unknown entities (1)'
50- )
51- }
37+ await t . test ( 'should throw for unknown entities (1)' , async function ( ) {
38+ try {
39+ fromXml ( '<root>&foo;</root>' )
40+ assert . fail ( )
41+ } catch ( error ) {
42+ assert . match ( String ( error ) , / ^ 1 : 7 : N a m e d e n t i t y i s n ' t d e f i n e d / )
43+ }
44+ } )
5245
53- try {
54- fromXml ( '<root>©</root>' )
55- assert . fail ( 'should fail (4)' )
56- } catch ( error ) {
57- assert . match (
58- String ( error ) ,
59- / ^ 1 : 7 : N a m e d e n t i t y i s n ' t d e f i n e d / ,
60- 'should throw for unknown entities (2)'
61- )
62- }
46+ await t . test ( 'should throw for unknown entities (2)' , async function ( ) {
47+ try {
48+ fromXml ( '<root>©</root>' )
49+ assert . fail ( )
50+ } catch ( error ) {
51+ assert . match ( String ( error ) , / ^ 1 : 7 : N a m e d e n t i t y i s n ' t d e f i n e d / )
52+ }
53+ } )
6354
64- try {
65- fromXml ( '<root><a><b><c/></a></b></root>' )
66- assert . fail ( 'should fail (5)' )
67- } catch ( error ) {
68- assert . match (
69- String ( error ) ,
70- / ^ 1 : 1 7 : M i s s i n g e n d t a g f o r e l e m e n t / ,
71- 'should throw on invalid nesting'
72- )
73- }
55+ await t . test ( 'should throw on invalid nesting' , async function ( ) {
56+ try {
57+ fromXml ( '<root><a><b><c/></a></b></root>' )
58+ assert . fail ( )
59+ } catch ( error ) {
60+ assert . match ( String ( error ) , / ^ 1 : 1 7 : M i s s i n g e n d t a g f o r e l e m e n t / )
61+ }
62+ } )
7463} )
7564
76- test ( 'fixtures' , async ( ) => {
65+ test ( 'fixtures' , async function ( t ) {
7766 const base = new URL ( 'fixtures/' , import . meta. url )
7867 const files = await fs . readdir ( base )
7968 let index = - 1
@@ -83,27 +72,29 @@ test('fixtures', async () => {
8372
8473 if ( isHidden ( folder ) ) continue
8574
86- const inputUrl = new URL ( folder + '/index.xml' , base )
87- const treeUrl = new URL ( folder + '/index.json' , base )
88- const input = await fs . readFile ( inputUrl )
89- /** @type {Root } */
90- // Remove `undefined`s.
91- const actual = JSON . parse ( JSON . stringify ( fromXml ( input ) ) )
92- /** @type {Root } */
93- let expected
75+ await t . test ( folder , async function ( ) {
76+ const inputUrl = new URL ( folder + '/index.xml' , base )
77+ const treeUrl = new URL ( folder + '/index.json' , base )
78+ const input = await fs . readFile ( inputUrl )
79+ /** @type {Root } */
80+ // Remove `undefined`s.
81+ const actual = JSON . parse ( JSON . stringify ( fromXml ( input ) ) )
82+ /** @type {Root } */
83+ let expected
9484
95- try {
96- expected = JSON . parse ( String ( await fs . readFile ( treeUrl ) ) )
85+ try {
86+ expected = JSON . parse ( String ( await fs . readFile ( treeUrl ) ) )
9787
98- if ( 'UPDATE' in process . env ) {
99- throw new Error ( 'Update' )
88+ if ( 'UPDATE' in process . env ) {
89+ throw new Error ( 'Update' )
90+ }
91+ } catch {
92+ // New folder.
93+ await fs . writeFile ( treeUrl , JSON . stringify ( actual , undefined , 2 ) + '\n' )
94+ return
10095 }
101- } catch {
102- // New folder.
103- await fs . writeFile ( treeUrl , JSON . stringify ( actual , null , 2 ) + '\n' )
104- continue
105- }
10696
107- assert . deepEqual ( actual , expected , folder )
97+ assert . deepEqual ( actual , expected )
98+ } )
10899 }
109100} )
0 commit comments