-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmel.test.mjs
68 lines (59 loc) · 2.02 KB
/
cmel.test.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
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { execSync } from "node:child_process";
import { readFileSync, readdirSync, lstatSync } from "node:fs";
const EXPECT_STRING = "// expect:";
const EXPECT_ERROR_STRING = "// expect error:";
process.on("unhandledRejection", (reason, _promise) => {
console.error(reason);
throw reason;
});
function execRun(cmd) {
let stdout, stderr;
try {
stdout = execSync(cmd, { stdio: ['ignore'] }).toString();
} catch (e) {
stderr = e.stderr.toString();
}
return { stdout, stderr };
}
function unescapeNewLines(str) {
return str.replaceAll('\\n', '\n');
}
function generateAssertions(str, stdout, stderr) {
str.split("\n").forEach((line) => {
if (line.startsWith(EXPECT_STRING)) {
const expectedString = unescapeNewLines(line.substring(EXPECT_STRING.length + 1));
assert.equal(stdout, expectedString + "\n");
} else if (line.startsWith(EXPECT_ERROR_STRING)) {
const expectedError = unescapeNewLines(line.substring(EXPECT_ERROR_STRING.length + 1));
assert.equal(stderr, expectedError + "\n");
}
});
}
function loadFile(fileName) {
it(fileName, async () => {
const fileContent = readFileSync(fileName, "utf8");
const { stdout, stderr } = execRun("./cmel " + fileName);
generateAssertions(fileContent, stdout, stderr);
});
}
function readDirectory(path) {
describe(path, () => {
for (const file of readdirSync(path)) {
const filePath = path + "/" + file;
if (lstatSync(filePath).isDirectory()) {
readDirectory(filePath);
} else if (lstatSync(filePath).isFile()) {
loadFile(filePath);
} else {
throw new Error(
'Attempted to read "' +
filePath +
'" which is neither a directory or a file.'
);
}
}
});
}
readDirectory("test");