Added handling for undefined and null (fails on negate flag too)

This commit is contained in:
Dai 2015-06-29 23:15:14 +01:00 committed by Grant Snodgrass
parent e5e6f5cbbd
commit e0ee3def23
2 changed files with 36 additions and 5 deletions

View file

@ -510,14 +510,16 @@ module.exports = function (chai, _) {
var obj = flag(this, 'object')
, expected = obj;
if (Array.isArray(obj) || 'string' === typeof object) {
expected = obj.length;
} else if (typeof obj === 'object') {
expected = Object.keys(obj).length;
if (obj == null) {
expected = flag(this, 'negate');
} else if (Array.isArray(obj) || 'string' === typeof obj) {
expected = obj.length === 0;
} else if ('object' === typeof obj) {
expected = Object.keys(obj).length === 0;
}
this.assert(
!expected
expected
, 'expected #{this} to be empty'
, 'expected #{this} not to be empty'
);

View file

@ -471,6 +471,35 @@ describe('expect', function () {
err(function(){
expect({foo: 'bar'}).to.be.empty;
}, "expected { foo: \'bar\' } to be empty");
err(function(){
expect(0).to.be.empty;
}, "expected 0 to be empty");
err(function(){
expect(null).to.be.empty;
}, "expected null to be empty");
err(function(){
expect(undefined).to.be.empty;
}, "expected undefined to be empty");
err(function(){
expect().to.be.empty;
}, "expected undefined to be empty");
err(function(){
expect(null).to.not.be.empty;
}, "expected null not to be empty");
err(function(){
expect(undefined).to.not.be.empty;
}, "expected undefined not to be empty");
err(function(){
expect().to.not.be.empty;
}, "expected undefined not to be empty");
});
it('NaN', function() {