Skip to content

Commit

Permalink
support {a:1,b:2}.should.include({a:1})
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroso committed Jan 5, 2014
1 parent f3ebb09 commit 2715e95
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
15 changes: 10 additions & 5 deletions lib/chai/core/assertions.js
Expand Up @@ -145,11 +145,16 @@ module.exports = function (chai, _) {

function include (val, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
this.assert(
~obj.indexOf(val)
, 'expected #{this} to include ' + _.inspect(val)
, 'expected #{this} to not include ' + _.inspect(val));
var obj = flag(this, 'object');

if (typeof val === 'object') {

This comment has been minimized.

Copy link
@logicalparadox

logicalparadox Jan 5, 2014

You probably want to use util.type here as typeof [] === 'object' which I don't think you want.

for (var k in val) this.property(k, val[k]);
} else {
this.assert(
obj && ~obj.indexOf(val)
, 'expected #{this} to include ' + _.inspect(val)
, 'expected #{this} to not include ' + _.inspect(val));
}
}

Assertion.addChainableMethod('include', include, includeChainingBehavior);
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
6 changes: 6 additions & 0 deletions test/expect.js
Expand Up @@ -487,6 +487,8 @@ 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});

err(function(){
expect(['foo']).to.include('bar', 'blah');
Expand All @@ -495,6 +497,10 @@ 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'");
});

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 2715e95

Please sign in to comment.