-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrunTests.js
275 lines (245 loc) · 7.2 KB
/
runTests.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// run parser on test cases
const assert = require("assert");
const colors = require("colors/safe");
const fs = require("fs");
const YAML = require("yaml");
const { apgLib } = require("apg-js");
const { parser: Parser, ast: AST, ids, utils, trace: Trace } = apgLib;
const Grammar = require("./grammar");
const config = YAML.parse(fs.readFileSync("./lib/config.yaml", "utf8"));
const parser = new Parser();
const grammar = new Grammar();
// set up parser: call back for every rule to measure rule coverage
const rulesTouched = [];
rulesTouched.length = grammar.rules.length;
grammar.rules.forEach((rule) => {
parser.callbacks[rule.lower] = touchedCB;
});
function touchedCB(result, chars, phraseIndex, data) {
if (result.state === ids.MATCH) {
//TODO: use ruleIndex for accessing constraints, prepare constraints array once?
const ruleName = grammar.rules[result.ruleIndex].name;
const allowedMatches = data.constraints[ruleName];
if (allowedMatches) {
const phrase = utils.charsToString(
chars,
phraseIndex,
result.phraseLength
);
if (!allowedMatches.includes(phrase)) {
result.state = ids.NOMATCH;
result.phraseLength = 0;
if (data.mismatches) data.mismatches.push({ phrase, ruleName });
}
}
if (result.state === ids.MATCH) data.rulesTouched[result.ruleIndex] = true;
}
}
// set up Trace
const trace = new Trace();
// don't trace boring rules
grammar.rules.forEach((rule) => {
if (
rule.name !== rule.name.toUpperCase() &&
!config.trace.omit.includes(rule.name)
)
trace.filter.rules[rule.name] = true;
});
function printRuleCoverage() {
let touched = 0;
const untouched = [];
for (const [i, t] of rulesTouched.entries()) {
if (t) ++touched;
else untouched.push(grammar.rules[i].name);
}
if (touched == grammar.rules.length) {
return colors.green(`Touched all ${grammar.rules.length} rules\n`);
} else {
return colors.yellow(
`Touched ${touched} of ${
grammar.rules.length
} rules, untouched rules:\n - ${untouched.join("\n - ")}\n`
);
}
}
function runTestSuite(testSuite) {
const log = [];
let successes = 0;
if (!testSuite.TestCases) {
log.push(colors.yellow("No test cases found"), "");
return log;
}
for (const tc of testSuite.TestCases) {
if (
parse(
log,
testSuite.Constraints || {},
tc.Name,
tc.Rule,
tc.Input,
tc.FailAt,
tc.Expect
)
) {
successes++;
}
}
if (successes === testSuite.TestCases.length) {
log.push(
colors.green(`All ${testSuite.TestCases.length} test cases passed`),
""
);
} else {
process.exitCode = 1;
const failed = testSuite.TestCases.length - successes;
log.push(
"",
colors.red(`${failed} test case${failed == 1 ? "" : "s"} failed`),
""
);
}
return log;
}
function parse(
log,
constraints,
testCaseName,
startRule,
inputString,
failAt,
expect
) {
const mismatches = [];
let inputCharacterCodes;
let result;
if (typeof startRule !== "string") {
log.push(colors.red(`${testCaseName}: invalid or missing start rule`));
return false;
}
if (typeof inputString !== "string") {
log.push(colors.red(`${testCaseName}: invalid or missing input string`));
return false;
}
try {
setupAST(log, testCaseName, expect);
inputCharacterCodes = utils.stringToChars(inputString);
result = parser.parse(grammar, startRule, inputCharacterCodes, {
rulesTouched,
constraints,
});
} catch (e) {
if (e.message.match(/start rule name .* not recognized/))
log.push(
colors.red(`${testCaseName}: unknown start rule '${startRule}'`)
);
else if (e.message.match(/ast.js: init:/)) {
const m = e.message.match(/ast.js: init: node '(?<ruleName>[^']*)/);
log.push(
colors.red(
`${testCaseName}: unknown Expect token rule '${m.groups.ruleName}'`
)
);
} else console.log(e.message);
return false;
}
if (result.success && failAt == null) {
return checkTokens(log, testCaseName, expect);
}
if (!result.success && result.maxMatched == failAt) {
//TODO: also check tokens for partial match?
return true;
}
const outcome = result.success ? "succeeds" : `fails at ${result.maxMatched}`;
const position = result.success ? `failing at ${failAt}` : `${failAt}`;
const instead =
Number.isInteger(failAt) && result.maxMatched != failAt
? ` instead of ${position}`
: "";
const message = `${testCaseName} ${outcome}${instead}:`;
const matchedInput = inputString.substring(0, result.maxMatched);
const remainingInput = inputString.substring(result.maxMatched);
log.push(
`${colors.red(message)} ${matchedInput}${colors.yellow(remainingInput)}`
);
// rerun parser with trace on
parser.trace = trace;
parser.parse(grammar, startRule, inputCharacterCodes, {
rulesTouched,
constraints,
mismatches,
});
parser.trace = null;
//x fs.writeFileSync("trace.html", trace.toHtml());
//x fs.writeFileSync("trace.json", JSON.stringify(trace.toTree(false), null, 2));
const traceLog = [];
collectMatches(traceLog, trace.toTree(false).tree, inputString);
log.push(...traceLog.reverse());
mismatches.map((m) =>
log.push(
matchedInput.endsWith(m.phrase)
? colors.yellow(`${m.phrase} is no ${m.ruleName}`)
: `${m.phrase} is no ${m.ruleName}`
)
);
return false;
}
function setupAST(log, testCaseName, expect) {
if (!expect) {
parser.ast = undefined;
return;
}
parser.ast = new AST();
for (const token of expect) {
const ruleName = token.substring(0, token.indexOf(":"));
parser.ast.callbacks[ruleName] = matchTokenCB(ruleName);
}
}
// helper for AST callbacks, see https://github.com/ldthomas/apg-js2-examples/blob/61a36fd963ba544c0630a533c053f60d18d878d2/ast/setup.js#L29-L74
function matchTokenCB(ruleName) {
return (state, chars, phraseIndex, phraseLength, data) => {
if (state === ids.SEM_PRE)
data.push(
`${ruleName}:${utils.charsToString(chars, phraseIndex, phraseLength)}`
);
return ids.SEM_SKIP;
};
}
function checkTokens(log, testCaseName, expect) {
if (expect) {
// construct token array from AST as an expected test result
const data = [];
parser.ast.translate(data);
try {
assert.deepStrictEqual(data, expect);
} catch (e) {
log.push(
e.message.replace(
/^Expected values to be strictly deep-equal:/,
colors.red(`${testCaseName} parses into unexpected tokens:`)
)
);
return false;
}
}
return true;
}
function collectMatches(log, node, inputString) {
const text = `${".".repeat(node.depth)}${node.opData}: ${inputString.substr(
node.phrase.index,
node.phrase.length
)}`;
let hasMatch = node.state.id === ids.MATCH;
for (let i = node.children.length - 1; i >= 0; --i) {
const n = node.children[i];
const has = collectMatches(log, n, inputString);
if (has) {
hasMatch = true;
}
}
if (hasMatch) {
if (node.state.id === ids.MATCH) log.push(colors.green(text));
else log.push(text);
}
return hasMatch;
}
module.exports = { runTestSuite, printRuleCoverage };