Skip to content

Commit

Permalink
Make 'empty' throw on non-string primitives and functions (#812)
Browse files Browse the repository at this point in the history
* Make 'empty' throw on non-string primitives and functions

* Update jsdoc

* improve error messages

* fix symbol to string coercion for Node 0.12

* use utils.inspect
  • Loading branch information
shvaikalesh authored and lucasfcosta committed Oct 1, 2016
1 parent 5dd96a0 commit 5b170b2
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 11 deletions.
26 changes: 21 additions & 5 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
throw new TypeError('.empty was passed non-string primitive ' + _.inspect(val));
}
itemsCount = Object.keys(val).length;
}

this.assert(
Object.keys(Object(obj)).length === 0
0 === itemsCount
, 'expected #{this} to be empty'
, 'expected #{this} not to be empty'
);
Expand Down
47 changes: 41 additions & 6 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,28 +680,63 @@ describe('expect', function () {

err(function(){
expect(null).to.be.empty;
}, "expected null to exist");
}, ".empty was passed non-string primitive null");

err(function(){
expect(undefined).to.be.empty;
}, "expected undefined to exist");
}, ".empty was passed non-string primitive undefined");

err(function(){
expect().to.be.empty;
}, "expected undefined to exist");
}, ".empty was passed non-string primitive undefined");

err(function(){
expect(null).to.not.be.empty;
}, "expected null to exist");
}, ".empty was passed non-string primitive null");

err(function(){
expect(undefined).to.not.be.empty;
}, "expected undefined to exist");
}, ".empty was passed non-string primitive undefined");

err(function(){
expect().to.not.be.empty;
}, "expected undefined to exist");
}, ".empty was passed non-string primitive undefined");

err(function(){
expect(0).to.be.empty;
}, ".empty was passed non-string primitive 0");

err(function(){
expect(1).to.be.empty;
}, ".empty was passed non-string primitive 1");

err(function(){
expect(true).to.be.empty;
}, ".empty was passed non-string primitive true");

err(function(){
expect(false).to.be.empty;
}, ".empty was passed non-string primitive false");

if (typeof Symbol !== 'undefined') {
err(function(){
expect(Symbol()).to.be.empty;
}, ".empty was passed non-string primitive Symbol()");

err(function(){
expect(Symbol.iterator).to.be.empty;
}, ".empty was passed non-string primitive Symbol(Symbol.iterator)");
}

err(function(){
expect(function() {}).to.be.empty;
}, ".empty was passed a function");

if (FakeArgs.name === 'FakeArgs') {
err(function(){
expect(FakeArgs).to.be.empty;
}, ".empty was passed a function FakeArgs");
}
});

it('NaN', function() {
Expand Down
36 changes: 36 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,42 @@ describe('should', function() {
err(function(){
({foo: 'bar'}).should.be.empty;
}, "expected { foo: \'bar\' } to be empty");

err(function(){
(0).should.be.empty;
}, ".empty was passed non-string primitive 0");

err(function(){
(1).should.be.empty;
}, ".empty was passed non-string primitive 1");

err(function(){
true.should.be.empty;
}, ".empty was passed non-string primitive true");

err(function(){
false.should.be.empty;
}, ".empty was passed non-string primitive false");

if (typeof Symbol !== 'undefined') {
err(function(){
Symbol().should.be.empty;
}, ".empty was passed non-string primitive Symbol()");

err(function(){
Symbol.iterator.should.to.be.empty;
}, ".empty was passed non-string primitive Symbol(Symbol.iterator)");
}

err(function(){
(function() {}).should.be.empty;
}, ".empty was passed a function");

if (FakeArgs.name === 'FakeArgs') {
err(function(){
FakeArgs.should.be.empty;
}, ".empty was passed a function FakeArgs");
}
});

it('finite(value)', function() {
Expand Down

0 comments on commit 5b170b2

Please sign in to comment.