@yaks/testing 0.0.0-stage → 0.2.36

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.
Files changed (93) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +427 -2
  3. package/_dist/assert.d.ts +40 -0
  4. package/_dist/assert.d.ts.map +1 -0
  5. package/_dist/choose.d.ts +39 -0
  6. package/_dist/choose.d.ts.map +1 -0
  7. package/_dist/clock.d.ts +8 -0
  8. package/_dist/clock.d.ts.map +1 -0
  9. package/_dist/deps.d.ts +113 -0
  10. package/_dist/deps.d.ts.map +1 -0
  11. package/_dist/examples.d.ts +69 -0
  12. package/_dist/examples.d.ts.map +1 -0
  13. package/_dist/find.d.ts +23 -0
  14. package/_dist/find.d.ts.map +1 -0
  15. package/_dist/kept.d.ts +27 -0
  16. package/_dist/kept.d.ts.map +1 -0
  17. package/_dist/lend.d.ts +15 -0
  18. package/_dist/lend.d.ts.map +1 -0
  19. package/_dist/main.d.ts +2 -0
  20. package/_dist/main.d.ts.map +1 -0
  21. package/_dist/mod.d.ts +45 -0
  22. package/_dist/mod.d.ts.map +1 -0
  23. package/_dist/names.d.ts +31 -0
  24. package/_dist/names.d.ts.map +1 -0
  25. package/_dist/own.d.ts +25 -0
  26. package/_dist/own.d.ts.map +1 -0
  27. package/_dist/reads.d.ts +8 -0
  28. package/_dist/reads.d.ts.map +1 -0
  29. package/_dist/suite.d.ts +82 -0
  30. package/_dist/suite.d.ts.map +1 -0
  31. package/_dist/typist.d.ts +24 -0
  32. package/_dist/typist.d.ts.map +1 -0
  33. package/_dist/wait.d.ts +21 -0
  34. package/_dist/wait.d.ts.map +1 -0
  35. package/assert.js +114 -0
  36. package/assert.js.map +1 -0
  37. package/assert.ts +143 -0
  38. package/choose.js +197 -0
  39. package/choose.js.map +1 -0
  40. package/choose.ts +327 -0
  41. package/clock.js +45 -0
  42. package/clock.js.map +1 -0
  43. package/clock.ts +60 -0
  44. package/deno.json +1 -0
  45. package/deps.js +868 -0
  46. package/deps.js.map +1 -0
  47. package/deps.ts +1004 -0
  48. package/drive.js +43 -0
  49. package/drive.js.map +1 -0
  50. package/drive.ts +41 -0
  51. package/examples.js +279 -0
  52. package/examples.js.map +1 -0
  53. package/examples.ts +380 -0
  54. package/find.js +86 -0
  55. package/find.js.map +1 -0
  56. package/find.ts +102 -0
  57. package/kept.js +128 -0
  58. package/kept.js.map +1 -0
  59. package/kept.ts +146 -0
  60. package/lend.js +30 -0
  61. package/lend.js.map +1 -0
  62. package/lend.ts +31 -0
  63. package/load.js +106 -0
  64. package/load.js.map +1 -0
  65. package/load.ts +139 -0
  66. package/main.js +214 -0
  67. package/main.js.map +1 -0
  68. package/main.ts +217 -0
  69. package/mod.js +45 -0
  70. package/mod.js.map +1 -0
  71. package/mod.ts +55 -0
  72. package/names.js +209 -0
  73. package/names.js.map +1 -0
  74. package/names.ts +204 -0
  75. package/own.js +109 -0
  76. package/own.js.map +1 -0
  77. package/own.ts +136 -0
  78. package/package.json +56 -4
  79. package/reads.js +88 -0
  80. package/reads.js.map +1 -0
  81. package/reads.ts +98 -0
  82. package/run.js +306 -0
  83. package/run.js.map +1 -0
  84. package/run.ts +405 -0
  85. package/suite.js +191 -0
  86. package/suite.js.map +1 -0
  87. package/suite.ts +224 -0
  88. package/typist.js +74 -0
  89. package/typist.js.map +1 -0
  90. package/typist.ts +79 -0
  91. package/wait.js +34 -0
  92. package/wait.js.map +1 -0
  93. package/wait.ts +60 -0
