UNPKG

@case-contract-testing/case

Version:

Next-generation contract testing suite

31 lines (30 loc) 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayContains = void 0; const context_1 = require("../../entities/context"); const results_1 = require("../../entities/results"); const types_1 = require("../../entities/types"); const strip = (matcher, matchContext) => matcher['case:matcher:matchers'].map((childMatcher, index) => matchContext.descendAndStrip(childMatcher, (0, context_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, context_1.addLocation)(`:actual[${index}]`, matchContext), actualEntry))).then((matchResult) => { const matched = matchResult.find((result) => !(0, results_1.hasErrors)(result)); if (matched === undefined) { return (0, results_1.combineResults)((0, results_1.makeResults)((0, results_1.matchingError)(parentMatcher, `Expected an array that contained an element that matched '${(0, types_1.isCaseNode)(matcher) ? matcher['case:matcher:type'] : (0, results_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, results_1.makeResults)((0, results_1.matchingError)(parentMatcher, `Expected an array that contained an element that matched '${(0, types_1.isCaseNode)(matcher) ? matcher['case:matcher:type'] : (0, results_1.matcherToString)(matcher)}', but the array was empty`, actual, matchContext)); const check = async (matcher, matchContext, actual) => (0, results_1.combineResults)(Array.isArray(actual) ? (0, results_1.combineResults)(...(await Promise.all(matcher['case:matcher:matchers'].map((childMatcher, index) => checkMatch(matcher, childMatcher, (0, context_1.addLocation)(`:arrayContainsExample[${index}]`, matchContext), actual))))) : (0, results_1.makeResults)((0, results_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, context_1.addLocation)(`:arrayContains[${index}]`, matchContext))) .join(' and ')},`, check, strip, };