Skip to content

Commit 102bbf0

Browse files
committed
add function assertions
1 parent 082c7e2 commit 102bbf0

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lib/chai/interface/assert.js

+51
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as chai from '../../../index.js';
88
import {Assertion} from '../assertion.js';
99
import {flag, inspect} from '../utils/index.js';
1010
import {AssertionError} from 'assertion-error';
11+
import {type} from '../utils/type-detect.js';
1112

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

594+
/**
595+
* TODO
596+
*/
597+
assert.isCallable = function (val, msg) {
598+
if (!['Function', 'AsyncFunction', 'GeneratorFunction', 'AsyncGeneratorFunction'].includes(type(val))) {
599+
throw new AssertionError(
600+
msg + ': expected ' + inspect(val) + ' to be callable',
601+
undefined,
602+
assert.isCallable
603+
);
604+
}
605+
}
606+
607+
/**
608+
* ### .isAsyncFunction(value, [message])
609+
*
610+
* Asserts that `value` is a async function.
611+
*
612+
* async function serveTea() { return 'cup of tea'; };
613+
* assert.isAsyncFunction(serveTea, 'great, we can have tea now');
614+
*
615+
* @name isAsyncFunction
616+
* @param {Mixed} value
617+
* @param {String} message
618+
* @namespace Assert
619+
* @api public
620+
*/
621+
assert.isAsyncFunction = function (val, msg) {
622+
if (!['AsyncFunction', 'AsyncGeneratorFunction'].includes(type(val))) {
623+
throw new AssertionError(
624+
msg + ': expected ' + inspect(val) + ' to be a AsyncFunction',
625+
undefined,
626+
assert.isAsyncFunction
627+
);
628+
}
629+
};
630+
631+
/**
632+
* TODO
633+
*/
634+
assert.isGeneratorFunction = function(val, msg) {
635+
if (!['GeneratorFunction', 'AsyncGeneratorFunction'].includes(type(val))) {
636+
throw new AssertionError(
637+
msg + ': expected ' + inspect(val) + ' to be a GeneratorFunction',
638+
undefined,
639+
assert.isGeneratorFunction
640+
);
641+
}
642+
}
643+
593644
/**
594645
* ### .isObject(value, [message])
595646
*

test/assert.js

+42
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,48 @@ describe('assert', function () {
530530
}, "blah: expected {} to be a function");
531531
});
532532

533+
it('isCallable', function() {
534+
var func = function() {};
535+
assert.isCallable(func);
536+
537+
var func = async function() {};
538+
assert.isCallable(func);
539+
540+
var func = function* () {}
541+
assert.isCallable(func);
542+
543+
var func = async function* () {}
544+
assert.isCallable(func);
545+
546+
err(function () {
547+
assert.isCallable({}, 'blah');
548+
}, "blah: expected {} to be callable");
549+
});
550+
551+
it('isAsyncFunction', function() {
552+
var func = async function() {};
553+
assert.isAsyncFunction(func);
554+
555+
var func = async function*() {};
556+
assert.isAsyncFunction(func);
557+
558+
err(function () {
559+
assert.isAsyncFunction(function() {}, 'blah');
560+
}, "blah: expected [Function] to be a AsyncFunction");
561+
});
562+
563+
it('isGeneratorFunction', function() {
564+
var func = function* () {}
565+
assert.isGeneratorFunction(func)
566+
567+
var func = async function* () {}
568+
assert.isGeneratorFunction(func)
569+
570+
err(function () {
571+
assert.isGeneratorFunction(function() {}, 'blah');
572+
}, "blah: expected [Function] to be a GeneratorFunction");
573+
})
574+
533575
it('isNotFunction', function () {
534576
assert.isNotFunction(5);
535577

0 commit comments

Comments
 (0)