GeezerVison is showing github.com — assert: make Maps be partially compared in partialDeepStrictEqual · nodejs/node@2314e49
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/assert.js +11-7Lines changed: 11 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -385,22 +385,26 @@ const typesToCallDeepStrictEqualWith = [ | 385 | 385 | isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer, | 386 | 386 | ]; | 387 | 387 |
| 388 | | -function compareMaps(actual, expected, comparedObjects) { | 389 | | - if (MapPrototypeGetSize(actual) !== MapPrototypeGetSize(expected)) { | | 388 | +function partiallyCompareMaps(actual, expected, comparedObjects) { | | 389 | + if (MapPrototypeGetSize(expected) > MapPrototypeGetSize(actual)) { | 390 | 390 | return false; | 391 | 391 | } | 392 | | - const safeIterator = FunctionPrototypeCall(SafeMap.prototype[SymbolIterator], actual); | 393 | 392 |
| 394 | 393 | comparedObjects ??= new SafeWeakSet(); | | 394 | + const expectedIterator = FunctionPrototypeCall(SafeMap.prototype[SymbolIterator], expected); | 395 | 395 |
| 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)) { | 398 | 398 | return false; | 399 | 399 | } | 400 | | - if (!compareBranch(val, MapPrototypeGet(expected, key), comparedObjects)) { | | 400 | + | | 401 | + const actualValue = MapPrototypeGet(actual, key); | | 402 | + | | 403 | + if (!compareBranch(actualValue, expectedValue, comparedObjects)) { | 401 | 404 | return false; | 402 | 405 | } | 403 | 406 | } | | 407 | + | 404 | 408 | return true; | 405 | 409 | } | 406 | 410 |
| @@ -553,7 +557,7 @@ function compareBranch( | 553 | 557 | ) { | 554 | 558 | // Check for Map object equality | 555 | 559 | if (isMap(actual) && isMap(expected)) { | 556 | | - return compareMaps(actual, expected, comparedObjects); | | 560 | + return partiallyCompareMaps(actual, expected, comparedObjects); | 557 | 561 | } | 558 | 562 |
| 559 | 563 | if ( | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-assert-objects.js +73-2Lines changed: 73 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -169,12 +169,26 @@ describe('Object Comparison Tests', () => { | 169 | 169 | }, | 170 | 170 | { | 171 | 171 | description: | 172 | | - 'throws when comparing two Map objects with different length', | | 172 | + 'throws when the expected Map has more entries than the actual Map', | 173 | 173 | actual: new Map([ | 174 | 174 | ['key1', 'value1'], | 175 | 175 | ['key2', 'value2'], | 176 | 176 | ]), | 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 | + ]), | 178 | 192 | }, | 179 | 193 | { | 180 | 194 | description: | @@ -564,6 +578,63 @@ describe('Object Comparison Tests', () => { | 564 | 578 | ['key2', 'value2'], | 565 | 579 | ]), | 566 | 580 | }, | | 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 | + }, | 567 | 638 | { | 568 | 639 | description: | 569 | 640 | 'compares two objects with identical getter/setter properties', | |
|