package/drive.js ADDED
@@ -0,0 +1,43 @@
1
+ // Drive declared files through the executor. Loading and dependency observation
2
+ // belong to the caller; the executor owns assertions, timing and failures.
3
+ import { apart, end, file } from "./run.js";
4
+ /** Load each file, then run its tests and end it (./run.ts `end`) before
5
+ * loading the next, each file with its own global error events (./run.ts
6
+ * `apart`). Says what loading took in all and each file's load, and what
7
+ * each file took whole.
8
+ * Once `full` says the runtime holds all it should, after a file, the files
9
+ * not yet loaded are left in `rest` for another runtime. */ export let drive = async (paths, load, { timeout = 50_000, verbose = false, full = ()=>false } = {})=>{
10
+ let outcomes = [];
11
+ let loading = 0;
12
+ let loads = {};
13
+ let took = {};
14
+ for (let [i, path] of paths.entries()){
15
+ if (i && await full()) {
16
+ return {
17
+ outcomes,
18
+ loading,
19
+ loads,
20
+ took,
21
+ rest: paths.slice(i)
22
+ };
23
+ }
24
+ console.log(`loading ${path}`);
25
+ apart();
26
+ let begun = performance.now();
27
+ let tests = await load(path);
28
+ let loaded = performance.now() - begun;
29
+ loading += loaded;
30
+ loads[path] = loaded;
31
+ outcomes.push(...await file(path, tests, timeout, loaded, verbose));
32
+ outcomes.push(...await end(path, timeout, verbose));
33
+ took[path] = Math.round(performance.now() - begun);
34
+ }
35
+ return {
36
+ outcomes,
37
+ loading,
38
+ loads,
39
+ took,
40
+ rest: []
41
+ };
42
+ };
43
+ //# sourceMappingURL=drive.js.map
package/drive.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drive.js","sources":["./drive.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,2EAA2E;AAC3E,SAAS,KAAK,EAAE,GAAG,EAAE,IAAI,mBAAgC;AAGzD;;;;;0DAK0D,GAC1D,OAAO,IAAI,QAAQ,OACjB,OACA,MACA,EACE,UAAU,MAAM,EAChB,UAAU,KAAK,EACf,OAAO,IAAkC,KAAK,EAC/C,GAAG,CAAC,CAAC;EAEN,IAAI,WAAsB,EAAE;EAC5B,IAAI,UAAU;EACd,IAAI,QAAgC,CAAC;EACrC,IAAI,OAA+B,CAAC;EACpC,KAAK,IAAI,CAAC,GAAG,KAAK,IAAI,MAAM,OAAO,GAAI;IACrC,IAAI,KAAK,MAAM,QAAQ;MACrB,OAAO;QAAE;QAAU;QAAS;QAAO;QAAM,MAAM,MAAM,KAAK,CAAC;MAAG;IAChE;IACA,QAAQ,GAAG,CAAC,CAAC,QAAQ,EAAE,MAAM;IAC7B;IACA,IAAI,QAAQ,YAAY,GAAG;IAC3B,IAAI,QAAQ,MAAM,KAAK;IACvB,IAAI,SAAS,YAAY,GAAG,KAAK;IACjC,WAAW;IACX,KAAK,CAAC,KAAK,GAAG;IACd,SAAS,IAAI,IAAI,MAAM,KAAK,MAAM,OAAO,SAAS,QAAQ;IAC1D,SAAS,IAAI,IAAI,MAAM,IAAI,MAAM,SAAS;IAC1C,IAAI,CAAC,KAAK,GAAG,KAAK,KAAK,CAAC,YAAY,GAAG,KAAK;EAC9C;EACA,OAAO;IAAE;IAAU;IAAS;IAAO;IAAM,MAAM,EAAE;EAAa;AAChE,EAAC"}
package/drive.ts ADDED
@@ -0,0 +1,41 @@
1
+ // Drive declared files through the executor. Loading and dependency observation
2
+ // belong to the caller; the executor owns assertions, timing and failures.
3
+ import { apart, end, file, type Outcome } from './run.js'
4
+ import type { Test } from './suite.ts'
5
+
6
+ /** Load each file, then run its tests and end it (./run.ts `end`) before
7
+ * loading the next, each file with its own global error events (./run.ts
8
+ * `apart`). Says what loading took in all and each file's load, and what
9
+ * each file took whole.
10
+ * Once `full` says the runtime holds all it should, after a file, the files
11
+ * not yet loaded are left in `rest` for another runtime. */
12
+ export let drive = async (
13
+ paths: string[],
14
+ load: (path: string) => Promise<Test[]>,
15
+ {
16
+ timeout = 50_000,
17
+ verbose = false,
18
+ full = (): boolean | Promise<boolean> => false,
19
+ } = {},
20
+ ) => {
21
+ let outcomes: Outcome[] = []
22
+ let loading = 0
23
+ let loads: Record<string, number> = {}
24
+ let took: Record<string, number> = {}
25
+ for (let [i, path] of paths.entries()) {
26
+ if (i && await full()) {
27
+ return { outcomes, loading, loads, took, rest: paths.slice(i) }
28
+ }
29
+ console.log(`loading ${path}`)
30
+ apart()
31
+ let begun = performance.now()
32
+ let tests = await load(path)
33
+ let loaded = performance.now() - begun
34
+ loading += loaded
35
+ loads[path] = loaded
36
+ outcomes.push(...await file(path, tests, timeout, loaded, verbose))
37
+ outcomes.push(...await end(path, timeout, verbose))
38
+ took[path] = Math.round(performance.now() - begun)
39
+ }
40
+ return { outcomes, loading, loads, took, rest: [] as string[] }
41
+ }
package/examples.js ADDED
@@ -0,0 +1,279 @@
1
+ // The examples written beside the code, run as tests. Two forms:
2
+ //
3
+ // - a doctest line in a module: `/// input -> output` (equality),
4
+ // `/// input ~> pattern` (a partial match, awaited), `/// input throws
5
+ // 'message'`, or a statement (`/// let n = 1`) the file's doctests share.
6
+ // `// /` skips a line, and a line indented past the first continues it;
7
+ // - a fenced ```ts block in a Markdown page or a module's doc comment. A
8
+ // block marked `ignore` is skipped.
9
+ //
10
+ // An example in a module sees that module's exports unimported, as
11
+ // `deno test --doc` gave them. Each fenced example compiles to a module of
12
+ // its own, and a page's doctests to one; each is written beside its page, so
13
+ // its imports resolve exactly as the page's do, and is gone once loaded. Its
14
+ // code keeps the page's line numbers, so a failure points at the page's line.
15
+ /** One doctest: its first line, its kind, and its code, or its two sides. */ /** A fenced test's page and line for failures, and code digest for listings. */ export let fenceName = async (page, fence)=>{
16
+ let digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(fence.code));
17
+ let hash = Array.from(new Uint8Array(digest), (b)=>b.toString(16).padStart(2, '0')).join('');
18
+ return `${page}:${fence.line} ${hash.slice(0, 12)}`;
19
+ };
20
+ let DOCTEST = /^\s*\/\/( ?)\/ (.*)$/;
21
+ /// doctests('/// f(1) -> 2')[0] ~> { line: 1, op: '->', code: 'f(1) -> 2' }
22
+ /// doctests('/// <reference lib="deno.ns" />') -> []
23
+ /// doctests('// / later() -> 2')[0].skip -> true
24
+ /**
25
+ * The doctests in a module's source, a line indented past its first joined
26
+ * to it.
27
+ */ export let doctests = (source)=>{
28
+ if (!source.includes('/// ') && !source.includes('// / ')) return [];
29
+ let out = [];
30
+ for (let [i, text] of source.split('\n').entries()){
31
+ let [, skip, code] = text.match(DOCTEST) ?? [];
32
+ if (code === undefined || code.startsWith('<')) continue;
33
+ let last = out.at(-1);
34
+ if (/^\s/.test(code) && last?.end == i - 1) {
35
+ last.code += `\n${code}`;
36
+ last.end = i;
37
+ } else out.push({
38
+ line: i + 1,
39
+ end: i,
40
+ skip: !!skip,
41
+ code
42
+ });
43
+ }
44
+ return out.map(({ line, skip, code })=>({
45
+ line,
46
+ skip,
47
+ code,
48
+ op: split(code)?.op
49
+ }));
50
+ };
51
+ /// split("f('a -> b') -> 'c -> d'") -> { actual: "f('a -> b')", op: '->', expected: "'c -> d'" }
52
+ /// split('g([1,\n 2])\n -> 3') -> { actual: 'g([1,\n 2])', op: '->', expected: '3' }
53
+ /// split('let n = 1') -> undefined
54
+ /** A doctest's two sides: the first operator outside a string or bracket. */ export let split = (code)=>{
55
+ let depth = 0, quote = '';
56
+ for(let i = 0; i < code.length; i++){
57
+ let c = code[i];
58
+ if (quote) {
59
+ if (c == '\\') i++;
60
+ else if (c == quote) quote = '';
61
+ } else if (`'"\``.includes(c)) quote = c;
62
+ else if ('([{'.includes(c)) depth++;
63
+ else if (')]}'.includes(c)) depth--;
64
+ else if (!depth && /\s/.test(c)) {
65
+ let [hit = '', op] = code.slice(i).match(/^\s+(->|~>|throws)\s/) ?? [];
66
+ if (op == '->' || op == '~>' || op == 'throws') {
67
+ let actual = code.slice(0, i);
68
+ return {
69
+ actual,
70
+ op,
71
+ expected: code.slice(i + hit.length).trimStart()
72
+ };
73
+ }
74
+ }
75
+ }
76
+ };
77
+ let LANGS = /^(ts|tsx|js|jsx|mjs|mts|typescript|javascript)$/;
78
+ /** Whether a page is a module, whose doc comments hold its examples. */ export let module = (path)=>/\.[mc]?[jt]sx?$/.test(path);
79
+ /// fences('```ts\nf()\n```')[0] -> { line: 1, skip: false, lang: 'ts', code: 'f()' }
80
+ /// fences('/**\n * ```ts ignore\n * f()\n * ```\n */', 'a.ts')[0] ~> { line: 2, skip: true }
81
+ /// fences('```json\n{}\n```') -> []
82
+ /**
83
+ * The fenced examples in a page: every block in a Markdown page, and in a
84
+ * module every block inside a doc comment, its ` * ` margin taken off.
85
+ */ export let fences = (source, path = 'page.md')=>{
86
+ if (!source.includes('```')) return [];
87
+ let lines = source.split('\n');
88
+ if (module(path)) {
89
+ let doc = false;
90
+ lines = lines.map((text)=>{
91
+ let open = doc || /^\s*\/\*\*/.test(text);
92
+ doc = open && !text.includes('*/');
93
+ return open ? text.replace(/^\s*(\/\*\*|\*\/|\*(?!\/))? ?/, '') : '';
94
+ });
95
+ }
96
+ let out = [];
97
+ let open;
98
+ let body = [];
99
+ for (let [i, text] of lines.entries()){
100
+ let fence = text.match(/^(\s*)```(\S*)\s*(.*)$/);
101
+ if (open && fence && !fence[2]) {
102
+ let { line, skip, lang } = open;
103
+ out.push({
104
+ line,
105
+ skip,
106
+ lang,
107
+ code: body.join('\n')
108
+ });
109
+ open = undefined;
110
+ } else if (open) body.push(text.slice(open.margin));
111
+ else if (fence && LANGS.test(fence[2])) {
112
+ open = {
113
+ line: i + 1,
114
+ skip: /\bignore\b/.test(fence[3]),
115
+ lang: fence[2],
116
+ margin: fence[1].length
117
+ };
118
+ body = [];
119
+ } else if (fence) {
120
+ // Another language's block, or plain text: skip to its close, so what
121
+ // it holds is never read as a fence of its own.
122
+ let close = lines.findIndex((l, j)=>j > i && /^\s*```\s*$/.test(l));
123
+ lines.fill('', i, close < 0 ? lines.length : close + 1);
124
+ }
125
+ }
126
+ return out;
127
+ };
128
+ export let scan = (source, page)=>({
129
+ fenced: fences(source, page).filter((f)=>!f.skip),
130
+ lines: module(page) ? doctests(source) : []
131
+ });
132
+ // Statements an example opens with that a module holds at its top level.
133
+ let IMPORT = /^import[\s{*'"]/;
134
+ let ENDS = /(^import\s*['"][^'"]+['"]|\bfrom\s*['"][^'"]+['"])(\s+with\s*\{[^}]*\})?\s*;?\s*$/;
135
+ /// unexport('export default worker({') -> 'void worker({'
136
+ /// unexport('export class Shop {') -> 'class Shop {'
137
+ /// unexport("export * from './x.ts'") -> ''
138
+ /**
139
+ * A line of an example as a statement in a function body: what it exported
140
+ * it still declares, a default export is only evaluated, and a re-export is
141
+ * gone.
142
+ */ export let unexport = (line)=>/^export\s+(\*|\{)/.test(line) ? '' : line.replace(/^export\s+default\s+/, 'void ').replace(/^export\s+/, '');
143
+ /// hoist("import { a } from 'x'\nimport {\n b,\n} from 'y'\na(b)") -> { imports: ["import { a } from 'x'", "import {\n b,\n} from 'y'"], rest: ['', '', '', '', 'a(b)'] }
144
+ /** An example's import statements, and the rest of its lines with blanks
145
+ * where the imports stood, so the lines keep their numbers. */ export let hoist = (code)=>{
146
+ let imports = [], rest = [];
147
+ let open;
148
+ for (let text of code.split('\n')){
149
+ if (!open && IMPORT.test(text)) open = [];
150
+ if (open) {
151
+ open.push(text);
152
+ rest.push('');
153
+ if (ENDS.test(text)) {
154
+ imports.push(open.join('\n'));
155
+ open = undefined;
156
+ }
157
+ } else rest.push(unexport(text));
158
+ }
159
+ return {
160
+ imports,
161
+ rest
162
+ };
163
+ };
164
+ /// bound("import d, { a, b as c, type T } from 'x'") -> ['d', 'a', 'c', 'T']
165
+ /// bound("import * as ns from 'x'") -> ['ns']
166
+ /// bound("import 'x'") -> []
167
+ /** The names an import statement binds. */ export let bound = (statement)=>(statement.match(/^import\s+(?:type\s+)?([\s\S]*?)\s*from\s/)?.[1] ?? '').replace(/\*\s+as\s+/, '').replace(/\{([\s\S]*)\}/, (_, list)=>list.split(',').map((s)=>s.trim().replace(/^type\s+/, '').split(/\s+as\s+/).at(-1)).join(',')).split(/[\s,]+/).filter((n)=>/^[A-Za-z_$][\w$]*$/.test(n));
168
+ /// specifiers("import { a } from 'x'\nimport 'y'") -> ['x', 'y']
169
+ /// specifiers("let { ram } = await import('@yaks/ram')") -> ['@yaks/ram']
170
+ /// specifiers('await import(`./${name}.ts`)') -> []
171
+ /** What an example's imports name: its import statements, and each
172
+ * `import(…)` of a literal specifier. */ export let specifiers = (code)=>[
173
+ ...hoist(code).imports.map((s)=>s.match(/(?:^import|\bfrom)\s*['"]([^'"]+)/)?.[1]).filter((s)=>!!s),
174
+ ...[
175
+ ...code.matchAll(/\bimport\(\s*(['"`])([^'"`$]+)\1\s*[,)]/g)
176
+ ].map((m)=>m[2])
177
+ ];
178
+ // Words that cannot be bound by a destructuring pattern.
179
+ let RESERVED = new Set(('break case catch class const continue debugger default delete do else ' + 'enum export extends false finally for function if import in ' + 'instanceof new null return super switch this throw true try typeof ' + 'var void while with yield let static implements interface package ' + 'private protected public await arguments eval').split(' '));
180
+ /** The names a module exports that an example can use unimported. */ export let usable = (exports, taken = [])=>exports.filter((n)=>/^[A-Za-z_$][\w$]*$/.test(n) && !RESERVED.has(n) && !taken.includes(n));
181
+ // The first line of a compiled module: the imports it hoists, all on one
182
+ // line, and the opening of the test's body. `$test` and the checks are
183
+ // named with a `$` so an example's own names never meet them.
184
+ let head = (parts)=>parts.map((p)=>p.replace(/\s*\n\s*/g, ' ')).join('; ');
185
+ /// imported(['a', 'b'], 'm.ts') -> "import { a, b } from './m.ts'"
186
+ /// imported([], 'm.ts') -> "import './m.ts'"
187
+ /**
188
+ * The page, with the exports an example uses unimported imported by name, so
189
+ * each stays the live binding the page's own code assigns. The example's code
190
+ * is a function's body, so it may declare one of those names again.
191
+ */ export let imported = (names, base)=>names.length ? `import { ${names.join(', ')} } from './${base}'` : `import './${base}'`;
192
+ // Lines `from` up to where a page's line `to` stands in a compiled module.
193
+ let pad = (from, to)=>'\n'.repeat(Math.max(1, to - from));
194
+ /** Where the runner's own words are, for a compiled module to import. */ let own = (name)=>new URL(name, import.meta.url).href;
195
+ /**
196
+ * One fenced example as a module that declares it as a test named for its
197
+ * page, line and code digest. `exports` are the page's, for an example in a
198
+ * module.
199
+ */ export let compileFence = (name, page, fence, exports = [])=>{
200
+ let { imports, rest } = hoist(fence.code);
201
+ let base = page.slice(page.lastIndexOf('/') + 1);
202
+ let names = usable(exports, imports.flatMap(bound));
203
+ return head([
204
+ ...imports,
205
+ `import { test as $test } from ${JSON.stringify(own('suite.ts'))}`,
206
+ ...module(page) ? [
207
+ imported(names, base)
208
+ ] : [],
209
+ `$test(${JSON.stringify(name)}, async () => {`
210
+ ]) + pad(1, fence.line + 1) + rest.join('\n') + `\n}, { skip: ${fence.skip} })\n`;
211
+ };
212
+ /// typed('m.ts', { line: 3, skip: false, lang: 'ts', code: "import { b } from 'b'\nf(b)" }, ['f']) -> "import { b } from 'b'; import { f } from './m.ts'; export {}\n\n\n\nf(b)\n"
213
+ /// typed('m.ts', { line: 1, skip: false, lang: 'ts', code: 'declare let f: () => void\nf()' }, ['f']) -> "import './m.ts'; export {}\ndeclare let f: () => void\nf()\n"
214
+ /**
215
+ * One fenced example as a module for a type checker, as `deno check --doc`
216
+ * reads it: its code at the top level, with the page's exports it neither
217
+ * imports nor declares imported, keeping the page's line numbers.
218
+ */ export let typed = (page, fence, exports = [])=>{
219
+ let { imports, rest } = hoist(fence.code);
220
+ let base = page.slice(page.lastIndexOf('/') + 1);
221
+ let names = usable(exports, [
222
+ ...imports.flatMap(bound),
223
+ ...declared(rest.join('\n'))
224
+ ]);
225
+ return head([
226
+ ...imports,
227
+ ...module(page) ? [
228
+ imported(names, base)
229
+ ] : [],
230
+ 'export {}'
231
+ ]) + pad(1, fence.line + 1) + rest.join('\n') + '\n';
232
+ };
233
+ /// declared('let a = 1\nexport async function b() {}\n let c = 2\ndeclare const d: 1') -> ['a', 'b', 'd']
234
+ /** The names code declares at its top level. */ export let declared = (code)=>[
235
+ ...code.matchAll(/^(?:export\s+)?(?:declare\s+)?(?:async\s+)?(?:abstract\s+)?(?:let|const|var|function\*?|class|interface|type|enum|namespace)\s+([A-Za-z_$][\w$]*)/gm)
236
+ ].map((m)=>m[1]);
237
+ /// exported('export let a = 1\nexport async function* b() {}\nexport type C = 1\nlet d = 2') -> ['a', 'b', 'C']
238
+ /// exported("export { a, b as c } from './x.ts'\nexport { a }\nexport default 1") -> ['a', 'c']
239
+ /** The names a module's source exports by declaration or by list, each
240
+ * once, for its examples to use unimported where they cannot load it. */ export let exported = (source)=>[
241
+ ...new Set([
242
+ ...declared(source.replace(/^(?!export\s).*$/gm, '')),
243
+ ...[
244
+ ...source.matchAll(/^export\s+(?:type\s+)?\{([^}]*)\}/gm)
245
+ ].flatMap((m)=>m[1].split(',').map((s)=>s.trim().replace(/^type\s+/, '').split(/\s+as\s+/).at(-1))).filter(Boolean)
246
+ ])
247
+ ];
248
+ /// import { basename as exampleBase } from 'node:path'
249
+ /// exampleBase('/a/b') -> 'b'
250
+ /**
251
+ * A page's doctests as one module: its statements run in order, and each
252
+ * check declares a test named for its line and code.
253
+ */ export let compileDoctests = (page, tests, exports = [])=>{
254
+ let base = page.slice(page.lastIndexOf('/') + 1);
255
+ let lines = 1;
256
+ let body = '';
257
+ let imports = [];
258
+ let at = (line)=>{
259
+ body += pad(lines, line);
260
+ lines = Math.max(lines + 1, line);
261
+ };
262
+ for (let t of tests){
263
+ at(t.line);
264
+ let parts = split(t.code);
265
+ let statement = !parts && !t.skip ? hoist(t.code) : undefined;
266
+ if (statement) imports.push(...statement.imports);
267
+ let check = !parts ? statement?.rest.join('\n') ?? '' : `$test(${JSON.stringify(`${page}:${t.line} ${t.code.split('\n')[0]}`)}, async () => ${parts.op == '->' ? `$equal(${parts.actual}, ${parts.expected})` : parts.op == '~>' ? `$match(await (${parts.actual}), ${parts.expected})` : `$throws(() => ${parts.actual}, ${parts.expected})`}, { skip: ${t.skip} })`;
268
+ body += check;
269
+ lines += t.code.split('\n').length - 1;
270
+ }
271
+ return head([
272
+ ...imports,
273
+ `import { test as $test } from ${JSON.stringify(own('suite.ts'))}`,
274
+ `import { equal as $equal, match as $match, throws as $throws } from ${JSON.stringify(own('assert.ts'))}`,
275
+ imported(usable(exports, imports.flatMap(bound)), base),
276
+ 'await (async () => {'
277
+ ]) + body + '\n})()\n';
278
+ };
279
+ //# sourceMappingURL=examples.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"examples.js","sources":["./examples.ts"],"names":[],"mappings":"AAAA,iEAAiE;AACjE,EAAE;AACF,kEAAkE;AAClE,yEAAyE;AACzE,4EAA4E;AAC5E,0EAA0E;AAC1E,yEAAyE;AACzE,sCAAsC;AACtC,EAAE;AACF,mEAAmE;A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