UNPKG

@case-contract-testing/case

Version:

Next-generation contract testing suite

51 lines (50 loc) 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.arrayStartsWith = exports.arrayContains = exports.arrayLength = exports.arrayEachEntryMatches = void 0; const entities_1 = require("../../../entities"); const types_1 = require("../../../entities/types"); /** * Matches an array where each element matches the provided matcher. * * @param matcher - The example object, array, primitive or matcher to match against */ const arrayEachEntryMatches = (matcher, example) => ({ 'case:matcher:type': types_1.ARRAY_EACH_ENTRY_MATCHES_TYPE, 'case:matcher:matcher': matcher, ...(example !== undefined ? { 'case:matcher:example': example } : {}), }); exports.arrayEachEntryMatches = arrayEachEntryMatches; /** * Matches an Array whose length is within a certain range. * * @param options - `ArrayLengthOptions { minLength?: number; maxLength?: number }` */ const arrayLength = (options) => { const matcher = (0, entities_1.coreArrayLengthMatcher)(options); if (matcher['case:matcher:minLength'] === 0 && matcher['case:matcher:maxLength'] !== 0) { throw new entities_1.CaseConfigurationError("Can't create an arrayLength matcher with minimum size 0 and maximum size not 0. Use a raw empty array instead. See the documentation for details."); // TODO write documentation for this } return matcher; }; exports.arrayLength = arrayLength; /** * Matches an Array which contains elements that match the given matchers. * * Note that two different matchers may be satisfied by the same item in the array. * * @param matchers - any number of matchers, each of which must be found inside the array. */ const arrayContains = (...matchers) => ({ 'case:matcher:type': types_1.ARRAY_CONTAINS_TYPE, 'case:matcher:matchers': matchers, }); exports.arrayContains = arrayContains; /** * Matches an Array which starts with the provided array * * @param matchers - An array of matchers that describes the start of the array */ const arrayStartsWith = (matchers) => (0, entities_1.coreShapedArrayMatcher)(matchers); exports.arrayStartsWith = arrayStartsWith;