Skip to content

Commit

Permalink
Add negation to other callable assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Jan 15, 2024
1 parent b651e60 commit 0ac465a
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,48 +709,75 @@ Assertion.addProperty('callable', function () {
}
});

/**
* @todo
*/
Assertion.addProperty('asyncFunction', 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 AsyncFunction` :
`${msg}expected ${_.inspect(val)} to be a AsyncFunction`;

const isAsyncCallable = ['AsyncFunction', 'AsyncGeneratorFunction'].includes(_.type(val));

if (!['AsyncFunction', 'AsyncGeneratorFunction'].includes(_.type(val))) {
if ((isAsyncCallable && negate) || (!isAsyncCallable && !negate)) {
throw new AssertionError(
msg + 'expected ' + _.inspect(val) + ' to be a AsyncFunction',
assertionMessage,
undefined,
ssfi
);
}
});

/**
* TODO
* @todo
*/
Assertion.addProperty('generatorFunction', 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');

if (!['GeneratorFunction', 'AsyncGeneratorFunction'].includes(_.type(val))) {
const assertionMessage = negate ?
`${msg}expected ${_.inspect(val)} not to be a GeneratorFunction` :
`${msg}expected ${_.inspect(val)} to be a GeneratorFunction`;

const isGeneratorCallable = ['GeneratorFunction', 'AsyncGeneratorFunction'].includes(_.type(val));

if ((isGeneratorCallable && negate) || (!isGeneratorCallable && !negate)) {
throw new AssertionError(
msg + 'expected ' + _.inspect(val) + ' to be a GeneratorFunction',
assertionMessage,
undefined,
ssfi
);
}
});

/**
* TODO
* @todo
*/
Assertion.addProperty('asyncGeneratorFunction', 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 AsyncGeneratorFunction` :
`${msg}expected ${_.inspect(val)} to be a AsyncGeneratorFunction`;

if (_.type(val) !== 'AsyncGeneratorFunction') {
const isAsyncGeneratorCallable = _.type(val) === 'AsyncGeneratorFunction';

if ((isAsyncGeneratorCallable && negate) || (!isAsyncGeneratorCallable && !negate)) {
throw new AssertionError(
msg + 'expected ' + _.inspect(val) + ' to be a AsyncGeneratorFunction',
assertionMessage,
undefined,
ssfi
);
Expand Down

0 comments on commit 0ac465a

Please sign in to comment.