Skip to content

Commit

Permalink
chore(lombok): Use fluent accessors (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseLion authored Nov 10, 2024
1 parent 966eb58 commit 1a61d26
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 29 deletions.
1 change: 1 addition & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
config.stopBubbling = true

lombok.accessors.fluent = true
lombok.addNullAnnotations = eclipse
lombok.copyableAnnotations += org.eclipse.jdt.annotation.Nullable
lombok.equalsAndHashCode.callSuper = call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ private static String locationOf(final @Nullable TestDescriptor desc, final int
}

private static String messageOf(final Throwable exception, final PrettyJupiterExtension extension) {
final var maxLines = extension.getFailure().getMaxMessageLines().get();
final var maxLines = extension.failure().maxMessageLines().get();
final var limitedMessage = Text.limited(exception.toString(), maxLines);

return Text.colored(Color.BRIGHT_RED, limitedMessage);
}

private static String traceOf(final Throwable exception, final PrettyJupiterExtension extension) {
final var maxLines = extension.getFailure().getMaxTraceLines().get();
final var maxLines = extension.failure().maxTraceLines().get();
final var firstLine = exception.toString().replace("\n", " ").concat("\n");
final var rest = stream(exception.getStackTrace())
.map(StackTraceElement::toString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public PrettyJupiterExtension(final ObjectFactory objects) {
this.failure = objects.newInstance(Failure.class);
}

public Duration getDuration() {
return this.duration;
}

public Failure getFailure() {
return this.failure;
}

public void duration(final Action<Duration> action) {
action.execute(this.duration);
}
Expand Down Expand Up @@ -62,7 +70,19 @@ public Duration(final ObjectFactory objects) {
this.customThreshold.convention(Map.of());
}

public Integer getThreshold(final Test testTask) {
public Property<Boolean> getEnabled() {
return this.enabled;
}

public Property<Integer> getThreshold() {
return this.threshold;
}

public MapProperty<String, Integer> getCustomThreshold() {
return this.customThreshold;
}

public Integer threshold(final Test testTask) {
return Optional.of(testTask)
.flatMap(this::findCustomThreshold)
.orElseGet(this.threshold::get);
Expand Down Expand Up @@ -98,5 +118,13 @@ public Failure(final ObjectFactory objects) {
this.maxMessageLines.convention(15);
this.maxTraceLines.convention(15);
}

public Property<Integer> getMaxMessageLines() {
return this.maxMessageLines;
}

public Property<Integer> getMaxTraceLines() {
return this.maxTraceLines;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void logResults(final TestDescriptor descriptor, final TestResult result)
final var status = this.metaOf(result.getResultType());
final var tabs = Common.tabsFor(descriptor);
final var desc = Text.colored(status.color(), descriptor.getDisplayName());
final var duration = this.getDuration(result);
final var duration = this.duration(result);
final var text = tabs.concat(status.icon().toString())
.concat(" ")
.concat(desc)
Expand Down Expand Up @@ -137,12 +137,12 @@ public void logSummary(final TestDescriptor descriptor, final TestResult result)
}
}

private String getDuration(final TestResult result) {
final var duration = this.extension.getDuration();
private String duration(final TestResult result) {
final var duration = this.extension.duration();

if (duration.getEnabled().get().booleanValue()) {
if (duration.enabled().get().booleanValue()) {
final var timeDiff = result.getEndTime() - result.getStartTime();
final var threshold = duration.getThreshold(this.testTask);
final var threshold = duration.threshold(this.testTask);
final var timeMs = Long.toString(timeDiff).concat("ms");
final var color = timeDiff >= threshold ? Color.RED : Color.YELLOW;
final var millis = timeDiff >= threshold / 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
@Test void returns_the_error_stack_trace() {
final var exception = new Exception("Some error message");
final var failure = Failure.of(exception, Helpers.descriptorOf(2), EXT);
final var maxTrace = EXT.getFailure().getMaxTraceLines().get();
final var maxTrace = EXT.failure().maxTraceLines().get();
final var traceDiff = exception.getStackTrace().length - maxTrace;
final var lines = failure.trace().lines().toList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@
@Test void assigns_default_values() {
final var project = ProjectBuilder.builder().build();
final var extension = project.getExtensions().create("prettyJupiter", PrettyJupiterExtension.class);
final var duration = extension.getDuration();
final var failure = extension.getFailure();
final var duration = extension.duration();
final var failure = extension.failure();

assertThat(duration.getEnabled().get()).isTrue();
assertThat(duration.getThreshold().get()).isEqualTo(200);
assertThat(duration.getCustomThreshold().get()).isEmpty();
assertThat(failure.getMaxMessageLines().get()).isEqualTo(15);
assertThat(failure.getMaxTraceLines().get()).isEqualTo(15);
assertThat(duration.enabled().get()).isTrue();
assertThat(duration.threshold().get()).isEqualTo(200);
assertThat(duration.customThreshold().get()).isEmpty();
assertThat(failure.maxMessageLines().get()).isEqualTo(15);
assertThat(failure.maxTraceLines().get()).isEqualTo(15);
}
}

@Nested class getThreshold {
@Nested class threshold {
@Nested class when_a_custom_threshold_exists {
@TestFactory Stream<DynamicTest> returns_the_threshold_for_the_specific_test_source() {
final var project = ProjectBuilder.builder().build();
final var extension = project.getExtensions().create("prettyJupiter", PrettyJupiterExtension.class);
final var customThreshold = extension.getDuration().getCustomThreshold();
final var customThreshold = extension.duration().customThreshold();

customThreshold.put("test", 100);
customThreshold.put("e2eTest", 200);
Expand All @@ -54,11 +54,11 @@
.map(entry ->
dynamicTest("[task log: %s]".formatted(entry.getKey()), () -> {
final var testTask = mock(org.gradle.api.tasks.testing.Test.class);
final var duration = extension.getDuration();
final var duration = extension.duration();

when(testTask.toString()).thenReturn(entry.getKey());

assertThat(duration.getThreshold(testTask)).isEqualTo(entry.getValue());
assertThat(duration.threshold(testTask)).isEqualTo(entry.getValue());
})
);
}
Expand All @@ -69,13 +69,13 @@
final var project = ProjectBuilder.builder().build();
final var extension = project.getExtensions().create("prettyJupiter", PrettyJupiterExtension.class);
final var testTask = mock(org.gradle.api.tasks.testing.Test.class);
final var duration = extension.getDuration();
final var duration = extension.duration();

when(testTask.toString()).thenReturn("task ':integrationTest'");
duration.getCustomThreshold().put("test", 100);
duration.getThreshold().set(150);
duration.customThreshold().put("test", 100);
duration.threshold().set(150);

assertThat(duration.getThreshold(testTask)).isEqualTo(150);
assertThat(duration.threshold(testTask)).isEqualTo(150);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@

prettyLogger.logResults(descriptor, result);

final var color = entry.getKey().getCode();
final var color = entry.getKey().code();
final var expected = "%s \u001B[90mThis is a test result!\u001B[0m (\u001B[%sm%sms\u001B[0m)".formatted(
Icon.SUCCESS,
color,
Expand All @@ -203,7 +203,7 @@
@Nested class when_the_duration_is_disabled {
@Test void logs_the_result_without_the_duration() {
final var logger = mock(Logger.class);
final var prettyLogger = prettyLoggerOf(logger, ext -> ext.getDuration().getEnabled().set(false));
final var prettyLogger = prettyLoggerOf(logger, ext -> ext.duration().enabled().set(false));
final var descriptor = MockDescriptor.empty().withDisplayName("Some tests without duration");
final var result = mock(TestResult.class);
when(result.getResultType()).thenReturn(ResultType.SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
void returns_the_colored_text_icon(final Icon icon) {
try (var mock = mockStatic(Common.class)) {
mock.when(Common::isTermDumb).thenReturn(false);
final var colored = Text.colored(icon.getColor(), icon.getText());
final var colored = Text.colored(icon.color(), icon.text());

assertThat(icon).hasToString(colored);
}
Expand All @@ -32,7 +32,7 @@ void returns_the_uncolored_icon(final Icon icon) {
try (var mock = mockStatic(Common.class)) {
mock.when(Common::isTermDumb).thenReturn(true);

assertThat(icon).hasToString(icon.getText());
assertThat(icon).hasToString(icon.text());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void returns_a_colored_text(final Color color) {
try (var mock = mockStatic(Common.class)) {
mock.when(Common::isTermDumb).thenReturn(false);
final var text = Text.colored(color, "This is a colored text!");
final var result = "\u001B[%dmThis is a colored text!\u001B[0m".formatted(color.getCode());
final var result = "\u001B[%dmThis is a colored text!\u001B[0m".formatted(color.code());

assertThat(text).isEqualTo(result);
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/testing/MockDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,29 @@ public class MockDescriptor implements TestDescriptor {
public static MockDescriptor empty() {
return new MockDescriptor("", "", null, false, null);
}

@Override
public String getClassName() {
return this.className();
}

@Override
public String getDisplayName() {
return this.displayName();
}

@Override
public String getName() {
return this.name();
}

@Override
public TestDescriptor getParent() {
return this.parent();
}

@Override
public boolean isComposite() {
return this.composite();
}
}

0 comments on commit 1a61d26

Please sign in to comment.