Skip to content

Commit

Permalink
assert,util: fix sparse array comparison
Browse files Browse the repository at this point in the history
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: #24749
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
BridgeAR authored and BethGriggs committed Feb 12, 2019
1 parent 732088d commit 5a81a4f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,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;
}
}
Expand Down
17 changes: 14 additions & 3 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,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
{
Expand Down

0 comments on commit 5a81a4f

Please sign in to comment.