@contract-case/case-core
Version:
Core functionality for the ContractCase contract testing suite
132 lines • 5.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeResultFormatter = void 0;
const case_plugin_base_1 = require("@contract-case/case-plugin-base");
const resultStringer_1 = require("./resultStringer");
const __1 = require("..");
const locationTitleLine = (location, context) => Array.isArray(location) && location.length > 3
? ` (at ${(0, case_plugin_base_1.locationString)({
...context,
'_case:currentRun:context:location': location.slice(2),
})}) `
: ' ';
const camelToCapital = (camel) => camel.replace(/([a-z])([A-Z])/g, '$1 $2').toLocaleUpperCase();
/**
* This function wraps a result printer so that it still delegates to the
* original {@link ResultPrinter}, but then returns the result of calling
* {@link resultStringer} with the same argument.
*
* @param printer - the {@link ResultPrinter} to wrap
* @returns a ResultPrinter that returns strings as described above
*/
const wrapResultPrinter = (printer) => ({
printMatchError: (matchErrorDescription, context) => {
if (context['_case:currentRun:context:printResults']) {
printer.printMatchError(matchErrorDescription);
}
return resultStringer_1.resultStringer.stringMatchError(matchErrorDescription);
},
printMessageError: (messageErrorDetails, context) => {
if (context['_case:currentRun:context:printResults']) {
printer.printMessageError(messageErrorDetails);
}
return resultStringer_1.resultStringer.stringMessageError(messageErrorDetails);
},
printTestTitle: (titleDetails, context) => {
if (context['_case:currentRun:context:printResults']) {
printer.printTestTitle(titleDetails);
}
return resultStringer_1.resultStringer.stringTestTitle(titleDetails);
},
});
const wrapWithAnnotation = (message, preamble) => preamble ? `${preamble}:\n${message}` : message;
const makePrintError = (unwrappedPrinter) => (error, context) => {
const printer = wrapResultPrinter(unwrappedPrinter);
const locationTag = 'userFacingStackTrace' in error &&
typeof error.userFacingStackTrace === 'string' &&
error.userFacingStackTrace !== ''
? error.userFacingStackTrace
: (0, case_plugin_base_1.locationString)({
...context,
'_case:currentRun:context:location': Array.isArray(error.location)
? error.location
: [],
});
const errorTypeTag = 'matcher' in error ? error.matcher['_case:matcher:type'] : error.code;
switch (error.type) {
case case_plugin_base_1.ERROR_TYPE_MATCHING:
case case_plugin_base_1.ERROR_TYPE_RAW_MATCH:
return printer.printMatchError({
kind: 'MATCHING ERROR',
message: error.message,
location: locationTitleLine(error.location, context),
expected: wrapWithAnnotation((0, case_plugin_base_1.actualToString)(error.expected, 10), error.annotations?.expected),
actual: wrapWithAnnotation((0, case_plugin_base_1.actualToString)(error.actual, 10), error.annotations?.actual),
locationTag,
errorTypeTag,
}, context);
case case_plugin_base_1.ERROR_TYPE_CONFIGURATION:
case case_plugin_base_1.ERROR_TYPE_TRIGGER:
return printer.printMessageError({
kind: camelToCapital(error.code),
message: error.message,
location: locationTitleLine(error.location, context),
locationTag,
errorTypeTag,
}, context);
case case_plugin_base_1.ERROR_TYPE_TEST_RESPONSE:
return printer.printMessageError({
kind: 'ERROR VERIFYING RETURNED OBJECT',
message: error.message,
location: locationTitleLine(error.location, context),
locationTag,
errorTypeTag,
}, context);
default:
return printer.printMessageError({
kind: 'ERROR',
message: error.message,
location: 'location' in error &&
Array.isArray(error.location)
? locationTitleLine(error.location, context)
: locationTitleLine([], context),
locationTag,
errorTypeTag,
}, context);
}
};
const makePrintFailureTitle = (unwrappedPrinter) => {
const printer = wrapResultPrinter(unwrappedPrinter);
return (example, index, context) => printer.printTestTitle({
kind: 'failure',
icon: `✘`,
title: (0, __1.exampleToNames)(example, index).mockName,
additionalText: ' Error details follow:\n',
}, context);
};
const makePrintSuccessTitle = (unwrappedPrinter) => {
const printer = wrapResultPrinter(unwrappedPrinter);
return (example, index, context) => printer.printTestTitle({
kind: 'success',
icon: `✅`,
title: (0, __1.exampleToNames)(example, index).mockName,
additionalText: '',
}, context);
};
const makePrintDownloadedContract = (unwrappedPrinter) => {
const printer = wrapResultPrinter(unwrappedPrinter);
return (filename, context) => printer.printTestTitle({
kind: 'success',
icon: `📥`,
title: `Downloaded contract ${filename}`,
additionalText: '',
}, context);
};
const makeResultFormatter = (printer) => ({
printError: makePrintError(printer),
printFailureTitle: makePrintFailureTitle(printer),
printSuccessTitle: makePrintSuccessTitle(printer),
printDownloadedContract: makePrintDownloadedContract(printer),
});
exports.makeResultFormatter = makeResultFormatter;
//# sourceMappingURL=resultFormatter.js.map