-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathgenerate-parsing-tests.ts
165 lines (135 loc) · 5.08 KB
/
generate-parsing-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { ParserOptions } from 'parse5';
import { ParserError } from 'parse5/dist/common/error-codes.js';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as assert from 'node:assert';
import { serializeToDatFileFormat } from './serialize-to-dat-file-format.js';
import { generateTestsForEachTreeAdapter } from './common.js';
import { parseDatFile, DatFile } from './parse-dat-file.js';
import type { TreeAdapter, TreeAdapterTypeMap } from 'parse5/dist/tree-adapters/interface.js';
export interface TreeConstructionTestData<T extends TreeAdapterTypeMap> extends DatFile<T> {
idx: number;
setName: string;
dirName: string;
}
export function loadTreeConstructionTestData<T extends TreeAdapterTypeMap>(
dataDir: URL,
treeAdapter: TreeAdapter<T>
): TreeConstructionTestData<T>[] {
const tests: TreeConstructionTestData<T>[] = [];
const dataDirPath = dataDir.pathname;
const testSetFileNames = fs.readdirSync(dataDir);
const dirName = path.basename(dataDirPath);
for (const fileName of testSetFileNames) {
if (path.extname(fileName) !== '.dat') {
continue;
}
const filePath = path.join(dataDirPath, fileName);
const testSet = fs.readFileSync(filePath, 'utf8');
const setName = fileName.replace('.dat', '');
for (const test of parseDatFile(testSet, treeAdapter)) {
tests.push({
...test,
idx: tests.length,
setName,
dirName,
});
}
}
return tests;
}
function prettyPrintParserAssertionArgs(actual: string, expected: string, chunks?: string[]): string {
let msg = '\nExpected:\n';
msg += '-----------------\n';
msg += `${expected}\n`;
msg += '\nActual:\n';
msg += '-----------------\n';
msg += `${actual}\n`;
if (chunks) {
msg += 'Chunks:\n';
msg += JSON.stringify(chunks);
}
return msg;
}
interface ParseMethodOptions<T extends TreeAdapterTypeMap> extends ParserOptions<T> {
treeAdapter: TreeAdapter<T>;
}
interface ParseResult<T extends TreeAdapterTypeMap> {
node: T['node'];
chunks?: string[];
}
type ParseMethod<T extends TreeAdapterTypeMap> = (
input: TreeConstructionTestData<T>,
options: ParseMethodOptions<T>
) => ParseResult<T> | Promise<ParseResult<T>>;
function createParsingTest<T extends TreeAdapterTypeMap>(
test: TreeConstructionTestData<T>,
treeAdapter: TreeAdapter<T>,
parse: ParseMethod<T>,
{ withoutErrors, expectError }: { withoutErrors?: boolean; expectError?: boolean } = {}
): () => Promise<void> {
return async (): Promise<void> => {
const errs: string[] = [];
const opts = {
scriptingEnabled: test.scriptingEnabled,
treeAdapter,
onParseError: (err: ParserError): void => {
let errStr = `(${err.startLine}:${err.startCol}`;
// NOTE: use ranges for token errors
if (err.startLine !== err.endLine || err.startCol !== err.endCol) {
errStr += `-${err.endLine}:${err.endCol}`;
}
errStr += `) ${err.code}`;
errs.push(errStr);
},
};
const { node, chunks } = await parse(test, opts);
const actual = serializeToDatFileFormat(node, opts.treeAdapter);
const msg = prettyPrintParserAssertionArgs(actual, test.expected, chunks);
let sawError = false;
try {
assert.ok(actual === test.expected, msg);
if (!withoutErrors) {
assert.deepEqual(errs.sort(), test.expectedErrors.sort());
}
} catch (error) {
if (expectError) {
return;
}
sawError = true;
throw error;
}
if (!sawError && expectError) {
throw new Error(`Expected error but none was thrown`);
}
};
}
// TODO: Stop using the fork here.
const treePath = new URL('../data/html5lib-tests-fork/tree-construction', import.meta.url);
export function generateParsingTests(
name: string,
prefix: string,
{
withoutErrors,
expectErrors: expectError = [],
suitePath = treePath,
}: { withoutErrors?: boolean; expectErrors?: string[]; suitePath?: URL },
parse: ParseMethod<TreeAdapterTypeMap>
): void {
generateTestsForEachTreeAdapter(name, (treeAdapter) => {
const errorsToExpect = new Set(expectError);
for (const test of loadTreeConstructionTestData(suitePath, treeAdapter)) {
const expectError = errorsToExpect.delete(`${test.idx}.${test.setName}`);
it(
`${prefix}(${test.dirName}) - ${test.idx}.${test.setName} - \`${test.input}\` (line ${test.lineNum})`,
createParsingTest<TreeAdapterTypeMap>(test, treeAdapter, parse, {
withoutErrors,
expectError,
})
);
}
if (errorsToExpect.size > 0) {
throw new Error(`Expected errors were not found: ${[...errorsToExpect].join(', ')}`);
}
});
}