Fix #265 #361

Merged
merged 1 commit into from Feb 9, 2015

Projects

None yet

2 participants

@gregglind
Contributor
  1. We didn't throw on 'too many args' before. Should we now? If so, what?
  2. (Do docs run as tests, if so how?)
  3. (Tests inbound. WIP mostly to claim the bug :) )
@gregglind
Contributor

See #265

@keithamus keithamus commented on an outdated diff Feb 8, 2015
lib/chai/core/assertions.js
- keys = keys instanceof Array
- ? keys
- : Array.prototype.slice.call(arguments);
+ }
+ } else {
+ keys = Array.prototype.slice.call(arguments);
+ }
@keithamus
keithamus Feb 8, 2015 Member

It might read clearer to use the _.type function:

if (_.type(keys) === 'object') {
    keys = Object.keys(keys);
} else if (arguments.length > 1) {
    keys = Array.prototype.slice.call(arguments);
}
@keithamus keithamus and 1 other commented on an outdated diff Feb 8, 2015
lib/chai/core/assertions.js
* @api public
*/
function assertKeys (keys) {
var obj = flag(this, 'object')
, str
- , ok = true;
+ , ok = true
+ , mixedArgsMsg = "keys: if first argument is instanceof Array|Object, must be only argument."
+
+ if (keys instanceof Array || keys instanceof Object) {
+ if (arguments.length > 1) {
+ throw (new Error(mixedArgsMsg));
@keithamus
keithamus Feb 8, 2015 Member

Not entirely sure about throwing errors here. Think this may be overcomplicating the method a little is all.

@gregglind
gregglind Feb 8, 2015 Contributor

The 'throw' was mentioned in the bug, so I added it. I would think it would be very strange to do keys(Array, string...) as dev, but it could happen by accident. Low-cost to check :)

@gregglind
Contributor

Went to switch based, using _.type. I think it's very explicit now, for good or ill.

@keithamus keithamus and 1 other commented on an outdated diff Feb 8, 2015
lib/chai/core/assertions.js
* @api public
*/
function assertKeys (keys) {
var obj = flag(this, 'object')
, str
- , ok = true;
-
- keys = keys instanceof Array
- ? keys
- : Array.prototype.slice.call(arguments);
+ , ok = true
+ , mixedArgsMsg = "keys: if first argument is instanceof Array|Object, must be only argument."
@keithamus
keithamus Feb 8, 2015 Member

I think this error needs rephrasing, maybe to something like 'keys must be given single argument of Array, Object, or multiple String arguments'

@gregglind
gregglind Feb 9, 2015 Contributor

Rephased to:

    var mixedArgsMsg = 'keys must be given single argument of Array|Object|String, or multiple String arguments'

I think that is accurate. I do wonder if we should emphasize the 'multiple arguments' part, because that is what triggered the error.

@gregglind gregglind fix #359. Add `.keys(object)
- `.keys(object)n => .keys(Object.keys(Object)`
- added exceptions for 'if first arg is non-string, then it must be only
arg.  => `.keys(Array|Object, ...)`

Warning:  `Object.keys` must exist on systems to use this functionality.
4f40b37
@keithamus
Member

I'd say this is good to go. Let's get it merged in. 👍

@keithamus keithamus merged commit 5753ecb into chaijs:master Feb 9, 2015

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@keithamus keithamus referenced this pull request Feb 12, 2015
Closed

Get a proper changelog #366

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment