summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2025-11-08 14:54:21 -0800
committerKai Stevenson <kai@kaistevenson.com>2025-11-08 15:16:48 -0800
commitb32604eef0cec59798cdfea53b82766819429717 (patch)
tree7267340193ee2cdce306e4e7a9672f96fb0c16e7 /tests
parent0a046893eaffc0d9ef762eb56b48f01bbd79846f (diff)
test generator harnesskai/test-generator
Diffstat (limited to 'tests')
-rw-r--r--tests/type-consistency/generateAll.ts5
-rw-r--r--tests/type-consistency/harness.ts85
-rw-r--r--tests/type-consistency/spec/array.ts17
-rw-r--r--tests/type-consistency/spec/function.ts26
-rw-r--r--tests/type-consistency/spec/index.ts4
5 files changed, 137 insertions, 0 deletions
diff --git a/tests/type-consistency/generateAll.ts b/tests/type-consistency/generateAll.ts
new file mode 100644
index 0000000..15464ae
--- /dev/null
+++ b/tests/type-consistency/generateAll.ts
@@ -0,0 +1,5 @@
+import harnesses from "./spec/index";
+
+Promise.all(harnesses.map((harness) => harness.writeTests())).catch(
+ (e) => `Failed to generate tests: ${e}`
+);
diff --git a/tests/type-consistency/harness.ts b/tests/type-consistency/harness.ts
new file mode 100644
index 0000000..a08e397
--- /dev/null
+++ b/tests/type-consistency/harness.ts
@@ -0,0 +1,85 @@
+import fs from "fs/promises";
+import path from "path";
+
+const convertName = (name: string) => name.replace(" ", "-").toLowerCase();
+
+export const createTestHarness = ({
+ harnessName,
+ generatedPath,
+}: {
+ harnessName: string;
+ generatedPath: string;
+}) => {
+ const tests: string[] = [];
+
+ return {
+ async writeTests() {
+ const file = `import { evaluateProgram, createFn } from "../../../src";
+import { describe, expect, expectTypeOf, test } from "vitest";
+
+describe(\`${harnessName}\`, () => {
+ ${tests.join("\n")}
+ });`;
+
+ await fs.mkdir(generatedPath, { recursive: true });
+
+ await fs.writeFile(
+ path.join(generatedPath, `${convertName(harnessName)}.test.ts`),
+ file,
+ "utf-8"
+ );
+
+ console.log(`Wrote ${tests.length} test(s) for "${harnessName}"!`);
+ },
+
+ createProgramTest({
+ name,
+ program,
+ expected,
+ }: {
+ name: string;
+ program: string;
+ expected: any;
+ }) {
+ tests.push(`test(\`${name}\`, () => {
+ const PROGRAM = \`${program}\` as const;
+ const expected: ${JSON.stringify(expected)} = ${JSON.stringify(expected)};
+ const result = evaluateProgram(PROGRAM);
+
+ expect(result).toStrictEqual(expected);
+ expectTypeOf<typeof result>().toEqualTypeOf<typeof expected>();
+ });`);
+
+ return this;
+ },
+
+ createFunctionTest({
+ name,
+ program,
+ cases,
+ }: {
+ name: string;
+ program: string;
+ cases: Array<{ input: any; output: any }>;
+ }) {
+ tests.push(`describe(\`${name}\`, () => {
+ const PROGRAM = \`${program}\` as const;
+ ${cases.map(
+ ({ input, output }) => `test(\`${JSON.stringify(
+ input
+ )} -> ${JSON.stringify(output)}\`, () => {
+ const input: ${JSON.stringify(input)} = ${JSON.stringify(input)};
+ const expected: ${JSON.stringify(output)} = ${JSON.stringify(output)};
+ const fn = createFn()(PROGRAM)
+ const result = fn(input)
+
+ expect(result).toStrictEqual(expected);
+ expectTypeOf<typeof result>().toEqualTypeOf<typeof expected>();
+ })
+ `
+ )}});`);
+
+ return this;
+ },
+ };
+};
diff --git a/tests/type-consistency/spec/array.ts b/tests/type-consistency/spec/array.ts
new file mode 100644
index 0000000..778a49f
--- /dev/null
+++ b/tests/type-consistency/spec/array.ts
@@ -0,0 +1,17 @@
+import path from "path";
+import { createTestHarness } from "../harness";
+
+export default createTestHarness({
+ harnessName: "Array",
+ generatedPath: path.join(__dirname, "..", "generated"),
+})
+ .createProgramTest({
+ name: "Number array",
+ program: "arr(1,2,3)",
+ expected: [[1, 2, 3]],
+ })
+ .createProgramTest({
+ name: "String array",
+ program: `arr("1","2","3")`,
+ expected: [["1", "2", "3"]],
+ });
diff --git a/tests/type-consistency/spec/function.ts b/tests/type-consistency/spec/function.ts
new file mode 100644
index 0000000..ffd069d
--- /dev/null
+++ b/tests/type-consistency/spec/function.ts
@@ -0,0 +1,26 @@
+import path from "path";
+import { createTestHarness } from "../harness";
+
+export default createTestHarness({
+ harnessName: "Function",
+ generatedPath: path.join(__dirname, "..", "generated"),
+})
+ .createFunctionTest({
+ name: "f(x) = x",
+ program: "fn(x, x)",
+ cases: [
+ { input: "hello", output: "hello" },
+ { input: 1, output: 1 },
+ { input: [1, 2, 3], output: [1, 2, 3] },
+ { input: true, output: true },
+ ],
+ })
+ .createFunctionTest({
+ name: "f(x) = x + 5",
+ program: "fn(x, add(x,5))",
+ cases: [
+ { input: 0, output: 5 },
+ { input: 5, output: 10 },
+ { input: 500, output: 505 },
+ ],
+ });
diff --git a/tests/type-consistency/spec/index.ts b/tests/type-consistency/spec/index.ts
new file mode 100644
index 0000000..9ef825f
--- /dev/null
+++ b/tests/type-consistency/spec/index.ts
@@ -0,0 +1,4 @@
+import array from "./array";
+import functions from "./function";
+
+export default [array, functions];