-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
82 lines (65 loc) · 2.42 KB
/
test.js
File metadata and controls
82 lines (65 loc) · 2.42 KB
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
require("typescript-require");
const fs = require("fs");
const { execSync } = require("child_process");
const { compile } = require("./src/compiler.ts");
const emulate = require("./emulator/main.js");
//execSync("npx tsc");
let testGroups = fs.readdirSync("./tests").filter(a => a.endsWith(".spooky2"));
for (let i=0; i<testGroups.length; i++) {
let name = testGroups[i].split(".")[0];
console.log("\x1b[0m# " + name + ":");
let testGroup = fs.readFileSync("./tests/" + testGroups[i]).toString();
let testData = testGroup.split("=".repeat(16));
testData.shift();
let maxTitleLen = 0;
let tests = [];
for (let j=0; j<testData.length; j++) {
let title = testData[j++].trim();
let data = testData[j].split("-".repeat(16));
let input = data[0];
let output = data[1].trim().replace(/\r\n/g, "\n");
tests.push({
title: title,
input: input,
output: output
});
maxTitleLen = Math.max(maxTitleLen, title.length);
}
let caseNo = 1;
for (let j=0; j<tests.length; j++) {
let { title, input, output } = tests[j];
if (title.startsWith("//")) {
console.log("\x1b[97;43m SKIP \x1b[m " + title.slice(2).trim());
continue;
}
title = title + " ".repeat(maxTitleLen - title.length);
let startBuild = process.hrtime.bigint();
let file = Buffer.from(compile(input));
//fs.writeFileSync("./tests/test.spook", file);
/*execSync("node build/main.js ./tests/test.spooky ./tests/test.spook", {
timeout: 2000
});*/
let startRun = process.hrtime.bigint();
/*let out = execSync("java --enable-preview -jar ../spooky.jar run ./tests/test.spook", {
timeout: 2000
});*/
let { out, instr } = emulate(file);
let endRun = process.hrtime.bigint();
out = out.toString().trim();
if (out !== output) {
console.log("\x1b[97;101m FAIL \x1b[m " + title);
console.log("\x1b[90m" + input.trim().split("\n").map(a => "\t" + a).join("\n"));
console.log("\n\t\x1b[mExpected:");
console.log("\t\t\x1b[92m" + output);
console.log("\t\x1b[mGot:");
console.log("\t\t\x1b[91m" + out);
console.log();
} else {
let buildTime = (startRun - startBuild) / 1000n / 1000n + "ms";
let runTime = (endRun - startRun) / 1000n / 1000n + "ms";
console.log("\x1b[97;102m PASS \x1b[m " + title + " \x1b[90m " + buildTime + " build " + runTime + " run " + instr + " cycles\x1b[0m");
}
caseNo++;
}
console.log();
}