Skip to content

Commit

Permalink
Merge acbf04d into f3ebb09
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroso committed Jan 5, 2014
2 parents f3ebb09 + acbf04d commit b1ffd77
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
16 changes: 14 additions & 2 deletions lib/chai/core/assertions.js
Expand Up @@ -145,9 +145,21 @@ module.exports = function (chai, _) {

function include (val, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
var obj = flag(this, 'object');

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);
} else {
var expected = obj && ~obj.indexOf(val)
}
this.assert(
~obj.indexOf(val)
expected
, 'expected #{this} to include ' + _.inspect(val)
, 'expected #{this} to not include ' + _.inspect(val));
}
Expand Down
28 changes: 2 additions & 26 deletions lib/chai/interface/assert.js
Expand Up @@ -657,19 +657,7 @@ module.exports = function (chai, util) {
*/

assert.include = function (exp, inc, msg) {
var obj = new Assertion(exp, msg);

if (Array.isArray(exp)) {
obj.to.include(inc);
} else if ('string' === typeof exp) {
obj.to.contain.string(inc);
} else {
throw new chai.AssertionError(
'expected an array or string'
, null
, assert.include
);
}
new Assertion(exp, msg).include(inc);
};

/**
Expand All @@ -689,19 +677,7 @@ module.exports = function (chai, util) {
*/

assert.notInclude = function (exp, inc, msg) {
var obj = new Assertion(exp, msg);

if (Array.isArray(exp)) {
obj.to.not.include(inc);
} else if ('string' === typeof exp) {
obj.to.not.contain.string(inc);
} else {
throw new chai.AssertionError(
'expected an array or string'
, null
, assert.notInclude
);
}
new Assertion(exp, msg).not.include(inc);
};

/**
Expand Down
12 changes: 5 additions & 7 deletions test/assert.js
Expand Up @@ -391,27 +391,25 @@ describe('assert', function () {
it('include', function() {
assert.include('foobar', 'bar');
assert.include([ 1, 2, 3], 3);
assert.include({a:1, b:2}, {b:2});

err(function () {
assert.include('foobar', 'baz');
}, "expected \'foobar\' to contain \'baz\'");
}, "expected \'foobar\' to include \'baz\'");

err(function () {
assert.include(undefined, 'bar');
}, "expected an array or string");
}, "expected undefined to include 'bar'");
});

it('notInclude', function () {
assert.notInclude('foobar', 'baz');
assert.notInclude([ 1, 2, 3 ], 4);
assert.notInclude(undefined, 'bar');

err(function () {
assert.notInclude('foobar', 'bar');
}, "expected \'foobar\' to not contain \'bar\'");

err(function () {
assert.notInclude(undefined, 'bar');
}, "expected an array or string");
}, "expected \'foobar\' to not include \'bar\'");
});

it('lengthOf', function() {
Expand Down
12 changes: 12 additions & 0 deletions test/expect.js
Expand Up @@ -487,6 +487,10 @@ describe('expect', function () {
expect([1,2]).to.include(1);
expect(['foo', 'bar']).to.not.include('baz');
expect(['foo', 'bar']).to.not.include(1);
expect({a:1,b:2}).to.include({b:2});
expect({a:1,b:2}).to.not.include({b:3});
expect({a:1,b:2}).to.include({a:1,b:2});
expect({a:1,b:2}).to.not.include({a:1,c:2});

err(function(){
expect(['foo']).to.include('bar', 'blah');
Expand All @@ -495,6 +499,14 @@ describe('expect', function () {
err(function(){
expect(['bar', 'foo']).to.not.include('foo', 'blah');
}, "blah: expected [ 'bar', 'foo' ] to not include 'foo'");

err(function(){
expect({a:1}).to.include({b:2});
}, "expected { a: 1 } to have a property 'b'");

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
6 changes: 6 additions & 0 deletions test/should.js
Expand Up @@ -415,6 +415,8 @@ describe('should', function() {
[1,2].should.include(1);
['foo', 'bar'].should.not.include('baz');
['foo', 'bar'].should.not.include(1);
({a:1,b:2}).should.include({b:2});
({a:1,b:2}).should.not.include({b:3});

err(function(){
['foo'].should.include('bar', 'blah');
Expand All @@ -423,6 +425,10 @@ describe('should', function() {
err(function(){
['bar', 'foo'].should.not.include('foo', 'blah');
}, "blah: expected [ 'bar', 'foo' ] to not include 'foo'");

err(function(){
({a:1}).should.include({b:2});
}, "expected { a: 1 } to have a property 'b'")
});

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

0 comments on commit b1ffd77

Please sign in to comment.