refactor(assert): minor cleanups (#4941)

* refactor(assert): minor cleanups

* tweaks and suggestions
This commit is contained in:
Asher Gomez 2024-06-03 23:30:20 +10:00 committed by GitHub
parent a5a658f7ca
commit e34245bdeb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 28 additions and 65 deletions

View File

@ -25,7 +25,8 @@ import { AssertionError } from "./assertion_error.ts";
*
* @param actual The actual value to compare.
* @param expected The expected value to compare.
* @param tolerance The tolerance to consider the values almost equal. The default is one hundred thousandth of a percent of the expected value.
* @param tolerance The tolerance to consider the values almost equal. The
* default is one hundred thousandth of a percent of the expected value.
* @param msg The optional message to include in the error.
*/
export function assertAlmostEquals(

View File

@ -23,9 +23,8 @@ export function assertMatch(
expected: RegExp,
msg?: string,
) {
if (!expected.test(actual)) {
const msgSuffix = msg ? `: ${msg}` : ".";
msg = `Expected actual: "${actual}" to match: "${expected}"${msgSuffix}`;
throw new AssertionError(msg);
}
if (expected.test(actual)) return;
const msgSuffix = msg ? `: ${msg}` : ".";
msg = `Expected actual: "${actual}" to match: "${expected}"${msgSuffix}`;
throw new AssertionError(msg);
}

View File

@ -23,10 +23,8 @@ export function assertNotMatch(
expected: RegExp,
msg?: string,
) {
if (expected.test(actual)) {
const msgSuffix = msg ? `: ${msg}` : ".";
msg =
`Expected actual: "${actual}" to not match: "${expected}"${msgSuffix}`;
throw new AssertionError(msg);
}
if (!expected.test(actual)) return;
const msgSuffix = msg ? `: ${msg}` : ".";
msg = `Expected actual: "${actual}" to not match: "${expected}"${msgSuffix}`;
throw new AssertionError(msg);
}

View File

@ -4,8 +4,9 @@ import { AssertionError } from "./assertion_error.ts";
import { format } from "@std/internal/format";
/**
* Make an assertion that `actual` and `expected` are not strictly equal.
* If the values are strictly equal then throw.
* Make an assertion that `actual` and `expected` are not strictly equal, using
* {@linkcode Object.is} for equality comparison. If the values are strictly
* equal then throw.
*
* @example Usage
* ```ts no-eval

View File

@ -4,7 +4,7 @@ import { buildMessage, diff, diffStr, format, red } from "@std/internal";
import { AssertionError } from "./assertion_error.ts";
/**
* Make an assertion that `actual` and `expected` are equal using
* Make an assertion that `actual` and `expected` are strictly equal, using
* {@linkcode Object.is} for equality comparison. If not, then throw.
*
* @example Usage

View File

@ -23,9 +23,8 @@ export function assertStringIncludes(
expected: string,
msg?: string,
) {
if (!actual.includes(expected)) {
const msgSuffix = msg ? `: ${msg}` : ".";
msg = `Expected actual: "${actual}" to contain: "${expected}"${msgSuffix}`;
throw new AssertionError(msg);
}
if (actual.includes(expected)) return;
const msgSuffix = msg ? `: ${msg}` : ".";
msg = `Expected actual: "${actual}" to contain: "${expected}"${msgSuffix}`;
throw new AssertionError(msg);
}

View File

@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { assert } from "./assert.ts";
import { AssertionError } from "./assertion_error.ts";
/**
* Forcefully throws a failed assertion.
@ -17,5 +17,5 @@ import { assert } from "./assert.ts";
*/
export function fail(msg?: string): never {
const msgSuffix = msg ? `: ${msg}` : ".";
assert(false, `Failed assertion${msgSuffix}`);
throw new AssertionError(`Failed assertion${msgSuffix}`);
}

View File

@ -1,20 +0,0 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import {
assertArrayIncludes,
assertEquals,
assertNotEquals,
assertNotStrictEquals,
assertStrictEquals,
} from "./mod.ts";
Deno.test({
name: "assert* functions with specified type parameter",
fn() {
assertEquals<string>("hello", "hello");
assertNotEquals<number>(1, 2);
assertArrayIncludes<boolean>([true, false], [true]);
const value = { x: 1 };
assertStrictEquals<typeof value>(value, value);
assertNotStrictEquals<object>(value, { x: 1 });
},
});

View File

@ -1,26 +1,11 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, AssertionError, unreachable } from "./mod.ts";
import { AssertionError, assertThrows, unreachable } from "./mod.ts";
Deno.test("AssertsUnreachable", function () {
let didThrow = false;
try {
unreachable();
} catch (e) {
assert(e instanceof AssertionError);
assert(e.message === "unreachable");
didThrow = true;
}
assert(didThrow);
});
Deno.test("unreachable with reason", function () {
let didThrow = false;
try {
unreachable("inconceivable!");
} catch (e) {
assert(e instanceof AssertionError);
assert(e.message === "inconceivable!");
didThrow = true;
}
assert(didThrow);
Deno.test("unreachable()", () => {
assertThrows(() => unreachable(), AssertionError, "unreachable");
assertThrows(
() => unreachable("custom message"),
AssertionError,
"custom message",
);
});