-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuilder.test.ts
More file actions
105 lines (86 loc) · 2.7 KB
/
builder.test.ts
File metadata and controls
105 lines (86 loc) · 2.7 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { beforeEach, describe, it, type TestContext } from 'node:test';
import { Builder, Endianess } from '../src/builder';
describe('LLParse/Builder', () => {
let b: Builder;
beforeEach(() => {
b = new Builder();
});
it('should build primitive graph', (t: TestContext) => {
const start = b.node('start');
const end = b.node('end');
start
.peek('e', end)
.match('a', start)
.otherwise(b.error(1, 'error'));
end
.skipTo(start);
const edges = start.getEdges();
t.assert.strictEqual(edges.length, 2);
t.assert.ok(!edges[0].noAdvance);
t.assert.strictEqual(edges[0].node, start);
t.assert.ok(edges[1].noAdvance);
t.assert.strictEqual(edges[1].node, end);
});
it('should disallow duplicate edges', (t: TestContext) => {
const start = b.node('start');
start.peek('e', start);
t.assert.throws(() => {
start.peek('e', start);
}, /duplicate edge/);
});
it('should disallow select to non-invoke', (t: TestContext) => {
const start = b.node('start');
t.assert.throws(() => {
start.select('a', 1, start);
}, /value to non-Invoke/);
});
it('should disallow select to match-invoke', (t: TestContext) => {
const start = b.node('start');
const invoke = b.invoke(b.code.match('something'));
t.assert.throws(() => {
start.select('a', 1, invoke);
}, /Invalid.*code signature/);
});
it('should disallow peek to value-invoke', (t: TestContext) => {
const start = b.node('start');
const invoke = b.invoke(b.code.value('something'));
t.assert.throws(() => {
start.peek('a', invoke);
}, /Invalid.*code signature/);
});
it('should allow select to value-invoke', (t: TestContext) => {
const start = b.node('start');
const invoke = b.invoke(b.code.value('something'));
t.assert.doesNotThrow(() => {
start.select('a', 1, invoke);
});
});
it('should create edges for Invoke', (t: TestContext) => {
const start = b.node('start');
const invoke = b.invoke(b.code.value('something'), {
'-1': start,
'1': start,
'10': start,
});
const edges = invoke.getEdges();
const keys = edges.map((edge) => edge.key!);
t.assert.deepStrictEqual(keys, [
-1,
1,
10,
]);
});
it('should create an unpack for Invoke'), (t: TestContext) => {
const start = b.node('start');
b.property('i16', 'unpack_value');
const unpack_error = b.error(0, 'failed to unpack value');
const invoke = b.invoke(
b.code.unpack('unpack_value', Endianess.Little),
start, unpack_error
);
const otherwise = invoke.getOtherwiseEdge();
t.assert.deepStrictEqual(
otherwise?.node, unpack_error
);
}
});