arch-unit-ts
Version:
98 lines • 5.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClassesShouldInternal = void 0;
const Optional_1 = require("../../../common/domain/Optional");
const AllowEmptyShould_1 = require("../AllowEmptyShould");
const ArchConditions_1 = require("../conditions/ArchConditions");
const EvaluationResult_1 = require("../EvaluationResult");
const SimpleConditionEvents_1 = require("../SimpleConditionEvents");
const ClassesThatInternal_1 = require("./ClassesThatInternal");
class ClassesShouldInternal {
classesTransformer;
conditionAggregator;
prepareCondition;
overriddenDescription = Optional_1.Optional.empty();
allowEmptyShouldValue;
constructor(classesTransformer, conditionAggregator, prepareCondition, allowEmptyShould) {
this.classesTransformer = classesTransformer;
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.classesTransformer.getFullDescription()} should ${this.conditionAggregator.getDescription()}${this.reason()}.\nErrors : ${evaluationResult.violationReport()}`);
}
}
reason() {
if (this.getDescription().length === 0) {
return '';
}
return ` because ${this.getDescription()}`;
}
onlyDependOnClassesThat() {
return new ClassesThatInternal_1.ClassesThatInternal(describedPredicate => {
return new ClassesShouldInternal(this.classesTransformer, this.conditionAggregator.add(ArchConditions_1.ArchConditions.onlyDependOnClassesThat(describedPredicate)), this.prepareCondition, this.allowEmptyShouldValue);
});
}
dependOnClassesThat() {
return new ClassesThatInternal_1.ClassesThatInternal(describedPredicate => {
return new ClassesShouldInternal(this.classesTransformer, this.conditionAggregator.add(ArchConditions_1.ArchConditions.dependOnClassesThat(describedPredicate)), this.prepareCondition, this.allowEmptyShouldValue);
});
}
onlyHaveDependentClassesThat() {
return new ClassesThatInternal_1.ClassesThatInternal(describedPredicate => {
return new ClassesShouldInternal(this.classesTransformer, this.conditionAggregator.add(ArchConditions_1.ArchConditions.onlyHaveDependentClassesThat(describedPredicate)), this.prepareCondition, this.allowEmptyShouldValue);
});
}
getDescription() {
return this.overriddenDescription.orElse('');
}
evaluate(classes) {
const classesFiltered = this.classesTransformer.transform(classes.get());
this.verifyNoEmptyShouldIfEnabled(classesFiltered);
const conditionEvents = new SimpleConditionEvents_1.SimpleConditionEvents();
classesFiltered.forEach(typeScriptClass => this.conditionAggregator
.getCondition()
.ifPresent(condition => this.prepareCondition(condition).check(typeScriptClass, conditionEvents)));
this.conditionAggregator.getCondition().ifPresent(condition => condition.finish(conditionEvents));
return new EvaluationResult_1.EvaluationResult(conditionEvents);
}
andShould() {
return new ClassesShouldInternal(this.classesTransformer, this.conditionAggregator.thatANDs(), this.prepareCondition, this.allowEmptyShouldValue);
}
orShould() {
return new ClassesShouldInternal(this.classesTransformer, this.conditionAggregator.thatORs(), this.prepareCondition, this.allowEmptyShouldValue);
}
allowEmptyShould(allowEmptyShould) {
return new ClassesShouldInternal(this.classesTransformer, this.conditionAggregator.thatORs(), this.prepareCondition, AllowEmptyShould_1.AllowEmptyShould.fromBoolean(allowEmptyShould));
}
haveSimpleNameStartingWith(prefix) {
return this.addCondition(ArchConditions_1.ArchConditions.haveSimpleNameStartingWith(prefix));
}
haveSimpleNameEndingWith(suffix) {
return this.addCondition(ArchConditions_1.ArchConditions.haveSimpleNameEndingWith(suffix));
}
addCondition(condition) {
return this.copyWithNewCondition(this.conditionAggregator.add(condition));
}
copyWithNewCondition(newCondition) {
return new ClassesShouldInternal(this.classesTransformer, this.conditionAggregator.add(newCondition.getCondition().orElseThrow()), this.prepareCondition, this.allowEmptyShouldValue);
}
verifyNoEmptyShouldIfEnabled(classesFiltered) {
if (classesFiltered.length === 0 && !this.allowEmptyShouldValue.isAllowed()) {
throw new Error(`Rule '${this.getDescription()}' failed to check any classes. ` +
'This means either that no classes have been passed to the rule at all, ' +
'or that no classes passed to the rule matched the `that()` clause. ' +
'To allow rules being evaluated without checking any classes you can ' +
"use '.allowEmptyShould(true)' on a single rule or use the default configuration.");
}
}
}
exports.ClassesShouldInternal = ClassesShouldInternal;
//# sourceMappingURL=ClassesShouldInternal.js.map