Skip to content

Commit a805012

Browse files
pgtebnoordhuis
authored andcommitted
assert: .deepEqual() support for RegExp objects
1 parent 213b8af commit a805012

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎lib/assert.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,17 @@ function _deepEqual(actual, expected) {
170170
} else if (actual instanceof Date && expected instanceof Date) {
171171
return actual.getTime() === expected.getTime();
172172

173-
// 7.3. Other pairs that do not both pass typeof value == 'object',
173+
// 7.3 If the expected value is a RegExp object, the actual value is
174+
// equivalent if it is also a RegExp object with the same source.
175+
} else if (actual instanceof RegExp && expected instanceof RegExp) {
176+
return actual.source === expected.source;
177+
178+
// 7.4. Other pairs that do not both pass typeof value == 'object',
174179
// equivalence is determined by ==.
175180
} else if (typeof actual != 'object' && typeof expected != 'object') {
176181
return actual == expected;
177182

178-
// 7.4. For all other Object pairs, including Array objects, equivalence is
183+
// 7.5 For all other Object pairs, including Array objects, equivalence is
179184
// determined by having the same number of owned properties (as verified
180185
// with Object.prototype.hasOwnProperty.call), the same set of keys
181186
// (although not necessarily the same order), equivalent values for every

‎test/simple/test-assert.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,18 @@ assert.throws(makeBlock(a.deepEqual, new Date(), new Date(2000, 3, 14)),
8282
'deepEqual date');
8383

8484
// 7.3
85+
assert.doesNotThrow(makeBlock(a.deepEqual, /a/, /a/));
86+
assert.throws(makeBlock(a.deepEqual, /ab/, /a/));
87+
88+
89+
// 7.4
8590
assert.doesNotThrow(makeBlock(a.deepEqual, 4, '4'), 'deepEqual == check');
8691
assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check');
8792
assert.throws(makeBlock(a.deepEqual, 4, '5'),
8893
a.AssertionError,
8994
'deepEqual == check');
9095

91-
// 7.4
96+
// 7.5
9297
// having the same number of owned properties && the same set of keys
9398
assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4}, {a: 4}));
9499
assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'}));

0 commit comments

Comments
 (0)