@contract-case/case-core
Version:
Core functionality for the ContractCase contract testing suite
26 lines • 2.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringPrefixMatcher = void 0;
const case_plugin_base_1 = require("@contract-case/case-plugin-base");
const check = async (matcher, matchContext, actual) => {
if (typeof actual !== 'string') {
return (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, `'${typeof actual}' is not a string`, actual, matchContext));
}
return actual.startsWith(matcher['_case:matcher:prefix'])
? matchContext.descendAndCheck(matcher['_case:matcher:suffix'], (0, case_plugin_base_1.addLocation)(':prefix', matchContext), actual.slice(matcher['_case:matcher:prefix'].length))
: (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, `The string '${actual}' did not start with the expected prefix '${matcher['_case:matcher:prefix']}'`, actual, matchContext));
};
exports.StringPrefixMatcher = {
describe: (matcher, matchContext) => `"${matcher['_case:matcher:prefix']}${matchContext
.descendAndDescribe(matcher['_case:matcher:suffix'], (0, case_plugin_base_1.addLocation)(':prefix', matchContext))
.replace(/^"+|"+$/g, '')}"`,
check,
strip: (matcher, matchContext) => `${matcher['_case:matcher:prefix']}${(0, case_plugin_base_1.mustResolveToString)(matcher['_case:matcher:suffix'], (0, case_plugin_base_1.addLocation)(':suffix', matchContext))}`,
validate: (matcher, matchContext) => Promise.resolve().then(() => {
if (typeof matcher['_case:matcher:prefix'] !== 'string') {
throw new case_plugin_base_1.CaseConfigurationError(`Expected a string for the prefix, but got '${typeof matcher['_case:matcher:prefix']}' instead. Did you put the arguments the wrong way around for the prefix matcher?`, matchContext);
}
return matchContext.descendAndValidate(matcher['_case:matcher:suffix'], (0, case_plugin_base_1.addLocation)(':prefix', matchContext));
}),
};
//# sourceMappingURL=StringPrefixMatcher.js.map