@contract-case/case-core
Version:
Core functionality for the ContractCase contract testing suite
35 lines • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonStringifiedString = void 0;
const case_plugin_base_1 = require("@contract-case/case-plugin-base");
const check = (matcher, matchContext, actual) => {
if (typeof actual !== 'string') {
return (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, `${(0, case_plugin_base_1.actualToString)(actual)} is not a string; so it can't match a json stringified string`, actual, matchContext, '<A string that would parse as json>'));
}
let decodedActual;
try {
decodedActual = JSON.parse(actual);
}
catch (parseError) {
return (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, `${(0, case_plugin_base_1.actualToString)(actual)} failed to parse as json (${parseError.message})`, actual, matchContext, '<A string that would parse as json>'));
}
return matchContext.descendAndCheck(matcher['_case:matcher:child'], (0, case_plugin_base_1.addLocation)(':jsonStringify', matchContext), decodedActual);
};
const strip = (matcher, matchContext) => {
const result = matchContext.descendAndStrip(matcher['_case:matcher:child'], (0, case_plugin_base_1.addLocation)(':jsonStringify', matchContext));
let encoded;
try {
encoded = JSON.stringify(result);
}
catch (parseError) {
throw new case_plugin_base_1.CaseConfigurationError(`Unable to json stringify '${result}' during stripMatchers (${parseError.message})`, matchContext, 'BAD_INTERACTION_DEFINITION');
}
return encoded;
};
exports.JsonStringifiedString = {
describe: (matcher, matchContext) => `json stringified '${matchContext.descendAndDescribe(matcher['_case:matcher:child'], (0, case_plugin_base_1.addLocation)(':jsonStringify', matchContext))}'`,
check,
strip,
validate: (matcher, matchContext) => matchContext.descendAndValidate(matcher['_case:matcher:child'], (0, case_plugin_base_1.addLocation)(':jsonStringify', matchContext)),
};
//# sourceMappingURL=JsonStringifiedString.js.map