Skip to content
This repository has been archived by the owner on Nov 4, 2020. It is now read-only.

Commit

Permalink
When objects both are empty we check if they are equals.
Browse files Browse the repository at this point in the history
  • Loading branch information
btd committed Feb 2, 2015
1 parent d88f700 commit 1e49897
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
98 changes: 45 additions & 53 deletions lib/ext/contain.js
Expand Up @@ -83,35 +83,31 @@ module.exports = function(should, Assertion) {
this.params = { operator: 'to contain ' + i(other) };

var obj = this.obj;
if(util.isArray(obj)) {
if(util.isArray(other)) {
var otherIdx = 0;
obj.forEach(function(item) {
try {
should(item).containDeepOrdered(other[otherIdx]);
otherIdx++;
} catch(e) {
if(e instanceof should.AssertionError) {
return;
}
throw e;
if(util.isArray(obj) && util.isArray(other)) {
var otherIdx = 0;
obj.forEach(function(item) {
try {
should(item).containDeepOrdered(other[otherIdx]);
otherIdx++;
} catch(e) {
if(e instanceof should.AssertionError) {
return;
}
}, this);
throw e;
}
}, this);

this.assert(otherIdx == other.length);
//search array contain other as sub sequence
} else {
this.assert(false);
}
this.assert(otherIdx == other.length);
} else if(util.isString(obj)) {// expect other to be string
this.assert(obj.indexOf(String(other)) >= 0);
} else if(util.isObject(obj)) {// object contains object case
if(util.isObject(other)) {
util.forOwn(other, function(value, key) {
should(obj[key]).containDeepOrdered(value);
});
} else {//one of the properties contain value
this.assert(false);
} else if(util.isObject(obj) && util.isObject(other)) {// object contains object case
util.forOwn(other, function(value, key) {
should(obj[key]).containDeepOrdered(value);
});

// if both objects is empty means we finish traversing - and we need to compare for hidden values
if(util.isEmptyObject(obj) && util.isEmptyObject(other)) {
this.eql(other);
}
} else {
this.eql(other);
Expand All @@ -135,38 +131,34 @@ module.exports = function(should, Assertion) {
this.params = { operator: 'to contain ' + i(other) };

var obj = this.obj;
if(util.isArray(obj)) {
if(util.isArray(other)) {
var usedKeys = {};
other.forEach(function(otherItem) {
this.assert(obj.some(function(item, index) {
if(index in usedKeys) return false;
if(util.isArray(obj) && util.isArray(other)) {
var usedKeys = {};
other.forEach(function(otherItem) {
this.assert(obj.some(function(item, index) {
if(index in usedKeys) return false;

try {
should(item).containDeep(otherItem);
usedKeys[index] = true;
return true;
} catch(e) {
if(e instanceof should.AssertionError) {
return false;
}
throw e;
try {
should(item).containDeep(otherItem);
usedKeys[index] = true;
return true;
} catch(e) {
if(e instanceof should.AssertionError) {
return false;
}
}));
}, this);

} else {
this.assert(false);
}
throw e;
}
}));
}, this);
} else if(util.isString(obj)) {// expect other to be string
this.assert(obj.indexOf(String(other)) >= 0);
} else if(util.isObject(obj)) {// object contains object case
if(util.isObject(other)) {
util.forOwn(other, function(value, key) {
should(obj[key]).containDeep(value);
});
} else {//one of the properties contain value
this.assert(false);
} else if(util.isObject(obj) && util.isObject(other)) {// object contains object case
util.forOwn(other, function(value, key) {
should(obj[key]).containDeep(value);
});

// if both objects is empty means we finish traversing - and we need to compare for hidden values
if(util.isEmptyObject(obj) && util.isEmptyObject(other)) {
this.eql(other);
}
} else {
this.eql(other);
Expand Down
9 changes: 9 additions & 0 deletions lib/util.js
Expand Up @@ -152,4 +152,13 @@ exports.formatEqlResult = function(r, a, b, format) {
(r.a === a ? '' : ', A has ' + format(r.a)) +
(r.b === b ? '' : ' and B has ' + format(r.b)) +
(r.showReason ? ' because ' + r.reason: '')).trim();
};

exports.isEmptyObject = function(obj) {
for(var prop in obj) {
if(hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true;
};
6 changes: 5 additions & 1 deletion test/ext/contain.test.js
Expand Up @@ -119,7 +119,11 @@ describe('property', function() {
]);
}, "expected [ { a: 'a' }, { b: 'b', c: 'c' } ] not to contain [ { b: 'b' } ]");

({hi: null}).should.containEql({hi: null})
({hi: null}).should.containEql({hi: null});

var firstDec = [{date: (new Date('2014-12-01 00:00:00'))}];
var secondDec = [{date: (new Date('2014-12-02 00:00:00'))}];
firstDec.should.not.containDeep(secondDec);
});

it('test .containDeepOrdered', function() {
Expand Down

0 comments on commit 1e49897

Please sign in to comment.