@contract-case/case-core
Version:
Core functionality for the ContractCase contract testing suite
78 lines • 4.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.traversals = void 0;
const case_entities_internal_1 = require("@contract-case/case-entities-internal");
const case_plugin_base_1 = require("@contract-case/case-plugin-base");
const inferMatcher_1 = require("./inferMatcher");
const MatcherExecutors_1 = require("./MatcherExecutors");
const getExecutor = (matcherOrData, parentMatchContext) => {
const matcher = (0, inferMatcher_1.inferMatcher)(matcherOrData);
const matchContext = (0, case_plugin_base_1.foldIntoContext)(matcher, parentMatchContext);
const executor = MatcherExecutors_1.MatcherExecutors[matcher['_case:matcher:type']];
if (!executor) {
throw new case_plugin_base_1.CaseCoreError(`Missing executor for matcher type '${matcher['_case:matcher:type']}'`);
}
return {
name: () => executor.describe(matcher, matchContext),
check: async (actual) => {
parentMatchContext.logger.deepMaintainerDebug(`Entering ${matcher['_case:matcher:type']}, actual is:`, actual);
const result = await executor.check(matcher, matchContext, actual);
parentMatchContext.logger.deepMaintainerDebug(`Exiting ${matcher['_case:matcher:type']}, with ${result.length} errors`);
return result;
},
strip: () => executor.strip(matcher, matchContext),
validate: () => executor.validate(matcher, matchContext),
};
};
const descendAndCheck = (matcherOrData, parentMatchContext, actual) => getExecutor(matcherOrData, parentMatchContext).check(actual);
const descendAndValidate = (matcherOrData, parentMatchContext) => getExecutor(matcherOrData, parentMatchContext).validate();
const descendAndDescribe = (matcherOrData, parentMatchContext) => getExecutor(matcherOrData, parentMatchContext).name();
const descendAndStrip = (matcherOrData, parentMatchContext) => {
if ((0, case_entities_internal_1.hasExample)(matcherOrData)) {
parentMatchContext.logger.deepMaintainerDebug(`Executing strip with matcher type: ${matcherOrData['_case:matcher:type']} and specific example`, matcherOrData['_case:matcher:example']);
return getExecutor(matcherOrData['_case:matcher:example'], parentMatchContext).strip();
}
parentMatchContext.logger.deepMaintainerDebug(`Executing strip with matcher ${typeof matcherOrData === 'object' &&
matcherOrData !== null &&
'_case:matcher:type' in matcherOrData
? `type: ${matcherOrData['_case:matcher:type']}`
: `inferred from ${typeof matcherOrData}`}`);
return getExecutor(matcherOrData, parentMatchContext).strip();
};
/**
* selfVerify is the entry point into the self-verification process:
*
* - First check that the parameters passed to the matchers are appropriate
* - Then validate that the matcher matches itself (ie, if stripped back to its
* example, does the example pass the matcher?)
*
* @param matcherOrData - The entry point matcher / example
* @param parentMatchContext - The match context for this run
* @returns a successful promise if verification passes, or a failed promise
* with details of what went wrong if verification failed.
*/
const selfVerify = (matcherOrData, parentMatchContext) => Promise.resolve()
.then(() =>
// First validate the parameters
// This function will reject the promise if validation fails
descendAndValidate(matcherOrData, (0, case_plugin_base_1.addLocation)(':selfVerifyValidate', parentMatchContext)))
.then(() =>
// Then validate that the matcher matches its example
getExecutor(matcherOrData, parentMatchContext).check(descendAndStrip(matcherOrData, (0, case_plugin_base_1.addLocation)(':selfVerifyCheck', parentMatchContext))))
.then((selfVerification) => {
if ((0, case_plugin_base_1.hasErrors)(selfVerification)) {
parentMatchContext.logger.deepMaintainerDebug('Self verification FAILED');
throw new case_plugin_base_1.CaseConfigurationError(
// TODO document this extensively.
`The matchers used have been given an example that doesn't pass the matcher: ${selfVerification[0]?.message} (at ${selfVerification[0]?.location})`, 'DONT_ADD_LOCATION', 'UNDOCUMENTED');
}
parentMatchContext.logger.deepMaintainerDebug('Self verification succeeded');
});
exports.traversals = {
descendAndDescribe,
descendAndCheck,
descendAndStrip,
descendAndValidate,
selfVerify,
};
//# sourceMappingURL=traversals.js.map