summaryrefslogtreecommitdiff
path: root/tests/type-consistency/harness.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tests/type-consistency/harness.ts')
-rw-r--r--tests/type-consistency/harness.ts85
1 files changed, 85 insertions, 0 deletions
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;
+ },
+ };
+};