-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
105 lines (99 loc) · 2.17 KB
/
test.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
import test from "node:test";
import assert from "node:assert";
import { init, Prolog, Atom, Rational, Compound, Exception } from "./dist/scryer.js";
await test("load", async (t) => {
await init();
});
test("query", async (t) => {
const pl = new Prolog();
const query = pl.query(`
X = 1
; X = 9007199254740991 % max safe integer
; X = 9007199254740992
; X = -9007199254740991 % min safe integer
; X = -9007199254740992
; X is 1 rdiv 3
; X = hello
; X = hello(world)
; X = [1, 2, 3]
; X = "hello".
`);
const want = [
1,
9007199254740991,
9007199254740992n,
-9007199254740991,
-9007199254740992n,
new Rational(1, 3),
new Atom("hello"),
new Compound("hello", [new Atom("world")]),
[1, 2, 3],
"hello",
];
let i = 0;
for (const answer of query) {
const cmp = want[i++];
assert.deepEqual(answer.bindings.X, cmp);
}
});
test("query var binding", async (t) => {
const pl = new Prolog();
const query = pl.query(`X = hello(Planet).`, { bind: { Planet: new Atom("world") } });
const want = [
new Compound("hello", [new Atom("world")])
];
let i = 0;
for (const answer of query) {
const cmp = want[i++];
assert.deepEqual(answer.bindings.X, cmp);
}
});
test("throw/1", async (t) => {
const pl = new Prolog();
let threw;
try {
const query = pl.query("throw(hi).");
query.next();
} catch (ex) {
threw = ex;
} finally {
if (!threw) {
assert.fail("didn't throw");
}
if (!(threw instanceof Exception)) {
assert.fail(`bad exception: ${ex}`);
}
}
});
test("consult module", async (t) => {
const pl = new Prolog();
pl.consultText(`
:- module(test_module, [working/1]).
working(yes).
`, "test_module");
// TODO: use_module/1 not importing things properly?
pl.consultText(`
:- use_module(test_module).
hello(world).
hello('Welt').
hello(世界).
hello(X) :- test_module:working(X).
`, "user");
const query = pl.query("hello(X).");
const want = [
new Atom("world"),
new Atom("Welt"),
new Atom("世界"),
new Atom("yes"),
];
let i = 0;
try {
for (const answer of query) {
const cmp = want[i++];
assert.deepEqual(answer.bindings.X, cmp);
}
} catch (ex) {
console.log(ex.cause);
throw ex;
}
});