Skip to content

Commit

Permalink
Added the and flags for assertion, with being the default behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Jones committed Dec 3, 2014
1 parent f06278f commit 4815b6f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 8 deletions.
68 changes: 60 additions & 8 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,41 @@ module.exports = function (chai, _) {
flag(this, 'deep', true);
});

/**
* ### .any
*
* Sets the `any` flag, (opposite of the `all` flag)
* later used in the `keys` assertion.
*
* expect(foo).to.have.any.keys('bar', 'baz');
*
* @name any
* @api public
*/

Assertion.addProperty('any', function () {
flag(this, 'any', true);
flag(this, 'all', false)
});


/**
* ### .all
*
* Sets the `all` flag (opposite of the `any` flag)
* later used by the `keys` assertion.
*
* expect(foo).to.have.all.keys('bar', 'baz');
*
* @name all
* @api public
*/

Assertion.addProperty('all', function () {
flag(this, 'all', true);
flag(this, 'any', false);
});

/**
* ### .a(type)
*
Expand Down Expand Up @@ -958,14 +993,26 @@ module.exports = function (chai, _) {
, expected = keys
, len = keys.length;

// Inclusion
ok = keys.every(function(key){
return ~actual.indexOf(key);
});
if (!flag(this, 'any') && !flag(this, 'all')) {
flag(this, 'all', true);
}

// Strict
if (!flag(this, 'negate') && !flag(this, 'contains')) {
ok = ok && keys.length == actual.length;
// Has any
if (flag(this, 'any')) {
var intersection = expected.filter(function(key) {
return ~actual.indexOf(key);
});
ok = intersection.length > 0;
}

// Has all
if (flag(this, 'all')) {
ok = keys.every(function(key){
return ~actual.indexOf(key);
});
if (!flag(this, 'negate') && !flag(this, 'contains')) {
ok = ok && keys.length == actual.length;
}
}

// Key string
Expand All @@ -974,7 +1021,12 @@ module.exports = function (chai, _) {
return _.inspect(key);
});
var last = keys.pop();
str = keys.join(', ') + ', and ' + last;
if (flag(this, 'all')) {
str = keys.join(', ') + ', and ' + last;
}
if (flag(this, 'any')) {
str = keys.join(', ') + ', or ' + last;
}
} else {
str = _.inspect(keys[0]);
}
Expand Down
26 changes: 26 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,19 @@ describe('expect', function () {
expect({ foo: 1, bar: 2 }).to.not.contain.keys('foo', 'baz');
expect({ foo: 1, bar: 2 }).to.not.contain.keys('baz', 'foo');

expect({ foo: 1, bar: 2 }).to.have.any.keys('foo', 'baz');
expect({ foo: 1, bar: 2 }).to.have.any.keys('foo');
expect({ foo: 1, bar: 2 }).to.contain.any.keys('bar', 'baz');
expect({ foo: 1, bar: 2 }).to.contain.any.keys(['foo']);
expect({ foo: 1, bar: 2 }).to.have.all.keys(['bar', 'foo']);
expect({ foo: 1, bar: 2 }).to.contain.all.keys(['bar', 'foo']);

expect({ foo: 1, bar: 2 }).to.not.have.any.keys('baz', 'abc', 'def');
expect({ foo: 1, bar: 2 }).to.not.have.any.keys('baz');
expect({ foo: 1, bar: 2 }).to.not.contain.any.keys('baz');
expect({ foo: 1, bar: 2 }).to.not.have.all.keys(['baz', 'foo']);
expect({ foo: 1, bar: 2 }).to.not.contain.all.keys(['baz', 'foo']);

err(function(){
expect({ foo: 1 }).to.have.keys();
}, "keys required");
Expand Down Expand Up @@ -635,6 +648,19 @@ describe('expect', function () {
err(function(){
expect({ foo: 1 }).to.contain.keys('foo', 'bar');
}, "expected { foo: 1 } to contain keys 'foo', and 'bar'");

err(function() {
expect({ foo: 1 }).to.have.any.keys('baz');
}, "expected { foo: 1 } to have key 'baz'");

err(function(){
expect({ foo: 1, bar: 2 }).to.not.have.all.keys(['foo', 'bar']);
}, "expected { foo: 1, bar: 2 } to not have keys 'foo', and 'bar'");

err(function(){
expect({ foo: 1, bar: 2 }).to.not.have.any.keys(['foo', 'baz']);
}, "expected { foo: 1, bar: 2 } to not have keys 'foo', or 'baz'");

});

it('chaining', function(){
Expand Down
25 changes: 25 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,19 @@ describe('should', function() {
({ foo: 1, bar: 2 }).should.not.contain.keys('foo', 'baz');
({ foo: 1, bar: 2 }).should.not.contain.keys('baz', 'foo');

({ foo: 1, bar: 2 }).should.have.any.keys('foo', 'baz');
({ foo: 1, bar: 2 }).should.have.any.keys('foo');
({ foo: 1, bar: 2 }).should.contain.any.keys('bar', 'baz');
({ foo: 1, bar: 2 }).should.contain.any.keys(['foo']);
({ foo: 1, bar: 2 }).should.have.all.keys(['bar', 'foo']);
({ foo: 1, bar: 2 }).should.contain.all.keys(['bar', 'foo']);

({ foo: 1, bar: 2 }).should.not.have.any.keys('baz', 'abc', 'def');
({ foo: 1, bar: 2 }).should.not.have.any.keys('baz');
({ foo: 1, bar: 2 }).should.not.contain.any.keys('baz');
({ foo: 1, bar: 2 }).should.not.have.all.keys(['baz', 'foo']);
({ foo: 1, bar: 2 }).should.not.contain.all.keys(['baz', 'foo']);

err(function(){
({ foo: 1 }).should.have.keys();
}, "keys required");
Expand Down Expand Up @@ -498,6 +511,18 @@ describe('should', function() {
err(function(){
({ foo: 1 }).should.contain.keys('foo', 'bar');
}, "expected { foo: 1 } to contain keys 'foo', and 'bar'");

err(function() {
({ foo: 1 }).should.have.any.keys('baz');
}, "expected { foo: 1 } to have key 'baz'");

err(function(){
({ foo: 1, bar: 2 }).should.not.have.all.keys(['foo', 'bar']);
}, "expected { foo: 1, bar: 2 } to not have keys 'foo', and 'bar'");

err(function(){
({ foo: 1, bar: 2 }).should.not.have.any.keys(['foo', 'baz']);
}, "expected { foo: 1, bar: 2 } to not have keys 'foo', or 'baz'");
});

it('throw', function () {
Expand Down

0 comments on commit 4815b6f

Please sign in to comment.