Skip to content

Commit

Permalink
alias isFunction to isCallable
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Jan 15, 2024
1 parent 8cfd914 commit bb68ea3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
14 changes: 11 additions & 3 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,11 +678,19 @@ Assertion.addProperty('true', function () {
Assertion.addProperty('callable', function () {
const val = flag(this, 'object')
const ssfi = flag(this, 'ssfi')
const msg = `${flag(this, 'message')}: ` || ''
const message = flag(this, 'message')
const msg = message ? `${message}: ` : ''
const negate = flag(this, 'negate');

const assertionMessage = negate ?
`${msg}expected ${_.inspect(val)} not to be a callable function` :
`${msg}expected ${_.inspect(val)} to be a callable function`;

const isCallable = !['Function', 'AsyncFunction', 'GeneratorFunction', 'AsyncGeneratorFunction'].includes(_.type(val));

if (!['Function', 'AsyncFunction', 'GeneratorFunction', 'AsyncGeneratorFunction'].includes(_.type(val))) {
if ((!isCallable && negate) || (isCallable && !negate)) {
throw new AssertionError(
msg + 'expected ' + _.inspect(val) + ' to be callable',
assertionMessage,
undefined,
ssfi
);
Expand Down
39 changes: 16 additions & 23 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,50 +554,41 @@ assert.isDefined = function (val, msg) {
};

/**
* ### .isFunction(value, [message])
* ### .isCallable(value, [message])
*
* Asserts that `value` is a function.
* Asserts that `value` is a callable function.
*
* function serveTea() { return 'cup of tea'; };
* assert.isFunction(serveTea, 'great, we can have tea now');
* assert.isCallable(serveTea, 'great, we can have tea now');
*
* @name isFunction
* @name isCallable
* @param {Mixed} value
* @param {String} message
* @namespace Assert
* @api public
*/

assert.isFunction = function (val, msg) {
new Assertion(val, msg, assert.isFunction, true).to.be.a('function');
};
assert.isCallable = function (val, msg) {
new Assertion(val, msg, assert.isCallable, true).is.callable;
}

/**
* ### .isNotFunction(value, [message])
* ### .isNotCallable(value, [message])
*
* Asserts that `value` is _not_ a function.
* Asserts that `value` is _not_ a callable function.
*
* var serveTea = [ 'heat', 'pour', 'sip' ];
* assert.isNotFunction(serveTea, 'great, we have listed the steps');
* assert.isNotCallable(serveTea, 'great, we have listed the steps');
*
* @name isNotFunction
* @name isNotCallable
* @param {Mixed} value
* @param {String} message
* @namespace Assert
* @api public
*/

assert.isNotFunction = function (val, msg) {
new Assertion(val, msg, assert.isNotFunction, true).to.not.be.a('function');
assert.isNotCallable = function (val, msg) {
new Assertion(val, msg, assert.isNotCallable, true).is.not.callable;
};

/**
* TODO
*/
assert.isCallable = function (val, msg) {
new Assertion(val, msg, assert.isCallable, true).is.callable;
}

/**
* ### .isAsyncFunction(value, [message])
*
Expand Down Expand Up @@ -3144,4 +3135,6 @@ assert.isNotEmpty = function(val, msg) {
('isFrozen', 'frozen')
('isNotFrozen', 'notFrozen')
('isEmpty', 'empty')
('isNotEmpty', 'notEmpty');
('isNotEmpty', 'notEmpty')
('isCallable', 'isFunction')
('isNotCallable', 'isNotFunction')
6 changes: 3 additions & 3 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ describe('assert', function () {

err(function () {
assert.isFunction({}, 'blah');
}, "blah: expected {} to be a function");
}, "blah: expected {} to be a callable function");
});

it('isCallable', function() {
Expand All @@ -545,7 +545,7 @@ describe('assert', function () {

err(function () {
assert.isCallable({}, 'blah');
}, "blah: expected {} to be callable");
}, "blah: expected {} to be a callable function");
});

it('isAsyncFunction', function() {
Expand Down Expand Up @@ -594,7 +594,7 @@ describe('assert', function () {

err(function () {
assert.isNotFunction(function () {}, 'blah');
}, "blah: expected [Function] not to be a function");
}, "blah: expected [Function] not to be a callable function");
});

it('isArray', function() {
Expand Down

0 comments on commit bb68ea3

Please sign in to comment.