diff options
| author | Vincent LE GOFF <g_n_s@hotmail.fr> | 2019-03-06 22:39:50 +0100 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-03-06 16:39:50 -0500 |
| commit | e36edfdb3fd4709358a5f499f13cfe3d53c2b4f7 (patch) | |
| tree | 1baef3f876a5e75288c3ec9056cdb93dd6b5787f /testing/asserts.ts | |
| parent | d29957ad17956016c35a04f5f1f98565e58e8a2e (diff) | |
Testing refactor (denoland/deno_std#240)
Original: https://github.com/denoland/deno_std/commit/e1d5c00279132aa639030c6c6d9b4e308bd4775e
Diffstat (limited to 'testing/asserts.ts')
| -rw-r--r-- | testing/asserts.ts | 35 |
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); } |
