-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.test.ts
84 lines (81 loc) · 2.67 KB
/
files.test.ts
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
import { readdirSync } from "fs";
import { readFile } from "fs/promises";
import { expect, test, suite } from "vitest";
import { parse } from "./src/parse";
import { core_patterns } from "./src/syntax-core-patterns";
import { initial_step } from "./src/expander";
import { pprint } from "./src/pprint";
import { StxError, syntax_error } from "./src/stx-error";
import { preexpand_helpers } from "./src/preexpand-helpers";
import { get_globals, init_global_context } from "./src/global-module";
const test_dir = __dirname + "/tests";
const md_dir = __dirname + "/examples";
async function compile_script(filename: string, test_name: string) {
const code = await readFile(filename, { encoding: "utf-8" });
const patterns = core_patterns(parse);
const globals = get_globals("es2024.full");
const [global_unit, global_context] = init_global_context(patterns, globals);
const helpers: preexpand_helpers = {
manager: {
resolve_import(loc) {
syntax_error(loc, "import not supported in tests");
},
resolve_label(_label) {
throw new Error("resolving labels is not supported in tests");
},
get_import_path(_cuid) {
throw new Error(`imports are not supported in tests`);
},
resolve_rib(_rib_id, _cuid) {
throw new Error(`imports are not supported in gui`);
},
},
global_unit,
global_context,
inspect(_loc, _reason, k) {
return k();
},
};
const [_loc0, expand] = initial_step(parse(code, test_name), test_name, ["es2024.full"]);
const result = await (async () => {
try {
const { loc } = await expand(helpers);
return { name: "DONE", loc, error: undefined };
} catch (err) {
if (err instanceof StxError) {
return err;
} else {
throw err;
}
}
})();
function ts(code: string): string {
return "```typescript\n" + code + "```\n\n";
}
function qq(code: string): string {
return "```\n" + code + "```\n\n";
}
function q(str: string): string {
return "`" + str + "`";
}
const prog = await pprint(result.loc, { prettify: true });
const out =
`## ${q(test_name)}\n\n` +
`### Status: ${q(result.name)}\n\n` +
(result.error ? qq(`${result.error}\n`) : "") +
`### Input Program\n\n` +
ts(code) +
`### Output Program\n\n` +
ts(prog) +
"";
return out;
}
suite("files in tests dir", async () => {
const test_files = readdirSync(test_dir).filter((x) => x.match(/\.ts$/));
test_files.forEach((x) =>
test(`expanding file ${x}`, async () => {
const test_path = `${test_dir}/${x}`;
await expect(await compile_script(test_path, x)).toMatchFileSnapshot(`${md_dir}/${x}.md`);
}),
);
});