From 20a4d3315578a452c0298e9253050f98e1758cca Mon Sep 17 00:00:00 2001 From: Dai Date: Mon, 29 Jun 2015 23:15:14 +0100 Subject: [PATCH 1/2] Added handling for undefined and null (fails on negate flag too) --- lib/chai/core/assertions.js | 12 +++++++----- test/expect.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index ce1b3bf2d..2cf367c10 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -376,14 +376,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' ); diff --git a/test/expect.js b/test/expect.js index 4507d4c75..8e105dd27 100644 --- a/test/expect.js +++ b/test/expect.js @@ -397,6 +397,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('property(name)', function(){ From 97cdd32c46b0cf5df62629bfa5bbce6828fd8779 Mon Sep 17 00:00:00 2001 From: Dai Date: Tue, 30 Jun 2015 17:14:46 +0100 Subject: [PATCH 2/2] Updated code based on discussion --- lib/chai/core/assertions.js | 15 +++------------ test/expect.js | 16 ++++++---------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 2cf367c10..beb61e8f1 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -373,19 +373,10 @@ module.exports = function (chai, _) { */ Assertion.addProperty('empty', function () { - var obj = flag(this, 'object') - , expected = obj; - - 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; - } - + var obj = flag(this, 'object'); + new Assertion(obj).to.exist; this.assert( - expected + Object.keys(Object(obj)).length === 0 , 'expected #{this} to be empty' , 'expected #{this} not to be empty' ); diff --git a/test/expect.js b/test/expect.js index 8e105dd27..c9077c757 100644 --- a/test/expect.js +++ b/test/expect.js @@ -398,33 +398,29 @@ describe('expect', 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"); + }, "expected null to exist"); err(function(){ expect(undefined).to.be.empty; - }, "expected undefined to be empty"); + }, "expected undefined to exist"); err(function(){ expect().to.be.empty; - }, "expected undefined to be empty"); + }, "expected undefined to exist"); err(function(){ expect(null).to.not.be.empty; - }, "expected null not to be empty"); + }, "expected null to exist"); err(function(){ expect(undefined).to.not.be.empty; - }, "expected undefined not to be empty"); + }, "expected undefined to exist"); err(function(){ expect().to.not.be.empty; - }, "expected undefined not to be empty"); + }, "expected undefined to exist"); });