| 1 | import { transform } from "@effect/doctest/Transform" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | |
| 4 | const imported = `import { assertEquals as __effect_doctest_assert_0 } from "@effect/doctest/Runtime"` |
| 5 | |
| 6 | describe("Transform", () => { |
| 7 | it("transforms expression assertions", () => { |
| 8 | assert.strictEqual( |
| 9 | transform("Option.some(1) // => Option.some(1)", "example.ts", 10), |
| 10 | `__effect_doctest_assert_0(Option.some(1), Option.some(1));\n\n${imported}` |
| 11 | ) |
| 12 | }) |
| 13 | |
| 14 | it("transforms const declaration assertions without evaluating the initializer twice", () => { |
| 15 | assert.strictEqual( |
| 16 | transform("const result = makeValue() // => Option.some(1)\nuse(result)", "example.ts", 10), |
| 17 | `const result = makeValue()\n__effect_doctest_assert_0(result, Option.some(1));\nuse(result)\n\n${imported}` |
| 18 | ) |
| 19 | }) |
| 20 | |
| 21 | it("preserves indentation in nested blocks", () => { |
| 22 | assert.strictEqual( |
| 23 | transform("if (enabled) {\n const result = makeValue() // => 1\n}", "example.ts", 10), |
| 24 | `if (enabled) {\n const result = makeValue()\n __effect_doctest_assert_0(result, 1);\n}\n\n${imported}` |
| 25 | ) |
| 26 | }) |
| 27 | |
| 28 | it("chooses an assertion binding that is not used by the snippet", () => { |
| 29 | const source = "const __effect_doctest_assert_0 = 1\nvalue // => 1" |
| 30 | assert.match(transform(source, "example.ts", 10), /assertEquals as __effect_doctest_assert_1/) |
| 31 | }) |
| 32 | |
| 33 | it("ignores ordinary and legacy output comments", () => { |
| 34 | const source = "console.log(value) // > value\nconst text = \"// => not an assertion\"" |
| 35 | assert.strictEqual(transform(source, "example.ts", 10), source) |
| 36 | }) |
| 37 | |
| 38 | it("rejects malformed expected expressions", () => { |
| 39 | assert.throws( |
| 40 | () => transform("value // => Option.some(", "example.ts", 10), |
| 41 | /example\.ts:11:7 invalid doctest expected expression/ |
| 42 | ) |
| 43 | }) |
| 44 | |
| 45 | it("rejects standalone markers", () => { |
| 46 | assert.throws( |
| 47 | () => transform("value\n// => 1", "example.ts", 10), |
| 48 | /doctest assertion must trail a complete statement on the same line/ |
| 49 | ) |
| 50 | }) |
| 51 | |
| 52 | it("rejects unsupported declarations", () => { |
| 53 | assert.throws( |
| 54 | () => transform("let value = 1 // => 1", "example.ts", 10), |
| 55 | /doctest assertions can only target expression statements or a single initialized const identifier/ |
| 56 | ) |
| 57 | }) |
| 58 | |
| 59 | it("rejects assertions in unbraced control flow", () => { |
| 60 | assert.throws( |
| 61 | () => transform("if (enabled) value // => 1", "example.ts", 10), |
| 62 | /doctest assertions in control flow require an explicit block/ |
| 63 | ) |
| 64 | }) |
| 65 | }) |