@@ -15,14 +15,14 @@ export interface TreeConstructionTestData<T extends TreeAdapterTypeMap> extends
15
15
}
16
16
17
17
export function loadTreeConstructionTestData < T extends TreeAdapterTypeMap > (
18
- dataDirs : ( string | URL ) [ ] ,
18
+ dataDirs : URL [ ] ,
19
19
treeAdapter : TreeAdapter < T >
20
20
) : TreeConstructionTestData < T > [ ] {
21
21
const tests : TreeConstructionTestData < T > [ ] = [ ] ;
22
22
23
23
for ( const dataDir of dataDirs ) {
24
- const dataDirPath = typeof dataDir === 'string' ? dataDir : dataDir . pathname ;
25
- const testSetFileNames = fs . readdirSync ( dataDirPath ) ;
24
+ const dataDirPath = dataDir . pathname ;
25
+ const testSetFileNames = fs . readdirSync ( dataDir ) ;
26
26
const dirName = path . basename ( dataDirPath ) ;
27
27
28
28
for ( const fileName of testSetFileNames ) {
@@ -83,7 +83,7 @@ function createParsingTest<T extends TreeAdapterTypeMap>(
83
83
test : TreeConstructionTestData < T > ,
84
84
treeAdapter : TreeAdapter < T > ,
85
85
parse : ParseMethod < T > ,
86
- { withoutErrors } : { withoutErrors ?: boolean }
86
+ { withoutErrors, expectError } : { withoutErrors ?: boolean ; expectError ?: boolean } = { }
87
87
) : ( ) => Promise < void > {
88
88
return async ( ) : Promise < void > => {
89
89
const errs : string [ ] = [ ] ;
@@ -109,37 +109,62 @@ function createParsingTest<T extends TreeAdapterTypeMap>(
109
109
const { node, chunks } = await parse ( test , opts ) ;
110
110
const actual = serializeToDatFileFormat ( node , opts . treeAdapter ) ;
111
111
const msg = prettyPrintParserAssertionArgs ( actual , test . expected , chunks ) ;
112
+ let sawError = false ;
112
113
113
- assert . ok ( actual === test . expected , msg ) ;
114
+ try {
115
+ assert . ok ( actual === test . expected , msg ) ;
114
116
115
- if ( ! withoutErrors ) {
116
- assert . deepEqual ( errs . sort ( ) , test . expectedErrors . sort ( ) ) ;
117
+ if ( ! withoutErrors ) {
118
+ assert . deepEqual ( errs . sort ( ) , test . expectedErrors . sort ( ) ) ;
119
+ }
120
+ } catch ( error ) {
121
+ if ( expectError ) {
122
+ return ;
123
+ }
124
+ sawError = true ;
125
+
126
+ throw error ;
127
+ }
128
+
129
+ if ( ! sawError && expectError ) {
130
+ throw new Error ( `Expected error but none was thrown` ) ;
117
131
}
118
132
} ;
119
133
}
120
134
121
135
// TODO: Stop using the fork here.
122
136
const treePath = new URL ( '../data/html5lib-tests-fork/tree-construction' , import . meta. url ) ;
123
- const treeRegressionPath = new URL ( '../data/tree-construction-regression' , import . meta. url ) ;
124
137
125
138
export function generateParsingTests (
126
139
name : string ,
127
140
prefix : string ,
128
141
{
129
142
skipFragments,
130
143
withoutErrors,
131
- testSuite = [ treePath . pathname , treeRegressionPath . pathname ] ,
132
- } : { skipFragments ?: boolean ; withoutErrors ?: boolean ; testSuite ?: string [ ] } ,
144
+ expectErrors : expectError = [ ] ,
145
+ testSuite = [ treePath ] ,
146
+ } : { skipFragments ?: boolean ; withoutErrors ?: boolean ; expectErrors ?: string [ ] ; testSuite ?: URL [ ] } ,
133
147
parse : ParseMethod < TreeAdapterTypeMap >
134
148
) : void {
135
149
generateTestsForEachTreeAdapter ( name , ( treeAdapter ) => {
150
+ const errorsToExpect = new Set ( expectError ) ;
151
+
136
152
for ( const test of loadTreeConstructionTestData ( testSuite , treeAdapter ) . filter (
137
153
( test ) => ! skipFragments || ! test . fragmentContext
138
154
) ) {
155
+ const expectError = errorsToExpect . delete ( `${ test . idx } .${ test . setName } ` ) ;
156
+
139
157
it (
140
158
`${ prefix } (${ test . dirName } ) - ${ test . idx } .${ test . setName } - \`${ test . input } \` (line ${ test . lineNum } )` ,
141
- createParsingTest < TreeAdapterTypeMap > ( test , treeAdapter , parse , { withoutErrors } )
159
+ createParsingTest < TreeAdapterTypeMap > ( test , treeAdapter , parse , {
160
+ withoutErrors,
161
+ expectError,
162
+ } )
142
163
) ;
143
164
}
165
+
166
+ if ( errorsToExpect . size > 0 ) {
167
+ throw new Error ( `Expected errors were not found: ${ [ ...errorsToExpect ] . join ( ', ' ) } ` ) ;
168
+ }
144
169
} ) ;
145
170
}
0 commit comments