Skip to content

Commit 2314e49

Browse files
puskin94targos
authored andcommitted
assert: make Maps be partially compared in partialDeepStrictEqual
PR-URL: #56195 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 9863d27 commit 2314e49

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

lib/assert.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -385,22 +385,26 @@ const typesToCallDeepStrictEqualWith = [
385385
isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer,
386386
];
387387

388-
function compareMaps(actual, expected, comparedObjects) {
389-
if (MapPrototypeGetSize(actual) !== MapPrototypeGetSize(expected)) {
388+
function partiallyCompareMaps(actual, expected, comparedObjects) {
389+
if (MapPrototypeGetSize(expected) > MapPrototypeGetSize(actual)) {
390390
return false;
391391
}
392-
const safeIterator = FunctionPrototypeCall(SafeMap.prototype[SymbolIterator], actual);
393392

394393
comparedObjects ??= new SafeWeakSet();
394+
const expectedIterator = FunctionPrototypeCall(SafeMap.prototype[SymbolIterator], expected);
395395

396-
for (const { 0: key, 1: val } of safeIterator) {
397-
if (!MapPrototypeHas(expected, key)) {
396+
for (const { 0: key, 1: expectedValue } of expectedIterator) {
397+
if (!MapPrototypeHas(actual, key)) {
398398
return false;
399399
}
400-
if (!compareBranch(val, MapPrototypeGet(expected, key), comparedObjects)) {
400+
401+
const actualValue = MapPrototypeGet(actual, key);
402+
403+
if (!compareBranch(actualValue, expectedValue, comparedObjects)) {
401404
return false;
402405
}
403406
}
407+
404408
return true;
405409
}
406410

@@ -553,7 +557,7 @@ function compareBranch(
553557
) {
554558
// Check for Map object equality
555559
if (isMap(actual) && isMap(expected)) {
556-
return compareMaps(actual, expected, comparedObjects);
560+
return partiallyCompareMaps(actual, expected, comparedObjects);
557561
}
558562

559563
if (

test/parallel/test-assert-objects.js

+73-2
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,26 @@ describe('Object Comparison Tests', () => {
169169
},
170170
{
171171
description:
172-
'throws when comparing two Map objects with different length',
172+
'throws when the expected Map has more entries than the actual Map',
173173
actual: new Map([
174174
['key1', 'value1'],
175175
['key2', 'value2'],
176176
]),
177-
expected: new Map([['key1', 'value1']]),
177+
expected: new Map([
178+
['key1', 'value1'],
179+
['key2', 'value2'],
180+
['key3', 'value3'],
181+
]),
182+
},
183+
{
184+
description: 'throws when the nested array in the Map is not a subset of the other nested array',
185+
actual: new Map([
186+
['key1', ['value1', 'value2']],
187+
['key2', 'value2'],
188+
]),
189+
expected: new Map([
190+
['key1', ['value3']],
191+
]),
178192
},
179193
{
180194
description:
@@ -564,6 +578,63 @@ describe('Object Comparison Tests', () => {
564578
['key2', 'value2'],
565579
]),
566580
},
581+
{
582+
description:
583+
'compares two Map objects where expected is a subset of actual',
584+
actual: new Map([
585+
['key1', 'value1'],
586+
['key2', 'value2'],
587+
]),
588+
expected: new Map([['key1', 'value1']]),
589+
},
590+
{
591+
description:
592+
'compares two deeply nested Maps',
593+
actual: {
594+
a: {
595+
b: {
596+
c: new Map([
597+
['key1', 'value1'],
598+
['key2', 'value2'],
599+
])
600+
},
601+
z: [1, 2, 3]
602+
}
603+
},
604+
expected: {
605+
a: {
606+
z: [1, 2, 3],
607+
b: {
608+
c: new Map([['key1', 'value1']])
609+
}
610+
}
611+
},
612+
},
613+
{
614+
description: 'compares Maps nested into Maps',
615+
actual: new Map([
616+
['key1', new Map([
617+
['nestedKey1', 'nestedValue1'],
618+
['nestedKey2', 'nestedValue2'],
619+
])],
620+
['key2', 'value2'],
621+
]),
622+
expected: new Map([
623+
['key1', new Map([
624+
['nestedKey1', 'nestedValue1'],
625+
])],
626+
])
627+
},
628+
{
629+
description: 'compares Maps with nested arrays inside',
630+
actual: new Map([
631+
['key1', ['value1', 'value2']],
632+
['key2', 'value2'],
633+
]),
634+
expected: new Map([
635+
['key1', ['value1', 'value2']],
636+
]),
637+
},
567638
{
568639
description:
569640
'compares two objects with identical getter/setter properties',

0 commit comments

Comments
 (0)