2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-05-25 13:32:34 -04:00
|
|
|
/** This module is browser compatible. Do not rely on good formatting of values
|
|
|
|
* for AssertionError messages in browsers. */
|
|
|
|
|
2020-09-27 06:22:32 -04:00
|
|
|
import { bold, gray, green, red, stripColor, white } from "../fmt/colors.ts";
|
2020-10-01 13:15:05 -04:00
|
|
|
import { diff, DiffResult, DiffType } from "./_diff.ts";
|
2019-08-14 10:22:31 -04:00
|
|
|
|
|
|
|
const CAN_NOT_DISPLAY = "[Cannot display]";
|
2019-03-05 14:58:28 -05:00
|
|
|
|
|
|
|
interface Constructor {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2019-03-05 14:58:28 -05:00
|
|
|
new (...args: any[]): any;
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:04:43 -05:00
|
|
|
export class AssertionError extends Error {
|
|
|
|
constructor(message: string) {
|
|
|
|
super(message);
|
|
|
|
this.name = "AssertionError";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 00:52:18 -04:00
|
|
|
export function _format(v: unknown): string {
|
2020-09-18 09:28:30 -04:00
|
|
|
return globalThis.Deno
|
2020-09-29 22:59:50 -04:00
|
|
|
? Deno.inspect(v, {
|
2020-07-14 15:24:17 -04:00
|
|
|
depth: Infinity,
|
|
|
|
sorted: true,
|
|
|
|
trailingComma: true,
|
|
|
|
compact: false,
|
|
|
|
iterableLimit: Infinity,
|
2020-09-29 22:59:50 -04:00
|
|
|
})
|
2020-09-18 09:28:30 -04:00
|
|
|
: `"${String(v).replace(/(?=["\\])/g, "\\")}"`;
|
2019-08-14 10:22:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function createColor(diffType: DiffType): (s: string) => string {
|
|
|
|
switch (diffType) {
|
|
|
|
case DiffType.added:
|
|
|
|
return (s: string): string => green(bold(s));
|
|
|
|
case DiffType.removed:
|
|
|
|
return (s: string): string => red(bold(s));
|
|
|
|
default:
|
|
|
|
return white;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSign(diffType: DiffType): string {
|
|
|
|
switch (diffType) {
|
|
|
|
case DiffType.added:
|
|
|
|
return "+ ";
|
|
|
|
case DiffType.removed:
|
|
|
|
return "- ";
|
|
|
|
default:
|
|
|
|
return " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildMessage(diffResult: ReadonlyArray<DiffResult<string>>): string[] {
|
|
|
|
const messages: string[] = [];
|
|
|
|
messages.push("");
|
|
|
|
messages.push("");
|
|
|
|
messages.push(
|
2020-07-14 15:24:17 -04:00
|
|
|
` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${
|
|
|
|
green(bold("Expected"))
|
|
|
|
}`,
|
2019-08-14 10:22:31 -04:00
|
|
|
);
|
|
|
|
messages.push("");
|
|
|
|
messages.push("");
|
2019-11-13 13:42:34 -05:00
|
|
|
diffResult.forEach((result: DiffResult<string>): void => {
|
|
|
|
const c = createColor(result.type);
|
|
|
|
messages.push(c(`${createSign(result.type)}${result.value}`));
|
|
|
|
});
|
2019-08-14 10:22:31 -04:00
|
|
|
messages.push("");
|
|
|
|
|
|
|
|
return messages;
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:21:43 -05:00
|
|
|
function isKeyedCollection(x: unknown): x is Set<unknown> {
|
2020-03-28 13:03:49 -04:00
|
|
|
return [Symbol.iterator, "size"].every((k) => k in (x as Set<unknown>));
|
2019-11-04 10:21:43 -05:00
|
|
|
}
|
|
|
|
|
2019-03-06 16:39:50 -05:00
|
|
|
export function equal(c: unknown, d: unknown): boolean {
|
|
|
|
const seen = new Map();
|
2019-04-24 07:41:23 -04:00
|
|
|
return (function compare(a: unknown, b: unknown): boolean {
|
2019-03-26 08:15:16 -04:00
|
|
|
// Have to render RegExp & Date for string comparison
|
|
|
|
// unless it's mistreated as object
|
|
|
|
if (
|
|
|
|
a &&
|
|
|
|
b &&
|
|
|
|
((a instanceof RegExp && b instanceof RegExp) ||
|
2020-06-13 10:01:05 -04:00
|
|
|
(a instanceof URL && b instanceof URL))
|
2019-03-26 08:15:16 -04:00
|
|
|
) {
|
|
|
|
return String(a) === String(b);
|
|
|
|
}
|
2020-07-05 22:21:03 -04:00
|
|
|
if (a instanceof Date && b instanceof Date) {
|
2020-08-28 19:59:28 -04:00
|
|
|
const aTime = a.getTime();
|
|
|
|
const bTime = b.getTime();
|
|
|
|
// Check for NaN equality manually since NaN is not
|
|
|
|
// equal to itself.
|
|
|
|
if (Number.isNaN(aTime) && Number.isNaN(bTime)) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-05 22:21:03 -04:00
|
|
|
return a.getTime() === b.getTime();
|
|
|
|
}
|
2019-03-06 16:39:50 -05:00
|
|
|
if (Object.is(a, b)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (a && typeof a === "object" && b && typeof b === "object") {
|
|
|
|
if (seen.get(a) === b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Object.keys(a || {}).length !== Object.keys(b || {}).length) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-04 10:21:43 -05:00
|
|
|
if (isKeyedCollection(a) && isKeyedCollection(b)) {
|
|
|
|
if (a.size !== b.size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let unmatchedEntries = a.size;
|
|
|
|
|
|
|
|
for (const [aKey, aValue] of a.entries()) {
|
|
|
|
for (const [bKey, bValue] of b.entries()) {
|
|
|
|
/* Given that Map keys can be references, we need
|
|
|
|
* to ensure that they are also deeply equal */
|
|
|
|
if (
|
|
|
|
(aKey === aValue && bKey === bValue && compare(aKey, bKey)) ||
|
|
|
|
(compare(aKey, bKey) && compare(aValue, bValue))
|
|
|
|
) {
|
|
|
|
unmatchedEntries--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return unmatchedEntries === 0;
|
|
|
|
}
|
2019-03-06 16:39:50 -05:00
|
|
|
const merged = { ...a, ...b };
|
|
|
|
for (const key in merged) {
|
|
|
|
type Key = keyof typeof merged;
|
|
|
|
if (!compare(a && a[key as Key], b && b[key as Key])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
seen.set(a, b);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})(c, d);
|
|
|
|
}
|
|
|
|
|
2020-07-02 12:03:15 -04:00
|
|
|
/** Make an assertion, error will be thrown if `expr` does not have truthy value. */
|
2019-11-13 13:42:34 -05:00
|
|
|
export function assert(expr: unknown, msg = ""): asserts expr {
|
2019-03-05 14:58:28 -05:00
|
|
|
if (!expr) {
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an assertion that `actual` and `expected` are equal, deeply. If not
|
|
|
|
* deeply equal, then throw.
|
2020-07-02 12:03:15 -04:00
|
|
|
*
|
|
|
|
* Type parameter can be specified to ensure values under comparison have the same type.
|
|
|
|
* For example:
|
|
|
|
*```ts
|
|
|
|
*assertEquals<number>(1, 2)
|
|
|
|
*```
|
2019-03-05 14:58:28 -05:00
|
|
|
*/
|
2020-07-02 12:03:15 -04:00
|
|
|
export function assertEquals(
|
|
|
|
actual: unknown,
|
|
|
|
expected: unknown,
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2020-07-02 12:03:15 -04:00
|
|
|
): void;
|
|
|
|
export function assertEquals<T>(actual: T, expected: T, msg?: string): void;
|
2019-03-06 19:42:24 -05:00
|
|
|
export function assertEquals(
|
2019-03-06 16:39:50 -05:00
|
|
|
actual: unknown,
|
|
|
|
expected: unknown,
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2019-03-06 16:39:50 -05:00
|
|
|
): void {
|
2019-08-14 10:22:31 -04:00
|
|
|
if (equal(actual, expected)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let message = "";
|
2020-07-11 00:52:18 -04:00
|
|
|
const actualString = _format(actual);
|
|
|
|
const expectedString = _format(expected);
|
2019-08-14 10:22:31 -04:00
|
|
|
try {
|
|
|
|
const diffResult = diff(
|
|
|
|
actualString.split("\n"),
|
2020-07-14 15:24:17 -04:00
|
|
|
expectedString.split("\n"),
|
2019-08-14 10:22:31 -04:00
|
|
|
);
|
2020-05-15 09:49:11 -04:00
|
|
|
const diffMsg = buildMessage(diffResult).join("\n");
|
|
|
|
message = `Values are not equal:\n${diffMsg}`;
|
2019-08-14 10:22:31 -04:00
|
|
|
} catch (e) {
|
|
|
|
message = `\n${red(CAN_NOT_DISPLAY)} + \n\n`;
|
|
|
|
}
|
|
|
|
if (msg) {
|
|
|
|
message = msg;
|
|
|
|
}
|
|
|
|
throw new AssertionError(message);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
|
2019-03-07 09:08:19 -05:00
|
|
|
/**
|
|
|
|
* Make an assertion that `actual` and `expected` are not equal, deeply.
|
|
|
|
* If not then throw.
|
2020-07-02 12:03:15 -04:00
|
|
|
*
|
|
|
|
* Type parameter can be specified to ensure values under comparison have the same type.
|
|
|
|
* For example:
|
|
|
|
*```ts
|
|
|
|
*assertNotEquals<number>(1, 2)
|
|
|
|
*```
|
2019-03-07 09:08:19 -05:00
|
|
|
*/
|
2020-07-02 12:03:15 -04:00
|
|
|
export function assertNotEquals(
|
|
|
|
actual: unknown,
|
|
|
|
expected: unknown,
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2020-07-02 12:03:15 -04:00
|
|
|
): void;
|
|
|
|
export function assertNotEquals<T>(actual: T, expected: T, msg?: string): void;
|
2019-03-07 09:08:19 -05:00
|
|
|
export function assertNotEquals(
|
|
|
|
actual: unknown,
|
|
|
|
expected: unknown,
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2019-03-07 09:08:19 -05:00
|
|
|
): void {
|
|
|
|
if (!equal(actual, expected)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let actualString: string;
|
|
|
|
let expectedString: string;
|
|
|
|
try {
|
|
|
|
actualString = String(actual);
|
|
|
|
} catch (e) {
|
|
|
|
actualString = "[Cannot display]";
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
expectedString = String(expected);
|
|
|
|
} catch (e) {
|
|
|
|
expectedString = "[Cannot display]";
|
|
|
|
}
|
|
|
|
if (!msg) {
|
|
|
|
msg = `actual: ${actualString} expected: ${expectedString}`;
|
|
|
|
}
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2019-03-07 09:08:19 -05:00
|
|
|
}
|
|
|
|
|
2019-03-05 14:58:28 -05:00
|
|
|
/**
|
|
|
|
* Make an assertion that `actual` and `expected` are strictly equal. If
|
|
|
|
* not then throw.
|
2020-07-02 12:03:15 -04:00
|
|
|
* ```ts
|
|
|
|
* assertStrictEquals(1, 2)
|
|
|
|
* ```
|
2019-03-05 14:58:28 -05:00
|
|
|
*/
|
2020-08-20 11:56:31 -04:00
|
|
|
export function assertStrictEquals(
|
|
|
|
actual: unknown,
|
|
|
|
expected: unknown,
|
|
|
|
msg?: string,
|
|
|
|
): void;
|
2020-07-02 12:03:15 -04:00
|
|
|
export function assertStrictEquals<T>(
|
|
|
|
actual: T,
|
|
|
|
expected: T,
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2020-08-20 11:56:31 -04:00
|
|
|
): void;
|
|
|
|
export function assertStrictEquals(
|
|
|
|
actual: unknown,
|
|
|
|
expected: unknown,
|
|
|
|
msg?: string,
|
2019-03-05 14:58:28 -05:00
|
|
|
): void {
|
2020-05-15 09:49:11 -04:00
|
|
|
if (actual === expected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let message: string;
|
|
|
|
|
|
|
|
if (msg) {
|
|
|
|
message = msg;
|
|
|
|
} else {
|
2020-07-11 00:52:18 -04:00
|
|
|
const actualString = _format(actual);
|
|
|
|
const expectedString = _format(expected);
|
2020-05-15 09:49:11 -04:00
|
|
|
|
|
|
|
if (actualString === expectedString) {
|
|
|
|
const withOffset = actualString
|
|
|
|
.split("\n")
|
2020-07-11 00:52:18 -04:00
|
|
|
.map((l) => ` ${l}`)
|
2020-05-15 09:49:11 -04:00
|
|
|
.join("\n");
|
2020-07-14 15:24:17 -04:00
|
|
|
message =
|
|
|
|
`Values have the same structure but are not reference-equal:\n\n${
|
|
|
|
red(withOffset)
|
|
|
|
}\n`;
|
2020-05-15 09:49:11 -04:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const diffResult = diff(
|
|
|
|
actualString.split("\n"),
|
2020-07-14 15:24:17 -04:00
|
|
|
expectedString.split("\n"),
|
2020-05-15 09:49:11 -04:00
|
|
|
);
|
|
|
|
const diffMsg = buildMessage(diffResult).join("\n");
|
|
|
|
message = `Values are not strictly equal:\n${diffMsg}`;
|
|
|
|
} catch (e) {
|
|
|
|
message = `\n${red(CAN_NOT_DISPLAY)} + \n\n`;
|
|
|
|
}
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-15 09:49:11 -04:00
|
|
|
|
|
|
|
throw new AssertionError(message);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:56:31 -04:00
|
|
|
/**
|
2020-09-16 06:42:51 -04:00
|
|
|
* Make an assertion that `actual` and `expected` are not strictly equal.
|
2020-08-20 11:56:31 -04:00
|
|
|
* If the values are strictly equal then throw.
|
|
|
|
* ```ts
|
|
|
|
* assertNotStrictEquals(1, 1)
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function assertNotStrictEquals(
|
|
|
|
actual: unknown,
|
|
|
|
expected: unknown,
|
|
|
|
msg?: string,
|
|
|
|
): void;
|
|
|
|
export function assertNotStrictEquals<T>(
|
|
|
|
actual: T,
|
|
|
|
expected: T,
|
|
|
|
msg?: string,
|
|
|
|
): void;
|
|
|
|
export function assertNotStrictEquals(
|
|
|
|
actual: unknown,
|
|
|
|
expected: unknown,
|
|
|
|
msg?: string,
|
|
|
|
): void {
|
|
|
|
if (actual !== expected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new AssertionError(
|
|
|
|
msg ?? `Expected "actual" to be strictly unequal to: ${_format(actual)}\n`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-26 11:46:38 -04:00
|
|
|
/**
|
|
|
|
* Make an assertion that actual is not null or undefined. If not
|
|
|
|
* then thrown.
|
|
|
|
*/
|
|
|
|
export function assertExists(
|
|
|
|
actual: unknown,
|
|
|
|
msg?: string,
|
|
|
|
): void {
|
|
|
|
if (actual === undefined || actual === null) {
|
|
|
|
if (!msg) {
|
|
|
|
msg =
|
|
|
|
`actual: "${actual}" expected to match anything but null or undefined`;
|
|
|
|
}
|
|
|
|
throw new AssertionError(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 14:58:28 -05:00
|
|
|
/**
|
2020-10-26 11:03:30 -04:00
|
|
|
* Make an assertion that actual includes expected. If not
|
2019-03-05 14:58:28 -05:00
|
|
|
* then thrown.
|
|
|
|
*/
|
2020-10-26 11:03:30 -04:00
|
|
|
export function assertStringIncludes(
|
2019-03-05 14:58:28 -05:00
|
|
|
actual: string,
|
|
|
|
expected: string,
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2019-03-05 14:58:28 -05:00
|
|
|
): void {
|
|
|
|
if (!actual.includes(expected)) {
|
|
|
|
if (!msg) {
|
2020-05-25 13:32:34 -04:00
|
|
|
msg = `actual: "${actual}" expected to contain: "${expected}"`;
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 09:08:19 -05:00
|
|
|
/**
|
2020-10-26 11:03:30 -04:00
|
|
|
* Make an assertion that `actual` includes the `expected` values.
|
2020-07-02 12:03:15 -04:00
|
|
|
* If not then an error will be thrown.
|
|
|
|
*
|
|
|
|
* Type parameter can be specified to ensure values under comparison have the same type.
|
|
|
|
* For example:
|
|
|
|
*```ts
|
2020-10-26 11:03:30 -04:00
|
|
|
*assertArrayIncludes<number>([1, 2], [2])
|
2020-07-02 12:03:15 -04:00
|
|
|
*```
|
2019-03-07 09:08:19 -05:00
|
|
|
*/
|
2020-10-26 11:03:30 -04:00
|
|
|
export function assertArrayIncludes(
|
2020-07-02 12:03:15 -04:00
|
|
|
actual: ArrayLike<unknown>,
|
|
|
|
expected: ArrayLike<unknown>,
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2020-07-02 12:03:15 -04:00
|
|
|
): void;
|
2020-10-26 11:03:30 -04:00
|
|
|
export function assertArrayIncludes<T>(
|
2020-07-02 12:03:15 -04:00
|
|
|
actual: ArrayLike<T>,
|
|
|
|
expected: ArrayLike<T>,
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2020-07-02 12:03:15 -04:00
|
|
|
): void;
|
2020-10-26 11:03:30 -04:00
|
|
|
export function assertArrayIncludes(
|
2020-06-24 08:29:50 -04:00
|
|
|
actual: ArrayLike<unknown>,
|
|
|
|
expected: ArrayLike<unknown>,
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2019-03-12 01:51:51 -04:00
|
|
|
): void {
|
2019-10-05 12:02:34 -04:00
|
|
|
const missing: unknown[] = [];
|
2019-03-07 09:08:19 -05:00
|
|
|
for (let i = 0; i < expected.length; i++) {
|
|
|
|
let found = false;
|
|
|
|
for (let j = 0; j < actual.length; j++) {
|
|
|
|
if (equal(expected[i], actual[j])) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
missing.push(expected[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (missing.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!msg) {
|
2020-10-26 11:03:30 -04:00
|
|
|
msg = `actual: "${_format(actual)}" expected to include: "${
|
2020-07-14 15:24:17 -04:00
|
|
|
_format(expected)
|
|
|
|
}"\nmissing: ${_format(missing)}`;
|
2019-03-07 09:08:19 -05:00
|
|
|
}
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2019-03-07 09:08:19 -05:00
|
|
|
}
|
|
|
|
|
2019-03-05 14:58:28 -05:00
|
|
|
/**
|
|
|
|
* Make an assertion that `actual` match RegExp `expected`. If not
|
|
|
|
* then thrown
|
|
|
|
*/
|
|
|
|
export function assertMatch(
|
|
|
|
actual: string,
|
|
|
|
expected: RegExp,
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2019-03-05 14:58:28 -05:00
|
|
|
): void {
|
|
|
|
if (!expected.test(actual)) {
|
|
|
|
if (!msg) {
|
|
|
|
msg = `actual: "${actual}" expected to match: "${expected}"`;
|
|
|
|
}
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2020-08-27 05:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an assertion that `actual` not match RegExp `expected`. If match
|
|
|
|
* then thrown
|
|
|
|
*/
|
|
|
|
export function assertNotMatch(
|
|
|
|
actual: string,
|
|
|
|
expected: RegExp,
|
|
|
|
msg?: string,
|
|
|
|
): void {
|
|
|
|
if (expected.test(actual)) {
|
|
|
|
if (!msg) {
|
|
|
|
msg = `actual: "${actual}" expected to not match: "${expected}"`;
|
|
|
|
}
|
|
|
|
throw new AssertionError(msg);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 12:53:27 -04:00
|
|
|
/**
|
|
|
|
* Make an assertion that `actual` object is a subset of `expected` object, deeply.
|
|
|
|
* If not, then throw.
|
|
|
|
*/
|
|
|
|
export function assertObjectMatch(
|
|
|
|
actual: Record<PropertyKey, unknown>,
|
|
|
|
expected: Record<PropertyKey, unknown>,
|
|
|
|
): void {
|
|
|
|
type loose = Record<PropertyKey, unknown>;
|
|
|
|
const seen = new WeakMap();
|
|
|
|
return assertEquals(
|
|
|
|
(function filter(a: loose, b: loose): loose {
|
|
|
|
// Prevent infinite loop with circular references with same filter
|
|
|
|
if ((seen.has(a)) && (seen.get(a) === b)) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
seen.set(a, b);
|
|
|
|
// Filter keys and symbols which are present in both actual and expected
|
|
|
|
const filtered = {} as loose;
|
|
|
|
const entries = [
|
|
|
|
...Object.getOwnPropertyNames(a),
|
|
|
|
...Object.getOwnPropertySymbols(a),
|
|
|
|
]
|
|
|
|
.filter((key) => key in b)
|
|
|
|
.map((key) => [key, a[key as string]]) as Array<[string, unknown]>;
|
|
|
|
// Build filtered object and filter recursively on nested objects references
|
|
|
|
for (const [key, value] of entries) {
|
|
|
|
if (typeof value === "object") {
|
|
|
|
const subset = (b as loose)[key];
|
|
|
|
if ((typeof subset === "object") && (subset)) {
|
|
|
|
filtered[key] = filter(value as loose, subset as loose);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filtered[key] = value;
|
|
|
|
}
|
|
|
|
return filtered;
|
|
|
|
})(actual, expected),
|
|
|
|
expected,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-05 14:58:28 -05:00
|
|
|
/**
|
|
|
|
* Forcefully throws a failed assertion
|
|
|
|
*/
|
|
|
|
export function fail(msg?: string): void {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
|
|
assert(false, `Failed assertion${msg ? `: ${msg}` : "."}`);
|
|
|
|
}
|
|
|
|
|
2020-06-23 10:47:09 -04:00
|
|
|
/**
|
|
|
|
* Executes a function, expecting it to throw. If it does not, then it
|
2019-03-05 14:58:28 -05:00
|
|
|
* throws. An error class and a string that should be included in the
|
|
|
|
* error message can also be asserted.
|
|
|
|
*/
|
2020-06-04 10:43:05 -04:00
|
|
|
export function assertThrows<T = void>(
|
|
|
|
fn: () => T,
|
2019-03-05 14:58:28 -05:00
|
|
|
ErrorClass?: Constructor,
|
|
|
|
msgIncludes = "",
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2019-11-14 22:22:33 -05:00
|
|
|
): Error {
|
2019-03-05 14:58:28 -05:00
|
|
|
let doesThrow = false;
|
2019-11-14 22:22:33 -05:00
|
|
|
let error = null;
|
2019-03-05 14:58:28 -05:00
|
|
|
try {
|
|
|
|
fn();
|
|
|
|
} catch (e) {
|
2020-06-23 10:47:09 -04:00
|
|
|
if (e instanceof Error === false) {
|
|
|
|
throw new AssertionError("A non-Error object was thrown.");
|
|
|
|
}
|
2020-07-14 14:41:05 -04:00
|
|
|
if (ErrorClass && !(e instanceof ErrorClass)) {
|
2020-07-14 15:24:17 -04:00
|
|
|
msg =
|
|
|
|
`Expected error to be instance of "${ErrorClass.name}", but was "${e.constructor.name}"${
|
|
|
|
msg ? `: ${msg}` : "."
|
|
|
|
}`;
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
2020-06-02 23:38:46 -04:00
|
|
|
if (
|
|
|
|
msgIncludes &&
|
|
|
|
!stripColor(e.message).includes(stripColor(msgIncludes))
|
|
|
|
) {
|
2020-07-14 15:24:17 -04:00
|
|
|
msg =
|
|
|
|
`Expected error message to include "${msgIncludes}", but got "${e.message}"${
|
|
|
|
msg ? `: ${msg}` : "."
|
|
|
|
}`;
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
doesThrow = true;
|
2019-11-14 22:22:33 -05:00
|
|
|
error = e;
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
if (!doesThrow) {
|
|
|
|
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
2019-11-14 22:22:33 -05:00
|
|
|
return error;
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:47:09 -04:00
|
|
|
/**
|
|
|
|
* Executes a function which returns a promise, expecting it to throw or reject.
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-06-04 10:43:05 -04:00
|
|
|
export async function assertThrowsAsync<T = void>(
|
|
|
|
fn: () => Promise<T>,
|
2019-03-05 14:58:28 -05:00
|
|
|
ErrorClass?: Constructor,
|
|
|
|
msgIncludes = "",
|
2020-07-14 15:24:17 -04:00
|
|
|
msg?: string,
|
2019-11-14 22:22:33 -05:00
|
|
|
): Promise<Error> {
|
2019-03-05 14:58:28 -05:00
|
|
|
let doesThrow = false;
|
2019-11-14 22:22:33 -05:00
|
|
|
let error = null;
|
2019-03-05 14:58:28 -05:00
|
|
|
try {
|
|
|
|
await fn();
|
|
|
|
} catch (e) {
|
2020-06-23 10:47:09 -04:00
|
|
|
if (e instanceof Error === false) {
|
|
|
|
throw new AssertionError("A non-Error object was thrown or rejected.");
|
|
|
|
}
|
2020-07-14 14:41:05 -04:00
|
|
|
if (ErrorClass && !(e instanceof ErrorClass)) {
|
2020-07-14 15:24:17 -04:00
|
|
|
msg =
|
|
|
|
`Expected error to be instance of "${ErrorClass.name}", but got "${e.name}"${
|
|
|
|
msg ? `: ${msg}` : "."
|
|
|
|
}`;
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
2020-06-02 23:38:46 -04:00
|
|
|
if (
|
|
|
|
msgIncludes &&
|
|
|
|
!stripColor(e.message).includes(stripColor(msgIncludes))
|
|
|
|
) {
|
2020-07-14 15:24:17 -04:00
|
|
|
msg =
|
|
|
|
`Expected error message to include "${msgIncludes}", but got "${e.message}"${
|
|
|
|
msg ? `: ${msg}` : "."
|
|
|
|
}`;
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
doesThrow = true;
|
2019-11-14 22:22:33 -05:00
|
|
|
error = e;
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
|
|
|
if (!doesThrow) {
|
|
|
|
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg);
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
2019-11-14 22:22:33 -05:00
|
|
|
return error;
|
2019-03-05 14:58:28 -05:00
|
|
|
}
|
2019-03-08 02:32:46 -05:00
|
|
|
|
|
|
|
/** Use this to stub out methods that will throw when invoked. */
|
|
|
|
export function unimplemented(msg?: string): never {
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError(msg || "unimplemented");
|
2019-03-08 02:32:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Use this to assert unreachable code. */
|
|
|
|
export function unreachable(): never {
|
2019-03-08 16:04:43 -05:00
|
|
|
throw new AssertionError("unreachable");
|
2019-03-08 02:32:46 -05:00
|
|
|
}
|