Skip to content

Commit

Permalink
Merge pull request Automattic#46 from bramstein/bs-add-regexp
Browse files Browse the repository at this point in the history
Add support for 'regexp' type and eql comparison of regular expressions.
  • Loading branch information
rauchg committed Jun 4, 2013
2 parents fd08107 + 1f94e7d commit e3be486
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
16 changes: 13 additions & 3 deletions expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,10 @@
// typeof with support for 'array'
this.assert(
'array' == type ? isArray(this.obj) :
'object' == type
? 'object' == typeof this.obj && null !== this.obj
: type == typeof this.obj
'regexp' == type ? isRegExp(this.obj) :
'object' == type
? 'object' == typeof this.obj && null !== this.obj
: type == typeof this.obj
, function(){ return 'expected ' + i(this.obj) + ' to be a' + n + ' ' + type }
, function(){ return 'expected ' + i(this.obj) + ' not to be a' + n + ' ' + type });
} else {
Expand Down Expand Up @@ -875,6 +876,10 @@
} else if (typeof actual != 'object' && typeof expected != 'object') {
return actual == expected;

// If both are regular expression use the special `regExpEquiv` method
// to determine equivalence.
} else if (isRegExp(actual) && isRegExp(expected)) {
return regExpEquiv(actual, expected);
// 7.4. 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
Expand All @@ -894,6 +899,11 @@
return Object.prototype.toString.call(object) == '[object Arguments]';
}

function regExpEquiv (a, b) {
return a.source === b.source && a.global === b.global &&
a.ignoreCase === b.ignoreCase && a.multiline === b.multiline;
}

function objEquiv (a, b) {
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
return false;
Expand Down
10 changes: 10 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ describe('expect', function () {
}, 'expected {} to be an array');
});

it('should test regex', function () {
expect(/a/).to.be.an('regexp');
expect(/a/).to.be.a('regexp');

err(function () {
expect(null).to.be.a('regexp');
}, 'expected null to be a regexp');
});

it('should test objects', function () {
expect({}).to.be.an('object');

Expand Down Expand Up @@ -288,6 +297,7 @@ describe('expect', function () {
expect({ foo: 'bar' }).to.eql({ foo: 'bar' });
expect(1).to.eql(1);
expect('4').to.eql(4);
expect(/a/gmi).to.eql(/a/mig);

err(function () {
expect(4).to.eql(3);
Expand Down

0 comments on commit e3be486

Please sign in to comment.