Skip to content

Commit 0ac465a

Browse files
committed
Add negation to other callable assertions
1 parent b651e60 commit 0ac465a

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

lib/chai/core/assertions.js

+38-11
Original file line numberDiff line numberDiff line change
@@ -709,48 +709,75 @@ Assertion.addProperty('callable', function () {
709709
}
710710
});
711711

712+
/**
713+
* @todo
714+
*/
712715
Assertion.addProperty('asyncFunction', function () {
713716
const val = flag(this, 'object')
714717
const ssfi = flag(this, 'ssfi')
715-
const msg = `${flag(this, 'message')}: ` || ''
718+
const message = flag(this, 'message')
719+
const msg = message ? `${message}: ` : ''
720+
const negate = flag(this, 'negate');
721+
722+
const assertionMessage = negate ?
723+
`${msg}expected ${_.inspect(val)} not to be a AsyncFunction` :
724+
`${msg}expected ${_.inspect(val)} to be a AsyncFunction`;
725+
726+
const isAsyncCallable = ['AsyncFunction', 'AsyncGeneratorFunction'].includes(_.type(val));
716727

717-
if (!['AsyncFunction', 'AsyncGeneratorFunction'].includes(_.type(val))) {
728+
if ((isAsyncCallable && negate) || (!isAsyncCallable && !negate)) {
718729
throw new AssertionError(
719-
msg + 'expected ' + _.inspect(val) + ' to be a AsyncFunction',
730+
assertionMessage,
720731
undefined,
721732
ssfi
722733
);
723734
}
724735
});
725736

726737
/**
727-
* TODO
738+
* @todo
728739
*/
729740
Assertion.addProperty('generatorFunction', function() {
730741
const val = flag(this, 'object')
731742
const ssfi = flag(this, 'ssfi')
732-
const msg = `${flag(this, 'message')}: ` || ''
743+
const message = flag(this, 'message')
744+
const msg = message ? `${message}: ` : ''
745+
const negate = flag(this, 'negate');
733746

734-
if (!['GeneratorFunction', 'AsyncGeneratorFunction'].includes(_.type(val))) {
747+
const assertionMessage = negate ?
748+
`${msg}expected ${_.inspect(val)} not to be a GeneratorFunction` :
749+
`${msg}expected ${_.inspect(val)} to be a GeneratorFunction`;
750+
751+
const isGeneratorCallable = ['GeneratorFunction', 'AsyncGeneratorFunction'].includes(_.type(val));
752+
753+
if ((isGeneratorCallable && negate) || (!isGeneratorCallable && !negate)) {
735754
throw new AssertionError(
736-
msg + 'expected ' + _.inspect(val) + ' to be a GeneratorFunction',
755+
assertionMessage,
737756
undefined,
738757
ssfi
739758
);
740759
}
741760
});
742761

743762
/**
744-
* TODO
763+
* @todo
745764
*/
746765
Assertion.addProperty('asyncGeneratorFunction', function() {
747766
const val = flag(this, 'object')
748767
const ssfi = flag(this, 'ssfi')
749-
const msg = `${flag(this, 'message')}: ` || ''
768+
const message = flag(this, 'message')
769+
const msg = message ? `${message}: ` : ''
770+
const negate = flag(this, 'negate');
771+
772+
const assertionMessage = negate ?
773+
`${msg}expected ${_.inspect(val)} not to be a AsyncGeneratorFunction` :
774+
`${msg}expected ${_.inspect(val)} to be a AsyncGeneratorFunction`;
750775

751-
if (_.type(val) !== 'AsyncGeneratorFunction') {
776+
const isAsyncGeneratorCallable = _.type(val) === 'AsyncGeneratorFunction';
777+
778+
if ((isAsyncGeneratorCallable && negate) || (!isAsyncGeneratorCallable && !negate)) {
752779
throw new AssertionError(
753-
msg + 'expected ' + _.inspect(val) + ' to be a AsyncGeneratorFunction',
780+
assertionMessage,
754781
undefined,
755782
ssfi
756783
);

0 commit comments

Comments
 (0)