Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expect(inList).to.be.oneOf assertion #534

Merged
merged 3 commits into from
Oct 16, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions CONTRIBUTING.md
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
33 changes: 33 additions & 0 deletions lib/chai/core/assertions.js
Expand Up @@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this requirement really matters? Would it be so wrong to have oneOf work with object or arrays? Referential equality would be fine for an initial implementation - we can add .deep.oneOf() later if we need to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could probably get that in there. Just so you know, it'll act like Python's in keyword. Meaning:

>>> [3] in [1, 2, [3]]
True
>>> [4] in [1, 2, [3, [4]]]
False
>>> [3, [4]] in [1, 2, [3, [4]]]
True

In other words, unless I'm matching something from the first level all the way to the end of that first level object-type, it'll fail. That's the simplest way I can think to handle object-types inside of a list without a deep flag.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the restrictions, and without deep, I'd expect the following:

expect(3).to.be.oneOf([1,2,3]) // pass
var three = [3];
expect(three).to.be.oneOf([1,2,three]) // pass
expect([3]).to.be.oneOf([1,2,[3]]) // fails because [3] !== [3] (two different array literals)

The above is fine and completely expected and acceptable piece of JavaScript. We can add .deep later to do more interesting things, but there's not reason to stop people doing the above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'll do that. I was using _.isEqual (which is like == for objects in lodash), but now I will use strict referencing via ===.


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)
*
Expand Down
20 changes: 19 additions & 1 deletion lib/chai/interface/assert.js
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
27 changes: 26 additions & 1 deletion test/assert.js
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,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 ]');

});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really nice comprehensive set of tests. Thanks for this.


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

Expand Down
7 changes: 6 additions & 1 deletion test/expect.js
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,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]);
Expand Down
9 changes: 8 additions & 1 deletion test/should.js
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