@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.
- package/LICENSE +202 -0
- package/README.md +427 -2
- package/_dist/assert.d.ts +40 -0
- package/_dist/assert.d.ts.map +1 -0
- package/_dist/choose.d.ts +39 -0
- package/_dist/choose.d.ts.map +1 -0
- package/_dist/clock.d.ts +8 -0
- package/_dist/clock.d.ts.map +1 -0
- package/_dist/deps.d.ts +113 -0
- package/_dist/deps.d.ts.map +1 -0
- package/_dist/examples.d.ts +69 -0
- package/_dist/examples.d.ts.map +1 -0
- package/_dist/find.d.ts +23 -0
- package/_dist/find.d.ts.map +1 -0
- package/_dist/kept.d.ts +27 -0
- package/_dist/kept.d.ts.map +1 -0
- package/_dist/lend.d.ts +15 -0
- package/_dist/lend.d.ts.map +1 -0
- package/_dist/main.d.ts +2 -0
- package/_dist/main.d.ts.map +1 -0
- package/_dist/mod.d.ts +45 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/names.d.ts +31 -0
- package/_dist/names.d.ts.map +1 -0
- package/_dist/own.d.ts +25 -0
- package/_dist/own.d.ts.map +1 -0
- package/_dist/reads.d.ts +8 -0
- package/_dist/reads.d.ts.map +1 -0
- package/_dist/suite.d.ts +82 -0
- package/_dist/suite.d.ts.map +1 -0
- package/_dist/typist.d.ts +24 -0
- package/_dist/typist.d.ts.map +1 -0
- package/_dist/wait.d.ts +21 -0
- package/_dist/wait.d.ts.map +1 -0
- package/assert.js +114 -0
- package/assert.js.map +1 -0
- package/assert.ts +143 -0
- package/choose.js +197 -0
- package/choose.js.map +1 -0
- package/choose.ts +327 -0
- package/clock.js +45 -0
- package/clock.js.map +1 -0
- package/clock.ts +60 -0
- package/deno.json +1 -0
- package/deps.js +868 -0
- package/deps.js.map +1 -0
- package/deps.ts +1004 -0
- package/drive.js +43 -0
- package/drive.js.map +1 -0
- package/drive.ts +41 -0
- package/examples.js +279 -0
- package/examples.js.map +1 -0
- package/examples.ts +380 -0
- package/find.js +86 -0
- package/find.js.map +1 -0
- package/find.ts +102 -0
- package/kept.js +128 -0
- package/kept.js.map +1 -0
- package/kept.ts +146 -0
- package/lend.js +30 -0
- package/lend.js.map +1 -0
- package/lend.ts +31 -0
- package/load.js +106 -0
- package/load.js.map +1 -0
- package/load.ts +139 -0
- package/main.js +214 -0
- package/main.js.map +1 -0
- package/main.ts +217 -0
- package/mod.js +45 -0
- package/mod.js.map +1 -0
- package/mod.ts +55 -0
- package/names.js +209 -0
- package/names.js.map +1 -0
- package/names.ts +204 -0
- package/own.js +109 -0
- package/own.js.map +1 -0
- package/own.ts +136 -0
- package/package.json +56 -4
- package/reads.js +88 -0
- package/reads.js.map +1 -0
- package/reads.ts +98 -0
- package/run.js +306 -0
- package/run.js.map +1 -0
- package/run.ts +405 -0
- package/suite.js +191 -0
- package/suite.js.map +1 -0
- package/suite.ts +224 -0
- package/typist.js +74 -0
- package/typist.js.map +1 -0
- package/typist.ts +79 -0
- package/wait.js +34 -0
- package/wait.js.map +1 -0
- package/wait.ts +60 -0
package/examples.ts
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
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
|
+
|
|
16
|
+
/** One doctest: its first line, its kind, and its code, or its two sides. */
|
|
17
|
+
export type Doctest = {
|
|
18
|
+
line: number
|
|
19
|
+
skip: boolean
|
|
20
|
+
op?: '->' | '~>' | 'throws'
|
|
21
|
+
code: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** One fenced example: the line its fence opens on, and its code. */
|
|
25
|
+
export type Fence = { line: number; skip: boolean; lang: string; code: string }
|
|
26
|
+
|
|
27
|
+
/** A fenced test's page and line for failures, and code digest for listings. */
|
|
28
|
+
export let fenceName = async (page: string, fence: Fence): Promise<string> => {
|
|
29
|
+
let digest = await crypto.subtle.digest(
|
|
30
|
+
'SHA-256',
|
|
31
|
+
new TextEncoder().encode(fence.code),
|
|
32
|
+
)
|
|
33
|
+
let hash = Array.from(
|
|
34
|
+
new Uint8Array(digest),
|
|
35
|
+
(b) => b.toString(16).padStart(2, '0'),
|
|
36
|
+
).join('')
|
|
37
|
+
return `${page}:${fence.line} ${hash.slice(0, 12)}`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let DOCTEST = /^\s*\/\/( ?)\/ (.*)$/
|
|
41
|
+
|
|
42
|
+
/// doctests('/// f(1) -> 2')[0] ~> { line: 1, op: '->', code: 'f(1) -> 2' }
|
|
43
|
+
/// doctests('/// <reference lib="deno.ns" />') -> []
|
|
44
|
+
/// doctests('// / later() -> 2')[0].skip -> true
|
|
45
|
+
/**
|
|
46
|
+
* The doctests in a module's source, a line indented past its first joined
|
|
47
|
+
* to it.
|
|
48
|
+
*/
|
|
49
|
+
export let doctests = (source: string): Doctest[] => {
|
|
50
|
+
if (!source.includes('/// ') && !source.includes('// / ')) return []
|
|
51
|
+
let out: (Doctest & { end: number })[] = []
|
|
52
|
+
for (let [i, text] of source.split('\n').entries()) {
|
|
53
|
+
let [, skip, code] = text.match(DOCTEST) ?? []
|
|
54
|
+
if (code === undefined || code.startsWith('<')) continue
|
|
55
|
+
let last = out.at(-1)
|
|
56
|
+
if (/^\s/.test(code) && last?.end == i - 1) {
|
|
57
|
+
last.code += `\n${code}`
|
|
58
|
+
last.end = i
|
|
59
|
+
} else out.push({ line: i + 1, end: i, skip: !!skip, code })
|
|
60
|
+
}
|
|
61
|
+
return out.map(({ line, skip, code }) => ({
|
|
62
|
+
line,
|
|
63
|
+
skip,
|
|
64
|
+
code,
|
|
65
|
+
op: split(code)?.op,
|
|
66
|
+
}))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// split("f('a -> b') -> 'c -> d'") -> { actual: "f('a -> b')", op: '->', expected: "'c -> d'" }
|
|
70
|
+
/// split('g([1,\n 2])\n -> 3') -> { actual: 'g([1,\n 2])', op: '->', expected: '3' }
|
|
71
|
+
/// split('let n = 1') -> undefined
|
|
72
|
+
/** A doctest's two sides: the first operator outside a string or bracket. */
|
|
73
|
+
export let split = (
|
|
74
|
+
code: string,
|
|
75
|
+
): { actual: string; op: Doctest['op']; expected: string } | undefined => {
|
|
76
|
+
let depth = 0, quote = ''
|
|
77
|
+
for (let i = 0; i < code.length; i++) {
|
|
78
|
+
let c = code[i]
|
|
79
|
+
if (quote) {
|
|
80
|
+
if (c == '\\') i++
|
|
81
|
+
else if (c == quote) quote = ''
|
|
82
|
+
} else if (`'"\``.includes(c)) quote = c
|
|
83
|
+
else if ('([{'.includes(c)) depth++
|
|
84
|
+
else if (')]}'.includes(c)) depth--
|
|
85
|
+
else if (!depth && /\s/.test(c)) {
|
|
86
|
+
let [hit = '', op] = code.slice(i).match(/^\s+(->|~>|throws)\s/) ?? []
|
|
87
|
+
if (op == '->' || op == '~>' || op == 'throws') {
|
|
88
|
+
let actual = code.slice(0, i)
|
|
89
|
+
return { actual, op, expected: code.slice(i + hit.length).trimStart() }
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let LANGS = /^(ts|tsx|js|jsx|mjs|mts|typescript|javascript)$/
|
|
96
|
+
|
|
97
|
+
/** Whether a page is a module, whose doc comments hold its examples. */
|
|
98
|
+
export let module = (path: string): boolean => /\.[mc]?[jt]sx?$/.test(path)
|
|
99
|
+
|
|
100
|
+
/// fences('```ts\nf()\n```')[0] -> { line: 1, skip: false, lang: 'ts', code: 'f()' }
|
|
101
|
+
/// fences('/**\n * ```ts ignore\n * f()\n * ```\n */', 'a.ts')[0] ~> { line: 2, skip: true }
|
|
102
|
+
/// fences('```json\n{}\n```') -> []
|
|
103
|
+
/**
|
|
104
|
+
* The fenced examples in a page: every block in a Markdown page, and in a
|
|
105
|
+
* module every block inside a doc comment, its ` * ` margin taken off.
|
|
106
|
+
*/
|
|
107
|
+
export let fences = (source: string, path = 'page.md'): Fence[] => {
|
|
108
|
+
if (!source.includes('```')) return []
|
|
109
|
+
let lines = source.split('\n')
|
|
110
|
+
if (module(path)) {
|
|
111
|
+
let doc = false
|
|
112
|
+
lines = lines.map((text) => {
|
|
113
|
+
let open = doc || /^\s*\/\*\*/.test(text)
|
|
114
|
+
doc = open && !text.includes('*/')
|
|
115
|
+
return open ? text.replace(/^\s*(\/\*\*|\*\/|\*(?!\/))? ?/, '') : ''
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
let out: Fence[] = []
|
|
119
|
+
let open:
|
|
120
|
+
| { line: number; skip: boolean; lang: string; margin: number }
|
|
121
|
+
| undefined
|
|
122
|
+
let body: string[] = []
|
|
123
|
+
for (let [i, text] of lines.entries()) {
|
|
124
|
+
let fence = text.match(/^(\s*)```(\S*)\s*(.*)$/)
|
|
125
|
+
if (open && fence && !fence[2]) {
|
|
126
|
+
let { line, skip, lang } = open
|
|
127
|
+
out.push({ line, skip, lang, code: body.join('\n') })
|
|
128
|
+
open = undefined
|
|
129
|
+
} else if (open) body.push(text.slice(open.margin))
|
|
130
|
+
else if (fence && LANGS.test(fence[2])) {
|
|
131
|
+
open = {
|
|
132
|
+
line: i + 1,
|
|
133
|
+
skip: /\bignore\b/.test(fence[3]),
|
|
134
|
+
lang: fence[2],
|
|
135
|
+
margin: fence[1].length,
|
|
136
|
+
}
|
|
137
|
+
body = []
|
|
138
|
+
} else if (fence) {
|
|
139
|
+
// Another language's block, or plain text: skip to its close, so what
|
|
140
|
+
// it holds is never read as a fence of its own.
|
|
141
|
+
let close = lines.findIndex((l, j) => j > i && /^\s*```\s*$/.test(l))
|
|
142
|
+
lines.fill('', i, close < 0 ? lines.length : close + 1)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return out
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** A page's runnable fences and doctest lines, parsed once for a command. */
|
|
149
|
+
export type Examples = { fenced: Fence[]; lines: Doctest[] }
|
|
150
|
+
export let scan = (source: string, page: string): Examples => ({
|
|
151
|
+
fenced: fences(source, page).filter((f) => !f.skip),
|
|
152
|
+
lines: module(page) ? doctests(source) : [],
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
// Statements an example opens with that a module holds at its top level.
|
|
156
|
+
let IMPORT = /^import[\s{*'"]/
|
|
157
|
+
let ENDS =
|
|
158
|
+
/(^import\s*['"][^'"]+['"]|\bfrom\s*['"][^'"]+['"])(\s+with\s*\{[^}]*\})?\s*;?\s*$/
|
|
159
|
+
|
|
160
|
+
/// unexport('export default worker({') -> 'void worker({'
|
|
161
|
+
/// unexport('export class Shop {') -> 'class Shop {'
|
|
162
|
+
/// unexport("export * from './x.ts'") -> ''
|
|
163
|
+
/**
|
|
164
|
+
* A line of an example as a statement in a function body: what it exported
|
|
165
|
+
* it still declares, a default export is only evaluated, and a re-export is
|
|
166
|
+
* gone.
|
|
167
|
+
*/
|
|
168
|
+
export let unexport = (line: string): string =>
|
|
169
|
+
/^export\s+(\*|\{)/.test(line)
|
|
170
|
+
? ''
|
|
171
|
+
: line.replace(/^export\s+default\s+/, 'void ').replace(/^export\s+/, '')
|
|
172
|
+
|
|
173
|
+
/// 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)'] }
|
|
174
|
+
/** An example's import statements, and the rest of its lines with blanks
|
|
175
|
+
* where the imports stood, so the lines keep their numbers. */
|
|
176
|
+
export let hoist = (code: string): { imports: string[]; rest: string[] } => {
|
|
177
|
+
let imports: string[] = [], rest: string[] = []
|
|
178
|
+
let open: string[] | undefined
|
|
179
|
+
for (let text of code.split('\n')) {
|
|
180
|
+
if (!open && IMPORT.test(text)) open = []
|
|
181
|
+
if (open) {
|
|
182
|
+
open.push(text)
|
|
183
|
+
rest.push('')
|
|
184
|
+
if (ENDS.test(text)) {
|
|
185
|
+
imports.push(open.join('\n'))
|
|
186
|
+
open = undefined
|
|
187
|
+
}
|
|
188
|
+
} else rest.push(unexport(text))
|
|
189
|
+
}
|
|
190
|
+
return { imports, rest }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/// bound("import d, { a, b as c, type T } from 'x'") -> ['d', 'a', 'c', 'T']
|
|
194
|
+
/// bound("import * as ns from 'x'") -> ['ns']
|
|
195
|
+
/// bound("import 'x'") -> []
|
|
196
|
+
/** The names an import statement binds. */
|
|
197
|
+
export let bound = (statement: string): string[] =>
|
|
198
|
+
(statement.match(/^import\s+(?:type\s+)?([\s\S]*?)\s*from\s/)?.[1] ?? '')
|
|
199
|
+
.replace(/\*\s+as\s+/, '')
|
|
200
|
+
.replace(
|
|
201
|
+
/\{([\s\S]*)\}/,
|
|
202
|
+
(_, list: string) =>
|
|
203
|
+
list.split(',').map((s) =>
|
|
204
|
+
s.trim().replace(/^type\s+/, '').split(/\s+as\s+/).at(-1)
|
|
205
|
+
).join(','),
|
|
206
|
+
)
|
|
207
|
+
.split(/[\s,]+/).filter((n) => /^[A-Za-z_$][\w$]*$/.test(n))
|
|
208
|
+
|
|
209
|
+
/// specifiers("import { a } from 'x'\nimport 'y'") -> ['x', 'y']
|
|
210
|
+
/// specifiers("let { ram } = await import('@yaks/ram')") -> ['@yaks/ram']
|
|
211
|
+
/// specifiers('await import(`./${name}.ts`)') -> []
|
|
212
|
+
/** What an example's imports name: its import statements, and each
|
|
213
|
+
* `import(…)` of a literal specifier. */
|
|
214
|
+
export let specifiers = (code: string): string[] => [
|
|
215
|
+
...hoist(code).imports.map((s) =>
|
|
216
|
+
s.match(/(?:^import|\bfrom)\s*['"]([^'"]+)/)?.[1]
|
|
217
|
+
).filter((s): s is string => !!s),
|
|
218
|
+
...[...code.matchAll(/\bimport\(\s*(['"`])([^'"`$]+)\1\s*[,)]/g)]
|
|
219
|
+
.map((m) => m[2]),
|
|
220
|
+
]
|
|
221
|
+
|
|
222
|
+
// Words that cannot be bound by a destructuring pattern.
|
|
223
|
+
let RESERVED: Set<string> = new Set(
|
|
224
|
+
('break case catch class const continue debugger default delete do else ' +
|
|
225
|
+
'enum export extends false finally for function if import in ' +
|
|
226
|
+
'instanceof new null return super switch this throw true try typeof ' +
|
|
227
|
+
'var void while with yield let static implements interface package ' +
|
|
228
|
+
'private protected public await arguments eval').split(' '),
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
/** The names a module exports that an example can use unimported. */
|
|
232
|
+
export let usable = (exports: string[], taken: string[] = []): string[] =>
|
|
233
|
+
exports.filter((n) =>
|
|
234
|
+
/^[A-Za-z_$][\w$]*$/.test(n) && !RESERVED.has(n) && !taken.includes(n)
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
// The first line of a compiled module: the imports it hoists, all on one
|
|
238
|
+
// line, and the opening of the test's body. `$test` and the checks are
|
|
239
|
+
// named with a `$` so an example's own names never meet them.
|
|
240
|
+
let head = (parts: string[]) =>
|
|
241
|
+
parts.map((p) => p.replace(/\s*\n\s*/g, ' '))
|
|
242
|
+
.join('; ')
|
|
243
|
+
|
|
244
|
+
/// imported(['a', 'b'], 'm.ts') -> "import { a, b } from './m.ts'"
|
|
245
|
+
/// imported([], 'm.ts') -> "import './m.ts'"
|
|
246
|
+
/**
|
|
247
|
+
* The page, with the exports an example uses unimported imported by name, so
|
|
248
|
+
* each stays the live binding the page's own code assigns. The example's code
|
|
249
|
+
* is a function's body, so it may declare one of those names again.
|
|
250
|
+
*/
|
|
251
|
+
export let imported = (names: string[], base: string): string =>
|
|
252
|
+
names.length
|
|
253
|
+
? `import { ${names.join(', ')} } from './${base}'`
|
|
254
|
+
: `import './${base}'`
|
|
255
|
+
|
|
256
|
+
// Lines `from` up to where a page's line `to` stands in a compiled module.
|
|
257
|
+
let pad = (from: number, to: number) => '\n'.repeat(Math.max(1, to - from))
|
|
258
|
+
|
|
259
|
+
/** Where the runner's own words are, for a compiled module to import. */
|
|
260
|
+
let own = (name: string) => new URL(name, import.meta.url).href
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* One fenced example as a module that declares it as a test named for its
|
|
264
|
+
* page, line and code digest. `exports` are the page's, for an example in a
|
|
265
|
+
* module.
|
|
266
|
+
*/
|
|
267
|
+
export let compileFence = (
|
|
268
|
+
name: string,
|
|
269
|
+
page: string,
|
|
270
|
+
fence: Fence,
|
|
271
|
+
exports: string[] = [],
|
|
272
|
+
): string => {
|
|
273
|
+
let { imports, rest } = hoist(fence.code)
|
|
274
|
+
let base = page.slice(page.lastIndexOf('/') + 1)
|
|
275
|
+
let names = usable(exports, imports.flatMap(bound))
|
|
276
|
+
return head([
|
|
277
|
+
...imports,
|
|
278
|
+
`import { test as $test } from ${JSON.stringify(own('suite.ts'))}`,
|
|
279
|
+
...module(page) ? [imported(names, base)] : [],
|
|
280
|
+
`$test(${JSON.stringify(name)}, async () => {`,
|
|
281
|
+
]) + pad(1, fence.line + 1) + rest.join('\n') +
|
|
282
|
+
`\n}, { skip: ${fence.skip} })\n`
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/// 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"
|
|
286
|
+
/// 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"
|
|
287
|
+
/**
|
|
288
|
+
* One fenced example as a module for a type checker, as `deno check --doc`
|
|
289
|
+
* reads it: its code at the top level, with the page's exports it neither
|
|
290
|
+
* imports nor declares imported, keeping the page's line numbers.
|
|
291
|
+
*/
|
|
292
|
+
export let typed = (
|
|
293
|
+
page: string,
|
|
294
|
+
fence: Fence,
|
|
295
|
+
exports: string[] = [],
|
|
296
|
+
): string => {
|
|
297
|
+
let { imports, rest } = hoist(fence.code)
|
|
298
|
+
let base = page.slice(page.lastIndexOf('/') + 1)
|
|
299
|
+
let names = usable(exports, [
|
|
300
|
+
...imports.flatMap(bound),
|
|
301
|
+
...declared(rest.join('\n')),
|
|
302
|
+
])
|
|
303
|
+
return head([
|
|
304
|
+
...imports,
|
|
305
|
+
...module(page) ? [imported(names, base)] : [],
|
|
306
|
+
'export {}',
|
|
307
|
+
]) + pad(1, fence.line + 1) + rest.join('\n') + '\n'
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/// declared('let a = 1\nexport async function b() {}\n let c = 2\ndeclare const d: 1') -> ['a', 'b', 'd']
|
|
311
|
+
/** The names code declares at its top level. */
|
|
312
|
+
export let declared = (code: string): string[] =>
|
|
313
|
+
[...code.matchAll(
|
|
314
|
+
/^(?:export\s+)?(?:declare\s+)?(?:async\s+)?(?:abstract\s+)?(?:let|const|var|function\*?|class|interface|type|enum|namespace)\s+([A-Za-z_$][\w$]*)/gm,
|
|
315
|
+
)].map((m) => m[1])
|
|
316
|
+
|
|
317
|
+
/// exported('export let a = 1\nexport async function* b() {}\nexport type C = 1\nlet d = 2') -> ['a', 'b', 'C']
|
|
318
|
+
/// exported("export { a, b as c } from './x.ts'\nexport { a }\nexport default 1") -> ['a', 'c']
|
|
319
|
+
/** The names a module's source exports by declaration or by list, each
|
|
320
|
+
* once, for its examples to use unimported where they cannot load it. */
|
|
321
|
+
export let exported = (source: string): string[] => [
|
|
322
|
+
...new Set([
|
|
323
|
+
...declared(source.replace(/^(?!export\s).*$/gm, '')),
|
|
324
|
+
...[...source.matchAll(/^export\s+(?:type\s+)?\{([^}]*)\}/gm)]
|
|
325
|
+
.flatMap((m) =>
|
|
326
|
+
m[1].split(',').map((s) =>
|
|
327
|
+
s.trim().replace(/^type\s+/, '').split(/\s+as\s+/).at(-1)!
|
|
328
|
+
)
|
|
329
|
+
).filter(Boolean),
|
|
330
|
+
]),
|
|
331
|
+
]
|
|
332
|
+
|
|
333
|
+
/// import { basename as exampleBase } from 'node:path'
|
|
334
|
+
/// exampleBase('/a/b') -> 'b'
|
|
335
|
+
/**
|
|
336
|
+
* A page's doctests as one module: its statements run in order, and each
|
|
337
|
+
* check declares a test named for its line and code.
|
|
338
|
+
*/
|
|
339
|
+
export let compileDoctests = (
|
|
340
|
+
page: string,
|
|
341
|
+
tests: Doctest[],
|
|
342
|
+
exports: string[] = [],
|
|
343
|
+
): string => {
|
|
344
|
+
let base = page.slice(page.lastIndexOf('/') + 1)
|
|
345
|
+
let lines = 1
|
|
346
|
+
let body = ''
|
|
347
|
+
let imports: string[] = []
|
|
348
|
+
let at = (line: number) => {
|
|
349
|
+
body += pad(lines, line)
|
|
350
|
+
lines = Math.max(lines + 1, line)
|
|
351
|
+
}
|
|
352
|
+
for (let t of tests) {
|
|
353
|
+
at(t.line)
|
|
354
|
+
let parts = split(t.code)
|
|
355
|
+
let statement = !parts && !t.skip ? hoist(t.code) : undefined
|
|
356
|
+
if (statement) imports.push(...statement.imports)
|
|
357
|
+
let check = !parts
|
|
358
|
+
? statement?.rest.join('\n') ?? ''
|
|
359
|
+
: `$test(${
|
|
360
|
+
JSON.stringify(`${page}:${t.line} ${t.code.split('\n')[0]}`)
|
|
361
|
+
}, async () => ${
|
|
362
|
+
parts.op == '->'
|
|
363
|
+
? `$equal(${parts.actual}, ${parts.expected})`
|
|
364
|
+
: parts.op == '~>'
|
|
365
|
+
? `$match(await (${parts.actual}), ${parts.expected})`
|
|
366
|
+
: `$throws(() => ${parts.actual}, ${parts.expected})`
|
|
367
|
+
}, { skip: ${t.skip} })`
|
|
368
|
+
body += check
|
|
369
|
+
lines += t.code.split('\n').length - 1
|
|
370
|
+
}
|
|
371
|
+
return head([
|
|
372
|
+
...imports,
|
|
373
|
+
`import { test as $test } from ${JSON.stringify(own('suite.ts'))}`,
|
|
374
|
+
`import { equal as $equal, match as $match, throws as $throws } from ${
|
|
375
|
+
JSON.stringify(own('assert.ts'))
|
|
376
|
+
}`,
|
|
377
|
+
imported(usable(exports, imports.flatMap(bound)), base),
|
|
378
|
+
'await (async () => {',
|
|
379
|
+
]) + body + '\n})()\n'
|
|
380
|
+
}
|
package/find.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import * as io from "@yaks/file/io";
|
|
2
|
+
// What a run is given: test modules, and the pages whose examples it runs.
|
|
3
|
+
// A path names a file or a directory; a directory is walked, never into
|
|
4
|
+
// another project's code (`node_modules`, `vendor`, a build's scratch) or a
|
|
5
|
+
// hidden one.
|
|
6
|
+
import { module, scan } from "./examples.js";
|
|
7
|
+
import { byStatus, stores } from "./kept.js";
|
|
8
|
+
let SKIP = [
|
|
9
|
+
'vendor',
|
|
10
|
+
'node_modules',
|
|
11
|
+
'.wrangler'
|
|
12
|
+
];
|
|
13
|
+
/// tested('a/b_test.ts') -> true
|
|
14
|
+
/// tested('a/b.ts') -> false
|
|
15
|
+
/** Whether a path is a test module. */ export let tested = (path)=>/_test\.[mc]?[jt]sx?$/.test(path);
|
|
16
|
+
/// paged('a/README.md') -> true
|
|
17
|
+
/// paged('a/b.ts') -> true
|
|
18
|
+
/// paged('a/b_test.ts') -> false
|
|
19
|
+
/// paged('a/b.json') -> false
|
|
20
|
+
/** Whether a path could hold examples: a module or a Markdown page. */ export let paged = (path)=>!tested(path) && (module(path) || path.endsWith('.md'));
|
|
21
|
+
/// written('.README.md.plan.4242.example.ts') -> 4242
|
|
22
|
+
/// written('.mod.ts.3.4242.example.tsx') -> 4242
|
|
23
|
+
/// written('README.md') -> undefined
|
|
24
|
+
/** The process that wrote a file beside a page while the page was keyed or
|
|
25
|
+
* its examples loaded (./load.ts `beside`), by the file's name; none for any
|
|
26
|
+
* other file. */ export let written = (name)=>{
|
|
27
|
+
let pid = name.match(/^\..+\.(\d+)\.example\.[jt]sx?$/)?.[1];
|
|
28
|
+
return pid ? Number(pid) : undefined;
|
|
29
|
+
};
|
|
30
|
+
/** Every file under the paths, sorted, each once. */ export let walk = (paths)=>Promise.try(()=>{
|
|
31
|
+
let out = new Set();
|
|
32
|
+
let visited = new Set();
|
|
33
|
+
let go = (path)=>{
|
|
34
|
+
if (visited.has(path)) return;
|
|
35
|
+
visited.add(path);
|
|
36
|
+
let info = io.statSync(path);
|
|
37
|
+
if (info.isFile) return void out.add(path);
|
|
38
|
+
for (let e of io.readDirSync(path)){
|
|
39
|
+
if (e.name.startsWith('.') || SKIP.includes(e.name)) continue;
|
|
40
|
+
let at = `${path}/${e.name}`;
|
|
41
|
+
if (e.isDirectory) go(at);
|
|
42
|
+
else if (e.isFile) out.add(at);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
for (let p of paths)go(p.replace(/^\.\//, '').replace(/\/+$/, ''));
|
|
46
|
+
return [
|
|
47
|
+
...out
|
|
48
|
+
].sort();
|
|
49
|
+
});
|
|
50
|
+
/** Whether a page's source holds an example that runs. */ export let examples = (path, source)=>{
|
|
51
|
+
let { fenced, lines } = scan(source, path);
|
|
52
|
+
return !!(fenced.length || lines.length);
|
|
53
|
+
};
|
|
54
|
+
/** Where what each page of a checkout held, as last scanned, is kept by its
|
|
55
|
+
* status (`discover`). */ export let PAGES = `${stores()}/pages.json`;
|
|
56
|
+
/** Test modules, example pages and their parsed inputs for one command.
|
|
57
|
+
* Given a `store`, what each page holds is kept there by the page's status
|
|
58
|
+
* (./kept.ts `byStatus`), and a page unchanged since is not read again. */ export let discover = async (paths, store)=>{
|
|
59
|
+
let files = await walk(paths);
|
|
60
|
+
let known = byStatus(store);
|
|
61
|
+
let parsed = new Map();
|
|
62
|
+
for (let file of files.filter(paged)){
|
|
63
|
+
let examples = known.of(file, io.statSync(file), ()=>{
|
|
64
|
+
let found = scan(io.readTextFileSync(file), file);
|
|
65
|
+
return found.fenced.length || found.lines.length ? found : 0;
|
|
66
|
+
});
|
|
67
|
+
if (examples) parsed.set(file, examples);
|
|
68
|
+
}
|
|
69
|
+
let roots = paths.map((p)=>p.replace(/^\.\//, '').replace(/\/+$/, ''));
|
|
70
|
+
known.save((file)=>!roots.some((r)=>file == r || file.startsWith(`${r}/`)));
|
|
71
|
+
return {
|
|
72
|
+
tests: files.filter(tested),
|
|
73
|
+
pages: [
|
|
74
|
+
...parsed.keys()
|
|
75
|
+
],
|
|
76
|
+
parsed
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
/** The test modules and the example pages under the paths. */ export let find = async (paths)=>{
|
|
80
|
+
let { tests, pages } = await discover(paths);
|
|
81
|
+
return {
|
|
82
|
+
tests,
|
|
83
|
+
pages
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=find.js.map
|
package/find.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"find.js","sources":["./find.ts"],"names":[],"mappings":"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"}
|
package/find.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import * as io from '@yaks/file/io'
|
|
2
|
+
// What a run is given: test modules, and the pages whose examples it runs.
|
|
3
|
+
// A path names a file or a directory; a directory is walked, never into
|
|
4
|
+
// another project's code (`node_modules`, `vendor`, a build's scratch) or a
|
|
5
|
+
// hidden one.
|
|
6
|
+
import { type Examples, module, scan } from './examples.js'
|
|
7
|
+
import { byStatus, stores } from './kept.js'
|
|
8
|
+
|
|
9
|
+
let SKIP = ['vendor', 'node_modules', '.wrangler']
|
|
10
|
+
|
|
11
|
+
/// tested('a/b_test.ts') -> true
|
|
12
|
+
/// tested('a/b.ts') -> false
|
|
13
|
+
/** Whether a path is a test module. */
|
|
14
|
+
export let tested = (path: string): boolean => /_test\.[mc]?[jt]sx?$/.test(path)
|
|
15
|
+
|
|
16
|
+
/// paged('a/README.md') -> true
|
|
17
|
+
/// paged('a/b.ts') -> true
|
|
18
|
+
/// paged('a/b_test.ts') -> false
|
|
19
|
+
/// paged('a/b.json') -> false
|
|
20
|
+
/** Whether a path could hold examples: a module or a Markdown page. */
|
|
21
|
+
export let paged = (path: string): boolean =>
|
|
22
|
+
!tested(path) && (module(path) || path.endsWith('.md'))
|
|
23
|
+
|
|
24
|
+
/// written('.README.md.plan.4242.example.ts') -> 4242
|
|
25
|
+
/// written('.mod.ts.3.4242.example.tsx') -> 4242
|
|
26
|
+
/// written('README.md') -> undefined
|
|
27
|
+
/** The process that wrote a file beside a page while the page was keyed or
|
|
28
|
+
* its examples loaded (./load.ts `beside`), by the file's name; none for any
|
|
29
|
+
* other file. */
|
|
30
|
+
export let written = (name: string): number | undefined => {
|
|
31
|
+
let pid = name.match(/^\..+\.(\d+)\.example\.[jt]sx?$/)?.[1]
|
|
32
|
+
return pid ? Number(pid) : undefined
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Every file under the paths, sorted, each once. */
|
|
36
|
+
export let walk = (paths: string[]): Promise<string[]> =>
|
|
37
|
+
Promise.try(() => {
|
|
38
|
+
let out = new Set<string>()
|
|
39
|
+
let visited = new Set<string>()
|
|
40
|
+
let go = (path: string): void => {
|
|
41
|
+
if (visited.has(path)) return
|
|
42
|
+
visited.add(path)
|
|
43
|
+
let info = io.statSync(path)
|
|
44
|
+
if (info.isFile) return void out.add(path)
|
|
45
|
+
for (let e of io.readDirSync(path)) {
|
|
46
|
+
if (e.name.startsWith('.') || SKIP.includes(e.name)) continue
|
|
47
|
+
let at = `${path}/${e.name}`
|
|
48
|
+
if (e.isDirectory) go(at)
|
|
49
|
+
else if (e.isFile) out.add(at)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
for (let p of paths) go(p.replace(/^\.\//, '').replace(/\/+$/, ''))
|
|
53
|
+
return [...out].sort()
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
/** Whether a page's source holds an example that runs. */
|
|
57
|
+
export let examples = (path: string, source: string): boolean => {
|
|
58
|
+
let { fenced, lines } = scan(source, path)
|
|
59
|
+
return !!(fenced.length || lines.length)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type Found = {
|
|
63
|
+
tests: string[]
|
|
64
|
+
pages: string[]
|
|
65
|
+
parsed?: ReadonlyMap<string, Examples>
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Where what each page of a checkout held, as last scanned, is kept by its
|
|
69
|
+
* status (`discover`). */
|
|
70
|
+
export let PAGES: string = `${stores()}/pages.json`
|
|
71
|
+
|
|
72
|
+
/** Test modules, example pages and their parsed inputs for one command.
|
|
73
|
+
* Given a `store`, what each page holds is kept there by the page's status
|
|
74
|
+
* (./kept.ts `byStatus`), and a page unchanged since is not read again. */
|
|
75
|
+
export let discover = async (
|
|
76
|
+
paths: string[],
|
|
77
|
+
store?: string,
|
|
78
|
+
): Promise<Found> => {
|
|
79
|
+
let files = await walk(paths)
|
|
80
|
+
let known = byStatus<Examples | 0>(store)
|
|
81
|
+
let parsed = new Map<string, Examples>()
|
|
82
|
+
for (let file of files.filter(paged)) {
|
|
83
|
+
let examples = known.of(file, io.statSync(file), () => {
|
|
84
|
+
let found = scan(io.readTextFileSync(file), file)
|
|
85
|
+
return found.fenced.length || found.lines.length ? found : 0
|
|
86
|
+
})
|
|
87
|
+
if (examples) parsed.set(file, examples)
|
|
88
|
+
}
|
|
89
|
+
let roots = paths.map((p) => p.replace(/^\.\//, '').replace(/\/+$/, ''))
|
|
90
|
+
known.save((file) =>
|
|
91
|
+
!roots.some((r) => file == r || file.startsWith(`${r}/`))
|
|
92
|
+
)
|
|
93
|
+
return { tests: files.filter(tested), pages: [...parsed.keys()], parsed }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The test modules and the example pages under the paths. */
|
|
97
|
+
export let find = async (
|
|
98
|
+
paths: string[],
|
|
99
|
+
): Promise<{ tests: string[]; pages: string[] }> => {
|
|
100
|
+
let { tests, pages } = await discover(paths)
|
|
101
|
+
return { tests, pages }
|
|
102
|
+
}
|