GeezerVison is showing github.com — assert: make Maps be partially compared in partialDeepStrictEqual · nodejs/node@e902477
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 |
|---|
@@ -159,12 +159,26 @@ describe('Object Comparison Tests', () => { | 159 | 159 | }, | 160 | 160 | { | 161 | 161 | description: | 162 | | - 'throws when comparing two Map objects with different length', | | 162 | + 'throws when the expected Map has more entries than the actual Map', | 163 | 163 | actual: new Map([ | 164 | 164 | ['key1', 'value1'], | 165 | 165 | ['key2', 'value2'], | 166 | 166 | ]), | 167 | | - expected: new Map([['key1', 'value1']]), | | 167 | + expected: new Map([ | | 168 | + ['key1', 'value1'], | | 169 | + ['key2', 'value2'], | | 170 | + ['key3', 'value3'], | | 171 | + ]), | | 172 | + }, | | 173 | + { | | 174 | + description: 'throws when the nested array in the Map is not a subset of the other nested array', | | 175 | + actual: new Map([ | | 176 | + ['key1', ['value1', 'value2']], | | 177 | + ['key2', 'value2'], | | 178 | + ]), | | 179 | + expected: new Map([ | | 180 | + ['key1', ['value3']], | | 181 | + ]), | 168 | 182 | }, | 169 | 183 | { | 170 | 184 | description: | @@ -554,6 +568,63 @@ describe('Object Comparison Tests', () => { | 554 | 568 | ['key2', 'value2'], | 555 | 569 | ]), | 556 | 570 | }, | | 571 | + { | | 572 | + description: | | 573 | + 'compares two Map objects where expected is a subset of actual', | | 574 | + actual: new Map([ | | 575 | + ['key1', 'value1'], | | 576 | + ['key2', 'value2'], | | 577 | + ]), | | 578 | + expected: new Map([['key1', 'value1']]), | | 579 | + }, | | 580 | + { | | 581 | + description: | | 582 | + 'compares two deeply nested Maps', | | 583 | + actual: { | | 584 | + a: { | | 585 | + b: { | | 586 | + c: new Map([ | | 587 | + ['key1', 'value1'], | | 588 | + ['key2', 'value2'], | | 589 | + ]) | | 590 | + }, | | 591 | + z: [1, 2, 3] | | 592 | + } | | 593 | + }, | | 594 | + expected: { | | 595 | + a: { | | 596 | + z: [1, 2, 3], | | 597 | + b: { | | 598 | + c: new Map([['key1', 'value1']]) | | 599 | + } | | 600 | + } | | 601 | + }, | | 602 | + }, | | 603 | + { | | 604 | + description: 'compares Maps nested into Maps', | | 605 | + actual: new Map([ | | 606 | + ['key1', new Map([ | | 607 | + ['nestedKey1', 'nestedValue1'], | | 608 | + ['nestedKey2', 'nestedValue2'], | | 609 | + ])], | | 610 | + ['key2', 'value2'], | | 611 | + ]), | | 612 | + expected: new Map([ | | 613 | + ['key1', new Map([ | | 614 | + ['nestedKey1', 'nestedValue1'], | | 615 | + ])], | | 616 | + ]) | | 617 | + }, | | 618 | + { | | 619 | + description: 'compares Maps with nested arrays inside', | | 620 | + actual: new Map([ | | 621 | + ['key1', ['value1', 'value2']], | | 622 | + ['key2', 'value2'], | | 623 | + ]), | | 624 | + expected: new Map([ | | 625 | + ['key1', ['value1', 'value2']], | | 626 | + ]), | | 627 | + }, | 557 | 628 | { | 558 | 629 | description: | 559 | 630 | 'compares two objects with identical getter/setter properties', | |
|