Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
assert: .deepEqual() support for RegExp objects
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte authored and bnoordhuis committed Dec 19, 2011
1 parent 213b8af commit a805012
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/assert.js
Expand Up @@ -170,12 +170,17 @@ function _deepEqual(actual, expected) {
} else if (actual instanceof Date && expected instanceof Date) {
return actual.getTime() === expected.getTime();

// 7.3. Other pairs that do not both pass typeof value == 'object',
// 7.3 If the expected value is a RegExp object, the actual value is
// equivalent if it is also a RegExp object with the same source.
} else if (actual instanceof RegExp && expected instanceof RegExp) {
return actual.source === expected.source;

// 7.4. Other pairs that do not both pass typeof value == 'object',
// equivalence is determined by ==.
} else if (typeof actual != 'object' && typeof expected != 'object') {
return actual == expected;

// 7.4. For all other Object pairs, including Array objects, equivalence is
// 7.5 For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
Expand Down
7 changes: 6 additions & 1 deletion test/simple/test-assert.js
Expand Up @@ -82,13 +82,18 @@ assert.throws(makeBlock(a.deepEqual, new Date(), new Date(2000, 3, 14)),
'deepEqual date');

// 7.3
assert.doesNotThrow(makeBlock(a.deepEqual, /a/, /a/));
assert.throws(makeBlock(a.deepEqual, /ab/, /a/));


// 7.4
assert.doesNotThrow(makeBlock(a.deepEqual, 4, '4'), 'deepEqual == check');
assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check');
assert.throws(makeBlock(a.deepEqual, 4, '5'),
a.AssertionError,
'deepEqual == check');

// 7.4
// 7.5
// having the same number of owned properties && the same set of keys
assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4}, {a: 4}));
assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'}));
Expand Down

0 comments on commit a805012

Please sign in to comment.