assert,util: fix deep comparison for sets and maps with mixed types · nodejs/node@6a61bcd

2 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -645,8 +645,10 @@ function setObjectEquiv(array, a, b, mode, memo) {

645645

if (b.has(val1)) {

646646

continue;

647647

}

648-

} else if (mode !== kLoose || b.has(val1)) {

648+

} else if (b.has(val1)) {

649649

continue;

650+

} else if (mode !== kLoose) {

651+

return false;

650652

}

651653

}

652654

@@ -792,11 +794,14 @@ function mapObjectEquiv(array, a, b, mode, memo) {

792794

const extraChecks = mode === kLoose || array.length !== a.size;

793795
794796

for (const { 0: key1, 1: item1 } of a) {

795-

if (extraChecks &&

796-

(typeof key1 !== 'object' || key1 === null) &&

797-

(mode !== kLoose ||

798-

(b.has(key1) && innerDeepEqual(item1, b.get(key1), mode, memo)))) { // Mixed mode

799-

continue;

797+

if (extraChecks && (typeof key1 !== 'object' || key1 === null)) {

798+

if (b.has(key1)) {

799+

if (mode !== kLoose || innerDeepEqual(item1, b.get(key1), mode, memo)) {

800+

continue;

801+

}

802+

} else if (mode !== kLoose) {

803+

return false;

804+

}

800805

}

801806
802807

let innerStart = start;

Original file line numberDiff line numberDiff line change

@@ -634,6 +634,21 @@ test('Handle sparse arrays', () => {

634634

assertNotDeepOrStrict(a, b, AssertionError, { partial: 'pass' });

635635

});

636636
637+

test('Handle sets and maps with mixed keys', () => {

638+

// https://github.com/nodejs/node/issues/61386

639+

const aSet = new Set([0, new Set([1, 2, 3]), new Set([4, 5, 6])]);

640+

const bSet = new Set([

641+

0,

642+

new Set([1, new Set([2, 3]), new Set([20, 30])]),

643+

new Set([4, new Set([5, 6]), new Set([50, 60])]),

644+

]);

645+

assertNotDeepOrStrict(aSet, bSet);

646+
647+

const aMap = new Map([[0, 'zero'], [1, 'one'], [new Set([1, 2, 3]), 'A']]);

648+

const bMap = new Map([[0, 'zero'], [new Set([1, 2, 3]), 'A'], [new Set([9]), 'B']]);

649+

assertNotDeepOrStrict(aMap, bMap);

650+

});

651+
637652

test('Handle different error messages', () => {

638653

const err1 = new Error('foo1');

639654

assertNotDeepOrStrict(err1, new Error('foo2'), assert.AssertionError);