Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support {a:1,b:2}.should.include({a:1}) #230

Merged
merged 3 commits into from
Jan 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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