Skip to content

Commit

Permalink
Make empty assertion work with es6 collections (#814)
Browse files Browse the repository at this point in the history
* support Map and Set

* throw on weak collections

* called on => passed

* account for partial implementations
  • Loading branch information
shvaikalesh authored and vieiralucas committed Oct 2, 2016
1 parent 5b170b2 commit c38bfec
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 54 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
54 changes: 54 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit c38bfec

Please sign in to comment.