Skip to content

Commit

Permalink
Fix comparison objects.
Browse files Browse the repository at this point in the history
Regexp and Date objects can to be deep equal to objects with the same type.
  • Loading branch information
George Kats committed Jun 5, 2013
1 parent ad35240 commit 044dcff
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
6 changes: 4 additions & 2 deletions chai.js
Original file line number Diff line number Diff line change
Expand Up @@ -3129,15 +3129,17 @@ function _deepEqual(actual, expected, memos) {

// 7.2. If the expected value is a Date object, the actual value is
// equivalent if it is also a Date object that refers to the same time.
} else if (actual instanceof Date && expected instanceof Date) {
} else if (expected instanceof Date) {
if (!(actual instanceof Date)) return false;
return actual.getTime() === expected.getTime();

// 7.3. 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;

} else if (actual instanceof RegExp && expected instanceof RegExp){
} else if (expected instanceof RegExp) {
if (!(actual instanceof RegExp)) return false;
return actual.toString() === expected.toString();

// 7.4. For all other Object pairs, including Array objects, equivalence is
Expand Down
6 changes: 4 additions & 2 deletions lib/chai/utils/eql.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ function _deepEqual(actual, expected, memos) {

// 7.2. If the expected value is a Date object, the actual value is
// equivalent if it is also a Date object that refers to the same time.
} else if (actual instanceof Date && expected instanceof Date) {
} else if (expected instanceof Date) {
if (!(actual instanceof Date)) return false;
return actual.getTime() === expected.getTime();

// 7.3. 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;

} else if (actual instanceof RegExp && expected instanceof RegExp){
} else if (expected instanceof RegExp) {
if (!(actual instanceof RegExp)) return false;
return actual.toString() === expected.toString();

// 7.4. For all other Object pairs, including Array objects, equivalence is
Expand Down
13 changes: 11 additions & 2 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,16 @@ suite('assert', function () {
}, "expected { tea: \'chai\' } to deeply equal { tea: \'black\' }");
});

test('deepEqual (ordering)', function () {
test('deepEqual (ordering)', function() {
var a = { a: 'b', c: 'd' }
, b = { c: 'd', a: 'b' };
assert.deepEqual(a, b);
});

test('deepEqual /regexp/', function(){
test('deepEqual /regexp/', function() {
assert.deepEqual(/a/, /a/);
assert.notDeepEqual(/a/, /b/);
assert.notDeepEqual(/a/, {});
assert.deepEqual(/a/g, /a/g);
assert.notDeepEqual(/a/g, /b/g);
assert.deepEqual(/a/i, /a/i);
Expand All @@ -198,6 +199,14 @@ suite('assert', function () {
assert.notDeepEqual(/a/m, /b/m);
});

test('deepEqual (Date)', function() {
var a = new Date(1, 2, 3)
, b = new Date(4, 5, 6);
assert.deepEqual(a, a);
assert.notDeepEqual(a, b);
assert.notDeepEqual(a, {});
});

test('deepEqual (circular)', function() {
var circularObject = {}
, secondCircularObject = {};
Expand Down
9 changes: 9 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ suite('expect', function () {
test('deep.equal(/regexp/)', function(){
expect(/a/).to.deep.equal(/a/);
expect(/a/).not.to.deep.equal(/b/);
expect(/a/).not.to.deep.equal({});
expect(/a/g).to.deep.equal(/a/g);
expect(/a/g).not.to.deep.equal(/b/g);
expect(/a/i).to.deep.equal(/a/i);
Expand All @@ -328,6 +329,14 @@ suite('expect', function () {
expect(/a/m).not.to.deep.equal(/b/m);
});

test('deep.equal(Date)', function(){
var a = new Date(1, 2, 3)
, b = new Date(4, 5, 6);
expect(a).to.deep.equal(a);
expect(a).not.to.deep.equal(b);
expect(a).not.to.deep.equal({});
});

test('empty', function(){
function FakeArgs() {};
FakeArgs.prototype.length = 0;
Expand Down
7 changes: 7 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,17 @@ suite('should', function() {
});

test('eql(val)', function(){
var a = new Date(1, 2, 3)
, b = new Date(4, 5, 6);

a.should.eql(a);
a.should.not.eql(b);
a.should.not.eql({});
'test'.should.eql('test');
({ foo: 'bar' }).should.eql({ foo: 'bar' });
/a/.should.eql(/a/);
/a/.should.not.eql(/b/);
/a/.should.not.eql({});
/a/g.should.eql(/a/g);
/a/g.should.not.eql(/b/g);
/a/i.should.eql(/a/i);
Expand Down

0 comments on commit 044dcff

Please sign in to comment.