Skip to content

Commit

Permalink
feat(matchers): Allow for objects in oneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Droogans committed Oct 15, 2015
1 parent 179d470 commit b474cee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
15 changes: 10 additions & 5 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1534,10 +1534,17 @@ module.exports = function (chai, _) {
/**
* ### .oneOf(list)
*
* Assert that a non-array, non-object value appears somewhere in the flat array `list`.
* Assert that a value appears somewhere in the top level of array `list`.
*
* expect('a').to.be.oneOf(['a', 'b', 'c']);
* expect(9).to.not.be.oneOf(['z']);
* expect([3]).to.not.be.oneOf([1, 2, [3]]);
*
* var three = [3];
* // for object-types, contents are not compared
* expect(three).to.not.be.oneOf([1, 2, [3]]);
* // comparing references works
* expect(three).to.be.oneOf([1, 2, three]);
*
* @name oneOf
* @param {Array<*>} list
Expand All @@ -1549,13 +1556,11 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var expected = flag(this, 'object');
new Assertion(list).to.be.an('array');
new Assertion(expected).to.not.be.an('array');
new Assertion(expected).to.not.be.an('object');

this.assert(
list.indexOf(expected) > -1
, 'expected #{this} to be in #{exp}'
, 'expected #{this} to not be in #{exp}'
, 'expected #{this} to be one of #{exp}'
, 'expected #{this} to not be one of #{exp}'
, list
, expected
);
Expand Down
22 changes: 14 additions & 8 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,25 +771,31 @@ describe('assert', function () {
it('oneOf', function() {
assert.oneOf(1, [1, 2, 3]);

err(function() {
assert.oneOf([1], []);
}, 'expected [ 1 ] not to be an array');
var three = [3];
assert.oneOf(three, [1, 2, three]);

err(function() {
assert.oneOf({a: 1}, []);
}, 'expected { a: 1 } not to be an object');
var four = { four: 4 };
assert.oneOf(four, [1, 2, four]);

err(function() {
assert.oneOf(1, 1);
}, 'expected 1 to be an array');

err(function() {
assert.oneOf(1, {a: 1});
assert.oneOf(1, { a: 1 });
}, 'expected { a: 1 } to be an array');

err(function() {
assert.oneOf(9, [1, 2, 3], 'Message');
}, 'Message: expected 9 to be in [ 1, 2, 3 ]');
}, 'Message: expected 9 to be one of [ 1, 2, 3 ]');

err(function() {
assert.oneOf([3], [1, 2, [3]]);
}, 'expected [ 3 ] to be one of [ 1, 2, [ 3 ] ]');

err(function() {
assert.oneOf({ four: 4 }, [1, 2, { four: 4 }]);
}, 'expected { four: 4 } to be one of [ 1, 2, { four: 4 } ]');

});

Expand Down
3 changes: 3 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,9 @@ describe('expect', function () {
it('oneOf', function() {
expect(1).to.be.oneOf([1, 2, 3]);
expect('1').to.not.be.oneOf([1, 2, 3]);
expect([3, [4]]).to.not.be.oneOf([1, 2, [3, 4]]);
var threeFour = [3, [4]];
expect(threeFour).to.be.oneOf([1, 2, threeFour]);
});

it('include.members', function() {
Expand Down

0 comments on commit b474cee

Please sign in to comment.