pud.dev 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +53 -27
- package/THIRD-PARTY-NOTICES +112 -0
- package/chunk-224av1ab.js +10 -0
- package/chunk-fstxscck.js +3 -0
- package/chunk-h90k6v7w.js +3 -0
- package/chunk-k7a83qqm.js +3 -0
- package/cli.js +37 -0
- package/index.js +2 -0
- package/lean/index.js +880 -0
- package/package.json +28 -19
- package/project-runner.js +2 -0
- package/types/assistant.d.ts +200 -0
- package/types/check.d.ts +55 -0
- package/types/expressions.d.ts +47 -0
- package/types/input-scope.d.ts +4 -0
- package/types/lean/command.d.ts +14 -0
- package/types/lean/index.d.ts +5 -0
- package/types/lean/number.d.ts +4 -0
- package/types/lean/program.d.ts +26 -0
- package/types/lean/replay-predicate.d.ts +5 -0
- package/types/lean/replay.d.ts +6 -0
- package/types/lean/scalar.d.ts +23 -0
- package/types/lean/subject.d.ts +25 -0
- package/types/lean/syntax.d.ts +42 -0
- package/types/module.d.ts +9 -0
- package/types/prover-process.d.ts +7 -0
- package/types/run.d.ts +30 -0
- package/types/source.d.ts +5 -0
- package/types/suite-authoring.d.ts +117 -0
- package/types/suite-check.d.ts +10 -0
- package/types/suite.d.ts +44 -0
- package/CLAUDE.md +0 -106
- package/scripts/pack.ts +0 -67
- package/scripts/setup-lean.ts +0 -88
- package/src/assistant.ts +0 -131
- package/src/check.ts +0 -173
- package/src/cli.ts +0 -70
- package/src/expressions.ts +0 -65
- package/src/input-scope.ts +0 -71
- package/src/lean/command.ts +0 -40
- package/src/lean/index.ts +0 -437
- package/src/lean/iteration.lean +0 -145
- package/src/lean/lean-assets.d.ts +0 -4
- package/src/lean/number.lean +0 -427
- package/src/lean/number.ts +0 -25
- package/src/lean/program.ts +0 -110
- package/src/lean/replay-predicate.ts +0 -95
- package/src/lean/replay.ts +0 -136
- package/src/lean/scalar.lean +0 -183
- package/src/lean/scalar.ts +0 -196
- package/src/lean/subject.ts +0 -443
- package/src/lean/syntax.ts +0 -287
- package/src/module.ts +0 -110
- package/src/project-runner.ts +0 -26
- package/src/prover-process.ts +0 -16
- package/src/reporter.ts +0 -173
- package/src/run.ts +0 -60
- package/src/source.ts +0 -54
- package/src/suite-authoring.ts +0 -135
- package/src/suite-check.ts +0 -157
- package/src/suite.ts +0 -515
- package/src/terminal-progress.ts +0 -145
- package/src/terminal.ts +0 -41
- package/test/authoring.test.ts +0 -151
- package/test/lean.test.ts +0 -394
- package/test/module.test.ts +0 -55
- package/test/number.test.ts +0 -318
- package/test/package.test.ts +0 -170
- package/test/suite.test.ts +0 -257
- package/tsconfig.json +0 -7
- package/tsconfig.pack.json +0 -12
- /package/{src/index.ts → types/index.d.ts} +0 -0
package/src/terminal.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import chalk, { Chalk, chalkStderr, type ChalkInstance } from "chalk";
|
|
2
|
-
|
|
3
|
-
const monochrome = new Chalk({ level: 0 });
|
|
4
|
-
|
|
5
|
-
export function terminalInk(out: NodeJS.WriteStream): ChalkInstance {
|
|
6
|
-
if (process.env.NO_COLOR) return monochrome;
|
|
7
|
-
return out === process.stderr ? chalkStderr : chalk;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function isInteractiveTerminal(out: NodeJS.WriteStream): boolean {
|
|
11
|
-
return Boolean(out.isTTY && process.env.TERM !== "dumb");
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const appearance = {
|
|
15
|
-
PASS: { text: "green", mark: "✓" },
|
|
16
|
-
FAIL: { text: "red", mark: "×" },
|
|
17
|
-
UNPROVED: { text: "yellow", mark: "?" },
|
|
18
|
-
UNSUPPORTED: { text: "yellow", mark: "!" },
|
|
19
|
-
ERROR: { text: "red", mark: "!" },
|
|
20
|
-
TODO: { text: "cyan", mark: "○" },
|
|
21
|
-
} as const;
|
|
22
|
-
|
|
23
|
-
export type Verdict = keyof typeof appearance;
|
|
24
|
-
|
|
25
|
-
export function verdictColor(ink: ChalkInstance, verdict: Verdict): ChalkInstance {
|
|
26
|
-
return ink[appearance[verdict].text];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function verdictMark(ink: ChalkInstance, verdict: Verdict): string {
|
|
30
|
-
return verdictColor(ink, verdict)(appearance[verdict].mark);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function pudBanner(out: NodeJS.WriteStream, title: string): string {
|
|
34
|
-
const ink = terminalInk(out);
|
|
35
|
-
const brand = [
|
|
36
|
-
ink.hex("#ff462d").bold("P"),
|
|
37
|
-
ink.hex("#f381bd").bold("u"),
|
|
38
|
-
ink.hex("#00bbd5").bold("d"),
|
|
39
|
-
].join("");
|
|
40
|
-
return `🍮 ${brand} ${ink.dim("·")} ${title}`;
|
|
41
|
-
}
|
package/test/authoring.test.ts
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { afterEach, expect, test } from "bun:test";
|
|
2
|
-
import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { check, emit } from "../src/index.ts";
|
|
6
|
-
|
|
7
|
-
const cli = join(import.meta.dir, "../src/cli.ts");
|
|
8
|
-
const lean = join(import.meta.dir, "../src/lean/index.ts");
|
|
9
|
-
const dirs: string[] = [];
|
|
10
|
-
async function fixture() {
|
|
11
|
-
const dir = await mkdtemp(join(tmpdir(), "pud-authoring-"));
|
|
12
|
-
dirs.push(dir);
|
|
13
|
-
return dir;
|
|
14
|
-
}
|
|
15
|
-
afterEach(async () => { for (const dir of dirs.splice(0)) await rm(dir, { recursive: true, force: true }); });
|
|
16
|
-
|
|
17
|
-
const pending = `import { describe, it } from "@pud.dev/lib";
|
|
18
|
-
import { f } from "./f";
|
|
19
|
-
describe(f, () => { it.todo("Ready"); });`;
|
|
20
|
-
|
|
21
|
-
test("the library and CLI resolve Bun imports and emit the same property by title", async () => {
|
|
22
|
-
const dir = await fixture(), file = join(dir, "pricing.proof.ts");
|
|
23
|
-
await writeFile(join(dir, "pud.config.ts"), `import { leanAdapter } from ${JSON.stringify(lean)};
|
|
24
|
-
export default { adapter: leanAdapter() };`);
|
|
25
|
-
await writeFile(join(dir, "pricing.ts"), "export type Unused = boolean; export function net(n: bigint): bigint { return n; }");
|
|
26
|
-
await writeFile(file, `import { describe, expect, it, pud } from "@pud.dev/lib";
|
|
27
|
-
import { net, type Unused } from "./pricing.js";
|
|
28
|
-
describe(net, () => {
|
|
29
|
-
it("Identity", { n: pud.bigint() }, ({ n }) => { expect(net(n)).to(pud.bigint().eq(n)); });
|
|
30
|
-
it("Positive", { n: pud.bigint() }, ({ n }) => { expect(net(n)).to(pud.bigint().gt(0n)); });
|
|
31
|
-
});`);
|
|
32
|
-
const fromLib = await emit(file, "Identity");
|
|
33
|
-
expect(fromLib).toContain("def claim : Prop :=");
|
|
34
|
-
expect(await emit(file, "Positive")).not.toBe(fromLib);
|
|
35
|
-
const command = Bun.spawnSync([process.execPath, cli, "--emit", file, "Identity"]);
|
|
36
|
-
expect(command.exitCode).toBe(0);
|
|
37
|
-
expect(command.stdout.toString()).toBe(fromLib);
|
|
38
|
-
|
|
39
|
-
await writeFile(join(dir, "pricing.js"), "export function net(n) { return n + 1n; }");
|
|
40
|
-
const fresh = Bun.spawnSync([process.execPath, cli, "check", file, "--json"]);
|
|
41
|
-
expect(fresh.exitCode).toBe(2);
|
|
42
|
-
expect(JSON.parse(fresh.stdout.toString()).files[0].results[0].detail).toContain(".ts implementation");
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test("the CLI asks for an adapter when a suite has active claims", async () => {
|
|
46
|
-
const dir = await fixture();
|
|
47
|
-
await writeFile(join(dir, "f.ts"), "export function f(): boolean { return true; }");
|
|
48
|
-
await writeFile(join(dir, "f.proof.ts"), `import { describe, expect, it, pud } from "@pud.dev/lib";
|
|
49
|
-
import { f } from "./f";
|
|
50
|
-
describe(f, () => {
|
|
51
|
-
it("returns true", {}, ({}) => { expect(f()).to(pud.boolean().eq(true)); });
|
|
52
|
-
});`);
|
|
53
|
-
const child = Bun.spawnSync([process.execPath, cli, "check", "--json"], { cwd: dir });
|
|
54
|
-
expect(child.exitCode).toBe(2);
|
|
55
|
-
expect(child.stderr.toString()).toBe("");
|
|
56
|
-
expect(JSON.parse(child.stdout.toString()).files[0].results[0]).toMatchObject({
|
|
57
|
-
status: "error", detail: expect.stringContaining("pud.config.ts"),
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test("sidecars are parsed as suites and their imports are never run", async () => {
|
|
62
|
-
const dir = await fixture(), file = join(dir, "f.proof.ts");
|
|
63
|
-
await writeFile(join(dir, "f.ts"), "export function f(): boolean { return true; }");
|
|
64
|
-
await writeFile(join(dir, "helper.ts"), 'throw Error("this module ran"); export function describe() {}');
|
|
65
|
-
await writeFile(file, 'import { f } from "./f"; import { describe } from "./helper"; describe(f, () => {});');
|
|
66
|
-
expect((await check(file)).results[0]).toMatchObject({
|
|
67
|
-
status: "unsupported", detail: expect.stringContaining("Use describe"),
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
await writeFile(file, 'import { f } from "./f"; export default { subject: f, property: (result) => result };');
|
|
71
|
-
expect((await check(file)).results[0]).toMatchObject({
|
|
72
|
-
status: "unsupported", detail: expect.stringContaining("Declare a suite with describe()"),
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test("directory selection deduplicates declarations and empty discovery produces a JSON diagnostic", async () => {
|
|
77
|
-
const dir = await fixture();
|
|
78
|
-
function run(args: string[]) {
|
|
79
|
-
const result = Bun.spawnSync([process.execPath, cli, ...args, "--json"], { cwd: dir });
|
|
80
|
-
expect(result.stderr.toString()).toBe("");
|
|
81
|
-
return { code: result.exitCode, report: JSON.parse(result.stdout.toString()) };
|
|
82
|
-
}
|
|
83
|
-
const empty = run([]);
|
|
84
|
-
expect(empty.code).toBe(2);
|
|
85
|
-
expect(empty.report.files).toEqual([]);
|
|
86
|
-
expect(empty.report.counts.error).toBe(1);
|
|
87
|
-
expect(empty.report.errors[0]).toContain("*.proof.ts");
|
|
88
|
-
expect(run(["check", "--invalid"]).code).toBe(2);
|
|
89
|
-
await mkdir(join(dir, "src"));
|
|
90
|
-
await writeFile(join(dir, "src/f.ts"), "export function f(): boolean { return false; }");
|
|
91
|
-
await writeFile(join(dir, "src/f.proof.ts"), pending);
|
|
92
|
-
const found = run(["check", "src", "src/f.proof.ts"]);
|
|
93
|
-
expect(found.code).toBe(0);
|
|
94
|
-
expect(found.report.files).toHaveLength(1);
|
|
95
|
-
expect(found.report.counts.todo).toBe(1);
|
|
96
|
-
expect(run([]).report).toEqual(found.report);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test.skipIf(process.platform === "win32")("discovery includes symlinked sidecars without traversing directory symlinks", async () => {
|
|
100
|
-
const dir = await fixture(), shared = await fixture();
|
|
101
|
-
await writeFile(join(shared, "f.ts"), "export const f = () => false;");
|
|
102
|
-
await writeFile(join(shared, "shared.proof.ts"), pending);
|
|
103
|
-
await symlink(join(shared, "shared.proof.ts"), join(dir, "linked.proof.ts"));
|
|
104
|
-
await symlink(dir, join(dir, "loop"));
|
|
105
|
-
const child = Bun.spawnSync([process.execPath, cli, "check", "--json"], { cwd: dir });
|
|
106
|
-
expect(child.exitCode).toBe(0);
|
|
107
|
-
const report = JSON.parse(child.stdout.toString());
|
|
108
|
-
expect(report.files.map((f: { file: string }) => f.file)).toEqual(["linked.proof.ts"]);
|
|
109
|
-
expect(report.files[0].results[0]).toMatchObject({ status: "todo", name: "Ready" });
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test("the CI guard distinguishes literal inputs from variable arguments and quantified postconditions", async () => {
|
|
113
|
-
const dir = await fixture(), file = join(dir, "identity.proof.ts");
|
|
114
|
-
await writeFile(join(dir, "pud.config.ts"), `export default { adapter: {
|
|
115
|
-
id: "scope", prepare() { return { parameters: [{ name: "x", type: { kind: "integer" } }, { name: "y", type: { kind: "integer" } }],
|
|
116
|
-
result: { kind: "integer" }, emit() { return ""; }, async verify(claims) { return claims.map(() => ({ status: "proved" })); } }; }
|
|
117
|
-
} };`);
|
|
118
|
-
await writeFile(join(dir, "identity.ts"), "export const identity = (x: bigint, y: bigint) => x;");
|
|
119
|
-
const head = `import { describe, it, expect, given, pud } from "@pud.dev/lib";
|
|
120
|
-
import { identity } from "./identity.ts";
|
|
121
|
-
describe(identity, () => {`;
|
|
122
|
-
const rule = `it("allows a fixed argument in a law", { x: pud.bigint(), y: pud.bigint().eq(2n) }, ({ x, y }) => {
|
|
123
|
-
expect(identity(x, y)).to(pud.bigint().eq(x));
|
|
124
|
-
});`;
|
|
125
|
-
const forall = `it("allows a quantified postcondition", { x: pud.bigint().eq(1n), y: pud.bigint().eq(2n) }, ({ x, y }) => {
|
|
126
|
-
expect(identity(x, y)).to(pud.bigint().refine(z => pud.forall(pud.bigint().gte(z), n => n >= z)));
|
|
127
|
-
});`;
|
|
128
|
-
const checkScope = () => Bun.spawnSync([process.execPath, cli, "check", file, "--json", "--deny-cases"]);
|
|
129
|
-
await writeFile(file, [head, rule, forall, "});"].join("\n"));
|
|
130
|
-
expect(checkScope().exitCode).toBe(0);
|
|
131
|
-
const fixed = `it("pins both inputs", { x: pud.bigint().refine(n => n === -1n), y: pud.bigint() }, ({ x, y: other }) => {
|
|
132
|
-
given(2n === other);
|
|
133
|
-
expect(identity(x, other)).to(pud.bigint().eq(x));
|
|
134
|
-
});`;
|
|
135
|
-
const bounded = `it("closes both bounds", { x: pud.bigint().gte(-1n), y: pud.bigint().eq(2n) }, ({ x, y }) => {
|
|
136
|
-
given(-1n >= x);
|
|
137
|
-
expect(identity(x, y)).to(pud.bigint().eq(x));
|
|
138
|
-
});`;
|
|
139
|
-
await writeFile(file, [head, rule, fixed, bounded, "});"].join("\n"));
|
|
140
|
-
const run = checkScope();
|
|
141
|
-
expect(run.exitCode).toBe(2);
|
|
142
|
-
const { files, errors } = JSON.parse(run.stdout.toString());
|
|
143
|
-
expect(files[0].results[0].concreteScope).toBeUndefined();
|
|
144
|
-
expect(files[0].results[1]).toMatchObject({ status: "proved", concreteScope: "literal-inputs" });
|
|
145
|
-
expect(errors[0]).toContain("pins both inputs");
|
|
146
|
-
expect(files[0].results[2].concreteScope).toBe("literal-inputs");
|
|
147
|
-
await writeFile(file, [head, rule.replace(".to(", ".toExactly("), "});"].join("\n"));
|
|
148
|
-
expect((await check(file)).results[0]).toMatchObject({
|
|
149
|
-
status: "unsupported", detail: expect.stringContaining("does not support exact result schemas"),
|
|
150
|
-
});
|
|
151
|
-
});
|
package/test/lean.test.ts
DELETED
|
@@ -1,394 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "bun:test";
|
|
2
|
-
import { readFileSync } from "node:fs";
|
|
3
|
-
import { cp, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { pathToFileURL } from "node:url";
|
|
7
|
-
import { check, checkFiles, emit, type CheckProgress, type CheckReport, type ProofAssistant } from "../src/index.ts";
|
|
8
|
-
import { leanCommand } from "../src/lean/command.ts";
|
|
9
|
-
import { leanAdapter } from "../src/lean/index.ts";
|
|
10
|
-
|
|
11
|
-
const examples = join(import.meta.dir, "../.examples");
|
|
12
|
-
const toolchain = readFileSync(join(examples, "lean-toolchain"), "utf8").trim();
|
|
13
|
-
const adapter = leanAdapter({ toolchain });
|
|
14
|
-
const command = leanCommand({ file: join(examples, "zigzag.ts"), name: "encode", source: "" }, { toolchain });
|
|
15
|
-
const leanTest = test.skipIf(!Bun.which(command.binary));
|
|
16
|
-
|
|
17
|
-
leanTest("pud check displays grouped Lean verdicts and diagnostics in a terminal", async () => {
|
|
18
|
-
const decoder = new TextDecoder();
|
|
19
|
-
const drained = Promise.withResolvers<void>();
|
|
20
|
-
let output = "";
|
|
21
|
-
const child = Bun.spawn([process.execPath, join(import.meta.dir, "../src/cli.ts"), "check",
|
|
22
|
-
"linear-search.proof.ts", "sum-positive.proof.ts"], {
|
|
23
|
-
cwd: examples,
|
|
24
|
-
env: { ...process.env, NO_COLOR: "1", TERM: "xterm-256color" },
|
|
25
|
-
timeout: 25_000,
|
|
26
|
-
terminal: {
|
|
27
|
-
cols: 96,
|
|
28
|
-
data(_terminal, chunk) { output += decoder.decode(chunk, { stream: true }); },
|
|
29
|
-
exit() { drained.resolve(); },
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
try {
|
|
33
|
-
const [status] = await Promise.all([child.exited, drained.promise]);
|
|
34
|
-
expect(status).toBe(2);
|
|
35
|
-
expect(output).toContain("Pud · Proof check");
|
|
36
|
-
expect(output).toContain("Validating input snapshots");
|
|
37
|
-
expect(output).toMatch(/✓ linear-search\.proof\.ts +\d+(?:ms|\.\d{2}s)/);
|
|
38
|
-
expect(output).toContain("└─ PASS returns the first matching index or minus one");
|
|
39
|
-
expect(output).toContain("├─ UNSUPPORTED returns a nonnegative total for every input list");
|
|
40
|
-
expect(output).toContain("Lean diagnostic");
|
|
41
|
-
expect(output).toContain("sum-positive.proof.ts:8");
|
|
42
|
-
expect(output).toContain("'ForStatement' before the final return.");
|
|
43
|
-
expect(output).toContain("└─ TODO adding a nonpositive element leaves the sum unchanged");
|
|
44
|
-
} finally {
|
|
45
|
-
child.terminal?.close();
|
|
46
|
-
}
|
|
47
|
-
}, 30_000);
|
|
48
|
-
|
|
49
|
-
async function fixture(run: (directory: string) => Promise<void>) {
|
|
50
|
-
const directory = await mkdtemp(join(tmpdir(), "pud-lean-test-"));
|
|
51
|
-
try { await run(directory); }
|
|
52
|
-
finally { await rm(directory, { recursive: true, force: true }); }
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
leanTest("the same availability law proves the refactor and replays a dropped batch in Bun", async () => {
|
|
56
|
-
await fixture(async directory => {
|
|
57
|
-
await cp(join(examples, "availability"), directory, { recursive: true });
|
|
58
|
-
const config = join(directory, "pud.config.ts");
|
|
59
|
-
const root = join(import.meta.dir, "../src");
|
|
60
|
-
// A temporary project resolves the source entry points directly.
|
|
61
|
-
await writeFile(config, [
|
|
62
|
-
`import { defineConfig } from ${JSON.stringify(join(root, "index.ts"))};`,
|
|
63
|
-
`import { leanAdapter } from ${JSON.stringify(join(root, "lean/index.ts"))};`,
|
|
64
|
-
"export default defineConfig({ adapter: leanAdapter({ replay: true }) });",
|
|
65
|
-
].join("\n"));
|
|
66
|
-
function run(exit: number) {
|
|
67
|
-
const child = Bun.spawnSync([process.execPath, join(root, "cli.ts"), "check", "--json", "count-free.proof.ts"], {
|
|
68
|
-
cwd: directory, env: { ...command.env, PUD_LEAN: command.binary }, timeout: 20_000,
|
|
69
|
-
});
|
|
70
|
-
const stdout = child.stdout.toString(), stderr = child.stderr.toString();
|
|
71
|
-
if (child.exitCode !== exit || stderr) throw new Error(`Pud exited ${child.exitCode}\n${stdout}\n${stderr}`);
|
|
72
|
-
return (JSON.parse(stdout).files[0] as CheckReport).results[0];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
expect(run(0)?.status).toBe("proved");
|
|
76
|
-
const implementation = join(directory, "count-free.ts");
|
|
77
|
-
const split = await readFile(join(directory, "split-count-free.ts"), "utf8");
|
|
78
|
-
await writeFile(implementation, split);
|
|
79
|
-
expect(run(0)?.status).toBe("proved");
|
|
80
|
-
|
|
81
|
-
const addition = "countBatch(right) + countBatch(left)";
|
|
82
|
-
expect(split).toContain(addition);
|
|
83
|
-
await writeFile(implementation, split.replace(addition, "countBatch(left)"));
|
|
84
|
-
expect(run(1)).toMatchObject({
|
|
85
|
-
status: "disproved",
|
|
86
|
-
counterexample: { input: { left: "[]", right: "[1n]" }, difference: { expected: "1n", received: "0n" } },
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
const proof = join(directory, "count-free.proof.ts");
|
|
90
|
-
const restricted = (await readFile(proof, "utf8")).replace("{ it, expect, use }", "{ it, expect, given, use }")
|
|
91
|
-
.replace(" expect(countFree", " given(pud.length(right) > 1n);\n expect(countFree");
|
|
92
|
-
await writeFile(proof, restricted);
|
|
93
|
-
const open = run(1);
|
|
94
|
-
expect(open?.status).toBe("unproved");
|
|
95
|
-
expect(open?.counterexample).toBeUndefined();
|
|
96
|
-
});
|
|
97
|
-
}, 45_000);
|
|
98
|
-
|
|
99
|
-
function sidecar(subject: string, functionImports: string, source: string, schemas: string, inputs: string, assertion: string) {
|
|
100
|
-
return `import { describe, expect, it, pud } from "@pud.dev/lib";
|
|
101
|
-
import { ${functionImports} } from "${source}";
|
|
102
|
-
describe(${subject}, () => {
|
|
103
|
-
it("observes the linked implementation", { ${schemas} }, ({ ${inputs} }) => {
|
|
104
|
-
${assertion}
|
|
105
|
-
});
|
|
106
|
-
});`;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
type Input = readonly (bigint | boolean)[];
|
|
110
|
-
|
|
111
|
-
function runtimeOutcome(run: () => bigint): string {
|
|
112
|
-
try { return `ok:${run()}`; }
|
|
113
|
-
catch (error) {
|
|
114
|
-
if (!(error instanceof Error)) throw error;
|
|
115
|
-
return `throw:${error.name}`;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async function evaluate(directory: string, file: string, inputs: readonly Input[], fallible: boolean, property?: string) {
|
|
120
|
-
const unit = await emit(file, { adapter, property });
|
|
121
|
-
const stop = unit.indexOf("\ntheorem checked : claim :=");
|
|
122
|
-
if (stop === -1) throw new Error("Expected an emitted proof unit.");
|
|
123
|
-
const applications = (functionName: string) => inputs.map(values => [functionName, ...values.map(value =>
|
|
124
|
-
typeof value === "bigint" ? `(${value} : Int)` : String(value))].join(" ")).join(",\n ");
|
|
125
|
-
const observeOutcome = `fun outcome => match outcome with
|
|
126
|
-
| .ok value => "ok:" ++ toString value
|
|
127
|
-
| .error .error => "throw:Error"
|
|
128
|
-
| .error .rangeError => "throw:RangeError"
|
|
129
|
-
| .error .typeError => "throw:TypeError"`;
|
|
130
|
-
const observe = fallible ? observeOutcome : 'fun value => "ok:" ++ toString value';
|
|
131
|
-
const source = unit.includes("\ndef implSource ")
|
|
132
|
-
? `\n#eval String.intercalate "," ([${applications("Pud.implSource")}].map (fun e => (${observeOutcome}) (Pud.Scalar.evaluate e)))\n`
|
|
133
|
-
: "";
|
|
134
|
-
const modelFile = join(directory, "Model.lean");
|
|
135
|
-
await writeFile(modelFile, unit.slice(0, stop) + `\nend Pud
|
|
136
|
-
|
|
137
|
-
#eval String.intercalate "," ([${applications("Pud.impl")}].map (${observe}))
|
|
138
|
-
${source}`);
|
|
139
|
-
const run = Bun.spawnSync([command.binary, modelFile], {
|
|
140
|
-
cwd: directory, env: { ...command.env, LEAN_PATH: directory }, stdout: "pipe", stderr: "pipe", timeout: 20_000,
|
|
141
|
-
});
|
|
142
|
-
if (!run.success) throw new Error(run.stdout.toString() + run.stderr.toString());
|
|
143
|
-
const [implementation, reference] = run.stdout.toString().split("\n").filter(line => line.startsWith('"'))
|
|
144
|
-
.map(line => (JSON.parse(line) as string).split(","));
|
|
145
|
-
if (!implementation) throw new Error("Lean did not print the implementation observations.");
|
|
146
|
-
if (source) expect(reference).toEqual(implementation);
|
|
147
|
-
return implementation;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
leanTest("the Lean model agrees with Bun on signed bigint calls, short circuits, exception order and catch", async () => {
|
|
151
|
-
await fixture(async directory => {
|
|
152
|
-
await writeFile(join(directory, "arithmetic.ts"), `
|
|
153
|
-
export function divide(n: bigint, d: bigint): bigint { return n / d; }
|
|
154
|
-
export function rejectZero(d: bigint): bigint {
|
|
155
|
-
if (d === 0n) throw new TypeError("zero");
|
|
156
|
-
return d;
|
|
157
|
-
}`);
|
|
158
|
-
const source = join(directory, "outcome.ts");
|
|
159
|
-
await writeFile(source, `import { divide as quotient, rejectZero } from "./arithmetic";
|
|
160
|
-
|
|
161
|
-
export function outcome(n: bigint, d: bigint, active: boolean, reverse: boolean): bigint {
|
|
162
|
-
if (active && n % -3n !== 0n) {
|
|
163
|
-
return reverse ? rejectZero(d) + quotient(n, d) : quotient(n, d) + rejectZero(d);
|
|
164
|
-
}
|
|
165
|
-
return !active || n % d === 0n ? 42n : quotient(n, d);
|
|
166
|
-
}
|
|
167
|
-
export function recover(n: bigint, d: bigint, active: boolean, reverse: boolean): bigint {
|
|
168
|
-
try { return outcome(n, d, active, reverse); }
|
|
169
|
-
catch { return -99n; }
|
|
170
|
-
}`);
|
|
171
|
-
const inputs = [-5n, -3n, 0n, 3n, 5n, (1n << 70n) + 5n].flatMap(n =>
|
|
172
|
-
[-3n, -1n, 0n, 1n, 3n].flatMap(d => [false, true].flatMap(active =>
|
|
173
|
-
[false, true].map(reverse => [n, d, active, reverse] as const))));
|
|
174
|
-
const module = await import(pathToFileURL(source).href);
|
|
175
|
-
for (const subject of ["outcome", "recover"]) {
|
|
176
|
-
const file = join(directory, subject + ".proof.ts");
|
|
177
|
-
await writeFile(file, sidecar(subject, subject, "./outcome.ts",
|
|
178
|
-
"n: pud.bigint(), d: pud.bigint(), active: pud.boolean(), reverse: pud.boolean()", "n, d, active, reverse",
|
|
179
|
-
`expect(${subject}(n, d, active, reverse)).to(pud.bigint().refine(_value => true));`));
|
|
180
|
-
const expected = inputs.map(args => runtimeOutcome(() => module[subject](...args)));
|
|
181
|
-
if (subject === "outcome") {
|
|
182
|
-
expect(expected).toContain("throw:RangeError");
|
|
183
|
-
expect(expected).toContain("throw:TypeError");
|
|
184
|
-
}
|
|
185
|
-
expect(await evaluate(directory, file, inputs, subject === "outcome")).toEqual(expected);
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
}, 30_000);
|
|
189
|
-
|
|
190
|
-
leanTest("both ZigZag callees are part of the theorem and edits to either invalidate its proof", async () => {
|
|
191
|
-
await fixture(async directory => {
|
|
192
|
-
const source = join(directory, "zigzag.ts"), file = join(directory, "zigzag.proof.ts");
|
|
193
|
-
const original = await readFile(join(examples, "zigzag.ts"), "utf8");
|
|
194
|
-
await writeFile(file, await readFile(join(examples, "zigzag-roundtrip.proof.ts"), "utf8"));
|
|
195
|
-
await writeFile(source, original);
|
|
196
|
-
expect((await check(file, { adapter })).results[0]?.status).toBe("proved");
|
|
197
|
-
for (const [before, after] of [["-2n * value - 1n", "-2n * value + 1n"], ["code / 2n", "code / 2n + 1n"]]) {
|
|
198
|
-
await writeFile(source, original.replace(before!, after!));
|
|
199
|
-
expect((await check(file, { adapter })).results[0]?.status).toBe("unproved");
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
}, 20_000);
|
|
203
|
-
|
|
204
|
-
leanTest("the same fold theorem checks guarded updates through a linked implementation", async () => {
|
|
205
|
-
await fixture(async directory => {
|
|
206
|
-
const file = join(directory, "sum-positive.proof.ts");
|
|
207
|
-
const subject = join(directory, "sum-positive.ts");
|
|
208
|
-
const helper = join(directory, "accumulate.ts");
|
|
209
|
-
const proof = await readFile(join(examples, "sum-positive.proof.ts"), "utf8");
|
|
210
|
-
await writeFile(file, proof);
|
|
211
|
-
await writeFile(join(directory, "pud.config.ts"), "export default {};");
|
|
212
|
-
await writeFile(join(directory, "sum-positive-lemmas.lean"), await readFile(join(examples, "sum-positive-lemmas.lean"), "utf8"));
|
|
213
|
-
function source(name: string, update: string) {
|
|
214
|
-
return `export function ${name}(values: readonly bigint[]): bigint {
|
|
215
|
-
const threshold = 0n;
|
|
216
|
-
let total = 0n;
|
|
217
|
-
for (const value of values) { ${update} }
|
|
218
|
-
return total;
|
|
219
|
-
}`;
|
|
220
|
-
}
|
|
221
|
-
const guard = "if (value > threshold) { total = total + value; }";
|
|
222
|
-
const verdict = async () => (await check(file, { adapter })).results[0]?.status;
|
|
223
|
-
await writeFile(subject, source("sumPositive", "total += value > threshold ? value : 0n;"));
|
|
224
|
-
expect(await verdict()).toBe("proved");
|
|
225
|
-
await writeFile(subject, source("sumPositive", guard));
|
|
226
|
-
expect(await verdict()).toBe("proved");
|
|
227
|
-
const unit = await emit(file, { adapter });
|
|
228
|
-
expect(unit).toContain("theorem implLoop0Conforms");
|
|
229
|
-
expect(unit).toContain("using (PositiveSum.nonnegative)");
|
|
230
|
-
|
|
231
|
-
await writeFile(subject, `import { accumulate } from "./accumulate.ts";
|
|
232
|
-
export function sumPositive(values: readonly bigint[]): bigint { return accumulate(values); }`);
|
|
233
|
-
await writeFile(helper, source("accumulate", `${guard} else { total += 0n; }`));
|
|
234
|
-
expect(await verdict()).toBe("proved");
|
|
235
|
-
await writeFile(helper, source("accumulate", guard.replace(">", "<")));
|
|
236
|
-
expect(await verdict()).toBe("unproved");
|
|
237
|
-
});
|
|
238
|
-
}, 20_000);
|
|
239
|
-
|
|
240
|
-
test("transitive imports are snapshotted and a change to their resolution invalidates a verdict", async () => {
|
|
241
|
-
await fixture(async directory => {
|
|
242
|
-
const file = join(directory, "subject.proof.ts"), dependency = join(directory, "dependency.ts");
|
|
243
|
-
const helper = "export function step(n: bigint): bigint { return n + 1n; }";
|
|
244
|
-
await writeFile(dependency, helper);
|
|
245
|
-
await writeFile(join(directory, "intermediate.ts"), 'import { step } from "./dependency";\nexport function next(n: bigint): bigint { return step(n); }');
|
|
246
|
-
await writeFile(join(directory, "subject.ts"), 'import { next } from "./intermediate.ts";\nexport function subject(n: bigint): bigint { return next(n); }');
|
|
247
|
-
await writeFile(file, sidecar("subject", "subject", "./subject.ts", "n: pud.bigint()", "n",
|
|
248
|
-
"expect(subject(n)).to(pud.bigint().eq(n + 1n));"));
|
|
249
|
-
for (const [path, contents, message] of [
|
|
250
|
-
[dependency, helper.replace("+ 1n", "- 1n"), "dependency.ts changed during verification"],
|
|
251
|
-
[join(directory, "dependency"), helper, "The import of"],
|
|
252
|
-
] as const) {
|
|
253
|
-
await writeFile(dependency, helper);
|
|
254
|
-
const editDuringVerification: ProofAssistant<"lean"> = {
|
|
255
|
-
...adapter,
|
|
256
|
-
async prepare(source) {
|
|
257
|
-
const program = await adapter.prepare(source);
|
|
258
|
-
return { ...program, async verify(claims, modules) {
|
|
259
|
-
await program.emit(claims[0]!, modules);
|
|
260
|
-
await writeFile(path, contents);
|
|
261
|
-
return [{ status: "proved" }];
|
|
262
|
-
} };
|
|
263
|
-
},
|
|
264
|
-
};
|
|
265
|
-
const result = (await check(file, { adapter: editDuringVerification })).results[0];
|
|
266
|
-
expect(result?.status).toBe("error");
|
|
267
|
-
expect(result?.detail).toContain(message);
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
leanTest("a zero divisor cannot prove a success property or the wrong exception", async () => {
|
|
273
|
-
await fixture(async directory => {
|
|
274
|
-
const source = "bigint-division.ts";
|
|
275
|
-
await writeFile(join(directory, source), await readFile(join(examples, source), "utf8"));
|
|
276
|
-
const file = join(directory, "division.proof.ts");
|
|
277
|
-
const inputs = "numerator, divisor";
|
|
278
|
-
const schemas = "numerator: pud.bigint(), divisor: pud.bigint().eq(0n)";
|
|
279
|
-
for (const assertion of [
|
|
280
|
-
`expect(divide(${inputs})).to(pud.bigint().refine(_value => true));`,
|
|
281
|
-
`expect(() => divide(${inputs})).toThrow("TypeError");`,
|
|
282
|
-
]) {
|
|
283
|
-
await writeFile(file, sidecar("divide", "divide", "./" + source, schemas, inputs, assertion));
|
|
284
|
-
const events: CheckProgress[] = [];
|
|
285
|
-
const result = await check(file, { adapter, onProgress: event => events.push(event) });
|
|
286
|
-
expect(result.results[0]?.status).toBe("unproved");
|
|
287
|
-
expect(events.find(event => event.kind === "finished")).toMatchObject({ indices: [0], statuses: ["unproved"] });
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
}, 20_000);
|
|
291
|
-
|
|
292
|
-
leanTest("progress visits each Lean item before the shared snapshot check", async () => {
|
|
293
|
-
const events: CheckProgress[] = [];
|
|
294
|
-
const files = [join(examples, "bigint-division.proof.ts"), join(examples, "zigzag-roundtrip.proof.ts")] as const;
|
|
295
|
-
const reports = await checkFiles(files, { adapter, onProgress: event => events.push(event) });
|
|
296
|
-
expect(reports.flatMap(report => report.results).map(result => result.status)).toEqual(["proved", "proved", "proved"]);
|
|
297
|
-
expect(events.filter(event => event.kind === "suite").map(event => [event.index, event.totalItems])).toEqual([[0, 3], [1, 3]]);
|
|
298
|
-
expect(events.filter(event => event.kind === "finished").flatMap(event => event.indices.map(index => [event.file, index])))
|
|
299
|
-
.toEqual([[files[0], 0], [files[0], 1], [files[1], 0]]);
|
|
300
|
-
const completedFirst = events.findIndex(event => event.kind === "finished" && event.file === files[0] &&
|
|
301
|
-
event.indices.length === 1 && event.indices[0] === 0);
|
|
302
|
-
const startedSecond = events.findIndex(event => event.kind === "running" && event.file === files[0] &&
|
|
303
|
-
event.indices.length === 1 && event.indices[0] === 1);
|
|
304
|
-
expect(completedFirst).toBeGreaterThan(0);
|
|
305
|
-
expect(events[completedFirst]).toMatchObject({ statuses: ["proved"] });
|
|
306
|
-
expect(startedSecond).toBeGreaterThan(completedFirst);
|
|
307
|
-
expect(events.at(-1)).toEqual({ kind: "validating" });
|
|
308
|
-
}, 30_000);
|
|
309
|
-
|
|
310
|
-
leanTest("the lowering certificate catches a mistranslation outside the user property's domain", async () => {
|
|
311
|
-
await fixture(async directory => {
|
|
312
|
-
const unit = await emit(join(examples, "bigint-division.proof.ts"), {
|
|
313
|
-
adapter, property: "returns a quotient truncated toward zero for every nonzero divisor",
|
|
314
|
-
});
|
|
315
|
-
expect(unit).toContain("theorem sourceChecked : sourceClaim");
|
|
316
|
-
const implementation = unit.indexOf("\ndef impl ");
|
|
317
|
-
const mutated = unit.slice(0, implementation) + unit.slice(implementation)
|
|
318
|
-
.replace("Except.error .rangeError", "Except.error .typeError");
|
|
319
|
-
expect(mutated).not.toBe(unit);
|
|
320
|
-
async function run(source: string) {
|
|
321
|
-
const path = join(directory, "Certificate.lean");
|
|
322
|
-
await writeFile(path, source);
|
|
323
|
-
return Bun.spawnSync([command.binary, path], { cwd: directory, env: command.env, timeout: 20_000 });
|
|
324
|
-
}
|
|
325
|
-
const correct = await run(unit);
|
|
326
|
-
expect(correct.stdout.toString()).not.toContain("error:");
|
|
327
|
-
expect(correct.exitCode).toBe(0);
|
|
328
|
-
expect((await run(mutated)).exitCode).toBe(1);
|
|
329
|
-
const propertyOnly = mutated.slice(0, mutated.indexOf("\ntheorem implLowering")) +
|
|
330
|
-
mutated.slice(mutated.indexOf("\ndef claim : Prop :="), mutated.indexOf("\ndef sourceClaim : Prop :=")) +
|
|
331
|
-
mutated.slice(mutated.indexOf("\ntheorem checked : claim :="), mutated.indexOf("\ntheorem sourceChecked : sourceClaim")) + "\nend Pud\n";
|
|
332
|
-
expect((await run(propertyOnly)).exitCode).toBe(0);
|
|
333
|
-
});
|
|
334
|
-
}, 30_000);
|
|
335
|
-
|
|
336
|
-
leanTest("the checked loop and recursion agree with Bun and reject missing decreases", async () => {
|
|
337
|
-
await fixture(async directory => {
|
|
338
|
-
await cp(join(examples, "pud.config.ts"), join(directory, "pud.config.ts"));
|
|
339
|
-
await cp(join(examples, "triangular-sum.lean"), join(directory, "triangular-sum.lean"));
|
|
340
|
-
await cp(join(examples, "triangular-sum.lean"), join(directory, "PudSupport0.lean"));
|
|
341
|
-
const support = Bun.spawnSync([command.binary, "-o", "PudSupport0.olean", "PudSupport0.lean"], { cwd: directory, env: command.env });
|
|
342
|
-
expect(support.exitCode).toBe(0);
|
|
343
|
-
const source = join(directory, "termination.ts");
|
|
344
|
-
const original = await readFile(join(examples, "termination.ts"), "utf8");
|
|
345
|
-
await writeFile(source, original);
|
|
346
|
-
const mod = await import(pathToFileURL(source).href);
|
|
347
|
-
const inputs = [-5n, -1n, 0n, 1n, 2n, 5n, 8n, 15n].map(value => [value] as const);
|
|
348
|
-
const cases = [
|
|
349
|
-
["countdown-sum", "countdownSum", "remaining -= 1n", "remaining += 1n", "unsupported"],
|
|
350
|
-
["recursive-sum", "recursiveSum", "recursiveSum(value - 1n)", "recursiveSum(value + 1n)", "unproved"],
|
|
351
|
-
] as const;
|
|
352
|
-
for (const [name, subject, before, after, rejection] of cases) {
|
|
353
|
-
await writeFile(source, original);
|
|
354
|
-
const file = join(directory, name + ".proof.ts");
|
|
355
|
-
const proof = await readFile(join(examples, name + ".proof.ts"), "utf8");
|
|
356
|
-
await writeFile(file, proof);
|
|
357
|
-
const positive = "sums every integer from one through the input";
|
|
358
|
-
expect((await check(file, { adapter })).results.map(result => result.status)).toEqual(["proved", "proved"]);
|
|
359
|
-
expect(await evaluate(directory, file, inputs, false, positive)).toEqual(inputs.map(args => runtimeOutcome(() => mod[subject](...args))));
|
|
360
|
-
await writeFile(source, original.replace(before, after));
|
|
361
|
-
expect((await check(file, { adapter })).results.every(result => result.status === rejection)).toBe(true);
|
|
362
|
-
}
|
|
363
|
-
const loopSource = join(directory, "loop.ts"), loopSuite = join(directory, "loop.proof.ts");
|
|
364
|
-
await writeFile(loopSource, `export function loop(value: bigint, divisor: bigint): bigint {
|
|
365
|
-
let counter = value;
|
|
366
|
-
let total = 0n;
|
|
367
|
-
let add = true;
|
|
368
|
-
while (counter >= -2n) {
|
|
369
|
-
if (add) total += counter / divisor;
|
|
370
|
-
add = !add;
|
|
371
|
-
counter -= 2n;
|
|
372
|
-
}
|
|
373
|
-
return total + counter;
|
|
374
|
-
}`);
|
|
375
|
-
await writeFile(loopSuite, sidecar("loop", "loop", "./loop.ts", "value: pud.bigint(), divisor: pud.bigint()", "value, divisor",
|
|
376
|
-
"expect(loop(value, divisor)).to(pud.bigint().refine(_value => true));"));
|
|
377
|
-
const loop = (await import(pathToFileURL(loopSource).href)).loop;
|
|
378
|
-
const loopInputs = inputs.flatMap(([value]) => [-2n, 0n, 2n].map(divisor => [value, divisor] as const));
|
|
379
|
-
expect(await evaluate(directory, loopSuite, loopInputs, true)).toEqual(loopInputs.map(args => runtimeOutcome(() => loop(...args))));
|
|
380
|
-
});
|
|
381
|
-
}, 30_000);
|
|
382
|
-
|
|
383
|
-
test("the linker rejects runtime initialization and a local binding that shadows a linked function", async () => {
|
|
384
|
-
await fixture(async directory => {
|
|
385
|
-
const file = join(directory, "subject.proof.ts"), source = join(directory, "subject.ts");
|
|
386
|
-
await writeFile(file, sidecar("subject", "subject", "./subject.ts", "n: pud.bigint()", "n",
|
|
387
|
-
"expect(subject(n)).to(pud.bigint().eq(n));"));
|
|
388
|
-
await writeFile(join(directory, "helper.ts"), 'console.log("runtime initializer");\nexport function step(n: bigint): bigint { return n; }');
|
|
389
|
-
await writeFile(source, 'import { step } from "./helper.ts";\nexport function subject(n: bigint): bigint { return n; }');
|
|
390
|
-
expect((await check(file, { adapter })).results[0]?.status).toBe("unsupported");
|
|
391
|
-
await writeFile(source, "function step(n: bigint): bigint { return n; }\nexport function subject(n: bigint): bigint { const result = step(n); const step = false; return result; }");
|
|
392
|
-
expect((await check(file, { adapter })).results[0]).toMatchObject({ status: "unsupported", detail: expect.stringContaining("shadowing") });
|
|
393
|
-
});
|
|
394
|
-
});
|
package/test/module.test.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "bun:test";
|
|
2
|
-
import { parse } from "@babel/parser";
|
|
3
|
-
import { selectSubject } from "../src/module.ts";
|
|
4
|
-
|
|
5
|
-
const subject = "export function max(a: bigint, b: bigint): bigint { return a < b ? b : a; }";
|
|
6
|
-
|
|
7
|
-
function select(declarations: string) {
|
|
8
|
-
const { program } = parse(subject + "\n" + declarations, { sourceType: "module", plugins: ["typescript"] });
|
|
9
|
-
return selectSubject(program, "bounds.ts", "max");
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
test("isolates the subject among erased types, constant data and unrelated functions", () => {
|
|
13
|
-
expect(select(`
|
|
14
|
-
import type { Flags } from "./options.ts";
|
|
15
|
-
export type { Flags };
|
|
16
|
-
export interface Bounds { limit: bigint }
|
|
17
|
-
type Enabled = boolean;
|
|
18
|
-
export const label = "bounds" as const, limit = -10n;
|
|
19
|
-
function format(a: bigint) { return String(max(a, 0n)); }
|
|
20
|
-
`).id?.name).toBe("max");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test.each([
|
|
24
|
-
'import "./setup.ts";',
|
|
25
|
-
'export const label = getLabel();',
|
|
26
|
-
'export const { label } = config;',
|
|
27
|
-
'max = (a, b) => b;',
|
|
28
|
-
])("refuses executable module initialization: %s", declaration => {
|
|
29
|
-
expect(() => select(declaration)).toThrow("only named function");
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("checks for a later change to the exported binding inside a sibling", () => {
|
|
33
|
-
expect(() => select("export function reset() { max = (a, b) => b; }")).toThrow("may reassign");
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test("permits member writes, destructuring keys, and local bindings named like the subject", () => {
|
|
37
|
-
expect(select(`
|
|
38
|
-
function format(s: {max: bigint}, x: bigint) {
|
|
39
|
-
s.max = x;
|
|
40
|
-
({max: x} = s);
|
|
41
|
-
for (const max of [x]) { s.max += max; }
|
|
42
|
-
}
|
|
43
|
-
function local(max: bigint) { max++; }
|
|
44
|
-
`).id?.name).toBe("max");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test.each([
|
|
48
|
-
"([{value: max}] = replacements);",
|
|
49
|
-
"({value: max = replacement} = config);",
|
|
50
|
-
"for (max of replacements) {}",
|
|
51
|
-
"(max as typeof max) = replacement;",
|
|
52
|
-
"max! = replacement;",
|
|
53
|
-
])("detects a write through the target %s", statement => {
|
|
54
|
-
expect(() => select(`function reset() { ${statement} }`)).toThrow("may reassign");
|
|
55
|
-
});
|