1
- import fs from 'node:fs '
2
- import path from 'node:path '
3
- import test from 'tape '
4
- import { rehype } from 'rehype '
1
+ import assert from 'node:assert/strict '
2
+ import fs from 'node:fs/promises '
3
+ import test from 'node:test '
4
+ import { isHidden } from 'is-hidden '
5
5
import { VFile } from 'vfile'
6
6
import { ParseLatin } from 'parse-latin'
7
7
import { ParseDutch } from 'parse-dutch'
8
8
import { ParseEnglish } from 'parse-english'
9
- import { isHidden } from 'is-hidden '
9
+ import { fromHtml } from 'hast-util-from-html '
10
10
import { toNlcst } from '../index.js'
11
11
12
- test ( 'hast-util-to-nlcst ' , ( t ) => {
13
- t . throws (
12
+ test ( 'toNlcst ' , ( ) => {
13
+ assert . throws (
14
14
( ) => {
15
15
// @ts -expect-error runtime.
16
16
toNlcst ( )
@@ -19,7 +19,7 @@ test('hast-util-to-nlcst', (t) => {
19
19
'should fail when not given a tree'
20
20
)
21
21
22
- t . throws (
22
+ assert . throws (
23
23
( ) => {
24
24
// @ts -expect-error runtime.
25
25
toNlcst ( { } )
@@ -28,7 +28,7 @@ test('hast-util-to-nlcst', (t) => {
28
28
'should fail when not given a tree (#2)'
29
29
)
30
30
31
- t . throws (
31
+ assert . throws (
32
32
( ) => {
33
33
// @ts -expect-error runtime.
34
34
toNlcst ( { type : 'foo' } )
@@ -37,7 +37,7 @@ test('hast-util-to-nlcst', (t) => {
37
37
'should fail when not given a file'
38
38
)
39
39
40
- t . throws (
40
+ assert . throws (
41
41
( ) => {
42
42
// @ts -expect-error runtime.
43
43
toNlcst ( { type : 'foo' } )
@@ -46,7 +46,7 @@ test('hast-util-to-nlcst', (t) => {
46
46
'should fail when not given a file (#2)'
47
47
)
48
48
49
- t . throws (
49
+ assert . throws (
50
50
( ) => {
51
51
// @ts -expect-error runtime.
52
52
toNlcst ( { type : 'text' , value : 'foo' } , { foo : 'bar' } )
@@ -55,7 +55,7 @@ test('hast-util-to-nlcst', (t) => {
55
55
'should fail when not given a file (#3)'
56
56
)
57
57
58
- t . throws (
58
+ assert . throws (
59
59
( ) => {
60
60
// @ts -expect-error runtime.
61
61
toNlcst ( { type : 'text' , value : 'foo' } , new VFile ( 'foo' ) )
@@ -64,15 +64,15 @@ test('hast-util-to-nlcst', (t) => {
64
64
'should fail without parser'
65
65
)
66
66
67
- t . throws (
67
+ assert . throws (
68
68
( ) => {
69
69
toNlcst ( { type : 'text' , value : 'foo' } , new VFile ( ) , ParseLatin )
70
70
} ,
71
71
/ h a s t - u t i l - t o - n l c s t e x p e c t e d p o s i t i o n o n n o d e s / ,
72
72
'should fail when not given positional information'
73
73
)
74
74
75
- t . doesNotThrow ( ( ) => {
75
+ assert . doesNotThrow ( ( ) => {
76
76
toNlcst (
77
77
{
78
78
type : 'text' ,
@@ -87,7 +87,7 @@ test('hast-util-to-nlcst', (t) => {
87
87
)
88
88
} , 'should accept a parser constructor' )
89
89
90
- t . doesNotThrow ( ( ) => {
90
+ assert . doesNotThrow ( ( ) => {
91
91
toNlcst (
92
92
{
93
93
type : 'text' ,
@@ -102,7 +102,7 @@ test('hast-util-to-nlcst', (t) => {
102
102
)
103
103
} , 'should accept a parser instance' )
104
104
105
- t . throws (
105
+ assert . throws (
106
106
( ) => {
107
107
toNlcst (
108
108
{
@@ -118,97 +118,83 @@ test('hast-util-to-nlcst', (t) => {
118
118
/ h a s t - u t i l - t o - n l c s t e x p e c t e d p o s i t i o n o n n o d e s / ,
119
119
'should fail when not given positional information (#2)'
120
120
)
121
+ } )
121
122
122
- t . test ( 'should accept nodes without offsets' , ( t ) => {
123
- const node = toNlcst (
124
- {
125
- type : 'text' ,
126
- value : 'foo' ,
127
- position : {
128
- start : { line : 1 , column : 1 } ,
129
- end : { line : 1 , column : 4 }
130
- }
131
- } ,
132
- new VFile ( 'foo' ) ,
133
- ParseLatin
134
- )
135
-
136
- t . equal (
137
- node . position && node . position . start . offset ,
138
- 0 ,
139
- 'should set starting offset'
140
- )
141
- t . equal (
142
- node . position && node . position . end . offset ,
143
- 3 ,
144
- 'should set ending offset'
145
- )
146
-
147
- t . end ( )
148
- } )
149
-
150
- t . test ( 'should accept comments' , ( t ) => {
151
- const node = toNlcst (
152
- {
153
- type : 'comment' ,
154
- value : 'a' ,
155
- position : { start : { line : 1 , column : 1 } , end : { line : 1 , column : 9 } }
156
- } ,
157
- new VFile ( '<!--a-->' ) ,
158
- ParseLatin
159
- )
123
+ await test ( 'should accept nodes without offsets' , ( ) => {
124
+ const node = toNlcst (
125
+ {
126
+ type : 'text' ,
127
+ value : 'foo' ,
128
+ position : {
129
+ start : { line : 1 , column : 1 } ,
130
+ end : { line : 1 , column : 4 }
131
+ }
132
+ } ,
133
+ new VFile ( 'foo' ) ,
134
+ ParseLatin
135
+ )
160
136
161
- t . deepEqual (
162
- node ,
163
- {
164
- type : 'RootNode' ,
165
- children : [ ] ,
166
- position : {
167
- start : { line : 1 , column : 1 , offset : 0 } ,
168
- end : { line : 1 , column : 9 , offset : 8 }
169
- }
170
- } ,
171
- 'should support comments'
172
- )
137
+ assert . equal (
138
+ node . position && node . position . start . offset ,
139
+ 0 ,
140
+ 'should set starting offset'
141
+ )
142
+ assert . equal (
143
+ node . position && node . position . end . offset ,
144
+ 3 ,
145
+ 'should set ending offset'
146
+ )
147
+ } )
173
148
174
- t . end ( )
175
- } )
149
+ await test ( 'should accept comments' , ( ) => {
150
+ const node = toNlcst (
151
+ {
152
+ type : 'comment' ,
153
+ value : 'a' ,
154
+ position : { start : { line : 1 , column : 1 } , end : { line : 1 , column : 9 } }
155
+ } ,
156
+ new VFile ( '<!--a-->' ) ,
157
+ ParseLatin
158
+ )
176
159
177
- t . end ( )
160
+ assert . deepEqual (
161
+ node ,
162
+ {
163
+ type : 'RootNode' ,
164
+ children : [ ] ,
165
+ position : {
166
+ start : { line : 1 , column : 1 , offset : 0 } ,
167
+ end : { line : 1 , column : 9 , offset : 8 }
168
+ }
169
+ } ,
170
+ 'should support comments'
171
+ )
178
172
} )
179
173
180
- test ( 'Fixtures ' , ( t ) => {
181
- const root = path . join ( 'test ', 'fixtures' )
182
- const files = fs . readdirSync ( root )
174
+ test ( 'fixtures ' , async ( ) => {
175
+ const root = new URL ( 'fixtures/ ', import . meta . url )
176
+ const files = await fs . readdir ( root )
183
177
let index = - 1
184
- /** @type {string } */
185
- let input
186
- /** @type {string } */
187
- let output
188
- /** @type {import('vfile').VFile } */
189
- let file
190
- /** @type {import('unist').Node } */
191
- let actual
192
- /** @type {import('unist').Node } */
193
- let expected
194
178
195
179
while ( ++ index < files . length ) {
196
- if ( isHidden ( files [ index ] ) ) continue
180
+ const folder = files [ index ]
197
181
198
- input = path . join ( root , files [ index ] , 'input.html' )
199
- output = path . join ( root , files [ index ] , 'output.json' )
200
- file = new VFile ( fs . readFileSync ( input ) )
201
- actual = toNlcst ( rehype ( ) . parse ( file ) , file , ParseLatin )
182
+ if ( isHidden ( folder ) ) continue
183
+
184
+ const input = new URL ( folder + '/input.html' , root )
185
+ const output = new URL ( folder + '/output.json' , root )
186
+ const file = new VFile ( await fs . readFile ( input ) )
187
+ const actual = toNlcst ( fromHtml ( file ) , file , ParseLatin )
188
+ /** @type {import('unist').Node } */
189
+ let expected
202
190
203
191
try {
204
- expected = JSON . parse ( String ( fs . readFileSync ( output ) ) )
192
+ expected = JSON . parse ( String ( await fs . readFile ( output ) ) )
205
193
} catch {
206
- fs . writeFileSync ( output , JSON . stringify ( actual , null , 2 ) + '\n' )
207
- return
194
+ await fs . writeFile ( output , JSON . stringify ( actual , null , 2 ) + '\n' )
195
+ continue
208
196
}
209
197
210
- t . deepEqual ( actual , expected , 'should work on `' + files [ index ] + '`' )
198
+ assert . deepEqual ( actual , expected , 'should work on `' + files [ index ] + '`' )
211
199
}
212
-
213
- t . end ( )
214
200
} )
0 commit comments