diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-11-30 10:21:14 +0100 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-12-03 13:03:44 +0100 |
commit | 4dd56a39f1f1c0e98b223b611c3a869775177c68 (patch) | |
tree | f40edd317a3885fd079f59ae9ec2d68cb2466725 | |
parent | 2eff120f6d4d88c29e692f67e396916ea71418e2 (diff) | |
download | android-node-v8-4dd56a39f1f1c0e98b223b611c3a869775177c68.tar.gz android-node-v8-4dd56a39f1f1c0e98b223b611c3a869775177c68.tar.bz2 android-node-v8-4dd56a39f1f1c0e98b223b611c3a869775177c68.zip |
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()`.
PR-URL: https://github.com/nodejs/node/pull/24749
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
-rw-r--r-- | lib/internal/util/comparisons.js | 3 | ||||
-rw-r--r-- | 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 43a8921f65..905946fdee 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 db93f3b6e7..38f3179d40 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 { |