mirror of
https://github.com/denoland/deno.git
synced 2025-10-02 23:24:37 +00:00
Refactor asserts in testing (denoland/deno_std#227)
Original: c734e32343
This commit is contained in:
parent
39fde3a454
commit
787207f11b
7 changed files with 261 additions and 140 deletions
142
testing/mod.ts
142
testing/mod.ts
|
@ -2,11 +2,16 @@
|
|||
|
||||
import { green, red } from "../colors/mod.ts";
|
||||
import { assertEqual as prettyAssertEqual } from "./pretty.ts";
|
||||
|
||||
interface Constructor {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
new (...args: any[]): any;
|
||||
}
|
||||
import {
|
||||
assert as assertImport,
|
||||
equal as AssertEqual,
|
||||
assertStrictEq,
|
||||
assertStrContains,
|
||||
assertMatch,
|
||||
fail,
|
||||
assertThrows,
|
||||
assertThrowsAsync
|
||||
} from "./asserts.ts";
|
||||
|
||||
export function equal(c: unknown, d: unknown): boolean {
|
||||
const seen = new Map();
|
||||
|
@ -36,125 +41,14 @@ export function equal(c: unknown, d: unknown): boolean {
|
|||
}
|
||||
|
||||
const assertions = {
|
||||
/** Make an assertion, if not `true`, then throw. */
|
||||
assert(expr: boolean, msg = ""): void {
|
||||
if (!expr) {
|
||||
throw new Error(msg);
|
||||
}
|
||||
},
|
||||
|
||||
/** Make an assertion that `actual` and `expected` are equal, deeply. If not
|
||||
* deeply equal, then throw.
|
||||
*/
|
||||
equal(actual: unknown, expected: unknown, msg?: string): void {
|
||||
prettyAssertEqual(actual, expected, msg);
|
||||
},
|
||||
|
||||
/** Make an assertion that `actual` and `expected` are strictly equal. If
|
||||
* not then throw.
|
||||
*/
|
||||
strictEqual(actual: unknown, expected: unknown, msg = ""): void {
|
||||
if (actual !== expected) {
|
||||
let actualString: string;
|
||||
let expectedString: string;
|
||||
try {
|
||||
actualString = String(actual);
|
||||
} catch (e) {
|
||||
actualString = "[Cannot display]";
|
||||
}
|
||||
try {
|
||||
expectedString = String(expected);
|
||||
} catch (e) {
|
||||
expectedString = "[Cannot display]";
|
||||
}
|
||||
console.error(
|
||||
"strictEqual failed. actual =",
|
||||
actualString,
|
||||
"expected =",
|
||||
expectedString
|
||||
);
|
||||
if (!msg) {
|
||||
msg = `actual: ${actualString} expected: ${expectedString}`;
|
||||
}
|
||||
throw new Error(msg);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Forcefully throws a failed assertion
|
||||
*/
|
||||
fail(msg?: string): void {
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
assert(false, `Failed assertion${msg ? `: ${msg}` : "."}`);
|
||||
},
|
||||
|
||||
/** Executes a function, expecting it to throw. If it does not, then it
|
||||
* throws. An error class and a string that should be included in the
|
||||
* error message can also be asserted.
|
||||
*/
|
||||
throws(
|
||||
fn: () => void,
|
||||
ErrorClass?: Constructor,
|
||||
msgIncludes = "",
|
||||
msg = ""
|
||||
): void {
|
||||
let doesThrow = false;
|
||||
try {
|
||||
fn();
|
||||
} catch (e) {
|
||||
if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
|
||||
msg = `Expected error to be instance of "${ErrorClass.name}"${
|
||||
msg ? `: ${msg}` : "."
|
||||
}`;
|
||||
throw new Error(msg);
|
||||
}
|
||||
if (msgIncludes) {
|
||||
if (!e.message.includes(msgIncludes)) {
|
||||
msg = `Expected error message to include "${msgIncludes}", but got "${
|
||||
e.message
|
||||
}"${msg ? `: ${msg}` : "."}`;
|
||||
throw new Error(msg);
|
||||
}
|
||||
}
|
||||
doesThrow = true;
|
||||
}
|
||||
if (!doesThrow) {
|
||||
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
|
||||
throw new Error(msg);
|
||||
}
|
||||
},
|
||||
|
||||
async throwsAsync(
|
||||
fn: () => Promise<void>,
|
||||
ErrorClass?: Constructor,
|
||||
msgIncludes = "",
|
||||
msg = ""
|
||||
): Promise<void> {
|
||||
let doesThrow = false;
|
||||
try {
|
||||
await fn();
|
||||
} catch (e) {
|
||||
if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
|
||||
msg = `Expected error to be instance of "${ErrorClass.name}"${
|
||||
msg ? `: ${msg}` : "."
|
||||
}`;
|
||||
throw new Error(msg);
|
||||
}
|
||||
if (msgIncludes) {
|
||||
if (!e.message.includes(msgIncludes)) {
|
||||
msg = `Expected error message to include "${msgIncludes}", but got "${
|
||||
e.message
|
||||
}"${msg ? `: ${msg}` : "."}`;
|
||||
throw new Error(msg);
|
||||
}
|
||||
}
|
||||
doesThrow = true;
|
||||
}
|
||||
if (!doesThrow) {
|
||||
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
|
||||
throw new Error(msg);
|
||||
}
|
||||
}
|
||||
assert: assertImport,
|
||||
equal: AssertEqual,
|
||||
strictEqual: assertStrictEq,
|
||||
assertStrContains: assertStrContains,
|
||||
assertMatch: assertMatch,
|
||||
fail: fail,
|
||||
throws: assertThrows,
|
||||
throwsAsync: assertThrowsAsync
|
||||
};
|
||||
|
||||
type Assert = typeof assertions.assert & typeof assertions;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue