From b474ceee545279b0ca287b3a3caba2eb784d3afa Mon Sep 17 00:00:00 2001 From: Droogans Date: Thu, 15 Oct 2015 15:02:43 -0500 Subject: [PATCH] feat(matchers): Allow for objects in oneOf --- lib/chai/core/assertions.js | 15 ++++++++++----- test/assert.js | 22 ++++++++++++++-------- test/expect.js | 3 +++ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 45925c0fa..2a79c92c3 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -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 @@ -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 ); diff --git a/test/assert.js b/test/assert.js index 36de62f63..9161922af 100644 --- a/test/assert.js +++ b/test/assert.js @@ -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 } ]'); }); diff --git a/test/expect.js b/test/expect.js index 43dd6456a..79068e444 100644 --- a/test/expect.js +++ b/test/expect.js @@ -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() {