1
1
/**
2
- * @typedef {import('../lib/index.js').Node } Node
3
- *
4
- *
2
+ * @typedef {import('xast').Root } Root
5
3
*/
6
4
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'
10
9
import { isHidden } from 'is-hidden'
11
10
import { fromXml } from '../index.js'
12
11
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' )
17
14
18
15
try {
19
16
fromXml ( '<root unquoted=attribute>' )
20
- t . fail ( 'should fail (1)' )
17
+ assert . fail ( 'should fail (1)' )
21
18
} catch ( error ) {
22
- t . equal (
19
+ assert . equal (
23
20
String ( error ) ,
24
21
'1:17: Unquoted attribute value' ,
25
22
'should throw messages'
@@ -28,9 +25,9 @@ test('xast-util-from-xml', (t) => {
28
25
29
26
try {
30
27
fromXml ( '<!ENTITY>' )
31
- t . fail ( 'should fail (2)' )
28
+ assert . fail ( 'should fail (2)' )
32
29
} catch ( error ) {
33
- t . deepLooseEqual (
30
+ assert . equal (
34
31
String ( error ) ,
35
32
'1:10: Unexpected SGML declaration' ,
36
33
'should throw for SGML directives'
@@ -39,9 +36,9 @@ test('xast-util-from-xml', (t) => {
39
36
40
37
try {
41
38
fromXml ( '<root>&foo;</root>' )
42
- t . fail ( 'should fail (3)' )
39
+ assert . fail ( 'should fail (3)' )
43
40
} catch ( error ) {
44
- t . deepLooseEqual (
41
+ assert . equal (
45
42
String ( error ) ,
46
43
'1:12: Invalid character entity' ,
47
44
'should throw for unknown entities (1)'
@@ -50,9 +47,9 @@ test('xast-util-from-xml', (t) => {
50
47
51
48
try {
52
49
fromXml ( '<root>©</root>' )
53
- t . fail ( 'should fail (4)' )
50
+ assert . fail ( 'should fail (4)' )
54
51
} catch ( error ) {
55
- t . deepLooseEqual (
52
+ assert . equal (
56
53
String ( error ) ,
57
54
'1:13: Invalid character entity' ,
58
55
'should throw for unknown entities (2)'
@@ -61,182 +58,181 @@ test('xast-util-from-xml', (t) => {
61
58
62
59
try {
63
60
fromXml ( '<root><a><b><c/></a></b></root>' )
64
- t . fail ( 'should fail (5)' )
61
+ assert . fail ( 'should fail (5)' )
65
62
} catch ( error ) {
66
- t . deepLooseEqual (
63
+ assert . equal (
67
64
String ( error ) ,
68
65
'1:21: Unexpected close tag' ,
69
66
'should throw on invalid nesting'
70
67
)
71
68
}
72
69
73
- t . throws (
70
+ assert . throws (
74
71
( ) => {
75
72
fromXml ( '<!doctype>' )
76
73
} ,
77
74
/ 1 : 1 1 : E x p e c t e d d o c t y p e n a m e / ,
78
75
'should throw on missing doctype name'
79
76
)
80
77
81
- t . throws (
78
+ assert . throws (
82
79
( ) => {
83
80
fromXml ( '<!doctype !>' )
84
81
} ,
85
82
/ 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 / ,
86
83
'should throw on invalid doctype name'
87
84
)
88
85
89
- t . throws (
86
+ assert . throws (
90
87
( ) => {
91
88
fromXml ( '<!DOCTYPE name[<!ELEMENT greeting (#PCDATA)>]>' )
92
89
} ,
93
90
/ 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 / ,
94
91
'should throw on internal subset directly after doctype name'
95
92
)
96
93
97
- t . throws (
94
+ assert . throws (
98
95
( ) => {
99
96
fromXml ( '<!DOCTYPE name [<!ELEMENT greeting (#PCDATA)>]>' )
100
97
} ,
101
98
/ 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 / ,
102
99
'should throw on internal subset after doctype name'
103
100
)
104
101
105
- t . throws (
102
+ assert . throws (
106
103
( ) => {
107
104
fromXml ( '<!DOCTYPE name!>' )
108
105
} ,
109
106
/ 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 / ,
110
107
'should throw on invalid character directly after doctype'
111
108
)
112
109
113
- t . throws (
110
+ assert . throws (
114
111
( ) => {
115
112
fromXml ( '<!DOCTYPE name !>' )
116
113
} ,
117
114
/ 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 / ,
118
115
'should throw on invalid character after doctype'
119
116
)
120
117
121
- t . throws (
118
+ assert . throws (
122
119
( ) => {
123
120
fromXml ( '<!DOCTYPE name PUB>' )
124
121
} ,
125
122
/ 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 ` \) / ,
126
123
'should throw on invalid external identifier (1)'
127
124
)
128
125
129
- t . throws (
126
+ assert . throws (
130
127
( ) => {
131
128
fromXml ( '<!DOCTYPE name SYSTEm>' )
132
129
} ,
133
130
/ 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 ` \) / ,
134
131
'should throw on invalid external identifier (2)'
135
132
)
136
133
137
- t . throws (
134
+ assert . throws (
138
135
( ) => {
139
136
fromXml ( '<!DOCTYPE name PUBLIC>' )
140
137
} ,
141
138
/ 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 ` / ,
142
139
'should throw on missing whitespace after public identifier'
143
140
)
144
141
145
- t . throws (
142
+ assert . throws (
146
143
( ) => {
147
144
fromXml ( '<!DOCTYPE name PUBLIC !>' )
148
145
} ,
149
146
/ 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 / ,
150
147
'should throw on invalid character after public identifier'
151
148
)
152
149
153
- t . throws (
150
+ assert . throws (
154
151
( ) => {
155
152
fromXml ( '<!DOCTYPE name PUBLIC "🤔">' )
156
153
} ,
157
154
/ 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 / ,
158
155
'should throw on invalid character in public identifier'
159
156
)
160
157
161
- t . throws (
158
+ assert . throws (
162
159
( ) => {
163
160
fromXml ( '<!DOCTYPE name PUBLIC "literal"!>' )
164
161
} ,
165
162
/ 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 / ,
166
163
'should throw on invalid character after public literal'
167
164
)
168
165
169
- t . throws (
166
+ assert . throws (
170
167
( ) => {
171
168
fromXml ( '<!DOCTYPE name SYSTEM>' )
172
169
} ,
173
170
/ 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 ` / ,
174
171
'should throw on missing whitespace after system identifier'
175
172
)
176
173
177
- t . throws (
174
+ assert . throws (
178
175
( ) => {
179
176
fromXml ( '<!DOCTYPE name SYSTEM !>' )
180
177
} ,
181
178
/ 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 / ,
182
179
'should throw on invalid character after system identifier'
183
180
)
184
181
185
- t . throws (
182
+ assert . throws (
186
183
( ) => {
187
184
fromXml ( '<!DOCTYPE name SYSTEM "asd>' )
188
185
} ,
189
186
/ 1 : 2 8 : U n e x p e c t e d e n d / ,
190
187
'should throw on unended system literal'
191
188
)
192
189
193
- t . throws (
190
+ assert . throws (
194
191
( ) => {
195
192
fromXml ( '<!DOCTYPE name SYSTEM "asd" [<!ELEMENT greeting (#PCDATA)>]>' )
196
193
} ,
197
194
/ 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 / ,
198
195
'should throw on internal subset after external id'
199
196
)
200
197
201
- t . throws (
198
+ assert . throws (
202
199
( ) => {
203
200
fromXml ( '<!DOCTYPE name SYSTEM "asd" !>' )
204
201
} ,
205
202
/ 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 / ,
206
203
'should throw on unexpected character after external id'
207
204
)
208
-
209
- t . end ( )
210
205
} )
211
206
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 )
215
210
let index = - 1
216
211
217
212
while ( ++ index < files . length ) {
218
- if ( ! isHidden ( files [ index ] ) ) {
219
- each ( files [ index ] )
220
- }
221
- }
213
+ const folder = files [ index ]
222
214
223
- t . end ( )
215
+ if ( isHidden ( folder ) ) continue
224
216
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 )
228
220
const actual = fromXml ( input )
229
- /** @type {Node } */
221
+ /** @type {Root } */
230
222
let expected
231
223
232
224
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
+ }
234
230
} 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
238
234
}
239
235
240
- t . deepEqual ( actual , expected , fixture )
236
+ assert . deepEqual ( actual , expected , folder )
241
237
}
242
238
} )
0 commit comments