Skip to content

Commit f28cdd0

Browse files
committed
chore(test): Make it possible to ignore tests
And start running the upstream parser tests. Also removes the upstreamed regression test (html5lib/html5lib-tests#145)
1 parent 439aeef commit f28cdd0

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed

packages/parse5-parser-stream/test/scripting.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ generateParsingTests(
1414
{
1515
skipFragments: true,
1616
withoutErrors: true,
17-
testSuite: [suitePath.pathname],
17+
testSuite: [suitePath],
1818
},
1919
async (test, opts) => {
2020
const chunks = makeChunks(test.input);

packages/parse5/lib/parser/index.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,43 @@ generateParsingTests('parser', 'Parser', {}, (test, opts) => ({
1616
: parse5.parse(test.input, opts),
1717
}));
1818

19+
generateParsingTests(
20+
'parser upstream',
21+
'Parser',
22+
{
23+
withoutErrors: true,
24+
testSuite: [new URL('../../../../test/data/html5lib-tests/tree-construction', import.meta.url)],
25+
expectErrors: [
26+
'271.foreign-fragment',
27+
'272.foreign-fragment',
28+
'309.foreign-fragment',
29+
'311.foreign-fragment',
30+
'318.foreign-fragment',
31+
'319.foreign-fragment',
32+
'329.foreign-fragment',
33+
'330.foreign-fragment',
34+
'331.foreign-fragment',
35+
'332.foreign-fragment',
36+
'333.foreign-fragment',
37+
'334.foreign-fragment',
38+
'335.foreign-fragment',
39+
'336.foreign-fragment',
40+
'337.foreign-fragment',
41+
'505.search-element',
42+
'506.search-element',
43+
'1408.tests26',
44+
'1409.tests26',
45+
'1410.tests26',
46+
'1411.tests26',
47+
],
48+
},
49+
(test, opts) => ({
50+
node: test.fragmentContext
51+
? parse5.parseFragment(test.fragmentContext, test.input, opts)
52+
: parse5.parse(test.input, opts),
53+
})
54+
);
55+
1956
describe('parser', () => {
2057
it('Regression - HTML5 Legacy Doctype Misparsed with htmlparser2 tree adapter (GH-45)', () => {
2158
const html = '<!DOCTYPE html SYSTEM "about:legacy-compat"><html><head></head><body>Hi there!</body></html>';

test/data/tree-construction-regression/gh40_form_in_template.dat

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/utils/generate-parsing-tests.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ export interface TreeConstructionTestData<T extends TreeAdapterTypeMap> extends
1515
}
1616

1717
export function loadTreeConstructionTestData<T extends TreeAdapterTypeMap>(
18-
dataDirs: (string | URL)[],
18+
dataDirs: URL[],
1919
treeAdapter: TreeAdapter<T>
2020
): TreeConstructionTestData<T>[] {
2121
const tests: TreeConstructionTestData<T>[] = [];
2222

2323
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);
2626
const dirName = path.basename(dataDirPath);
2727

2828
for (const fileName of testSetFileNames) {
@@ -83,7 +83,7 @@ function createParsingTest<T extends TreeAdapterTypeMap>(
8383
test: TreeConstructionTestData<T>,
8484
treeAdapter: TreeAdapter<T>,
8585
parse: ParseMethod<T>,
86-
{ withoutErrors }: { withoutErrors?: boolean }
86+
{ withoutErrors, expectError }: { withoutErrors?: boolean; expectError?: boolean } = {}
8787
): () => Promise<void> {
8888
return async (): Promise<void> => {
8989
const errs: string[] = [];
@@ -109,37 +109,62 @@ function createParsingTest<T extends TreeAdapterTypeMap>(
109109
const { node, chunks } = await parse(test, opts);
110110
const actual = serializeToDatFileFormat(node, opts.treeAdapter);
111111
const msg = prettyPrintParserAssertionArgs(actual, test.expected, chunks);
112+
let sawError = false;
112113

113-
assert.ok(actual === test.expected, msg);
114+
try {
115+
assert.ok(actual === test.expected, msg);
114116

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`);
117131
}
118132
};
119133
}
120134

121135
// TODO: Stop using the fork here.
122136
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);
124137

125138
export function generateParsingTests(
126139
name: string,
127140
prefix: string,
128141
{
129142
skipFragments,
130143
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[] },
133147
parse: ParseMethod<TreeAdapterTypeMap>
134148
): void {
135149
generateTestsForEachTreeAdapter(name, (treeAdapter) => {
150+
const errorsToExpect = new Set(expectError);
151+
136152
for (const test of loadTreeConstructionTestData(testSuite, treeAdapter).filter(
137153
(test) => !skipFragments || !test.fragmentContext
138154
)) {
155+
const expectError = errorsToExpect.delete(`${test.idx}.${test.setName}`);
156+
139157
it(
140158
`${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+
})
142163
);
143164
}
165+
166+
if (errorsToExpect.size > 0) {
167+
throw new Error(`Expected errors were not found: ${[...errorsToExpect].join(', ')}`);
168+
}
144169
});
145170
}

0 commit comments

Comments
 (0)