-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_wasm.js
54 lines (46 loc) · 1.3 KB
/
test_wasm.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
/**
* Test the WASM package using node
*/
const fs = require("fs");
const path = require("path");
const jsonlogic = require("../js");
const load_test_json = () => {
let data = fs.readFileSync(
path.join(__dirname, "data/tests.json"), { encoding: "utf-8" }
);
return JSON.parse(data);
};
const print_case = (c, res) => {
console.log(` Logic: ${JSON.stringify(c[0])}`);
console.log(` Data: ${JSON.stringify(c[1])}`);
console.log(` Expected: ${JSON.stringify(c[2])}`);
console.log(` Actual: ${res && JSON.stringify(res)}`);
}
const run_tests = (cases) => {
const no_comments = cases.filter(i => typeof i !== "string");
for (c of no_comments) {
const logic = c[0];
const data = c[1];
const exp = c[2];
let res;
try {
res = jsonlogic.apply(logic, data);
// res = jsonlogic.apply("apple", {});
}
catch (e) {
console.log("Test errored!");
console.log(` Error: ${e}}`);
print_case(c);
process.exit(2);
}
if (JSON.stringify(res) !== JSON.stringify(exp)) {
console.log("Failed Test!")
print_case(c, res)
process.exit(1);
}
}
};
const main = () => {
run_tests(load_test_json());
};
main();