Skip to content

Commit

Permalink
👍 Add name option to assert and ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Sep 24, 2023
1 parent 019fd38 commit ff62231
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
17 changes: 12 additions & 5 deletions util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import type { Predicate } from "./is.ts";
export type AssertMessageFactory = (
x: unknown,
pred: Predicate<unknown>,
name?: string,
) => string;

export const defaultAssertMessageFactory: AssertMessageFactory = (x, pred) => {
export const defaultAssertMessageFactory: AssertMessageFactory = (
x,
pred,
name,
) => {
const p = pred.name || "anonymous predicate";
const t = typeof x;
const v = JSON.stringify(x, null, 2);
return `Expected a value that satisfies the predicate ${p}, got ${t}: ${v}`;
return `Expected ${
name ?? "a value"
} that satisfies the predicate ${p}, got ${t}: ${v}`;
};

let assertMessageFactory = defaultAssertMessageFactory;
Expand Down Expand Up @@ -79,11 +86,11 @@ export function setAssertMessageFactory(factory: AssertMessageFactory): void {
export function assert<T>(
x: unknown,
pred: Predicate<T>,
options: { message?: string } = {},
options: { message?: string; name?: string } = {},
): asserts x is T {
if (!pred(x)) {
throw new AssertError(
options.message ?? assertMessageFactory(x, pred),
options.message ?? assertMessageFactory(x, pred, options.name),
);
}
}
Expand All @@ -108,7 +115,7 @@ export function assert<T>(
export function ensure<T>(
x: unknown,
pred: Predicate<T>,
options: { message?: string } = {},
options: { message?: string; name?: string } = {},
): T {
assert(x, pred, options);
return x;
Expand Down
28 changes: 21 additions & 7 deletions util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ Deno.test("assert", async (t) => {
);
});

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 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",
Expand All @@ -67,6 +70,17 @@ Deno.test("ensure", async (t) => {
);
});

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",
() => {
Expand Down

0 comments on commit ff62231

Please sign in to comment.