From 64957d92efdfbb8008e51c9d501b0d6754a777b7 Mon Sep 17 00:00:00 2001 From: James Wright Date: Wed, 30 Oct 2019 00:23:54 +0000 Subject: [PATCH] Support for deep `Map` equality with `asserts#equal` (#3236) --- std/testing/asserts.ts | 13 +++++++++++++ std/testing/asserts_test.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts index 0f33bb3d24..b242184b3e 100644 --- a/std/testing/asserts.ts +++ b/std/testing/asserts.ts @@ -81,6 +81,19 @@ export function equal(c: unknown, d: unknown): boolean { } return true; } + if (a && b && a instanceof Map && b instanceof Map) { + if (a.size !== b.size) { + return false; + } + + for (const [key, value] of a) { + if (!compare(value, b.get(key))) { + return false; + } + } + + return true; + } // Have to render RegExp & Date for string comparison // unless it's mistreated as object if ( diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts index b480fe7c93..062fac8cca 100644 --- a/std/testing/asserts_test.ts +++ b/std/testing/asserts_test.ts @@ -47,6 +47,42 @@ test(function testingEqual(): void { assert(!equal(new Set([1, 2]), new Set([3, 2, 1]))); assert(!equal(new Set([1, 2, 3]), new Set([4, 5, 6]))); assert(equal(new Set("denosaurus"), new Set("denosaurussss"))); + assert(equal(new Map(), new Map())); + assert( + equal( + new Map([["foo", "bar"], ["baz", "baz"]]), + new Map([["foo", "bar"], ["baz", "baz"]]) + ) + ); + assert( + equal( + new Map([["foo", new Map([["bar", "baz"]])]]), + new Map([["foo", new Map([["bar", "baz"]])]]) + ) + ); + assert( + equal( + new Map([["foo", { bar: "baz" }]]), + new Map([["foo", { bar: "baz" }]]) + ) + ); + assert( + equal( + new Map([["foo", "bar"], ["baz", "qux"]]), + new Map([["baz", "qux"], ["foo", "bar"]]) + ) + ); + assert(equal(new Map([["foo", ["bar"]]]), new Map([["foo", ["bar"]]]))); + assert(!equal(new Map([["foo", "bar"]]), new Map([["bar", "baz"]]))); + assert( + !equal(new Map([["foo", "bar"]]), new Map([["foo", "bar"], ["bar", "baz"]])) + ); + assert( + !equal( + new Map([["foo", new Map([["bar", "baz"]])]]), + new Map([["foo", new Map([["bar", "qux"]])]]) + ) + ); }); test(function testingNotEquals(): void {