Skip to content

Commit

Permalink
Merge pull request #534 from Droogans/be-oneOf-assertion
Browse files Browse the repository at this point in the history
`expect(inList).to.be.oneOf` assertion
  • Loading branch information
keithamus committed Oct 16, 2015
2 parents 73122c5 + b474cee commit 6472cb9
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 7 deletions.
17 changes: 14 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,30 @@ git checkout -b <topic-branch-name>

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 <dev-branch>
```

6. Push your topic branch up to your fork:
7. Push your topic branch up to your fork:

```bash
git push origin <topic-branch-name>
```

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.

Expand Down
38 changes: 38 additions & 0 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,44 @@ module.exports = function (chai, _) {
);
});

/**
* ### .oneOf(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
* @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');

this.assert(
list.indexOf(expected) > -1
, 'expected #{this} to be one of #{exp}'
, 'expected #{this} to not be one of #{exp}'
, list
, expected
);
}

Assertion.addMethod('oneOf', oneOf);


/**
* ### .change(function)
*
Expand Down
20 changes: 19 additions & 1 deletion lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
*
Expand Down Expand Up @@ -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)
*
Expand Down
33 changes: 32 additions & 1 deletion test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -768,6 +768,37 @@ describe('assert', function () {
}, 'expected [ 1, 54 ] to have the same members as [ 6, 1, 54 ]');
});

it('oneOf', function() {
assert.oneOf(1, [1, 2, 3]);

var three = [3];
assert.oneOf(three, [1, 2, three]);

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 });
}, 'expected { a: 1 } to be an array');

err(function() {
assert.oneOf(9, [1, 2, 3], 'Message');
}, '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 } ]');

});

it('above', function() {
assert.isAbove(5, 2, '5 should be above 2');

Expand Down
10 changes: 9 additions & 1 deletion test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -1079,6 +1079,14 @@ 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]);
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() {
expect([1, 2, 3]).to.include.members([]);
expect([1, 2, 3]).to.include.members([3, 2]);
Expand Down
9 changes: 8 additions & 1 deletion test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 6472cb9

Please sign in to comment.