summaryrefslogtreecommitdiff
path: root/testing/asserts.ts
diff options
context:
space:
mode:
Diffstat (limited to 'testing/asserts.ts')
-rw-r--r--testing/asserts.ts35
1 files changed, 33 insertions, 2 deletions
diff --git a/testing/asserts.ts b/testing/asserts.ts
index a2110b8d9..8932793d4 100644
--- a/testing/asserts.ts
+++ b/testing/asserts.ts
@@ -1,11 +1,38 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import { assertEqual as prettyAssertEqual } from "./pretty.ts";
+import { assertEq as prettyAssertEqual } from "./pretty.ts";
interface Constructor {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new (...args: any[]): any;
}
+export function equal(c: unknown, d: unknown): boolean {
+ const seen = new Map();
+ return (function compare(a: unknown, b: unknown) {
+ 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;
+ }
+ 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);
+}
+
/** Make an assertion, if not `true`, then throw. */
export function assert(expr: boolean, msg = ""): void {
if (!expr) {
@@ -17,7 +44,11 @@ export function assert(expr: boolean, msg = ""): void {
* Make an assertion that `actual` and `expected` are equal, deeply. If not
* deeply equal, then throw.
*/
-export function equal(actual: unknown, expected: unknown, msg?: string): void {
+export function assertEq(
+ actual: unknown,
+ expected: unknown,
+ msg?: string
+): void {
prettyAssertEqual(actual, expected, msg);
}