-
-
Notifications
You must be signed in to change notification settings - Fork 698
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
Make 'empty' throw on non-string primitives and functions #812
Changes from 3 commits
89b9865
ccc00bc
92a40d4
79e6fba
a1c1dc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -515,8 +515,8 @@ module.exports = function (chai, _) { | |
* ### .empty | ||
* | ||
* Asserts that the target's length is `0`. For arrays and strings, it checks | ||
* the `length` property. For objects, it gets the count of | ||
* enumerable keys. | ||
* the `length` property. For non-function objects, it gets the count of own | ||
* enumerable string keys. | ||
* | ||
* expect([]).to.be.empty; | ||
* expect('').to.be.empty; | ||
|
@@ -528,10 +528,26 @@ module.exports = function (chai, _) { | |
*/ | ||
|
||
Assertion.addProperty('empty', function () { | ||
var obj = flag(this, 'object'); | ||
new Assertion(obj).to.exist; | ||
var val = flag(this, 'object') | ||
, itemsCount; | ||
|
||
switch (_.type(val)) { | ||
case 'array': | ||
case 'string': | ||
itemsCount = val.length; | ||
break; | ||
case 'function': | ||
var name = val.name ? ' ' + val.name : ''; | ||
throw new TypeError('.empty was passed a function' + name); | ||
default: | ||
if (val !== Object(val)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other option is to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That blew my mind to! |
||
throw new TypeError('.empty was passed non-string primitive ' + String(val)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need EDIT: oops, that fails in Node 0.12 because of partial implementation. Let's do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use the inspect utility. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'm okay without it since the error message is pretty self-explanatory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I just noticed you want to print the value of what was passed, not it's type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, so, if we're going to use
PS.: Sorry for all this mess in the comments @shvaikalesh 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, calm down everyone hahahha: THUMBS UP IF YOU VOTE FOR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, let's do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so we're going for LGTM! |
||
} | ||
itemsCount = Object.keys(val).length; | ||
} | ||
|
||
this.assert( | ||
Object.keys(Object(obj)).length === 0 | ||
0 === itemsCount | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any ESLint config we can add to the project? This will greatly improve contribution/review experience: yoda comparisons and comma-first are not quite popular and it would be awesome to automate codestyle checks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shvaikalesh Yes, we thought about that too, we already discussed that here and we came to the conclusion that by moving all our code to separate repos with their own linting rules would be better, however, if you disagree you're welcome to share your thoughts with us 😄 . |
||
, 'expected #{this} to be empty' | ||
, 'expected #{this} not to be empty' | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should handle generators too (because of recent updates to
type-detect
) andasync
function in the future.