-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathguut.ts
More file actions
43 lines (38 loc) · 1.25 KB
/
guut.ts
File metadata and controls
43 lines (38 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { deepStrictEqual, notDeepStrictEqual } from 'node:assert'
import { suite, describe, it } from 'node:test'
type TestParameters =
| {
type: 'is' | 'is not'
assertions: Record<string, string | undefined>
}
| {
type: 'is-self'
assertions: string[]
}
const assert = (unit: TestParameters, fn: (...args: unknown[]) => unknown) => {
if (unit.type === 'is' || unit.type === 'is not')
Object.entries(unit.assertions).forEach(([input, expected]) =>
it(`${fn.name}(${input}) ${unit.type} ${expected}`, () => {
if (unit.type === 'is') deepStrictEqual(fn(input), expected)
if (unit.type === 'is not') notDeepStrictEqual(fn(input), expected)
}),
)
if (unit.type === 'is-self')
Object.entries(unit.assertions).forEach(([input, expected]) =>
it(`${fn.name}(${input}) ${unit.type} ${expected}`, () => {
deepStrictEqual(fn(input), input)
}),
)
}
export type TestUnit = {
name: string
functions: string[]
} & TestParameters
export const run = (units: TestUnit[], fns: Record<string, any>) =>
units.forEach(({ name, functions, ...unit }) =>
describe(name, () =>
functions.forEach((func) =>
describe(func, () => assert(unit, fns[func])),
),
),
)