UNPKG

@case-contract-testing/case

Version:

Next-generation contract testing suite

70 lines (69 loc) 2.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stringifiedJson = exports.encodedStringBase64 = exports.stringSuffix = exports.stringPrefix = exports.stringContaining = void 0; const types_1 = require("../../../entities/types"); const primitives_1 = require("./primitives"); /** * Matches any string that contains the given substring. * * @param substring - The substring that the matcher must contain * @param example - An example string that passes this matcher */ const stringContaining = (substring, example) => ({ 'case:matcher:type': types_1.STRING_CONTAINS_TYPE, 'case:matcher:contains': substring, 'case:matcher:resolvesTo': 'string', 'case:matcher:example': example, }); exports.stringContaining = stringContaining; /** * Matches any string that begins with the given constant string prefix * * @param prefix - The prefix string. Must be a string and not a matcher * @param suffix - An optional string or matcher to match against the suffix */ const stringPrefix = (prefix, suffix = (0, primitives_1.anyString)()) => ({ 'case:matcher:type': types_1.STRING_PREFIX_TYPE, 'case:matcher:prefix': prefix, 'case:matcher:suffix': suffix, 'case:matcher:resolvesTo': 'string', }); exports.stringPrefix = stringPrefix; /** * Matches any string that ends with the given constant string suffix * * @param prefix - A string or matcher to match against the prefix. If you don't mind what the prefix is, pass `anyString()` * @param suffix - The suffix string. Must be a string and not a matcher */ const stringSuffix = (prefix, suffix) => ({ 'case:matcher:type': types_1.STRING_SUFFIX_TYPE, 'case:matcher:prefix': prefix !== undefined ? prefix : (0, primitives_1.anyString)(), 'case:matcher:suffix': suffix, 'case:matcher:resolvesTo': 'string', }); exports.stringSuffix = stringSuffix; /** * Matches any string that matches a base64 encoded version of the given string or string matcher * * WARNING: Since many strings are accidentally decodable as base64, this matcher is * best combined with a more restrictive string matcher (eg stringifiedJson()). * * @param child - The string or string matcher to match against */ const encodedStringBase64 = (child) => ({ 'case:matcher:type': types_1.BASE64_ENCODED_TYPE, 'case:matcher:child': child, 'case:matcher:resolvesTo': 'string', }); exports.encodedStringBase64 = encodedStringBase64; /** * Matches any string that matches a JSON.stringify()ed version of the given object (which may itself contain matchers) * * @param child - The string or string matcher to match against */ const stringifiedJson = (child) => ({ 'case:matcher:type': types_1.JSON_STRINGIFIED_TYPE, 'case:matcher:child': child, 'case:matcher:resolvesTo': 'string', }); exports.stringifiedJson = stringifiedJson;