From 6622de7f84970b865952a4f2003671e2c8a5df33 Mon Sep 17 00:00:00 2001 From: Ashley Scopes <73482956+ascopes@users.noreply.github.com> Date: Sat, 21 Jan 2023 19:12:05 +0000 Subject: [PATCH] Add ability to customise the names of compiler objects --- .../jct/compilers/AbstractJctCompiler.java | 24 ++++----- .../ascopes/jct/compilers/JctCompiler.java | 15 ++++++ .../compilers/AbstractJctCompilerTest.java | 49 ++++++++++++++++--- 3 files changed, 70 insertions(+), 18 deletions(-) diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/AbstractJctCompiler.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/AbstractJctCompiler.java index 6d4e0c7e2..08611cbeb 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/AbstractJctCompiler.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/AbstractJctCompiler.java @@ -58,12 +58,12 @@ public abstract class AbstractJctCompiler> implements JctCompiler { - private final String name; private final JavaCompiler jsr199Compiler; private final JctFlagBuilder flagBuilder; private final List annotationProcessors; private final List annotationProcessorOptions; private final List compilerOptions; + private String name; private boolean showWarnings; private boolean showDeprecationWarnings; private boolean failOnWarnings; @@ -87,7 +87,7 @@ public abstract class AbstractJctCompiler> /** * Initialize this compiler. * - * @param name the friendly name of the compiler. + * @param name the friendly name of the compiler to default to. * @param jsr199Compiler the JSR-199 compiler implementation to use. * @param flagBuilder the flag builder to use. */ @@ -141,6 +141,17 @@ public final A configure(JctCompilerConfigurer configur return myself(); } + @Override + public String getName() { + return name; + } + + @Override + public A name(String name) { + this.name = requireNonNull(name, "name"); + return myself(); + } + /** * Get the flag builder to use. * @@ -159,15 +170,6 @@ public JavaCompiler getJsr199Compiler() { return jsr199Compiler; } - /** - * Get the friendly name of the compiler implementation. - * - * @return the friendly name. - */ - public String getName() { - return name; - } - @Override public boolean isVerbose() { return verbose; diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/JctCompiler.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/JctCompiler.java index aa57f477a..2862fdba6 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/JctCompiler.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/JctCompiler.java @@ -202,6 +202,21 @@ default R compile(Workspace workspace, String firstClassName, String... addition */ C configure(JctCompilerConfigurer configurer) throws E; + /** + * Get the friendly printable name of this compiler object. + * + * @return the name of the compiler. + */ + String getName(); + + /** + * Set the friendly name of this compiler. + * + * @param name the name to set. + * @return this compiler object for further call chaining. + */ + C name(String name); + /** * Get an immutable snapshot view of the current annotation processor options * that are set. diff --git a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/AbstractJctCompilerTest.java b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/AbstractJctCompilerTest.java index 067789301..17f54bd4b 100644 --- a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/AbstractJctCompilerTest.java +++ b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/AbstractJctCompilerTest.java @@ -452,6 +452,48 @@ void configureReturnsTheCompiler() throws Throwable { } } + @DisplayName(".getName() returns the name") + @Test + void getNameReturnsName() { + // Then + assertThat(compiler.getName()).isEqualTo(name); + } + + @DisplayName("AbstractJctCompiler#name tests") + @Nested + class NameTest { + @DisplayName(".name(...) sets the name") + @ValueSource(strings = {"foo", "bar baz", "bork"}) + @ParameterizedTest(name = "for name = {0}") + void nameSetsTheName(String name) { + // When + compiler.name(name); + + // Then + assertThatCompilerField("name").isEqualTo(name); + } + + @DisplayName(".name(...) returns the compiler") + @Test + void nameReturnsTheCompiler() { + // When + var result = compiler.name("foo"); + + // Then + assertThat(result).isSameAs(compiler); + } + + @DisplayName(".name(null) throws a NullPointerException") + @SuppressWarnings("DataFlowIssue") + @Test + void passingNullToNameThrowsNullPointerException() { + // Then + assertThatThrownBy(() -> compiler.name(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("name"); + } + } + @DisplayName(".getFlagBuilder() returns the flag builder") @Test void getFlagBuilderReturnsFlagBuilder() { @@ -466,13 +508,6 @@ void getJsr199CompilerReturnsTheCompiler() { assertThat(compiler.getJsr199Compiler()).isSameAs(jsr199Compiler); } - @DisplayName(".getName() returns the name") - @Test - void getNameReturnsName() { - // Then - assertThat(compiler.getName()).isSameAs(name); - } - @DisplayName(".isVerbose() returns the expected values") @ValueSource(booleans = {true, false}) @ParameterizedTest(name = "for verbose = {0}")