UNPKG

@contract-case/case-core-plugin-function

Version:

ContractCase core function plugin, allowing validation of arbitrary functions

34 lines 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FunctionArgumentMatcherExecutor = void 0; const case_plugin_base_1 = require("@contract-case/case-plugin-base"); const strip = (matcher, matchContext) => matchContext.descendAndStrip(matcher.arguments, (0, case_plugin_base_1.addLocation)(`arguments`, matchContext)); const describe = ({ arguments: expectedArguments }, context) => expectedArguments.length === 0 ? 'a function invoked with no arguments' : `a function invoked with ${expectedArguments.length} argument${expectedArguments.length === 1 ? '' : 's'} ( ${expectedArguments .map((arg, index) => context.descendAndDescribe(arg, (0, case_plugin_base_1.addLocation)(`[${index}]`, context))) .join(', ')} )`; const check = async (matcher, matchContext, actual) => { if (!actual) { throw new case_plugin_base_1.CaseConfigurationError('The expected function was never invoked. Please ensure you are calling the mock function', matchContext); } if (!Array.isArray(actual)) { matchContext.logger.maintainerDebug('Failing because actual appears not to be an array of function arguments. Actual is:', actual); throw new case_plugin_base_1.CaseCoreError('Expected a function call to receive a number of arguments, please ensure your example is correctly defined.', matchContext); } const argResults = matcher.arguments.length === actual.length ? await (0, case_plugin_base_1.combineResultPromises)(...matcher.arguments.map((expectedArg, index) => matchContext.descendAndCheck(expectedArg, (0, case_plugin_base_1.addLocation)(`[${index}]`, matchContext), actual[index]))) : []; return (0, case_plugin_base_1.combineResultPromises)((0, case_plugin_base_1.errorWhen)(matcher.arguments.length !== actual.length, (0, case_plugin_base_1.matchingError)(matcher, `The function expected ${matcher.arguments.length} argument${matcher.arguments.length === 1 ? '' : 's'}, but received ${actual.length} argument${actual.length === 1 ? '' : 's'}`, describe({ ...matcher, arguments: actual }, (0, case_plugin_base_1.addLocation)(':describingActual', matchContext)), matchContext, describe(matcher, (0, case_plugin_base_1.addLocation)(':describingExpected', matchContext)))), (0, case_plugin_base_1.errorWhen)((0, case_plugin_base_1.hasErrors)(argResults), (0, case_plugin_base_1.matchingError)(matcher, `The function arguments didn't match`, describe({ ...matcher, arguments: actual }, (0, case_plugin_base_1.addLocation)(':describingActual', matchContext)), matchContext, describe(matcher, (0, case_plugin_base_1.addLocation)(':describingExpected', matchContext)))), argResults); }; const validate = (matcher, matchContext) => Promise.resolve().then(() => { if (!matcher.arguments) { throw new case_plugin_base_1.CaseConfigurationError('Missing the arguments for the function arguments matcher', matchContext); } if (!Array.isArray(matcher.arguments)) { throw new case_plugin_base_1.CaseConfigurationError(`Function arguments matchers require an array argument, but ${typeof matcher.arguments} was provided instead`, matchContext); } return matchContext.descendAndValidate(matcher.arguments, (0, case_plugin_base_1.addLocation)(`arguments`, matchContext)); }); exports.FunctionArgumentMatcherExecutor = { describe, check, strip, validate }; //# sourceMappingURL=FunctionArgumentsMatcher.js.map