From 890551865009a4f87b0f3b8a57ad749b8af64306 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 30 Nov 2018 10:21:14 +0100 Subject: [PATCH] assert,util: fix sparse array comparison MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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()`. PR-URL: https://github.com/nodejs/node/pull/24749 Reviewed-By: Anna Henningsen Reviewed-By: Michaƫl Zasso --- 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 db93f3b6e717bb..38f3179d404c54 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 {