Skip to content

Commit 68b723d

Browse files
committed
assert: add support for Set and Map
Previously, assert.deepEqual used on maps and sets was not functioning correctly. This change adds support for correctly comparing Maps and Sets. For Maps, comparison is done by iterating over each item in the first Map and verifying that the key exists and the value is the same (depending on whether strict is applied). For Sets, each item is iterated over in the first Set and checked if it exists in the other Set. For both Maps and Sets, the size of both being compared is checked as well.
1 parent 5d2acfb commit 68b723d

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

lib/assert.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,36 @@ function isArguments(object) {
185185
return Object.prototype.toString.call(object) == '[object Arguments]';
186186
}
187187

188+
function compareMaps(a, b, strict) {
189+
// check size of the maps
190+
if (a.size !== b.size)
191+
return false;
192+
193+
// iterate over all keys and check if they exist in the other
194+
for (let kvs of a) {
195+
const key = kvs[0];
196+
const val = kvs[1];
197+
if (!b.has(key)) {
198+
return false;
199+
}
200+
201+
if (!_deepEqual(val, b.get(key), strict))
202+
return false;
203+
}
204+
return true;
205+
}
206+
207+
function compareSets(a, b) {
208+
if (a.size !== b.size)
209+
return false;
210+
211+
for (let item of a) {
212+
if (!b.has(item))
213+
return false;
214+
}
215+
return true;
216+
}
217+
188218
function objEquiv(a, b, strict) {
189219
if (a === null || a === undefined || b === null || b === undefined)
190220
return false;
@@ -202,6 +232,13 @@ function objEquiv(a, b, strict) {
202232
b = pSlice.call(b);
203233
return _deepEqual(a, b, strict);
204234
}
235+
const aStr = Object.prototype.toString.call(a);
236+
const bStr = Object.prototype.toString.call(b);
237+
if (aStr === '[object Map]' && bStr === '[object Map]') {
238+
return compareMaps(a, b, strict);
239+
} else if (aStr === '[object Set]' && bStr === '[object Set]') {
240+
return compareSets(a, b);
241+
}
205242
var ka = Object.keys(a),
206243
kb = Object.keys(b),
207244
key, i;

test/parallel/test-assert.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,63 @@ testBlockTypeError(assert.doesNotThrow, null);
465465
testBlockTypeError(assert.throws, undefined);
466466
testBlockTypeError(assert.doesNotThrow, undefined);
467467

468+
// Map deep equal, deep strict equal
469+
const map1 = new Map();
470+
const map2 = new Map();
471+
map1.set('1', '2');
472+
map2.set('1', '2');
473+
assert.deepEqual(map1, map2);
474+
map1.set('2', '2');
475+
assert.throws(function() {
476+
assert.deepEqual(map1, map2);
477+
});
478+
map1.delete('2');
479+
map2.set('1', 2);
480+
assert.deepEqual(map1, map2);
481+
assert.throws(function() {
482+
assert.deepStrictEqual(map1, map2);
483+
});
484+
map1.clear();
485+
map2.clear();
486+
map1.set(NaN, 'not a number');
487+
map2.set(NaN, 'not a number');
488+
assert.deepEqual(map1, map2);
489+
map1.clear();
490+
map2.clear();
491+
map1.set('1', { name: 'test' });
492+
map2.set('1', { name: 'test' });
493+
assert.deepStrictEqual(map1, map2);
494+
map1.clear();
495+
map2.clear();
496+
// verify that insertion order doesn't matter
497+
map1.set('1', '1');
498+
map1.set('2', '2');
499+
map2.set('2', '2');
500+
map2.set('1', '1');
501+
assert.deepEqual(map1, map2);
502+
503+
// Set deep equal, deep strict equal
504+
var set1 = new Set();
505+
var set2 = new Set();
506+
set1.add(1);
507+
set2.add(1);
508+
assert.deepEqual(set1, set2);
509+
set1.add(2);
510+
assert.throws(function() {
511+
assert.deepEqual(set1, set2);
512+
});
513+
set1.delete(2);
514+
set2.delete(1);
515+
set2.add('1');
516+
// throws because we can't really check without being strict
517+
assert.throws(function() {
518+
assert.deepEqual(set1, set2);
519+
});
520+
521+
assert.deepEqual(new Set([NaN]), new Set([NaN]));
522+
// verify that insertion order doesn't matter
523+
set1 = new Set([1, 2]);
524+
set2 = new Set([2, 1]);
525+
assert.deepEqual(set1, set2);
526+
468527
console.log('All OK');

0 commit comments

Comments
 (0)