From eff23abc3280026cb44e3cf41e91bc804afbf969 Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Mon, 22 Apr 2019 16:39:33 +0300 Subject: [PATCH] Support Sets in asserts.equals (denoland/deno_std#350) Original: https://github.com/denoland/deno_std/commit/9432d703291f74b230f39b30614d57a5623faa59 --- testing/asserts.ts | 11 +++++++++++ testing/asserts_test.ts | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/testing/asserts.ts b/testing/asserts.ts index eb5e119692..83fa7c8e50 100644 --- a/testing/asserts.ts +++ b/testing/asserts.ts @@ -16,6 +16,17 @@ export class AssertionError extends Error { export function equal(c: unknown, d: unknown): boolean { const seen = new Map(); return (function compare(a: unknown, b: unknown) { + if (a && a instanceof Set && b && b instanceof Set) { + if (a.size !== b.size) { + return false; + } + for (const item of b) { + if (!a.has(item)) { + return false; + } + } + return true; + } // Have to render RegExp & Date for string comparison // unless it's mistreated as object if ( diff --git a/testing/asserts_test.ts b/testing/asserts_test.ts index 87e0335ac5..ed58d74df8 100644 --- a/testing/asserts_test.ts +++ b/testing/asserts_test.ts @@ -40,6 +40,12 @@ test(function testingEqual() { assert(!equal(/deno/, /node/)); assert(equal(new Date(2019, 0, 3), new Date(2019, 0, 3))); assert(!equal(new Date(2019, 0, 3), new Date(2019, 1, 3))); + assert(equal(new Set([1]), new Set([1]))); + assert(!equal(new Set([1]), new Set([2]))); + assert(equal(new Set([1, 2, 3]), new Set([3, 2, 1]))); + 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"))); }); test(function testingNotEquals() {