From 1a61d26c7f020c30f72ac8814bae45dce20bbfd8 Mon Sep 17 00:00:00 2001 From: Jose Luis Leon Date: Sun, 10 Nov 2024 18:18:45 -0500 Subject: [PATCH] chore(lombok): Use fluent accessors (#172) --- lombok.config | 1 + .../joselion/prettyjupiter/lib/Failure.java | 4 +-- .../lib/PrettyJupiterExtension.java | 30 ++++++++++++++++++- .../prettyjupiter/lib/PrettyLogger.java | 10 +++---- .../prettyjupiter/lib/FailureTest.java | 2 +- .../lib/PrettyJupiterExtensionTest.java | 30 +++++++++---------- .../prettyjupiter/lib/PrettyLoggerTest.java | 4 +-- .../prettyjupiter/lib/helpers/IconTest.java | 4 +-- .../prettyjupiter/lib/helpers/TextTest.java | 2 +- src/test/java/testing/MockDescriptor.java | 25 ++++++++++++++++ 10 files changed, 83 insertions(+), 29 deletions(-) diff --git a/lombok.config b/lombok.config index fc2e85a..a3bb140 100644 --- a/lombok.config +++ b/lombok.config @@ -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 diff --git a/src/main/java/io/github/joselion/prettyjupiter/lib/Failure.java b/src/main/java/io/github/joselion/prettyjupiter/lib/Failure.java index a8bda7f..90f2724 100644 --- a/src/main/java/io/github/joselion/prettyjupiter/lib/Failure.java +++ b/src/main/java/io/github/joselion/prettyjupiter/lib/Failure.java @@ -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) diff --git a/src/main/java/io/github/joselion/prettyjupiter/lib/PrettyJupiterExtension.java b/src/main/java/io/github/joselion/prettyjupiter/lib/PrettyJupiterExtension.java index 79a4e4b..25c0957 100644 --- a/src/main/java/io/github/joselion/prettyjupiter/lib/PrettyJupiterExtension.java +++ b/src/main/java/io/github/joselion/prettyjupiter/lib/PrettyJupiterExtension.java @@ -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 action) { action.execute(this.duration); } @@ -62,7 +70,19 @@ public Duration(final ObjectFactory objects) { this.customThreshold.convention(Map.of()); } - public Integer getThreshold(final Test testTask) { + public Property getEnabled() { + return this.enabled; + } + + public Property getThreshold() { + return this.threshold; + } + + public MapProperty getCustomThreshold() { + return this.customThreshold; + } + + public Integer threshold(final Test testTask) { return Optional.of(testTask) .flatMap(this::findCustomThreshold) .orElseGet(this.threshold::get); @@ -98,5 +118,13 @@ public Failure(final ObjectFactory objects) { this.maxMessageLines.convention(15); this.maxTraceLines.convention(15); } + + public Property getMaxMessageLines() { + return this.maxMessageLines; + } + + public Property getMaxTraceLines() { + return this.maxTraceLines; + } } } diff --git a/src/main/java/io/github/joselion/prettyjupiter/lib/PrettyLogger.java b/src/main/java/io/github/joselion/prettyjupiter/lib/PrettyLogger.java index 69e3b11..a696461 100644 --- a/src/main/java/io/github/joselion/prettyjupiter/lib/PrettyLogger.java +++ b/src/main/java/io/github/joselion/prettyjupiter/lib/PrettyLogger.java @@ -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) @@ -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 diff --git a/src/test/java/io/github/joselion/prettyjupiter/lib/FailureTest.java b/src/test/java/io/github/joselion/prettyjupiter/lib/FailureTest.java index 350916e..47dd063 100644 --- a/src/test/java/io/github/joselion/prettyjupiter/lib/FailureTest.java +++ b/src/test/java/io/github/joselion/prettyjupiter/lib/FailureTest.java @@ -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(); diff --git a/src/test/java/io/github/joselion/prettyjupiter/lib/PrettyJupiterExtensionTest.java b/src/test/java/io/github/joselion/prettyjupiter/lib/PrettyJupiterExtensionTest.java index d63c215..5cf42cd 100644 --- a/src/test/java/io/github/joselion/prettyjupiter/lib/PrettyJupiterExtensionTest.java +++ b/src/test/java/io/github/joselion/prettyjupiter/lib/PrettyJupiterExtensionTest.java @@ -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 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); @@ -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()); }) ); } @@ -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); } } } diff --git a/src/test/java/io/github/joselion/prettyjupiter/lib/PrettyLoggerTest.java b/src/test/java/io/github/joselion/prettyjupiter/lib/PrettyLoggerTest.java index bb320e9..b8ae3d9 100644 --- a/src/test/java/io/github/joselion/prettyjupiter/lib/PrettyLoggerTest.java +++ b/src/test/java/io/github/joselion/prettyjupiter/lib/PrettyLoggerTest.java @@ -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, @@ -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); diff --git a/src/test/java/io/github/joselion/prettyjupiter/lib/helpers/IconTest.java b/src/test/java/io/github/joselion/prettyjupiter/lib/helpers/IconTest.java index 83fc9fa..eb16e44 100644 --- a/src/test/java/io/github/joselion/prettyjupiter/lib/helpers/IconTest.java +++ b/src/test/java/io/github/joselion/prettyjupiter/lib/helpers/IconTest.java @@ -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); } @@ -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()); } } } diff --git a/src/test/java/io/github/joselion/prettyjupiter/lib/helpers/TextTest.java b/src/test/java/io/github/joselion/prettyjupiter/lib/helpers/TextTest.java index 1d50c1d..2506cbf 100644 --- a/src/test/java/io/github/joselion/prettyjupiter/lib/helpers/TextTest.java +++ b/src/test/java/io/github/joselion/prettyjupiter/lib/helpers/TextTest.java @@ -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); } diff --git a/src/test/java/testing/MockDescriptor.java b/src/test/java/testing/MockDescriptor.java index b1969ce..b4d28cc 100644 --- a/src/test/java/testing/MockDescriptor.java +++ b/src/test/java/testing/MockDescriptor.java @@ -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(); + } }