Skip to content

Commit

Permalink
Merge pull request garronej#36 from psychedelicious/psyche/feat/messa…
Browse files Browse the repository at this point in the history
…ge-callback

feat(assert): support callback for message
  • Loading branch information
garronej authored Nov 8, 2024
2 parents 9aa7b3b + 03005cf commit 18576b8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ export class AssertionError extends Error {
}

/** https://docs.tsafe.dev/assert */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function assert<_T extends true>(condition?: any, msg?: string): asserts condition {
export function assert<_T extends true>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
condition?: any,
msg?: string | (() => string),
): asserts condition {
if (arguments.length === 0) {
condition = true;
}
Expand All @@ -40,6 +43,6 @@ export function assert<_T extends true>(condition?: any, msg?: string): asserts
}

if (!condition) {
throw new AssertionError(msg);
throw new AssertionError(typeof msg === "function" ? msg() : msg);
}
}
77 changes: 76 additions & 1 deletion test/assertIs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { assert } from "../src/assert";
import { assert, AssertionError } from "../src/assert";
import { is } from "../src/is";
import type { Equals } from "../src/Equals";

Expand Down Expand Up @@ -47,3 +47,78 @@ scope: {

throw new Error("Fail");
}

/**
* When an assertion fails and no message was provided, it should throw an `AssertionError` with the
* message 'Wrong assertion encountered'.
*/
{
try {
assert(false);
} catch (e) {
assert(e instanceof AssertionError);
assert(e.message === "Wrong assertion encountered");
console.log("PASS");
}
}

/**
* When an assertion fails and a message string was provided, it should throw an `AssertionError` with
* the message 'Wrong assertion encountered: "<message>"'.
*/
{
try {
assert(false, "message");
} catch (e) {
assert(e instanceof AssertionError);
assert(e.message === 'Wrong assertion encountered: "message"');
console.log("PASS");
}
}

/**
* When an assertion passes and a message callback was provided, the callback should not be called.
*/
{
let called = false as boolean;
const message = () => {
called = true;
return "message";
};
assert(true, message);
assert(called === false);
console.log("PASS");
}

/**
* When an assertion fails and a message callback was provided, the callback should be called.
*/
{
let called = false as boolean;
const message = () => {
called = true;
return "message";
};
try {
assert(false, message);
} catch (e) {
assert(e instanceof AssertionError);
assert(called === true);
console.log("PASS");
}
}

/**
* When an assertion fails and a message callback was provided, it should throw an `AssertionError` with
* the message 'Wrong assertion encountered: "<return value from message callback>"'.
*/
{
const message = () => "message";
try {
assert(false, message);
} catch (e) {
assert(e instanceof AssertionError);
assert(e.message === 'Wrong assertion encountered: "message"');
console.log("PASS");
}
}

0 comments on commit 18576b8

Please sign in to comment.