mirror of
https://github.com/denoland/deno.git
synced 2024-11-27 16:10:57 -05:00
parent
41829f3e2b
commit
8d474a7911
1 changed files with 97 additions and 92 deletions
|
@ -2,7 +2,22 @@
|
||||||
|
|
||||||
// Do not add imports in this file in order to be compatible with Node.
|
// Do not add imports in this file in order to be compatible with Node.
|
||||||
|
|
||||||
export function assertEqual(actual: unknown, expected: unknown, msg?: string) {
|
interface Constructor {
|
||||||
|
new (...args: any[]): any;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
if (!equal(actual, expected)) {
|
if (!equal(actual, expected)) {
|
||||||
let actualString: string;
|
let actualString: string;
|
||||||
let expectedString: string;
|
let expectedString: string;
|
||||||
|
@ -27,41 +42,12 @@ export function assertEqual(actual: unknown, expected: unknown, msg?: string) {
|
||||||
}
|
}
|
||||||
throw new Error(msg);
|
throw new Error(msg);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
interface Constructor {
|
|
||||||
new (...args: any[]): any;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Assert {
|
|
||||||
/** Make an assertion, if not true, then throw. */
|
|
||||||
(expr: boolean, msg?: string): void;
|
|
||||||
|
|
||||||
/** Make an assertion that `actual` and `expected` are equal, deeply. If not
|
|
||||||
* deeply equal, then throw.
|
|
||||||
*/
|
|
||||||
equal(actual: unknown, expected: unknown, msg?: string): void;
|
|
||||||
|
|
||||||
/** Make an assertion that `actual` and `expected` are strictly equal. If
|
/** Make an assertion that `actual` and `expected` are strictly equal. If
|
||||||
* not then throw.
|
* not then throw.
|
||||||
*/
|
*/
|
||||||
strictEqual(actual: unknown, expected: unknown, msg?: string): void;
|
strictEqual(actual: unknown, expected: unknown, msg = ""): void {
|
||||||
|
|
||||||
/** 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?: string): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const assert = function assert(expr: boolean, msg = "") {
|
|
||||||
if (!expr) {
|
|
||||||
throw new Error(msg);
|
|
||||||
}
|
|
||||||
} as Assert;
|
|
||||||
|
|
||||||
assert.equal = assertEqual;
|
|
||||||
assert.strictEqual = function strictEqual(actual, expected, msg = "") {
|
|
||||||
if (actual !== expected) {
|
if (actual !== expected) {
|
||||||
let actualString: string;
|
let actualString: string;
|
||||||
let expectedString: string;
|
let expectedString: string;
|
||||||
|
@ -86,13 +72,18 @@ assert.strictEqual = function strictEqual(actual, expected, msg = "") {
|
||||||
}
|
}
|
||||||
throw new Error(msg);
|
throw new Error(msg);
|
||||||
}
|
}
|
||||||
};
|
},
|
||||||
assert.throws = function throws(
|
|
||||||
fn,
|
/** Executes a function, expecting it to throw. If it does not, then it
|
||||||
ErrorClass: Constructor,
|
* 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 = "",
|
msgIncludes = "",
|
||||||
msg = ""
|
msg = ""
|
||||||
) {
|
): void {
|
||||||
let doesThrow = false;
|
let doesThrow = false;
|
||||||
try {
|
try {
|
||||||
fn();
|
fn();
|
||||||
|
@ -117,8 +108,22 @@ assert.throws = function throws(
|
||||||
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
|
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
|
||||||
throw new Error(msg);
|
throw new Error(msg);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type Assert = typeof assertions.assert & typeof assertions;
|
||||||
|
|
||||||
|
// Decorate assertions.assert with all the assertions
|
||||||
|
Object.assign(assertions.assert, assertions);
|
||||||
|
|
||||||
|
export const assert = assertions.assert as Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An alias to assert.equal
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
export const assertEqual = assert.equal;
|
||||||
|
|
||||||
export function equal(c: unknown, d: unknown): boolean {
|
export function equal(c: unknown, d: unknown): boolean {
|
||||||
const seen = new Map();
|
const seen = new Map();
|
||||||
return (function compare(a: unknown, b: unknown) {
|
return (function compare(a: unknown, b: unknown) {
|
||||||
|
|
Loading…
Reference in a new issue