@contract-case/case-core
Version:
Core functionality for the ContractCase contract testing suite
46 lines • 2.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShapedObjectExecutor = void 0;
const case_plugin_base_1 = require("@contract-case/case-plugin-base");
const strip = (matcher, matchContext) => Object.entries(matcher['_case:matcher:children'])
.map(([expectedKey, expectedValueMatcher]) => ({
[expectedKey]: matchContext.descendAndStrip(expectedValueMatcher, (0, case_plugin_base_1.addLocation)(expectedKey, matchContext)),
}))
.reduce((acc, entry) => ({
...acc,
...entry,
}), {});
const whyNotAnObject = (actual) => {
if (typeof actual !== 'object') {
return `Expected an object, but the type was '${typeof actual}' instead`;
}
if (Array.isArray(actual)) {
return 'Expected an object, but it was an array';
}
if (actual != null) {
return 'Expected an object, but it was null or undefined';
}
if (actual !== Object(actual)) {
return `Expected an object, but '${(0, case_plugin_base_1.actualToString)(actual)}' is not an object because the Object constructor doesn't return a reference to it`;
}
return `If you are seeing this message, there is a bug in whyNotAnObject, where it can't see a reason that '${(0, case_plugin_base_1.actualToString)(actual)} is not an Object.`;
};
const check = async (matcher, matchContext, actual) => [
...(typeof actual === 'object' &&
actual === Object(actual) &&
!Array.isArray(actual) &&
actual != null
? (0, case_plugin_base_1.combineResults)((await Promise.all(Object.entries(matcher['_case:matcher:children']).map(([expectedKey, expectedValueMatcher]) => expectedKey in actual
? Promise.resolve(matchContext.descendAndCheck(expectedValueMatcher, (0, case_plugin_base_1.addLocation)(expectedKey, matchContext), actual[expectedKey]))
: Promise.resolve((0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, `missing key '${expectedKey}' in object: ${(0, case_plugin_base_1.actualToString)(actual)}`, actual, matchContext)))))).flat())
: (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, whyNotAnObject(actual), actual, matchContext))),
];
exports.ShapedObjectExecutor = {
describe: (matcher, context) => `an object shaped like {${Object.entries(matcher['_case:matcher:children'])
.map(([key, child]) => `${key}: ${context.descendAndDescribe(child, (0, case_plugin_base_1.addLocation)(key, context))}`)
.join(',')}}`,
check,
strip,
validate: (matcher, matchContext) => Promise.all(Object.entries(matcher['_case:matcher:children']).map(([key, child]) => matchContext.descendAndValidate(child, (0, case_plugin_base_1.addLocation)(`${key}`, matchContext)))).then(() => { }),
};
//# sourceMappingURL=ShapedObjectExecutor.js.map