-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ensure_test.ts
67 lines (57 loc) · 1.71 KB
/
ensure_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
import { assertStrictEquals, assertThrows } from "@std/assert";
import {
AssertError,
defaultAssertMessageFactory,
setAssertMessageFactory,
} from "./assert.ts";
import { ensure } from "./ensure.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("ensure", async (t) => {
await t.step("returns `x` as-is on true predicate", () => {
assertStrictEquals(ensure(x, truePredicate), x);
});
await t.step("throws an `AssertError` on false predicate", () => {
assertThrows(
() => ensure(x, falsePredicate),
AssertError,
`Expected a value that satisfies the predicate falsePredicate, got symbol: undefined`,
);
});
await t.step(
"throws an `AssertError` on false predicate with a custom name",
() => {
assertThrows(
() => ensure(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(
() => ensure(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 `ensure` failure", () => {
assertThrows(
() => ensure(x, falsePredicate),
AssertError,
"Hello symbol falsePredicate",
);
});
setAssertMessageFactory(defaultAssertMessageFactory);
});