From 9e9874700633a5e514a78cbdac75fe8ddfe0f07b Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Wed, 5 Oct 2016 10:23:30 +0300 Subject: [PATCH 1/4] improve jsdoc --- lib/chai/core/assertions.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index c93e713d7..9c8fe5a9e 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -513,12 +513,15 @@ module.exports = function (chai, _) { /** * ### .empty * - * Asserts that the target's length is `0`. For arrays and strings, it checks - * the `length` property. For non-function objects, it gets the count of own + * Asserts that the target does not contain any values. + * For arrays and strings, it checks the `length` property. + * For `Map` and `Set` instances, it checks the `size` property. + * For non-function objects, it gets the count of own * enumerable string keys. * * expect([]).to.be.empty; * expect('').to.be.empty; + * expect(new Map).to.be.empty; * expect({}).to.be.empty; * * @name empty From 1d037fc4fbbdb1994f3d1a472f1488ac837882b3 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Wed, 5 Oct 2016 10:23:38 +0300 Subject: [PATCH 2/4] add .empty to assert interface --- lib/chai/interface/assert.js | 56 +++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/chai/interface/assert.js b/lib/chai/interface/assert.js index edafc32ea..819988f90 100644 --- a/lib/chai/interface/assert.js +++ b/lib/chai/interface/assert.js @@ -2768,6 +2768,58 @@ module.exports = function (chai, util) { new Assertion(obj, msg).to.not.be.frozen; }; + /** + * ### .isEmpty(target) + * + * Asserts that the target does not contain any values. + * For arrays and strings, it checks the `length` property. + * For `Map` and `Set` instances, it checks the `size` property. + * For non-function objects, it gets the count of own + * enumerable string keys. + * + * assert.isEmpty([]); + * assert.isEmpty(''); + * assert.isEmpty(new Map); + * assert.isEmpty({}); + * + * @name isEmpty + * @alias empty + * @param {Object|Array|String} target + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.isEmpty = function(val, msg) { + new Assertion(val, msg).to.be.empty; + }; + + /** + * ### .isNotEmpty(target) + * + * Asserts that the target contains values. + * For arrays and strings, it checks the `length` property. + * For `Map` and `Set` instances, it checks the `size` property. + * For non-function objects, it gets the count of own + * enumerable string keys. + * + * assert.isNotEmpty([1, 2]); + * assert.isNotEmpty('34'); + * assert.isNotEmpty(new Set([5, 6])); + * assert.isNotEmpty({ key: 7 }); + * + * @name isNotEmpty + * @alias notEmpty + * @param {Object|Array|String} target + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.isNotEmpty = function(val, msg) { + new Assertion(val, msg).to.not.be.empty; + }; + /*! * Aliases. */ @@ -2785,5 +2837,7 @@ module.exports = function (chai, util) { ('isSealed', 'sealed') ('isNotSealed', 'notSealed') ('isFrozen', 'frozen') - ('isNotFrozen', 'notFrozen'); + ('isNotFrozen', 'notFrozen') + ('isEmpty', 'empty') + ('isNotEmpty', 'notEmpty'); }; From 990dd73bf2aefa40be92505cc04d8bae8c715716 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Wed, 5 Oct 2016 10:23:49 +0300 Subject: [PATCH 3/4] add assert.empty tests --- test/assert.js | 214 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) diff --git a/test/assert.js b/test/assert.js index d57beebe7..b512b3835 100644 --- a/test/assert.js +++ b/test/assert.js @@ -2017,6 +2017,220 @@ describe('assert', function () { }); }); + it('isEmpty / empty', function() { + ['isEmpty', 'empty'].forEach(function (isEmpty) { + function FakeArgs() {}; + FakeArgs.prototype.length = 0; + + assert[isEmpty](''); + assert[isEmpty]([]); + assert[isEmpty](new FakeArgs); + assert[isEmpty]({}); + + if (typeof WeakMap === 'function') { + err(function(){ + assert[isEmpty](new WeakMap); + }, ".empty was passed a weak collection"); + } + + if (typeof WeakSet === 'function') { + err(function(){ + assert[isEmpty](new WeakSet); + }, ".empty was passed a weak collection"); + } + + if (typeof Map === 'function') { + assert[isEmpty](new Map); + + var map = new Map; + map.key = 'val'; + assert[isEmpty](map); + } + + if (typeof Set === 'function') { + assert[isEmpty](new Set); + + var set = new Set; + set.key = 'val'; + assert[isEmpty](set); + } + + err(function(){ + assert[isEmpty]('foo'); + }, "expected \'foo\' to be empty"); + + err(function(){ + assert[isEmpty](['foo']); + }, "expected [ \'foo\' ] to be empty"); + + err(function(){ + assert[isEmpty]({arguments: 0}); + }, "expected { arguments: 0 } to be empty"); + + err(function(){ + assert[isEmpty]({foo: 'bar'}); + }, "expected { foo: \'bar\' } to be empty"); + + err(function(){ + assert[isEmpty](null); + }, ".empty was passed non-string primitive null"); + + err(function(){ + assert[isEmpty](undefined); + }, ".empty was passed non-string primitive undefined"); + + err(function(){ + assert[isEmpty](); + }, ".empty was passed non-string primitive undefined"); + + err(function(){ + assert[isEmpty](0); + }, ".empty was passed non-string primitive 0"); + + err(function(){ + assert[isEmpty](1); + }, ".empty was passed non-string primitive 1"); + + err(function(){ + assert[isEmpty](true); + }, ".empty was passed non-string primitive true"); + + err(function(){ + assert[isEmpty](false); + }, ".empty was passed non-string primitive false"); + + if (typeof Symbol !== 'undefined') { + err(function(){ + assert[isEmpty](Symbol()); + }, ".empty was passed non-string primitive Symbol()"); + + err(function(){ + assert[isEmpty](Symbol.iterator); + }, ".empty was passed non-string primitive Symbol(Symbol.iterator)"); + } + + err(function(){ + assert[isEmpty](function() {}); + }, ".empty was passed a function"); + + if (FakeArgs.name === 'FakeArgs') { + err(function(){ + assert[isEmpty](FakeArgs); + }, ".empty was passed a function FakeArgs"); + } + }); + }); + + it('isNotEmpty / notEmpty', function() { + ['isNotEmpty', 'notEmpty'].forEach(function (isNotEmpty) { + function FakeArgs() {}; + FakeArgs.prototype.length = 0; + + assert[isNotEmpty]('foo'); + assert[isNotEmpty](['foo']); + assert[isNotEmpty]({arguments: 0}); + assert[isNotEmpty]({foo: 'bar'}); + + if (typeof WeakMap === 'function') { + err(function(){ + assert[isNotEmpty](new WeakMap); + }, ".empty was passed a weak collection"); + } + + if (typeof WeakSet === 'function') { + err(function(){ + assert[isNotEmpty](new WeakSet); + }, ".empty was passed a weak collection"); + } + + if (typeof Map === 'function') { + // Not using Map constructor args because not supported in IE 11. + var map = new Map; + map.set('a', 1); + assert[isNotEmpty](map); + + err(function(){ + assert[isNotEmpty](new Map); + }, "expected {} not to be empty"); + } + + if (typeof Set === 'function') { + // Not using Set constructor args because not supported in IE 11. + var set = new Set; + set.add(1); + assert[isNotEmpty](set); + + err(function(){ + assert[isNotEmpty](new Set); + }, "expected {} not to be empty"); + } + + err(function(){ + assert[isNotEmpty](''); + }, "expected \'\' not to be empty"); + + err(function(){ + assert[isNotEmpty]([]); + }, "expected [] not to be empty"); + + err(function(){ + assert[isNotEmpty](new FakeArgs); + }, "expected { length: 0 } not to be empty"); + + err(function(){ + assert[isNotEmpty]({}); + }, "expected {} not to be empty"); + + err(function(){ + assert[isNotEmpty](null); + }, ".empty was passed non-string primitive null"); + + err(function(){ + assert[isNotEmpty](undefined); + }, ".empty was passed non-string primitive undefined"); + + err(function(){ + assert[isNotEmpty](); + }, ".empty was passed non-string primitive undefined"); + + err(function(){ + assert[isNotEmpty](0); + }, ".empty was passed non-string primitive 0"); + + err(function(){ + assert[isNotEmpty](1); + }, ".empty was passed non-string primitive 1"); + + err(function(){ + assert[isNotEmpty](true); + }, ".empty was passed non-string primitive true"); + + err(function(){ + assert[isNotEmpty](false); + }, ".empty was passed non-string primitive false"); + + if (typeof Symbol !== 'undefined') { + err(function(){ + assert[isNotEmpty](Symbol()); + }, ".empty was passed non-string primitive Symbol()"); + + err(function(){ + assert[isNotEmpty](Symbol.iterator); + }, ".empty was passed non-string primitive Symbol(Symbol.iterator)"); + } + + err(function(){ + assert[isNotEmpty](function() {}); + }, ".empty was passed a function"); + + if (FakeArgs.name === 'FakeArgs') { + err(function(){ + assert[isNotEmpty](FakeArgs); + }, ".empty was passed a function FakeArgs"); + } + }); + }); + it('showDiff true with actual and expected args', function() { try { new chai.Assertion().assert( From 6e423fac8b3a9f63ac5f3fc8b6d690b1ce8f8191 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Wed, 5 Oct 2016 15:06:26 +0300 Subject: [PATCH 4/4] add Map and Set to jsdoc --- lib/chai/interface/assert.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chai/interface/assert.js b/lib/chai/interface/assert.js index 819988f90..b010afa6f 100644 --- a/lib/chai/interface/assert.js +++ b/lib/chai/interface/assert.js @@ -2784,7 +2784,7 @@ module.exports = function (chai, util) { * * @name isEmpty * @alias empty - * @param {Object|Array|String} target + * @param {Object|Array|String|Map|Set} target * @param {String} message _optional_ * @namespace Assert * @api public @@ -2810,7 +2810,7 @@ module.exports = function (chai, util) { * * @name isNotEmpty * @alias notEmpty - * @param {Object|Array|String} target + * @param {Object|Array|String|Map|Set} target * @param {String} message _optional_ * @namespace Assert * @api public