From db21e4872673cc2ebdfbb46e0d8e1bc26db0195b Mon Sep 17 00:00:00 2001 From: Droogans Date: Mon, 12 Oct 2015 22:10:07 -0500 Subject: [PATCH 1/3] docs(contributing): Highlight test steps --- CONTRIBUTING.md | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1f6ab84b3..ba019388c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -121,19 +121,30 @@ git checkout -b 4. Commit your changes in logical chunks. Use Git's [interactive rebase](https://help.github.com/articles/interactive-rebase) feature to tidy up your commits before making them public. -5. Locally merge (or rebase) the upstream development branch into your topic branch: +5. Run you code to make sure it works. + +```bash +npm i +rm chai.js +make chai.js +npm test +# when finished running tests... +git checkout chai.js +``` + +6. Locally merge (or rebase) the upstream development branch into your topic branch: ```bash git pull [--rebase] upstream ``` -6. Push your topic branch up to your fork: +7. Push your topic branch up to your fork: ```bash git push origin ``` -7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/) with a clear title and description. +8. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/) with a clear title and description. **IMPORTANT**: By submitting a patch, you agree to allow the project owner to license your work under the same license as that used by the project. From 179d4701b89d636524d760bd74e28e792ad41fe7 Mon Sep 17 00:00:00 2001 From: Droogans Date: Mon, 12 Oct 2015 22:10:40 -0500 Subject: [PATCH 2/3] feat(matchers): Add value-in-list assertion Adds a way to test if a value appears in a flat list. Compliments `include.members` to reverse the order of the input. Now the list appears in the assertion, as opposed to the `expect`. Closes #532 --- lib/chai/core/assertions.js | 33 +++++++++++++++++++++++++++++++++ lib/chai/interface/assert.js | 20 +++++++++++++++++++- test/assert.js | 27 ++++++++++++++++++++++++++- test/expect.js | 7 ++++++- test/should.js | 9 ++++++++- 5 files changed, 92 insertions(+), 4 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 650e71816..45925c0fa 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -1531,6 +1531,39 @@ module.exports = function (chai, _) { ); }); + /** + * ### .oneOf(list) + * + * Assert that a non-array, non-object value appears somewhere in the flat array `list`. + * + * expect('a').to.be.oneOf(['a', 'b', 'c']); + * expect(9).to.not.be.oneOf(['z']); + * + * @name oneOf + * @param {Array<*>} list + * @param {String} message _optional_ + * @api public + */ + + function oneOf (list, msg) { + 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}' + , list + , expected + ); + } + + Assertion.addMethod('oneOf', oneOf); + + /** * ### .change(function) * diff --git a/lib/chai/interface/assert.js b/lib/chai/interface/assert.js index bd96222ca..07dad5a99 100644 --- a/lib/chai/interface/assert.js +++ b/lib/chai/interface/assert.js @@ -1165,7 +1165,7 @@ module.exports = function (chai, util) { assert.closeTo = function (act, exp, delta, msg) { new Assertion(act, msg).to.be.closeTo(exp, delta); }; - + /** * ### .approximately(actual, expected, delta, [message]) * @@ -1242,6 +1242,24 @@ module.exports = function (chai, util) { new Assertion(superset, msg).to.include.members(subset); } + /** + * ### .oneOf(inList, list, [message]) + * + * Asserts that non-object, non-array value `inList` appears in the flat array `list`. + * + * assert.oneOf(1, [ 2, 1 ], 'Not found in list'); + * + * @name oneOf + * @param {*} inList + * @param {Array<*>} list + * @param {String} message + * @api public + */ + + assert.oneOf = function (inList, list, msg) { + new Assertion(inList, msg).to.be.oneOf(list); + } + /** * ### .changes(function, object, property) * diff --git a/test/assert.js b/test/assert.js index bd164dde3..36de62f63 100644 --- a/test/assert.js +++ b/test/assert.js @@ -713,7 +713,7 @@ describe('assert', function () { assert.closeTo(1.5, 1.0, true); }, "the arguments to closeTo or approximately must be numbers"); }); - + it('approximately', function(){ assert.approximately(1.5, 1.0, 0.5); assert.approximately(10, 20, 20); @@ -768,6 +768,31 @@ describe('assert', function () { }, 'expected [ 1, 54 ] to have the same members as [ 6, 1, 54 ]'); }); + it('oneOf', function() { + assert.oneOf(1, [1, 2, 3]); + + err(function() { + assert.oneOf([1], []); + }, 'expected [ 1 ] not to be an array'); + + err(function() { + assert.oneOf({a: 1}, []); + }, 'expected { a: 1 } not to be an object'); + + err(function() { + assert.oneOf(1, 1); + }, 'expected 1 to be an array'); + + err(function() { + 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 ]'); + + }); + it('above', function() { assert.isAbove(5, 2, '5 should be above 2'); diff --git a/test/expect.js b/test/expect.js index 47b3edfd5..43dd6456a 100644 --- a/test/expect.js +++ b/test/expect.js @@ -1052,7 +1052,7 @@ describe('expect', function () { expect(1.5).to.be.closeTo(1.0, true); }, "the arguments to closeTo or approximately must be numbers"); }); - + it('approximately', function(){ expect(1.5).to.be.approximately(1.0, 0.5); expect(10).to.be.approximately(20, 20); @@ -1079,6 +1079,11 @@ describe('expect', function () { }, "the arguments to closeTo or approximately must be numbers"); }); + it('oneOf', function() { + expect(1).to.be.oneOf([1, 2, 3]); + expect('1').to.not.be.oneOf([1, 2, 3]); + }); + it('include.members', function() { expect([1, 2, 3]).to.include.members([]); expect([1, 2, 3]).to.include.members([3, 2]); diff --git a/test/should.js b/test/should.js index 0d3aa537b..0fde10b9d 100644 --- a/test/should.js +++ b/test/should.js @@ -457,6 +457,13 @@ describe('should', function() { }, "blah: expected 'foobar' to not contain 'bar'"); }); + it('oneOf()', function(){ + 'foo'.should.be.oneOf(['foo', 'bar']); + 'bar'.should.be.oneOf(['foo', 'bar']); + 'baz'.should.not.be.oneOf(['foo', 'bar']); + 'baz'.should.not.be.oneOf([]); + }); + it('include()', function(){ ['foo', 'bar'].should.include('foo'); ['foo', 'bar'].should.contain('foo'); @@ -881,7 +888,7 @@ describe('should', function() { (1.5).should.be.closeTo(1.0, true); }, "the arguments to closeTo or approximately must be numbers"); }); - + it('approximately', function(){ (1.5).should.be.approximately(1.0, 0.5); From b474ceee545279b0ca287b3a3caba2eb784d3afa Mon Sep 17 00:00:00 2001 From: Droogans Date: Thu, 15 Oct 2015 15:02:43 -0500 Subject: [PATCH 3/3] 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() {