-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtestutils.ts
More file actions
57 lines (50 loc) · 1.42 KB
/
testutils.ts
File metadata and controls
57 lines (50 loc) · 1.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
import { expect as globalExpect } from 'vitest';
import { Parser, Interpreter } from '../src';
import { Value } from '../src/interpreter/value';
export async function exe(script: string): Promise<Value | undefined> {
const parser = new Parser();
let result = undefined;
const interpreter = new Interpreter({}, {
out(value) {
if (!result) result = value;
else if (!Array.isArray(result)) result = [result, value];
else result.push(value);
},
log(type, {val}) {
if (type === 'end') result ??= val;
},
maxStep: 9999,
});
const ast = parser.parse(script);
await interpreter.exec(ast);
return result;
};
export function exeSync(script: string): Value | undefined {
const parser = new Parser();
const interpreter = new Interpreter({}, {
out(value) {
},
log(type, {val}) {
},
maxStep: 9999,
});
const ast = parser.parse(script);
return interpreter.execSync(ast);
};
export const getMeta = (script: string) => {
const parser = new Parser();
const ast = parser.parse(script);
const metadata = Interpreter.collectMetadata(ast);
return metadata;
};
export const eq = (a: Value | undefined, b: Value | undefined, expect = globalExpect) => {
expect(a).not.toBeUndefined();
expect(b).not.toBeUndefined();
expect(a!.type).toEqual(b!.type);
if ('value' in a!) {
expect('value' in b!).toBe(true);
expect(a.value).toEqual((b as { value: unknown }).value);
} else {
expect('value' in b!).toBe(false);
}
};