@contract-case/case-core
Version:
Core functionality for the ContractCase contract testing suite
99 lines • 4.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addLookupableMatcher = exports.findVariable = exports.addVariable = exports.findMatcher = exports.getPendingCount = exports.getSuccessCount = exports.getFailures = exports.hasFailure = exports.isEmpty = exports.addExample = exports.makeContract = void 0;
const versionString_1 = require("../../entities/versionString");
const lookup_1 = require("./lookup");
const internals_1 = require("./lookup/internals");
/**
* Internal function to generate a variable name from the current test, for lookup purposes.
*
* This exists to stop default variables with the same name from clashing if their contents are different
*
* Currently concatenates the test name with the variable name, but don't rely on this.
*
* @internal
*
* @param variableName - The actual variable name
* @param context - The current run context. Used to get the test name
* @returns a unique name.
*/
const variableNameFor = (variableName, context) => `${variableName}::test[${context['_case:currentRun:context:testName']}]`;
const makeContract = (description) => ({
contractType: 'case::contract',
description,
metadata: {
_case: {
version: process.env['CASE_MAINTAINER_TESTING_VERSION_OVERRIDE']
? 'case-internal-tests'
: versionString_1.caseVersion,
},
},
matcherLookup: {},
examples: new Array(),
});
exports.makeContract = makeContract;
const addExample = (contract, example, context) => ({
...contract,
matcherLookup: (0, lookup_1.addMock)(contract.matcherLookup, example.mock, context),
examples: [...contract.examples, example],
});
exports.addExample = addExample;
/**
* Whether the contract has any examples or not
*
* @param contract - a ContractData object
* @returns true if the contract has no examples
*/
const isEmpty = (contract) => contract.examples.length === 0;
exports.isEmpty = isEmpty;
/**
* Whether the contract has any examples that are failures
*
* @param contract - a ContractData object
* @returns true if the contract has any failed examples
*/
const hasFailure = (contract) => contract.examples.find((example) => example.result === 'FAILED') !==
undefined;
exports.hasFailure = hasFailure;
const getFailures = (contract) => contract.examples
.filter((e) => e.result === 'FAILED')
.map((e) => e.errors)
.flat();
exports.getFailures = getFailures;
/**
* Counts the number of interactions that are VERIFIED (ie, have passed during this run, or previously before the contract was written)
* @param contract - a ContractData object
* @returns the number of interactions that are VERIFIED
*/
const getSuccessCount = (contract) => contract.examples.filter((e) => e.result === 'VERIFIED').length;
exports.getSuccessCount = getSuccessCount;
/**
* Counts the number of interactions that are PENDING (ie, not failed or verified)
* @param contract - a ContractData object
* @returns the number of interactions that are PENDING
*/
const getPendingCount = (contract) => contract.examples.filter((e) => e.result === 'PENDING').length;
exports.getPendingCount = getPendingCount;
const findMatcher = (contract, uniqueName, context) => (0, internals_1.findLookup)(contract.matcherLookup, 'matcher', uniqueName, context);
exports.findMatcher = findMatcher;
const addVariable = (contract, uniqueName, type, variable, context) => ({
...contract,
matcherLookup: (0, internals_1.addLookup)(contract.matcherLookup, `variable:${type}`, variableNameFor(uniqueName, context), variable, context),
examples: [...contract.examples],
});
exports.addVariable = addVariable;
const findVariable = (contract, uniqueName, context) => {
const stateVariable = (0, internals_1.findLookup)(contract.matcherLookup, 'variable:state', variableNameFor(uniqueName, context), context);
if (stateVariable !== undefined) {
return stateVariable;
}
return (0, internals_1.findLookup)(contract.matcherLookup, `variable:default`, variableNameFor(uniqueName, context), context);
};
exports.findVariable = findVariable;
const addLookupableMatcher = (contract, matcher, context) => ({
...contract,
matcherLookup: (0, lookup_1.addMatcher)(contract.matcherLookup, matcher, context),
examples: [...contract.examples],
});
exports.addLookupableMatcher = addLookupableMatcher;
//# sourceMappingURL=structure.js.map