forked from Zezombye/overpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunTests.mjs
144 lines (122 loc) · 4.83 KB
/
runTests.mjs
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
// @ts-check
"use strict";
import { readdirSync, readFileSync, existsSync, writeFileSync } from "fs";
import { Diff } from "diff";
import colors from "colors";
// @ts-ignore - Standalone OverPy conditionally exports when run in Node
import { compile, decompileAllRules, readyPromise } from "./out/overpy_standalone.js";
// Copy of the diffLines function from the 'diff' package,
// but with the ability to ignore whitespace-only lines
const CustomDiff = new Diff();
CustomDiff.tokenize = function(value, options) {
// remove one \r before \n to match GNU diff's --strip-trailing-cr behavior
value = value.replace(/\r\n/g, '\n');
let retLines = [],
linesAndNewlines = value.split(/(\n|\r\n)/);
// Ignore the final empty token that occurs if the string ends with a new line
if (!linesAndNewlines[linesAndNewlines.length - 1]) {
linesAndNewlines.pop();
}
// Merge the content and line separators into single tokens
for (let i = 0; i < linesAndNewlines.length; i++) {
let line = linesAndNewlines[i];
if (i % 2) {
retLines[retLines.length - 1] += line;
} else {
retLines.push(line);
}
}
return retLines;
};
CustomDiff.removeEmpty = (array) => {
return array
.map((line) => {
if (line.trim() === "") {
return "";
}
return line;
});
};
const compileTestsFolder = "./src/tests/";
const compileExpectedResultsFolder = "./src/tests/results/";
const files = readdirSync(compileTestsFolder);
const opyFiles = files.filter(file => file.endsWith(".opy"));
console.log(colors.bold(`Running ${opyFiles.length} compilation tests`));
for (let file of opyFiles) {
console.log("Running test " + file);
const inputContent = readFileSync(compileTestsFolder + file, "utf8");
let compileResult;
try {
await readyPromise;
compileResult = (await compile(inputContent)).result;
} catch (error) {
console.error(`Test ${file} failed:`);
console.error(error);
process.exit(1);
}
const resultFilePath = compileExpectedResultsFolder + file.replace(".opy", ".txt");
if (!existsSync(resultFilePath)) {
console.log(`No expected output for ${file}, writing compile result to ${compileExpectedResultsFolder + file.replace(".opy", ".txt")}`);
writeFileSync(resultFilePath, compileResult);
continue;
}
const expectedOutput = readFileSync(resultFilePath, "utf8");
// @ts-ignore
const differences = CustomDiff.diff(expectedOutput, compileResult);
const hasDifferences = differences.some(diff => diff.added || diff.removed);
if (hasDifferences) {
console.error(`Test ${file} failed:`);
console.log("Differences found:");
differences.forEach(diff => {
let diffString = diff.value;
if (diff.added) diffString = `+${diffString}`;
else if (diff.removed) diffString = `-${diffString}`;
else diffString = ` ${diffString}`;
const color = diff.added ? "green" : diff.removed ? "red" : "grey";
process.stdout.write(colors[color](diffString));
});
process.exit(1);
}
}
const decompileTestsFolder = "./src/tests/decompiler/";
const decompilerExpectedResultsFolder = "./src/tests/decompiler/results/";
const decompilerFiles = readdirSync(decompileTestsFolder);
const decompilerFilesFiltered = decompilerFiles.filter(file => file.endsWith(".txt"));
console.log(colors.bold(`Running ${decompilerFilesFiltered.length} decompilation tests`));
for (let inputFile of decompilerFilesFiltered) {
console.log("Running test " + inputFile);
const inputContent = readFileSync(decompileTestsFolder + inputFile, "utf8");
let decompileResult;
try {
await readyPromise;
decompileResult = (await decompileAllRules(inputContent));
} catch (error) {
console.error(`Test ${inputFile} failed:`);
console.error(error);
process.exit(1);
}
const resultFilePath = decompilerExpectedResultsFolder + inputFile.replace(".txt", ".opy");
if (!existsSync(resultFilePath)) {
console.log(`No expected output for ${inputFile}, writing decompile result to ${decompilerExpectedResultsFolder + inputFile}`);
writeFileSync(resultFilePath, decompileResult);
continue;
}
const expectedOutput = readFileSync(resultFilePath, "utf8");
// @ts-ignore
const differences = CustomDiff.diff(expectedOutput, decompileResult);
const hasDifferences = differences.some(diff => diff.added || diff.removed);
if (hasDifferences) {
console.error(`Test ${inputFile} failed:`);
console.log("Differences found:");
differences.forEach(diff => {
let diffString = diff.value;
if (diff.added) diffString = `+${diffString}`;
else if (diff.removed) diffString = `-${diffString}`;
else diffString = ` ${diffString}`;
const color = diff.added ? "green" : diff.removed ? "red" : "grey";
process.stdout.write(colors[color](diffString));
});
process.exit(1);
}
}
console.log(colors.bold(colors.green("All tests passed")));