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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 called on a weak collection');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that to keep consistency with the TypeError messages added to #812, this should be something like this:
.empty was passed a weak collection

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks, fixed that. Another error message was just 3 lines away, what's wrong with me...

case 'function':
var name = val.name ? ' ' + val.name : '';
throw new TypeError('.empty was passed a function' + name);
Expand Down
46 changes: 46 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,52 @@ 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 called on a weak collection");
}

if (typeof WeakSet === 'function') {
err(function(){
expect(new WeakSet).not.to.be.empty;
}, ".empty was called on a weak collection");
}

if (typeof Map === 'function') {
expect(new Map).to.be.empty;
expect(new Map([[1,2]])).not.to.be.empty;

err(function(){
expect(new Map).not.to.be.empty;
}, "expected {} not to be empty");

var 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;
expect(new Set([3,4])).not.to.be.empty;

err(function(){
expect(new Set).not.to.be.empty;
}, "expected {} not to be empty");

var 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
46 changes: 46 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,52 @@ 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 called on a weak collection");
}

if (typeof WeakSet === 'function') {
err(function(){
(new WeakSet).should.not.be.empty;
}, ".empty was called on a weak collection");
}

if (typeof Map === 'function') {
(new Map).should.be.empty;
(new Map([[1,2]])).should.not.be.empty;

err(function(){
(new Map).should.not.be.empty;
}, "expected {} not to be empty");

var 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;
(new Set([3,4])).should.not.be.empty;

err(function(){
(new Set).should.not.be.empty;
}, "expected {} not to be empty");

var 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