From 2a21991c34eaf4332bf792cc90af419c3b26dbe6 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 30 Nov 2018 10:17:51 +0100 Subject: [PATCH 1/3] assert: fix loose deepEqual map comparison Loose map comparison had an logic error. It will now be properly compared. --- lib/internal/util/comparisons.js | 4 +--- test/parallel/test-assert-deep.js | 8 ++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index 9adffea09a14c5..43a8921f657130 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -416,9 +416,7 @@ function mapMightHaveLoosePrim(a, b, prim, item, memo) { !innerDeepEqual(item, curB, false, memo)) { return false; } - const curA = a.get(altValue); - return curA === undefined && a.has(altValue) || - innerDeepEqual(item, curA, false, memo); + return !a.has(altValue) && innerDeepEqual(item, curB, false, memo); } function setEquiv(a, b, strict, memo) { diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index ddbc5e78eafcaf..4afc98351dd3de 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -394,6 +394,14 @@ assertOnlyDeepEqual( new Map([[1, {}]]), new Map([[true, {}]]) ); +assertOnlyDeepEqual( + new Map([[undefined, true]]), + new Map([[null, true]]) +); +assertNotDeepOrStrict( + new Map([[undefined, true]]), + new Map([[true, true]]) +); // GH-6416. Make sure circular refs don't throw. { From 02c2bd7b974b5ffe0a7ae2bce3534e56f1dd30ac Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 30 Nov 2018 10:21:14 +0100 Subject: [PATCH 2/3] assert,util: fix sparse array comparison Comparing sparse arrays did not work properly. That is fixed and tests were added to verify that everything works as expected. This had an impact on `util.isDeepStrictEqual()` and `assert.deepStrictEqual()` and their counterpart `assert.notDeepStrictEqual()`. --- lib/internal/util/comparisons.js | 3 +-- test/parallel/test-assert-deep.js | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index 43a8921f657130..905946fdee3f44 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -558,11 +558,10 @@ function objEquiv(a, b, strict, keys, memos, iterationType) { } else { // Array is sparse. const keysA = objectKeys(a); - i++; for (; i < keysA.length; i++) { const key = keysA[i]; if (!hasOwnProperty(b, key) || - !innerDeepEqual(a[key], b[i], strict, memos)) { + !innerDeepEqual(a[key], b[key], strict, memos)) { return false; } } diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index 4afc98351dd3de..24268d449e8b7a 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -571,9 +571,20 @@ assertNotDeepOrStrict( assertDeepAndStrictEqual(m3, m4); } -// Handle sparse arrays -assertDeepAndStrictEqual([1, , , 3], [1, , , 3]); -assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]); +// Handle sparse arrays. +{ + assertDeepAndStrictEqual([1, , , 3], [1, , , 3]); + assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]); + const a = new Array(3); + const b = new Array(3); + a[2] = true; + b[1] = true; + assertNotDeepOrStrict(a, b); + b[2] = true; + assertNotDeepOrStrict(a, b); + a[0] = true; + assertNotDeepOrStrict(a, b); +} // Handle different error messages { From d851e087119f7b017fefffdaa66c1b36e3e1c9d5 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 30 Nov 2018 10:23:07 +0100 Subject: [PATCH 3/3] test: improve comparison coverage to 100% --- test/parallel/test-assert-deep.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index 24268d449e8b7a..fc63d95f641b66 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -239,6 +239,7 @@ assertNotDeepOrStrict(new Set([1, 2, 3, 4]), new Set([1, 2, 3])); assertDeepAndStrictEqual(new Set(['1', '2', '3']), new Set(['1', '2', '3'])); assertDeepAndStrictEqual(new Set([[1, 2], [3, 4]]), new Set([[3, 4], [1, 2]])); assertNotDeepOrStrict(new Set([{ a: 0 }]), new Set([{ a: 1 }])); +assertNotDeepOrStrict(new Set([Symbol()]), new Set([Symbol()])); { const a = [ 1, 2 ]; @@ -636,6 +637,8 @@ assertDeepAndStrictEqual(-0, -0); Object.defineProperty(obj2, Symbol(), { value: 1 }); assertOnlyDeepEqual(obj1, obj3); assertDeepAndStrictEqual(obj1, obj2); + obj2[Symbol()] = true; + assertOnlyDeepEqual(obj1, obj2); // TypedArrays have a fast path. Test for this as well. const a = new Uint8Array(4); const b = new Uint8Array(4);