Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
assert: fix memos semantics
This commit changes semantics of the memos cycles tracker. Before
the change it was used to track all currently wisited nodes of an
object tree, which is a bit shifted from its original intention of
tracking cycles. The change brings intended semantics, by tracking
only objects of the current branch of the object tree.

Fixes: #13314
  • Loading branch information
rmdm committed Jun 1, 2017
commit 2d4a64561d829011245ea70729730f3e57318723
27 changes: 16 additions & 11 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,27 @@ function _deepEqual(actual, expected, strict, memos) {
// Use memos to handle cycles.
if (!memos) {
memos = {
actual: { map: new Map(), position: 0 },
expected: { map: new Map(), position: 0 }
actual: new Map(),
expected: new Map(),
position: 0
};
}

const actualPosition = memos.actual.map.get(actual);
if (actualPosition !== undefined) {
return actualPosition === memos.expected.map.get(expected);
} else {
memos.actual.map.set(actual, memos.actual.position++);
memos.position++;
}
if (!memos.expected.map.has(expected)) {
memos.expected.map.set(expected, memos.expected.position++);

if (memos.actual.has(actual)) {
return memos.actual.get(actual) === memos.expected.get(expected);
}

return objEquiv(actual, expected, strict, memos);
memos.actual.set(actual, memos.position);
memos.expected.set(expected, memos.position);

const areEq = objEquiv(actual, expected, strict, memos);

memos.actual.delete(actual);
memos.expected.delete(expected);

return areEq;
}

function setHasSimilarElement(set, val1, strict, memo) {
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ 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]]));

const a = [ 1, 2 ];
const b = [ 3, 4 ];
const c = [ 1, 2 ];
const d = [ 3, 4 ];

assertDeepAndStrictEqual(
{ a: a, b: b, s: new Set([a, b]) },
{ a: c, b: d, s: new Set([d, c]) }
);

assertDeepAndStrictEqual(new Map([[1, 1], [2, 2]]), new Map([[1, 1], [2, 2]]));
assertDeepAndStrictEqual(new Map([[1, 1], [2, 2]]), new Map([[2, 2], [1, 1]]));
assertNotDeepOrStrict(new Map([[1, 1], [2, 2]]), new Map([[1, 2], [2, 1]]));
Expand Down