arch-unit-ts
Version:
81 lines • 4.24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MethodsShouldInternal = void 0;
const Optional_1 = require("../../../common/domain/Optional");
const AllowEmptyShould_1 = require("../AllowEmptyShould");
const MethodConditions_1 = require("../conditions/MethodConditions");
const EvaluationResult_1 = require("../EvaluationResult");
const SimpleConditionEvents_1 = require("../SimpleConditionEvents");
const ClassesThatInternal_1 = require("./ClassesThatInternal");
class MethodsShouldInternal {
methodsTransformer;
conditionAggregator;
prepareCondition;
overriddenDescription = Optional_1.Optional.empty();
allowEmptyShouldValue;
constructor(methodsTransformer, conditionAggregator, prepareCondition, allowEmptyShould) {
this.methodsTransformer = methodsTransformer;
this.conditionAggregator = conditionAggregator;
this.prepareCondition = prepareCondition;
this.allowEmptyShouldValue = allowEmptyShould;
}
because(reason) {
this.overriddenDescription = Optional_1.Optional.ofUndefinable(reason);
return this;
}
check(classes) {
const evaluationResult = this.evaluate(classes);
if (evaluationResult.hasErrors()) {
throw new Error(`Architecture violation : Rule ${this.methodsTransformer.getFullDescription()} should ${this.conditionAggregator.getDescription()}${this.reason()}.\nErrors : ${evaluationResult.violationReport()}`);
}
}
reason() {
if (this.getDescription().length === 0) {
return '';
}
return ` because ${this.getDescription()}`;
}
getDescription() {
return this.overriddenDescription.orElse('');
}
evaluate(classes) {
const methods = this.methodsTransformer.transform(classes.get());
this.verifyNoEmptyShouldIfEnabled(methods);
const conditionEvents = new SimpleConditionEvents_1.SimpleConditionEvents();
methods.forEach(method => this.conditionAggregator.getCondition().ifPresent(condition => this.prepareCondition(condition).check(method, conditionEvents)));
this.conditionAggregator.getCondition().ifPresent(condition => condition.finish(conditionEvents));
return new EvaluationResult_1.EvaluationResult(conditionEvents);
}
beDecoratedWith(decorator) {
return this.addCondition(MethodConditions_1.MethodConditions.beDecoratedWith(decorator));
}
beAsync() {
return this.addCondition(MethodConditions_1.MethodConditions.beAsync());
}
beDeclaredInClassesThat() {
return new ClassesThatInternal_1.ClassesThatInternal((classPredicate) => this.addCondition(MethodConditions_1.MethodConditions.beDeclaredInClassThat(classPredicate)));
}
orShould() {
return new MethodsShouldInternal(this.methodsTransformer, this.conditionAggregator.thatORs(), this.prepareCondition, this.allowEmptyShouldValue);
}
andShould() {
return new MethodsShouldInternal(this.methodsTransformer, this.conditionAggregator.thatANDs(), this.prepareCondition, this.allowEmptyShouldValue);
}
allowEmptyShould(allow) {
return new MethodsShouldInternal(this.methodsTransformer, this.conditionAggregator, this.prepareCondition, AllowEmptyShould_1.AllowEmptyShould.fromBoolean(allow));
}
addCondition(condition) {
return new MethodsShouldInternal(this.methodsTransformer, this.conditionAggregator.add(condition), this.prepareCondition, this.allowEmptyShouldValue);
}
verifyNoEmptyShouldIfEnabled(methods) {
if (methods.length === 0 && !this.allowEmptyShouldValue.isAllowed()) {
throw new Error(`Rule '${this.getDescription()}' failed to check any methods. ` +
'This means either that no methods have been passed to the rule at all, ' +
'or that no methods passed to the rule matched the `that()` clause. ' +
'To allow rules being evaluated without checking any methods you can ' +
"use '.allowEmptyShould(true)' on a single rule or use the default configuration.");
}
}
}
exports.MethodsShouldInternal = MethodsShouldInternal;
//# sourceMappingURL=MethodsShouldInternal.js.map