arch-unit-ts
Version:
173 lines • 6.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConditionByPredicate = exports.ArchCondition = void 0;
const Assert_1 = require("../../error/domain/Assert");
const ViolatedAndSatisfiedConditionEvents_1 = require("./conditions/ViolatedAndSatisfiedConditionEvents");
const SimpleConditionEvent_1 = require("./SimpleConditionEvent");
class ArchCondition {
description;
constructor(description) {
this.description = description;
}
finish(events) { }
and(condition) {
return new AndCondition(this, condition);
}
or(condition) {
return new OrCondition(this, condition);
}
static from(predicate) {
return ConditionByPredicate.of(predicate);
}
as(description) {
return new AsCondition(this, description);
}
}
exports.ArchCondition = ArchCondition;
class AsCondition extends ArchCondition {
condition;
constructor(condition, description) {
Assert_1.Assert.notNullOrUndefined('condition', condition);
super(description);
this.condition = condition;
}
check(item, events) {
this.condition.check(item, events);
}
}
class JoinCondition extends ArchCondition {
conditions;
constructor(infix, conditions) {
super(JoinCondition.joinDescriptionsOf(infix, conditions));
this.conditions = conditions;
}
evaluateConditions(item) {
return this.conditions.map(condition => ConditionWithEvents.of(condition, item));
}
static joinDescriptionsOf(infix, conditions) {
return conditions.map(condition => condition.description).join(' ' + infix + ' ');
}
}
class JoinConditionEvent {
correspondingObject;
evaluatedConditions;
constructor(correspondingObject, evaluatedConditions) {
this.correspondingObject = correspondingObject;
this.evaluatedConditions = evaluatedConditions;
}
getUniqueLinesOfViolations() {
const result = new Set();
for (const evaluation of this.evaluatedConditions) {
for (const event of evaluation.events.getViolating()) {
event.getDescriptionLines().forEach(line => result.add(line));
}
}
return Array.from(result);
}
invertConditionWithEventsArray(evaluatedConditions) {
return evaluatedConditions.map(conditionWithEvents => this.invertConditionWithEvents(conditionWithEvents));
}
invertConditionWithEvents(evaluation) {
const invertedEvents = new ViolatedAndSatisfiedConditionEvents_1.ViolatedAndSatisfiedConditionEvents();
[...evaluation.events.getViolating(), ...evaluation.events.getAllowed()].forEach(event => invertedEvents.add(event.invert()));
return new ConditionWithEvents(evaluation.condition, invertedEvents);
}
}
class OrCondition extends JoinCondition {
current;
other;
constructor(current, other) {
Assert_1.Assert.notNullOrUndefined('current', current);
Assert_1.Assert.notNullOrUndefined('other', other);
super('or', [current, other]);
this.current = current;
this.other = other;
}
check(item, events) {
events.add(new OrConditionEvent(item, super.evaluateConditions(item)));
}
}
class OrConditionEvent extends JoinConditionEvent {
constructor(item, evaluatedConditions) {
super(item, evaluatedConditions);
}
isViolation() {
return this.evaluatedConditions.every(evaluation => evaluation.events.containViolation());
}
invert() {
return new AndConditionEvent(this.correspondingObject, super.invertConditionWithEventsArray(this.evaluatedConditions));
}
getDescriptionLines() {
return super.getUniqueLinesOfViolations();
}
}
class AndCondition extends JoinCondition {
current;
other;
constructor(current, other) {
Assert_1.Assert.notNullOrUndefined('current', current);
Assert_1.Assert.notNullOrUndefined('other', other);
super('and', [current, other]);
this.current = current;
this.other = other;
}
check(item, events) {
events.add(new AndConditionEvent(item, super.evaluateConditions(item)));
}
}
class AndConditionEvent extends JoinConditionEvent {
constructor(item, evaluatedConditions) {
super(item, evaluatedConditions);
}
isViolation() {
return this.evaluatedConditions.some(evaluation => evaluation.events.containViolation());
}
invert() {
return new OrConditionEvent(this.correspondingObject, super.invertConditionWithEventsArray(this.evaluatedConditions));
}
getDescriptionLines() {
return super.getUniqueLinesOfViolations();
}
}
class ConditionWithEvents {
condition;
events;
constructor(condition, events) {
this.condition = condition;
this.events = events;
}
static of(condition, item) {
return new ConditionWithEvents(condition, this.check(condition, item));
}
static check(condition, item) {
const events = new ViolatedAndSatisfiedConditionEvents_1.ViolatedAndSatisfiedConditionEvents();
condition.check(item, events);
return events;
}
}
class ConditionByPredicate extends ArchCondition {
predicate;
eventDescriber;
static of(predicate) {
return new ConditionByPredicate(predicate, predicate.description, (predicateDescription, satisfied) => (satisfied ? 'has ' : 'does not have ') + predicateDescription);
}
constructor(predicate, description, eventDescriber) {
super(description);
this.predicate = predicate;
this.eventDescriber = eventDescriber;
}
as(description) {
return new ConditionByPredicate(this.predicate, description, this.eventDescriber);
}
check(typeScriptClass, events) {
const satisfied = this.predicate.test(typeScriptClass);
const message = typeScriptClass.name.get() +
' ' +
this.eventDescriber.apply(this, [this.predicate.description, satisfied]) +
' in ' +
typeScriptClass.getPath().get();
events.add(new SimpleConditionEvent_1.SimpleConditionEvent(message, !satisfied));
}
}
exports.ConditionByPredicate = ConditionByPredicate;
//# sourceMappingURL=ArchCondition.js.map