@case-contract-testing/case
Version:
Next-generation contract testing suite
23 lines (22 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShapedArrayExecutor = void 0;
const context_1 = require("../../entities/context");
const results_1 = require("../../entities/results");
const strip = (matcher, matchContext) => matcher['case:matcher:children'].map((expectedChild, index) => matchContext.descendAndStrip(expectedChild, (0, context_1.addLocation)(`[${index}]`, matchContext)));
const check = async (matcher, matchContext, actual) => (0, results_1.combineResults)(Array.isArray(actual)
? (0, results_1.combineResults)(actual.length >= matcher['case:matcher:children'].length
? (await Promise.all(matcher['case:matcher:children']
.map((expectedChild, index) => matchContext.descendAndCheck(expectedChild, (0, context_1.addLocation)(`[${index}]`, matchContext), actual[index]))
.flat())).flat()
: (0, results_1.makeResults)((0, results_1.matchingError)(matcher, `Array has different lengths - expected at least '${matcher['case:matcher:children'].length}' elements, but found only '${actual.length} elements`, actual, matchContext)), matcher['case:matcher:children'].length === 0 && actual.length !== 0
? (0, results_1.makeResults)((0, results_1.matchingError)(matcher, `Expected an empty array, but instead found ${actual.length} elements`, actual, matchContext))
: (0, results_1.makeNoErrorResult)())
: (0, results_1.makeResults)((0, results_1.matchingError)(matcher, `'${typeof actual}' is not an array`, actual, matchContext)));
exports.ShapedArrayExecutor = {
describe: (matcher, context) => `an array shaped like [${matcher['case:matcher:children']
.map((child, index) => context.descendAndDescribe(child, (0, context_1.addLocation)(`[${index}]`, context)))
.join(',')}]`,
check,
strip,
};