Skip to content

Commit

Permalink
Add .Appended id subparts to enum additions
Browse files Browse the repository at this point in the history
  • Loading branch information
rchache committed May 3, 2024
1 parent a6f1d1c commit bb70a57
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public final class ChangedEnumTrait extends AbstractDiffEvaluator {
private static final String ORDER_CHANGED = ".OrderChanged.";
private static final String NAME_CHANGED = ".NameChanged.";
private static final String REMOVED = ".Removed.";
private static final String APPENDED = ".Appended.";

@Override
public List<ValidationEvent> evaluate(Differences differences) {
Expand Down Expand Up @@ -135,8 +136,14 @@ private List<ValidationEvent> validateEnum(ChangedShape<Shape> change, Pair<Enum
if (newTrait.isSynthetic()) {
appendedSource = change.getNewShape().getSourceLocation();
}
events.add(note(change.getNewShape(), appendedSource, String.format(
"Enum value `%s` was appended", definition.getValue())));
events.add(
ValidationEvent.builder()
.severity(Severity.NOTE)
.message(String.format("Enum value `%s` was appended", definition.getValue()))
.shape(change.getNewShape())
.sourceLocation(appendedSource)
.id(getEventId() + APPENDED + newPosition)
.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,22 @@ public void detectsAppendedEnums() {
.addTrait(EnumTrait.builder()
.addEnum(EnumDefinition.builder().value("foo").build())
.addEnum(EnumDefinition.builder().value("baz").build())
.addEnum(EnumDefinition.builder().value("bar").build())
.sourceLocation(source)
.build())
.build();
Model modelA = Model.assembler().addShape(s1).assemble().unwrap();
Model modelB = Model.assembler().addShape(s2).assemble().unwrap();
List<ValidationEvent> events = ModelDiff.compare(modelA, modelB);

assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").size(), equalTo(1));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").get(0).getSeverity(), equalTo(Severity.NOTE));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").get(0).getSourceLocation(), equalTo(source));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").size(), equalTo(2));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended").size(), equalTo(2));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended.1").size(), equalTo(1));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended.2").size(), equalTo(1));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended").stream()
.allMatch(e -> e.getSeverity() == Severity.NOTE), equalTo(true));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended").stream()
.allMatch(e -> source.equals(e.getSourceLocation())), equalTo(true));
}

@Test
Expand Down Expand Up @@ -86,9 +92,9 @@ public void detectsAppendedEnumsEnumTraitNoNameToEnumShape() {
equalTo(Severity.ERROR));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.NameChanged.0").get(0).getSourceLocation(),
equalTo(source));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").get(1).getSeverity(),
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended.1").get(0).getSeverity(),
equalTo(Severity.NOTE));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").get(1).getSourceLocation(),
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended.1").get(0).getSourceLocation(),
equalTo(source));
}

Expand All @@ -115,10 +121,10 @@ public void detectsAppendedEnumsEnumTraitWithNameToEnumShape() {
Model modelB = Model.assembler().addShape(s2).assemble().unwrap();
List<ValidationEvent> events = ModelDiff.compare(modelA, modelB);

assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").size(), equalTo(1));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended.1").size(), equalTo(1));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").get(0).getSourceLocation(),
equalTo(source));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").stream()
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended").stream()
.allMatch(e -> e.getSeverity() == Severity.NOTE), equalTo(true));
}

Expand Down Expand Up @@ -455,16 +461,15 @@ public void detectsAppendedEnumsAfterRemovedEnums() {

List<ValidationEvent> changeEvents = TestHelper.findEvents(allEvents, "ChangedEnumTrait");
assertThat(changeEvents.size(), equalTo(2));
assertThat(TestHelper.findEvents(changeEvents, "ChangedEnumTrait.Removed.1").size(), equalTo(1));
assertThat(TestHelper.findEvents(changeEvents, "ChangedEnumTrait.Removed.1").get(0).getSourceLocation(),
equalTo(beforeSource));

ValidationEvent removedEvent = changeEvents.get(0);
assertThat(TestHelper.findEvents(changeEvents, "ChangedEnumTrait.Removed.1").size(), equalTo(1));
ValidationEvent removedEvent = TestHelper.findEvents(changeEvents, "ChangedEnumTrait.Removed.1").get(0);
assertThat(removedEvent.getSeverity(), equalTo(Severity.ERROR));
assertThat(removedEvent.getMessage(), stringContainsInOrder("Enum value `old2` was removed"));
assertThat(removedEvent.getSourceLocation(), equalTo(beforeSource));

ValidationEvent appendedEvent = changeEvents.get(1);
assertThat(TestHelper.findEvents(changeEvents, "ChangedEnumTrait.Appended.2").size(), equalTo(1));
ValidationEvent appendedEvent = TestHelper.findEvents(changeEvents, "ChangedEnumTrait.Appended.2").get(0);
assertThat(appendedEvent.getSeverity(), equalTo(Severity.NOTE));
assertThat(appendedEvent.getMessage(), stringContainsInOrder("Enum value `new1` was appended"));
assertThat(appendedEvent.getSourceLocation(), equalTo(afterSource));
Expand Down Expand Up @@ -512,7 +517,7 @@ public void detectsAppendedEnumsAfterRemovedEnumsEnumTraitNoNameToEnumShape() {
equalTo(afterSource));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").subList(0, 3).stream()
.allMatch(e -> e.getSeverity() == Severity.ERROR), equalTo(true));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").subList(3, 4).stream()
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended.2").stream()
.allMatch(e -> e.getSeverity() == Severity.NOTE), equalTo(true));
}

Expand Down Expand Up @@ -553,7 +558,7 @@ public void detectsAppendedEnumsAfterRemovedEnumsEnumTraitWithNameToEnumShape()
equalTo(source));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Removed.1").stream()
.allMatch(e -> e.getSeverity() == Severity.ERROR), equalTo(true));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").subList(1, 2).stream()
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait.Appended.2").stream()
.allMatch(e -> e.getSeverity() == Severity.NOTE), equalTo(true));
}
}

0 comments on commit bb70a57

Please sign in to comment.