UNPKG

@contract-case/case-core

Version:

Core functionality for the ContractCase contract testing suite

66 lines 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addLookup = exports.findLookup = void 0; const case_plugin_base_1 = require("@contract-case/case-plugin-base"); const case_plugin_dsl_types_1 = require("@contract-case/case-plugin-dsl-types"); const rawEquals_1 = require("../../../../diffmatch/rawEquals"); const lookupName_1 = require("./lookupName"); const findLookup = (matcherLookup, lookupType, uniqueName, context) => { const key = (0, lookupName_1.lookupName)({ lookupType, uniqueName }); const lookupResult = matcherLookup[(0, lookupName_1.lookupName)({ lookupType, uniqueName })]; context.logger.deepMaintainerDebug(`Lookup for '${key}': `, lookupResult != null ? 'found' : 'not found'); return lookupResult; }; exports.findLookup = findLookup; const unboxAllLookups = (matcherLookup, matcherOrData, context) => { if (matcherOrData == null) { // Coerces undefined return null; } if (typeof matcherOrData === 'string' || typeof matcherOrData === 'number' || typeof matcherOrData === 'boolean') return matcherOrData; if (typeof matcherOrData === 'function' || typeof matcherOrData === 'bigint' || typeof matcherOrData === 'symbol') { throw new case_plugin_base_1.CaseConfigurationError(`It looks like a value of type '${typeof matcherOrData}' was attempted to be serialised in the contract. This is unsupported`, context, 'UNDOCUMENTED'); } if (Array.isArray(matcherOrData)) { return matcherOrData.map((item) => unboxAllLookups(matcherLookup, item, context)); } if ((0, case_plugin_dsl_types_1.isLookupableMatcher)(matcherOrData)) { const replacement = (0, exports.findLookup)(matcherLookup, `matcher`, matcherOrData['_case:matcher:uniqueName'], context); if (replacement === undefined) { throw new case_plugin_base_1.CaseCoreError(`The matcher '${matcherOrData['_case:matcher:uniqueName']}' referenced recursively does not appear to exist. This should have been prevented at the time this matcher was saved.`); } return unboxAllLookups(matcherLookup, replacement, context); } return Object.entries(matcherOrData) .map(([key, value]) => ({ [key]: unboxAllLookups(matcherLookup, value, context), })) .reduce((acc, curr) => ({ ...acc, ...curr }), {}); }; const addLookup = (matcherLookup, lookupType, uniqueName, matcher, context) => { const name = (0, lookupName_1.lookupName)({ lookupType, uniqueName }); context.logger.deepMaintainerDebug(`Saving lookup ${lookupType} (${name}):`, matcher); const candidateMatcher = matcherLookup[name]; if (candidateMatcher) { if (!(0, rawEquals_1.rawEquality)(unboxAllLookups(matcherLookup, matcher, context), unboxAllLookups(matcherLookup, candidateMatcher, context))) { context.logger.error(`The ${lookupType} with the name '${(0, lookupName_1.stripType)({ lookupType, name })}' has more than one definition, and they are not the same`); context.logger.error(`New matcher is (${typeof matcher})`, matcher); context.logger.error(`Existing matcher is (${typeof matcher})`, matcherLookup[name]); throw new case_plugin_base_1.CaseConfigurationError(`The ${lookupType} with the name '${(0, lookupName_1.stripType)({ lookupType, name })}' has more than one definition, and they are not the same`, context, 'UNDOCUMENTED'); } else { context.logger.deepMaintainerDebug(`The ${lookupType} with the name '${(0, lookupName_1.stripType)({ lookupType, name })}' is already stored exactly as given`); } } return { ...matcherLookup, [name]: matcher, }; }; exports.addLookup = addLookup; //# sourceMappingURL=lookupInternals.js.map