-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
generate-parsing-tests.ts
144 lines (119 loc) · 4.73 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
import { ParserOptions } from 'parse5/dist/parser/index.js';
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 'parse5-test-utils/utils/parse-dat-file.js';
import type { TreeAdapter, TreeAdapterTypeMap } from 'parse5/dist/tree-adapters/interface';
export interface TreeConstructionTestData<T extends TreeAdapterTypeMap> extends DatFile<T> {
idx: number;
setName: string;
dirName: string;
}
export function loadTreeConstructionTestData<T extends TreeAdapterTypeMap>(
dataDirs: (string | URL)[],
treeAdapter: TreeAdapter<T>
): TreeConstructionTestData<T>[] {
const tests: TreeConstructionTestData<T>[] = [];
for (const dataDir of dataDirs) {
const dataDirPath = typeof dataDir === 'string' ? dataDir : dataDir.pathname;
const testSetFileNames = fs.readdirSync(dataDirPath);
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, 'utf-8');
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 }: { withoutErrors?: 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);
assert.ok(actual === test.expected, msg);
if (!withoutErrors) {
assert.deepEqual(errs.sort(), test.expectedErrors.sort());
}
};
}
const treePath = new URL('../data/html5lib-tests/tree-construction', import.meta.url);
const treeRegressionPath = new URL('../data/tree-construction-regression', import.meta.url);
export function generateParsingTests(
name: string,
prefix: string,
{
skipFragments,
withoutErrors,
testSuite = [treePath.pathname, treeRegressionPath.pathname],
}: { skipFragments?: boolean; withoutErrors?: boolean; testSuite?: string[] },
parse: ParseMethod<TreeAdapterTypeMap>
): void {
generateTestsForEachTreeAdapter(name, (treeAdapter) => {
for (const test of loadTreeConstructionTestData(testSuite, treeAdapter).filter(
(test) => !skipFragments || !test.fragmentContext
)) {
it(
`${prefix}(${test.dirName}) - ${test.idx}.${test.setName} - \`${test.input}\` (line ${test.lineNum})`,
createParsingTest<TreeAdapterTypeMap>(test, treeAdapter, parse, { withoutErrors })
);
}
});
}