Skip to content

Commit

Permalink
Support for deep Map equality with asserts#equal (denoland/deno#3236
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jamesseanwright authored and ry committed Oct 30, 2019
1 parent c17fce4 commit 3975a5d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions testing/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
36 changes: 36 additions & 0 deletions testing/asserts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3975a5d

Please sign in to comment.