-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
assert_test.ts
78 lines (67 loc) · 1.96 KB
/
assert_test.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { assertThrows } from "@std/assert";
import {
assert,
AssertError,
defaultAssertMessageFactory,
setAssertMessageFactory,
} from "./assert.ts";
const x: unknown = Symbol("x");
function truePredicate(_x: unknown): _x is string {
return true;
}
function falsePredicate(_x: unknown): _x is string {
return false;
}
Deno.test("assert", async (t) => {
await t.step("does nothing on true predicate", () => {
assert(x, truePredicate);
});
await t.step("throws an `AssertError` on false predicate", () => {
assertThrows(
() => assert(x, falsePredicate),
AssertError,
`Expected a value that satisfies the predicate falsePredicate, got symbol: undefined`,
);
});
await t.step(
"throws an `AssertError` on false predicate with an anonymous predicate",
() => {
assertThrows(
() => assert(x, (_x: unknown): _x is string => false),
AssertError,
`Expected a value that satisfies the predicate anonymous predicate, got symbol: undefined`,
);
},
);
await t.step(
"throws an `AssertError` on false predicate with a custom name",
() => {
assertThrows(
() => assert(x, falsePredicate, { name: "hello world" }),
AssertError,
`Expected hello world that satisfies the predicate falsePredicate, got symbol: undefined`,
);
},
);
await t.step(
"throws an `AssertError` with a custom message on false predicate",
() => {
assertThrows(
() => assert(x, falsePredicate, { message: "Hello" }),
AssertError,
"Hello",
);
},
);
});
Deno.test("setAssertMessageFactory", async (t) => {
setAssertMessageFactory((x, pred) => `Hello ${typeof x} ${pred.name}`);
await t.step("change `AssertError` message on `assert` failure", () => {
assertThrows(
() => assert(x, falsePredicate),
AssertError,
"Hello symbol falsePredicate",
);
});
setAssertMessageFactory(defaultAssertMessageFactory);
});