Skip to content

Commit

Permalink
add function assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Jan 5, 2024
1 parent 082c7e2 commit 102bbf0
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as chai from '../../../index.js';
import {Assertion} from '../assertion.js';
import {flag, inspect} from '../utils/index.js';
import {AssertionError} from 'assertion-error';
import {type} from '../utils/type-detect.js';

/**
* ### assert(expression, message)
Expand Down Expand Up @@ -590,6 +591,56 @@ assert.isNotFunction = function (val, msg) {
new Assertion(val, msg, assert.isNotFunction, true).to.not.be.a('function');
};

/**
* TODO
*/
assert.isCallable = function (val, msg) {
if (!['Function', 'AsyncFunction', 'GeneratorFunction', 'AsyncGeneratorFunction'].includes(type(val))) {
throw new AssertionError(
msg + ': expected ' + inspect(val) + ' to be callable',
undefined,
assert.isCallable
);
}
}

/**
* ### .isAsyncFunction(value, [message])
*
* Asserts that `value` is a async function.
*
* async function serveTea() { return 'cup of tea'; };
* assert.isAsyncFunction(serveTea, 'great, we can have tea now');
*
* @name isAsyncFunction
* @param {Mixed} value
* @param {String} message
* @namespace Assert
* @api public
*/
assert.isAsyncFunction = function (val, msg) {
if (!['AsyncFunction', 'AsyncGeneratorFunction'].includes(type(val))) {
throw new AssertionError(
msg + ': expected ' + inspect(val) + ' to be a AsyncFunction',
undefined,
assert.isAsyncFunction
);
}
};

/**
* TODO
*/
assert.isGeneratorFunction = function(val, msg) {
if (!['GeneratorFunction', 'AsyncGeneratorFunction'].includes(type(val))) {
throw new AssertionError(
msg + ': expected ' + inspect(val) + ' to be a GeneratorFunction',
undefined,
assert.isGeneratorFunction
);
}
}

/**
* ### .isObject(value, [message])
*
Expand Down
42 changes: 42 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,48 @@ describe('assert', function () {
}, "blah: expected {} to be a function");
});

it('isCallable', function() {
var func = function() {};
assert.isCallable(func);

var func = async function() {};
assert.isCallable(func);

var func = function* () {}
assert.isCallable(func);

var func = async function* () {}
assert.isCallable(func);

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

it('isAsyncFunction', function() {
var func = async function() {};
assert.isAsyncFunction(func);

var func = async function*() {};
assert.isAsyncFunction(func);

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

it('isGeneratorFunction', function() {
var func = function* () {}
assert.isGeneratorFunction(func)

var func = async function* () {}
assert.isGeneratorFunction(func)

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

it('isNotFunction', function () {
assert.isNotFunction(5);

Expand Down

0 comments on commit 102bbf0

Please sign in to comment.