Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make empty assertion work with es6 collections #814

Merged
merged 4 commits into from
Oct 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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