UNPKG

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

Version:

ContractCase core function plugin, allowing validation of arbitrary functions

51 lines 4.05 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 getFunctionName_1 = require("./getFunctionName"); const advice = (context) => { if (context['_case:currentRun:context:adviceOverrides'] && context['_case:currentRun:context:adviceOverrides']['FAKE_NEVER_CALLED'] != null) { return context['_case:currentRun:context:adviceOverrides']['FAKE_NEVER_CALLED']; } return `Please ensure you are calling the mock function`; }; const ARGUMENTS_ARRAY_TITLE = 'Arguments (index in array is index of argument)'; const strip = (matcher, matchContext) => matchContext.descendAndStrip(matcher.arguments, (0, case_plugin_base_1.addLocation)(`arguments`, matchContext)); const describe = ({ arguments: expectedArguments }, context) => expectedArguments.length === 0 ? `An invocation of ${(0, getFunctionName_1.getFunctionName)(context)}()` : `An invocation of ${(0, getFunctionName_1.getFunctionName)(context)}( ${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. ${advice(matchContext)}`, matchContext, 'FAKE_NEVER_CALLED'); } if (!Array.isArray(actual)) { matchContext.logger.maintainerDebug('Failing because actual appears not to be an array of function arguments. Actual is:', actual); // This is a CoreError because the ArgumentsMatcher is an internal matcher 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'}`, actual, matchContext, matchContext.descendAndStrip(matcher.arguments, (0, case_plugin_base_1.addLocation)(':stripExpected', matchContext)), { actual: ARGUMENTS_ARRAY_TITLE, expected: `${matcher.arguments.length} ${ARGUMENTS_ARRAY_TITLE}`, })), (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`, actual, matchContext, matchContext.descendAndStrip(matcher.arguments, (0, case_plugin_base_1.addLocation)(':stripExpected', matchContext)), { actual: ARGUMENTS_ARRAY_TITLE, expected: ARGUMENTS_ARRAY_TITLE, })), 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