@contract-case/case-core
Version:
Core functionality for the ContractCase contract testing suite
37 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayContains = void 0;
const case_plugin_base_1 = require("@contract-case/case-plugin-base");
const strip = (matcher, matchContext) => matcher['_case:matcher:matchers'].map((childMatcher, index) => matchContext.descendAndStrip(childMatcher, (0, case_plugin_base_1.addLocation)(`:arrayContainsExample[${index}]`, matchContext)));
const checkMatch = async (parentMatcher, matcher, matchContext, actual) => actual.length !== 0
? Promise.all(actual.map((actualEntry, index) => matchContext.descendAndCheck(matcher, (0, case_plugin_base_1.addLocation)(`:actual[${index}]`, matchContext), actualEntry))).then((matchResult) => {
const matched = matchResult.find((result) => !(0, case_plugin_base_1.hasErrors)(result));
if (matched === undefined) {
return (0, case_plugin_base_1.combineResults)((0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(parentMatcher, `Expected an array that contained an element that matched '${(0, case_plugin_base_1.isCaseNode)(matcher)
? matcher['_case:matcher:type']
: (0, case_plugin_base_1.matcherToString)(matcher)}', but no elements matched. All matching errors follow; this list may be long if there are many elements in the array`, actual, matchContext)), ...matchResult);
}
return matched;
})
: (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(parentMatcher, `Expected an array that contained an element that matched '${(0, case_plugin_base_1.isCaseNode)(matcher)
? matcher['_case:matcher:type']
: (0, case_plugin_base_1.matcherToString)(matcher)}', but the array was empty`, actual, matchContext));
const check = async (matcher, matchContext, actual) => (0, case_plugin_base_1.combineResults)(Array.isArray(actual)
? (0, case_plugin_base_1.combineResults)(...(await Promise.all(matcher['_case:matcher:matchers'].map((childMatcher, index) => checkMatch(matcher, childMatcher, (0, case_plugin_base_1.addLocation)(`:arrayContainsExample[${index}]`, matchContext), actual)))))
: (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, `'${typeof actual}' is not an array`, actual, matchContext)));
exports.ArrayContains = {
describe: (matcher, matchContext) => `an array containing ${matcher['_case:matcher:matchers']
.map((childMatcher, index) => matchContext.descendAndDescribe(childMatcher, (0, case_plugin_base_1.addLocation)(`:arrayContains[${index}]`, matchContext)))
.join(' and ')},`,
check,
strip,
validate: (matcher, matchContext) => Promise.resolve()
.then(() => {
if (!Array.isArray(matcher['_case:matcher:matchers'])) {
throw new case_plugin_base_1.CaseConfigurationError("The ArrayContains matchers was given something that wasn't an array", matchContext, 'BAD_INTERACTION_DEFINITION');
}
})
.then(() => Promise.all(matcher['_case:matcher:matchers'].map((childMatcher, index) => matchContext.descendAndValidate(childMatcher, (0, case_plugin_base_1.addLocation)(`:arrayContains[${index}]`, matchContext)))))
.then(() => { }),
};
//# sourceMappingURL=ArrayContains.js.map