1import * as Source from "@effect/doctest/Source"
2import { assert, describe, it } from "@effect/vitest"
3
4describe("Source", () => {
5 describe("JSDoc", () => {
6 it("extracts marked TypeScript fences in source order", () => {
7 const source = [
8 "/**",
9 " * First example.",
10 " * ```ts import.meta.vitest name=first",
11 " * const first = 1",
12 " * ```",
13 " */",
14 "const first = 1",
15 "/**",
16 " * ~~~typescript import.meta.vitest name='second example'",
17 " * const second = 2",
18 " * ~~~",
19 " */",
20 "const second = 2"
21 ].join("\n")
22
23 assert.deepStrictEqual(Source.extract(source), [
24 { source: "const first = 1", line: 3, name: "first" },
25 { source: "const second = 2", line: 9, name: "second example" }
26 ])
27 })
28
29 it("ignores fences outside JSDoc comments", () => {
30 const source = [
31 "```ts import.meta.vitest",
32 "const outside = true",
33 "```",
34 "/**",
35 " * ```ts import.meta.vitest",
36 " * const inside = true",
37 " * ```",
38 " */"
39 ].join("\n")
40
41 assert.deepStrictEqual(Source.extract(source), [
42 { source: "const inside = true", line: 5, name: undefined }
43 ])
44 })
45
46 it("ignores unmarked and non-TypeScript fences", () => {
47 const source = [
48 "/**",
49 " * ```ts",
50 " * const unmarked = true",
51 " * ```",
52 " * ```js import.meta.vitest",
53 " * const javascript = true",
54 " * ```",
55 " */"
56 ].join("\n")
57
58 assert.isEmpty(Source.extract(source))
59 })
60
61 it("preserves assertion comments in snippet source", () => {
62 const source = [
63 "/**",
64 " * ```ts import.meta.vitest",
65 " * const value = 1 // => 1",
66 " * ```",
67 " */"
68 ].join("\n")
69
70 assert.deepStrictEqual(Source.extract(source), [{
71 source: "const value = 1 // => 1",
72 line: 2,
73 name: undefined
74 }])
75 })
76
77 it("extracts inline output and ignores explanatory comments", () => {
78 const source = [
79 "/**",
80 " * ```ts import.meta.vitest",
81 " * // Explain the output to the reader.",
82 " * const value = 0 // ordinary inline comment",
83 " * console.log(1) // > 1",
84 " * ```",
85 " */"
86 ].join("\n")
87
88 assert.deepStrictEqual(Source.extract(source), [{
89 source: [
90 "// Explain the output to the reader.",
91 "const value = 0 // ordinary inline comment",
92 "console.log(1) // > 1"
93 ].join("\n"),
94 line: 2,
95 name: undefined
96 }])
97 })
98 })
99
100 describe("Markdown", () => {
101 it("extracts only marked TypeScript fences", () => {
102 const source = [
103 "# Examples",
104 "",
105 "```ts",
106 "const ignored = true",
107 "```",
108 "",
109 "~~~typescript import.meta.vitest name=example",
110 "const value = 1",
111 "~~~"
112 ].join("\n")
113
114 assert.deepStrictEqual(Source.extract(source, "markdown"), [
115 { source: "const value = 1", line: 7, name: "example" }
116 ])
117 })
118 })
119
120 describe("MDX", () => {
121 it("extracts marked TypeScript fences from mdx content", () => {
122 const source = [
123 "import { SomeComponent } from './component'",
124 "",
125 "## Example",
126 "",
127 "<SomeComponent />",
128 "",
129 "```ts import.meta.vitest",
130 "const result = 42",
131 "```"
132 ].join("\n")
133
134 assert.deepStrictEqual(Source.extract(source, "markdown"), [
135 { source: "const result = 42", line: 7, name: undefined }
136 ])
137 })
138 })
139})