Skip to content

Commit ff62231

Browse files
committed
👍 Add name option to assert and ensure
1 parent 019fd38 commit ff62231

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

util.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ import type { Predicate } from "./is.ts";
33
export type AssertMessageFactory = (
44
x: unknown,
55
pred: Predicate<unknown>,
6+
name?: string,
67
) => string;
78

8-
export const defaultAssertMessageFactory: AssertMessageFactory = (x, pred) => {
9+
export const defaultAssertMessageFactory: AssertMessageFactory = (
10+
x,
11+
pred,
12+
name,
13+
) => {
914
const p = pred.name || "anonymous predicate";
1015
const t = typeof x;
1116
const v = JSON.stringify(x, null, 2);
12-
return `Expected a value that satisfies the predicate ${p}, got ${t}: ${v}`;
17+
return `Expected ${
18+
name ?? "a value"
19+
} that satisfies the predicate ${p}, got ${t}: ${v}`;
1320
};
1421

1522
let assertMessageFactory = defaultAssertMessageFactory;
@@ -79,11 +86,11 @@ export function setAssertMessageFactory(factory: AssertMessageFactory): void {
7986
export function assert<T>(
8087
x: unknown,
8188
pred: Predicate<T>,
82-
options: { message?: string } = {},
89+
options: { message?: string; name?: string } = {},
8390
): asserts x is T {
8491
if (!pred(x)) {
8592
throw new AssertError(
86-
options.message ?? assertMessageFactory(x, pred),
93+
options.message ?? assertMessageFactory(x, pred, options.name),
8794
);
8895
}
8996
}
@@ -108,7 +115,7 @@ export function assert<T>(
108115
export function ensure<T>(
109116
x: unknown,
110117
pred: Predicate<T>,
111-
options: { message?: string } = {},
118+
options: { message?: string; name?: string } = {},
112119
): T {
113120
assert(x, pred, options);
114121
return x;

util_test.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ Deno.test("assert", async (t) => {
3434
);
3535
});
3636

37-
await t.step("throws an `AssertError` on false predicate", () => {
38-
assertThrows(
39-
() => assert(x, falsePredicate),
40-
AssertError,
41-
`Expected a value that satisfies the predicate falsePredicate, got symbol: undefined`,
42-
);
43-
});
37+
await t.step(
38+
"throws an `AssertError` on false predicate with a custom name",
39+
() => {
40+
assertThrows(
41+
() => assert(x, falsePredicate, { name: "hello world" }),
42+
AssertError,
43+
`Expected hello world that satisfies the predicate falsePredicate, got symbol: undefined`,
44+
);
45+
},
46+
);
4447

4548
await t.step(
4649
"throws an `AssertError` with a custom message on false predicate",
@@ -67,6 +70,17 @@ Deno.test("ensure", async (t) => {
6770
);
6871
});
6972

73+
await t.step(
74+
"throws an `AssertError` on false predicate with a custom name",
75+
() => {
76+
assertThrows(
77+
() => ensure(x, falsePredicate, { name: "hello world" }),
78+
AssertError,
79+
`Expected hello world that satisfies the predicate falsePredicate, got symbol: undefined`,
80+
);
81+
},
82+
);
83+
7084
await t.step(
7185
"throws an `AssertError` with a custom message on false predicate",
7286
() => {

0 commit comments

Comments
 (0)