11/**
2- * @typedef {import('../lib/index.js').Node } Node
3- *
4- *
2+ * @typedef {import('xast').Root } Root
53 */
64
7- import fs from 'node:fs'
8- import path from 'node:path'
9- import test from 'tape'
5+ import assert from 'node:assert/strict'
6+ import fs from 'node:fs/promises'
7+ import process from 'node:process'
8+ import test from 'node:test'
109import { isHidden } from 'is-hidden'
1110import { fromXml } from '../index.js'
1211
13- const join = path . join
14-
15- test ( 'xast-util-from-xml' , ( t ) => {
16- t . equal ( typeof fromXml , 'function' , 'should expose a function' )
12+ test ( 'fromXml' , ( ) => {
13+ assert . equal ( typeof fromXml , 'function' , 'should expose a function' )
1714
1815 try {
1916 fromXml ( '<root unquoted=attribute>' )
20- t . fail ( 'should fail (1)' )
17+ assert . fail ( 'should fail (1)' )
2118 } catch ( error ) {
22- t . equal (
19+ assert . equal (
2320 String ( error ) ,
2421 '1:17: Unquoted attribute value' ,
2522 'should throw messages'
@@ -28,9 +25,9 @@ test('xast-util-from-xml', (t) => {
2825
2926 try {
3027 fromXml ( '<!ENTITY>' )
31- t . fail ( 'should fail (2)' )
28+ assert . fail ( 'should fail (2)' )
3229 } catch ( error ) {
33- t . deepLooseEqual (
30+ assert . equal (
3431 String ( error ) ,
3532 '1:10: Unexpected SGML declaration' ,
3633 'should throw for SGML directives'
@@ -39,9 +36,9 @@ test('xast-util-from-xml', (t) => {
3936
4037 try {
4138 fromXml ( '<root>&foo;</root>' )
42- t . fail ( 'should fail (3)' )
39+ assert . fail ( 'should fail (3)' )
4340 } catch ( error ) {
44- t . deepLooseEqual (
41+ assert . equal (
4542 String ( error ) ,
4643 '1:12: Invalid character entity' ,
4744 'should throw for unknown entities (1)'
@@ -50,9 +47,9 @@ test('xast-util-from-xml', (t) => {
5047
5148 try {
5249 fromXml ( '<root>©</root>' )
53- t . fail ( 'should fail (4)' )
50+ assert . fail ( 'should fail (4)' )
5451 } catch ( error ) {
55- t . deepLooseEqual (
52+ assert . equal (
5653 String ( error ) ,
5754 '1:13: Invalid character entity' ,
5855 'should throw for unknown entities (2)'
@@ -61,182 +58,181 @@ test('xast-util-from-xml', (t) => {
6158
6259 try {
6360 fromXml ( '<root><a><b><c/></a></b></root>' )
64- t . fail ( 'should fail (5)' )
61+ assert . fail ( 'should fail (5)' )
6562 } catch ( error ) {
66- t . deepLooseEqual (
63+ assert . equal (
6764 String ( error ) ,
6865 '1:21: Unexpected close tag' ,
6966 'should throw on invalid nesting'
7067 )
7168 }
7269
73- t . throws (
70+ assert . throws (
7471 ( ) => {
7572 fromXml ( '<!doctype>' )
7673 } ,
7774 / 1 : 1 1 : E x p e c t e d d o c t y p e n a m e / ,
7875 'should throw on missing doctype name'
7976 )
8077
81- t . throws (
78+ assert . throws (
8279 ( ) => {
8380 fromXml ( '<!doctype !>' )
8481 } ,
8582 / 1 : 1 3 : E x p e c t e d s t a r t o f d o c t y p e n a m e / ,
8683 'should throw on invalid doctype name'
8784 )
8885
89- t . throws (
86+ assert . throws (
9087 ( ) => {
9188 fromXml ( '<!DOCTYPE name[<!ELEMENT greeting (#PCDATA)>]>' )
9289 } ,
9390 / 1 : 4 7 : U n e x p e c t e d i n t e r n a l s u b s e t / ,
9491 'should throw on internal subset directly after doctype name'
9592 )
9693
97- t . throws (
94+ assert . throws (
9895 ( ) => {
9996 fromXml ( '<!DOCTYPE name [<!ELEMENT greeting (#PCDATA)>]>' )
10097 } ,
10198 / 1 : 4 8 : U n e x p e c t e d i n t e r n a l s u b s e t / ,
10299 'should throw on internal subset after doctype name'
103100 )
104101
105- t . throws (
102+ assert . throws (
106103 ( ) => {
107104 fromXml ( '<!DOCTYPE name!>' )
108105 } ,
109106 / 1 : 1 7 : E x p e c t e d d o c t y p e n a m e c h a r a c t e r , w h i t e s p a c e , o r d o c t y p e e n d / ,
110107 'should throw on invalid character directly after doctype'
111108 )
112109
113- t . throws (
110+ assert . throws (
114111 ( ) => {
115112 fromXml ( '<!DOCTYPE name !>' )
116113 } ,
117114 / 1 : 1 8 : E x p e c t e d e x t e r n a l i d e n t i f i e r \( ` P U B L I C ` o r ` S Y S T E M ` \) , w h i t e s p a c e , o r d o c t y p e e n d / ,
118115 'should throw on invalid character after doctype'
119116 )
120117
121- t . throws (
118+ assert . throws (
122119 ( ) => {
123120 fromXml ( '<!DOCTYPE name PUB>' )
124121 } ,
125122 / 1 : 2 0 : E x p e c t e d e x t e r n a l i d e n t i f i e r \( ` P U B L I C ` o r ` S Y S T E M ` \) / ,
126123 'should throw on invalid external identifier (1)'
127124 )
128125
129- t . throws (
126+ assert . throws (
130127 ( ) => {
131128 fromXml ( '<!DOCTYPE name SYSTEm>' )
132129 } ,
133130 / 1 : 2 3 : E x p e c t e d e x t e r n a l i d e n t i f i e r \( ` P U B L I C ` o r ` S Y S T E M ` \) / ,
134131 'should throw on invalid external identifier (2)'
135132 )
136133
137- t . throws (
134+ assert . throws (
138135 ( ) => {
139136 fromXml ( '<!DOCTYPE name PUBLIC>' )
140137 } ,
141138 / 1 : 2 3 : E x p e c t e d w h i t e s p a c e a f t e r ` P U B L I C ` / ,
142139 'should throw on missing whitespace after public identifier'
143140 )
144141
145- t . throws (
142+ assert . throws (
146143 ( ) => {
147144 fromXml ( '<!DOCTYPE name PUBLIC !>' )
148145 } ,
149146 / 1 : 2 5 : E x p e c t e d q u o t e o r a p o s t r o p h e t o s t a r t p u b l i c l i t e r a l / ,
150147 'should throw on invalid character after public identifier'
151148 )
152149
153- t . throws (
150+ assert . throws (
154151 ( ) => {
155152 fromXml ( '<!DOCTYPE name PUBLIC "🤔">' )
156153 } ,
157154 / 1 : 2 8 : E x p e c t e d p u b i d c h a r a c t e r i n p u b l i c l i t e r a l / ,
158155 'should throw on invalid character in public identifier'
159156 )
160157
161- t . throws (
158+ assert . throws (
162159 ( ) => {
163160 fromXml ( '<!DOCTYPE name PUBLIC "literal"!>' )
164161 } ,
165162 / 1 : 3 4 : E x p e c t e d w h i t e s p a c e a f t e r p u b l i c l i t e r a l / ,
166163 'should throw on invalid character after public literal'
167164 )
168165
169- t . throws (
166+ assert . throws (
170167 ( ) => {
171168 fromXml ( '<!DOCTYPE name SYSTEM>' )
172169 } ,
173170 / 1 : 2 3 : E x p e c t e d w h i t e s p a c e a f t e r ` S Y S T E M ` / ,
174171 'should throw on missing whitespace after system identifier'
175172 )
176173
177- t . throws (
174+ assert . throws (
178175 ( ) => {
179176 fromXml ( '<!DOCTYPE name SYSTEM !>' )
180177 } ,
181178 / 1 : 2 5 : E x p e c t e d q u o t e o r a p o s t r o p h e t o s t a r t s y s t e m l i t e r a l / ,
182179 'should throw on invalid character after system identifier'
183180 )
184181
185- t . throws (
182+ assert . throws (
186183 ( ) => {
187184 fromXml ( '<!DOCTYPE name SYSTEM "asd>' )
188185 } ,
189186 / 1 : 2 8 : U n e x p e c t e d e n d / ,
190187 'should throw on unended system literal'
191188 )
192189
193- t . throws (
190+ assert . throws (
194191 ( ) => {
195192 fromXml ( '<!DOCTYPE name SYSTEM "asd" [<!ELEMENT greeting (#PCDATA)>]>' )
196193 } ,
197194 / 1 : 6 1 : U n e x p e c t e d i n t e r n a l s u b s e t / ,
198195 'should throw on internal subset after external id'
199196 )
200197
201- t . throws (
198+ assert . throws (
202199 ( ) => {
203200 fromXml ( '<!DOCTYPE name SYSTEM "asd" !>' )
204201 } ,
205202 / 1 : 3 1 : E x p e c t e d w h i t e s p a c e o r e n d o f d o c t y p e / ,
206203 'should throw on unexpected character after external id'
207204 )
208-
209- t . end ( )
210205} )
211206
212- test ( 'fixtures' , ( t ) => {
213- const base = join ( 'test ', 'fixtures' )
214- const files = fs . readdirSync ( base )
207+ test ( 'fixtures' , async ( ) => {
208+ const base = new URL ( 'fixtures/ ', import . meta . url )
209+ const files = await fs . readdir ( base )
215210 let index = - 1
216211
217212 while ( ++ index < files . length ) {
218- if ( ! isHidden ( files [ index ] ) ) {
219- each ( files [ index ] )
220- }
221- }
213+ const folder = files [ index ]
222214
223- t . end ( )
215+ if ( isHidden ( folder ) ) continue
224216
225- function each ( /** @type { string } */ fixture ) {
226- const input = fs . readFileSync ( join ( base , fixture , ' index.xml' ) )
227- const fp = join ( base , fixture , 'index.json' )
217+ const inputUrl = new URL ( folder + '/index.xml' , base )
218+ const treeUrl = new URL ( folder + '/ index.json' , base )
219+ const input = await fs . readFile ( inputUrl )
228220 const actual = fromXml ( input )
229- /** @type {Node } */
221+ /** @type {Root } */
230222 let expected
231223
232224 try {
233- expected = JSON . parse ( String ( fs . readFileSync ( fp ) ) )
225+ expected = JSON . parse ( String ( await fs . readFile ( treeUrl ) ) )
226+
227+ if ( 'UPDATE' in process . env ) {
228+ throw new Error ( 'Update' )
229+ }
234230 } catch {
235- // New fixture .
236- fs . writeFileSync ( fp , JSON . stringify ( actual , null , 2 ) + '\n' )
237- return
231+ // New folder .
232+ await fs . writeFile ( treeUrl , JSON . stringify ( actual , null , 2 ) + '\n' )
233+ continue
238234 }
239235
240- t . deepEqual ( actual , expected , fixture )
236+ assert . deepEqual ( actual , expected , folder )
241237 }
242238} )
0 commit comments