diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 829c1740c..355d9481b 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -536,6 +536,13 @@ module.exports = function (chai, _) { case 'string': itemsCount = val.length; break; + case 'map': + case 'set': + itemsCount = val.size; + break; + case 'weakmap': + case 'weakset': + throw new TypeError('.empty was passed a weak collection'); case 'function': var name = val.name ? ' ' + val.name : ''; throw new TypeError('.empty was passed a function' + name); diff --git a/test/expect.js b/test/expect.js index c70602152..7b1a75ffd 100644 --- a/test/expect.js +++ b/test/expect.js @@ -646,6 +646,60 @@ describe('expect', function () { expect({}).to.be.empty; expect({foo: 'bar'}).not.to.be.empty; + if (typeof WeakMap === 'function') { + err(function(){ + expect(new WeakMap).not.to.be.empty; + }, ".empty was passed a weak collection"); + } + + if (typeof WeakSet === 'function') { + err(function(){ + expect(new WeakSet).not.to.be.empty; + }, ".empty was passed a weak collection"); + } + + if (typeof Map === 'function') { + expect(new Map).to.be.empty; + + // Not using Map constructor args because not supported in IE 11. + var map = new Map; + map.set('a', 1); + expect(map).not.to.be.empty; + + err(function(){ + expect(new Map).not.to.be.empty; + }, "expected {} not to be empty"); + + map = new Map; + map.key = 'val'; + expect(map).to.be.empty; + + err(function(){ + expect(map).not.to.be.empty; + }, "expected { key: 'val' } not to be empty"); + } + + if (typeof Set === 'function') { + expect(new Set).to.be.empty; + + // Not using Set constructor args because not supported in IE 11. + var set = new Set; + set.add(1); + expect(set).not.to.be.empty; + + err(function(){ + expect(new Set).not.to.be.empty; + }, "expected {} not to be empty"); + + set = new Set; + set.key = 'val'; + expect(set).to.be.empty; + + err(function(){ + expect(set).not.to.be.empty; + }, "expected { key: 'val' } not to be empty"); + } + err(function(){ expect('').not.to.be.empty; }, "expected \'\' not to be empty"); diff --git a/test/should.js b/test/should.js index b827784c3..e07e722a1 100644 --- a/test/should.js +++ b/test/should.js @@ -626,6 +626,60 @@ describe('should', function() { ({}).should.be.empty; ({foo: 'bar'}).should.not.be.empty; + if (typeof WeakMap === 'function') { + err(function(){ + (new WeakMap).should.not.be.empty; + }, ".empty was passed a weak collection"); + } + + if (typeof WeakSet === 'function') { + err(function(){ + (new WeakSet).should.not.be.empty; + }, ".empty was passed a weak collection"); + } + + if (typeof Map === 'function') { + (new Map).should.be.empty; + + // Not using Map constructor args because not supported in IE 11. + var map = new Map; + map.set('a', 1); + map.should.not.be.empty; + + err(function(){ + (new Map).should.not.be.empty; + }, "expected {} not to be empty"); + + map = new Map; + map.key = 'val'; + map.should.be.empty; + + err(function(){ + map.should.not.be.empty; + }, "expected { key: 'val' } not to be empty"); + } + + if (typeof Set === 'function') { + (new Set).should.be.empty; + + // Not using Set constructor args because not supported in IE 11. + var set = new Set; + set.add(1); + set.should.not.be.empty; + + err(function(){ + (new Set).should.not.be.empty; + }, "expected {} not to be empty"); + + set = new Set; + set.key = 'val'; + set.should.be.empty; + + err(function(){ + set.should.not.be.empty; + }, "expected { key: 'val' } not to be empty"); + } + err(function(){ ''.should.not.be.empty; }, "expected \'\' not to be empty");