-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from lambdalisue/better-default-message
👍 Improve the assertion error message
- Loading branch information
Showing
2 changed files
with
102 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,51 @@ import { | |
assertStrictEquals, | ||
assertThrows, | ||
} from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { assert, AssertError, ensure, maybe } from "./util.ts"; | ||
import { | ||
assert, | ||
AssertError, | ||
defaultAssertMessageFactory, | ||
ensure, | ||
maybe, | ||
setAssertMessageFactory, | ||
} from "./util.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, (_x): _x is string => true); | ||
assert(x, truePredicate); | ||
}); | ||
|
||
await t.step("throws an `AssertError` on false predicate", () => { | ||
assertThrows(() => assert(x, (_x): _x is string => false), AssertError); | ||
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", () => { | ||
assertThrows( | ||
() => assert(x, falsePredicate), | ||
AssertError, | ||
`Expected a value that satisfies the predicate falsePredicate, got symbol: undefined`, | ||
); | ||
}); | ||
|
||
await t.step( | ||
"throws an `AssertError` with a custom message on false predicate", | ||
() => { | ||
assertThrows( | ||
() => assert(x, (_x): _x is string => false, { message: "Hello" }), | ||
() => assert(x, falsePredicate, { message: "Hello" }), | ||
AssertError, | ||
"Hello", | ||
); | ||
|
@@ -29,18 +56,22 @@ Deno.test("assert", async (t) => { | |
|
||
Deno.test("ensure", async (t) => { | ||
await t.step("returns `x` as-is on true predicate", () => { | ||
assertStrictEquals(ensure(x, (_x): _x is string => true), x); | ||
assertStrictEquals(ensure(x, truePredicate), x); | ||
}); | ||
|
||
await t.step("throws an `AssertError` on false predicate", () => { | ||
assertThrows(() => ensure(x, (_x): _x is string => false), AssertError); | ||
assertThrows( | ||
() => ensure(x, falsePredicate), | ||
AssertError, | ||
`Expected a value that satisfies the predicate falsePredicate, got symbol: undefined`, | ||
); | ||
}); | ||
|
||
await t.step( | ||
"throws an `AssertError` with a custom message on false predicate", | ||
() => { | ||
assertThrows( | ||
() => ensure(x, (_x): _x is string => false, { message: "Hello" }), | ||
() => ensure(x, falsePredicate, { message: "Hello" }), | ||
AssertError, | ||
"Hello", | ||
); | ||
|
@@ -50,10 +81,32 @@ Deno.test("ensure", async (t) => { | |
|
||
Deno.test("maybe", async (t) => { | ||
await t.step("returns `x` as-is on true predicate", () => { | ||
assertStrictEquals(maybe(x, (_x): _x is string => true), x); | ||
assertStrictEquals(maybe(x, truePredicate), x); | ||
}); | ||
|
||
await t.step("returns `undefined` on false predicate", () => { | ||
assertStrictEquals(maybe(x, (_x): _x is string => false), undefined); | ||
assertStrictEquals(maybe(x, falsePredicate), undefined); | ||
}); | ||
}); | ||
|
||
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", | ||
); | ||
}); | ||
|
||
await t.step("change `AssertError` message on `ensure` failure", () => { | ||
assertThrows( | ||
() => ensure(x, falsePredicate), | ||
AssertError, | ||
"Hello symbol falsePredicate", | ||
); | ||
}); | ||
|
||
setAssertMessageFactory(defaultAssertMessageFactory); | ||
}); |