Skip to content

Commit

Permalink
Merge pull request #244 from leider/fix_for_contains
Browse files Browse the repository at this point in the history
fixing #239 (without changing chai.js)
  • Loading branch information
logicalparadox committed Feb 25, 2014
2 parents ce1990a + 2c78b57 commit 4e25ff0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/chai/core/assertions.js
Expand Up @@ -147,17 +147,24 @@ module.exports = function (chai, _) {
function include (val, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');

if (_.type(val) === 'object') {
var expected = false;
if (_.type(obj) === 'array' && _.type(val) === 'object') {
for (var i in obj) {
if (_.eql(obj[i], val)) {
expected = true;
break;
}
}
} else if (_.type(val) === 'object') {
if (!flag(this, 'negate')) {
for (var k in val) new Assertion(obj).property(k, val[k]);
return;
}
var subset = {}
for (var k in val) subset[k] = obj[k]
var expected = _.eql(subset, val);
expected = _.eql(subset, val);
} else {
var expected = obj && ~obj.indexOf(val)
expected = obj && ~obj.indexOf(val)
}
this.assert(
expected
Expand Down
8 changes: 8 additions & 0 deletions test/expect.js
Expand Up @@ -492,6 +492,10 @@ describe('expect', function () {
expect({a:1,b:2}).to.include({a:1,b:2});
expect({a:1,b:2}).to.not.include({a:1,c:2});

expect([{a:1},{b:2}]).to.include({a:1});
expect([{a:1}]).to.include({a:1});
expect([{a:1}]).to.not.include({b:1});

err(function(){
expect(['foo']).to.include('bar', 'blah');
}, "blah: expected [ 'foo' ] to include 'bar'");
Expand All @@ -507,6 +511,10 @@ describe('expect', function () {
err(function(){
expect({a:1,b:2}).to.not.include({b:2});
}, "expected { a: 1, b: 2 } to not include { b: 2 }");

err(function(){
expect([{a:1},{b:2}]).to.not.include({b:2});
}, "expected [ { a: 1 }, { b: 2 } ] to not include { b: 2 }");
});

it('keys(array)', function(){
Expand Down

0 comments on commit 4e25ff0

Please sign in to comment.