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

Add .empty to assert interface #828

Merged
merged 4 commits into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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: 5 additions & 2 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 55 additions & 1 deletion lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it worth it to include Map and Set here? Just to keep things explicit, what do you guys think?

* @param {String} message _optional_
* @namespace Assert
* @api public
*/

assert.isNotEmpty = function(val, msg) {
new Assertion(val, msg).to.not.be.empty;
};

/*!
* Aliases.
*/
Expand All @@ -2785,5 +2837,7 @@ module.exports = function (chai, util) {
('isSealed', 'sealed')
('isNotSealed', 'notSealed')
('isFrozen', 'frozen')
('isNotFrozen', 'notFrozen');
('isNotFrozen', 'notFrozen')
('isEmpty', 'empty')
('isNotEmpty', 'notEmpty');
};
214 changes: 214 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down