-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
_testutil.ts
61 lines (58 loc) · 2 KB
/
_testutil.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { assertEquals } from "@std/assert";
import type { Predicate } from "./type.ts";
const examples = {
string: ["", "Hello world"],
number: [0, 1234567890],
bigint: [0n, 1234567890n],
boolean: [true, false],
array: [[], [0, 1, 2], ["a", "b", "c"], [0, "a", true]],
set: [new Set(), new Set([0, 1, 2]), new Set(["a", "b", "c"])],
record: [{}, { a: 0, b: 1, c: 2 }, { a: "a", b: "b", c: "c" }],
map: [
new Map(),
new Map([["a", 0], ["b", 1], ["c", 2]]),
new Map([["a", "a"], ["b", "b"], ["c", "c"]]),
],
syncFunction: [function a() {}, () => {}],
asyncFunction: [async function b() {}, async () => {}],
null: [null],
undefined: [undefined],
symbol: [Symbol("a"), Symbol("b"), Symbol("c")],
date: [new Date(1690248225000), new Date(0)],
promise: [new Promise(() => {})],
} as const;
export async function testWithExamples<T>(
t: Deno.TestContext,
pred: Predicate<T>,
opts?: {
validExamples?: (keyof typeof examples)[];
excludeExamples?: (keyof typeof examples)[];
},
): Promise<void> {
const { validExamples = [], excludeExamples = [] } = opts ?? {};
const exampleEntries = (Object.entries(examples) as unknown as [
name: keyof typeof examples,
example: unknown[],
][]).filter(([k]) => !excludeExamples.includes(k));
for (const [name, example] of exampleEntries) {
const expect = validExamples.includes(name);
for (const v of example) {
await t.step(
`returns ${expect} on ${stringify(v)}`,
() => {
assertEquals(pred(v), expect);
},
);
}
}
}
export function stringify(x: unknown): string {
if (x instanceof Date) return `Date(${x.valueOf()})`;
if (x instanceof Promise) return "Promise";
if (x instanceof Set) return `Set(${stringify([...x.values()])})`;
if (x instanceof Map) return `Map(${stringify([...x.entries()])})`;
if (typeof x === "function") return x.toString();
if (typeof x === "bigint") return `${x}n`;
if (typeof x === "symbol") return x.toString();
return JSON.stringify(x);
}