From 27ab9bfcbd4cbc6c4da34530655a861e22b96c0c Mon Sep 17 00:00:00 2001 From: Ashley Scopes <73482956+ascopes@users.noreply.github.com> Date: Sun, 26 Mar 2023 11:57:43 +0100 Subject: [PATCH] Add ability to read compiler arguments in JctCompilation result --- .../acceptance-tests-avaje-inject/pom.xml | 2 +- .../acceptance-tests-avaje-jsonb/pom.xml | 2 +- .../acceptance-tests-checkerframework/pom.xml | 2 +- .../acceptance-tests-dagger/pom.xml | 2 +- .../acceptance-tests-dogfood/pom.xml | 2 +- .../acceptance-tests-error-prone/pom.xml | 2 +- .../pom.xml | 2 +- .../pom.xml | 2 +- .../pom.xml | 2 +- .../acceptance-tests-immutables/pom.xml | 2 +- .../acceptance-tests-lombok/pom.xml | 2 +- .../acceptance-tests-mapstruct/pom.xml | 2 +- .../acceptance-tests-micronaut/pom.xml | 2 +- .../pom.xml | 2 +- .../acceptance-tests-serviceloader/pom.xml | 2 +- .../acceptance-tests-spring/pom.xml | 2 +- acceptance-tests/pom.xml | 2 +- java-compiler-testing/pom.xml | 2 +- .../jct/assertions/JctCompilationAssert.java | 25 +++ .../ascopes/jct/compilers/JctCompilation.java | 17 ++ .../impl/JctCompilationFactoryImpl.java | 1 + .../compilers/impl/JctCompilationImpl.java | 24 +++ .../ascopes/jct/tests/helpers/Fixtures.java | 13 +- .../assertions/JctCompilationAssertTest.java | 38 +++++ .../unit/compilers/JctCompilationTest.java | 30 ++++ .../impl/JctCompilationFactoryImplTest.java | 19 +++ .../impl/JctCompilationImplTest.java | 145 ++++++++++++++---- pom.xml | 2 +- 28 files changed, 294 insertions(+), 56 deletions(-) diff --git a/acceptance-tests/acceptance-tests-avaje-inject/pom.xml b/acceptance-tests/acceptance-tests-avaje-inject/pom.xml index 5d8144330..32c3f3327 100644 --- a/acceptance-tests/acceptance-tests-avaje-inject/pom.xml +++ b/acceptance-tests/acceptance-tests-avaje-inject/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-avaje-jsonb/pom.xml b/acceptance-tests/acceptance-tests-avaje-jsonb/pom.xml index b9d78894e..3b6caeeee 100644 --- a/acceptance-tests/acceptance-tests-avaje-jsonb/pom.xml +++ b/acceptance-tests/acceptance-tests-avaje-jsonb/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-checkerframework/pom.xml b/acceptance-tests/acceptance-tests-checkerframework/pom.xml index e87de275b..53c2a6a06 100644 --- a/acceptance-tests/acceptance-tests-checkerframework/pom.xml +++ b/acceptance-tests/acceptance-tests-checkerframework/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-dagger/pom.xml b/acceptance-tests/acceptance-tests-dagger/pom.xml index 9fe43c43f..4c51ff441 100644 --- a/acceptance-tests/acceptance-tests-dagger/pom.xml +++ b/acceptance-tests/acceptance-tests-dagger/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-dogfood/pom.xml b/acceptance-tests/acceptance-tests-dogfood/pom.xml index 4b0c62c10..1db0ca0ff 100644 --- a/acceptance-tests/acceptance-tests-dogfood/pom.xml +++ b/acceptance-tests/acceptance-tests-dogfood/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-error-prone/pom.xml b/acceptance-tests/acceptance-tests-error-prone/pom.xml index bf0ccbbd2..18e72ab12 100644 --- a/acceptance-tests/acceptance-tests-error-prone/pom.xml +++ b/acceptance-tests/acceptance-tests-error-prone/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-google-auto-factory/pom.xml b/acceptance-tests/acceptance-tests-google-auto-factory/pom.xml index 5d69fad76..e7a5165b4 100644 --- a/acceptance-tests/acceptance-tests-google-auto-factory/pom.xml +++ b/acceptance-tests/acceptance-tests-google-auto-factory/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-google-auto-service/pom.xml b/acceptance-tests/acceptance-tests-google-auto-service/pom.xml index c0662490e..4a5ff9088 100644 --- a/acceptance-tests/acceptance-tests-google-auto-service/pom.xml +++ b/acceptance-tests/acceptance-tests-google-auto-service/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-google-auto-value/pom.xml b/acceptance-tests/acceptance-tests-google-auto-value/pom.xml index 2d569c913..5a46d7cb9 100644 --- a/acceptance-tests/acceptance-tests-google-auto-value/pom.xml +++ b/acceptance-tests/acceptance-tests-google-auto-value/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-immutables/pom.xml b/acceptance-tests/acceptance-tests-immutables/pom.xml index d99d7a9eb..05707bf01 100644 --- a/acceptance-tests/acceptance-tests-immutables/pom.xml +++ b/acceptance-tests/acceptance-tests-immutables/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-lombok/pom.xml b/acceptance-tests/acceptance-tests-lombok/pom.xml index ceda860e4..c7263bb2d 100644 --- a/acceptance-tests/acceptance-tests-lombok/pom.xml +++ b/acceptance-tests/acceptance-tests-lombok/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-mapstruct/pom.xml b/acceptance-tests/acceptance-tests-mapstruct/pom.xml index a1e0b17f4..38be33e74 100644 --- a/acceptance-tests/acceptance-tests-mapstruct/pom.xml +++ b/acceptance-tests/acceptance-tests-mapstruct/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-micronaut/pom.xml b/acceptance-tests/acceptance-tests-micronaut/pom.xml index 75ec444a6..cb10a83ee 100644 --- a/acceptance-tests/acceptance-tests-micronaut/pom.xml +++ b/acceptance-tests/acceptance-tests-micronaut/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-serviceloader-jpms/pom.xml b/acceptance-tests/acceptance-tests-serviceloader-jpms/pom.xml index c15ce8e40..772b90ab7 100644 --- a/acceptance-tests/acceptance-tests-serviceloader-jpms/pom.xml +++ b/acceptance-tests/acceptance-tests-serviceloader-jpms/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-serviceloader/pom.xml b/acceptance-tests/acceptance-tests-serviceloader/pom.xml index 0459d3dc7..81098a790 100644 --- a/acceptance-tests/acceptance-tests-serviceloader/pom.xml +++ b/acceptance-tests/acceptance-tests-serviceloader/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/acceptance-tests-spring/pom.xml b/acceptance-tests/acceptance-tests-spring/pom.xml index f8bbe50c5..614d2e7af 100644 --- a/acceptance-tests/acceptance-tests-spring/pom.xml +++ b/acceptance-tests/acceptance-tests-spring/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct acceptance-tests - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/acceptance-tests/pom.xml b/acceptance-tests/pom.xml index b4dc5a86e..b93054cdd 100644 --- a/acceptance-tests/pom.xml +++ b/acceptance-tests/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct java-compiler-testing-parent - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/java-compiler-testing/pom.xml b/java-compiler-testing/pom.xml index 311dc538f..30c41ef96 100644 --- a/java-compiler-testing/pom.xml +++ b/java-compiler-testing/pom.xml @@ -22,7 +22,7 @@ io.github.ascopes.jct java-compiler-testing-parent - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT ../pom.xml diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/JctCompilationAssert.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/JctCompilationAssert.java index decc92792..62b92d66f 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/JctCompilationAssert.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/JctCompilationAssert.java @@ -20,13 +20,21 @@ import io.github.ascopes.jct.compilers.JctCompilation; import io.github.ascopes.jct.repr.TraceDiagnosticListRepresentation; +import io.github.ascopes.jct.utils.StringUtils; import java.util.Collection; +import java.util.List; import javax.tools.Diagnostic.Kind; import javax.tools.JavaFileManager.Location; import javax.tools.StandardLocation; import org.apiguardian.api.API; import org.apiguardian.api.API.Status; import org.assertj.core.api.AbstractAssert; +import org.assertj.core.api.AbstractListAssert; +import org.assertj.core.api.AbstractStringAssert; +import org.assertj.core.api.AssertFactory; +import org.assertj.core.api.Assertions; +import org.assertj.core.api.FactoryBasedNavigableListAssert; +import org.assertj.core.api.StringAssert; import org.jspecify.annotations.Nullable; /** @@ -48,6 +56,23 @@ public JctCompilationAssert(@Nullable JctCompilation value) { super(value, JctCompilationAssert.class); } + /** + * Assert that the arguments passed to the compiler were the expected values. + * + * @return a list assertion object to perform assertions on the arguments with. + * @throws AssertionError if the compilation was null. + */ + public AbstractListAssert, String, ? extends AbstractStringAssert> arguments() { + isNotNull(); + + var arguments = actual.getArguments(); + + // TODO(ascopes): find a way to use Assertions::assertThat here instead of passing the + // StringAssert constructor around. + return FactoryBasedNavigableListAssert.assertThat(arguments, StringAssert::new) + .as("Compiler arguments %s", StringUtils.quotedIterable(arguments)); + } + /** * Assert that the compilation was successful. * diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/JctCompilation.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/JctCompilation.java index 9006d04ab..796fd54ac 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/JctCompilation.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/JctCompilation.java @@ -36,6 +36,23 @@ @API(since = "0.0.1", status = Status.STABLE) public interface JctCompilation { + /** + * Get the command line arguments that were passed to the compiler. + * + *

Note that this throws an unsupported operation exception by default + * to prevent breaking existing functionality. In v1.0.0, this will become + * required behaviour. + * + * @return the command line arguments. + * @since 0.5.0 + */ + @API(since = "0.5.0", status = Status.STABLE) + default List getArguments() { + throw new UnsupportedOperationException( + "This operation is not implemented, but will be mandatory in v1.0.0" + ); + } + /** * Determine if warnings were treated as errors. * diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/impl/JctCompilationFactoryImpl.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/impl/JctCompilationFactoryImpl.java index 39c47fb64..e28d8e705 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/impl/JctCompilationFactoryImpl.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/impl/JctCompilationFactoryImpl.java @@ -152,6 +152,7 @@ private JctCompilation createCheckedCompilation( return JctCompilationImpl .builder() + .arguments(flags) .compilationUnits(Set.copyOf(compilationUnits)) .fileManager(fileManager) .outputLines(writer.getContent().lines().collect(toList())) diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/impl/JctCompilationImpl.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/impl/JctCompilationImpl.java index 31c2d16b4..ffc1bc95a 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/impl/JctCompilationImpl.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/impl/JctCompilationImpl.java @@ -39,6 +39,7 @@ @API(since = "0.0.1", status = Status.INTERNAL) public final class JctCompilationImpl implements JctCompilation { + private final List arguments; private final boolean success; private final boolean failOnWarnings; private final List outputLines; @@ -47,6 +48,10 @@ public final class JctCompilationImpl implements JctCompilation { private final JctFileManager fileManager; private JctCompilationImpl(Builder builder) { + arguments = unmodifiableList( + requireNonNullValues(builder.arguments, "arguments") + ); + success = requireNonNull( builder.success, "success" ); @@ -67,6 +72,11 @@ private JctCompilationImpl(Builder builder) { ); } + @Override + public List getArguments() { + return arguments; + } + @Override public boolean isSuccessful() { return success; @@ -103,6 +113,7 @@ public String toString() { .attribute("success", success) .attribute("failOnWarnings", failOnWarnings) .attribute("fileManager", fileManager) + .attribute("arguments", arguments) .toString(); } @@ -124,6 +135,7 @@ public static Builder builder() { @API(since = "0.0.1", status = Status.INTERNAL) public static final class Builder { + private List arguments; private Boolean failOnWarnings; private Boolean success; private List outputLines; @@ -133,6 +145,7 @@ public static final class Builder { private Builder() { // Only initialized in this file. + arguments = null; failOnWarnings = null; success = null; outputLines = null; @@ -141,6 +154,17 @@ private Builder() { fileManager = null; } + /** + * Set the command-line arguments that were passed to the compiler. + * + * @param arguments the command-line arguments that were passed to the compiler. + * @return this builder. + */ + public Builder arguments(List arguments) { + this.arguments = arguments; + return this; + } + /** * Set whether to treat warnings as errors. * diff --git a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/helpers/Fixtures.java b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/helpers/Fixtures.java index 859a3d83a..37b60a2da 100644 --- a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/helpers/Fixtures.java +++ b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/helpers/Fixtures.java @@ -160,6 +160,15 @@ public static byte[] someBinaryData() { return unboxed; } + /** + * Get a random command line flag. + * + * @return a flag. + */ + public static String someFlag() { + return "--" + UUID.randomUUID(); + } + /** * Get some random command line flags. * @@ -167,9 +176,7 @@ public static byte[] someBinaryData() { */ public static List someFlags() { return Stream - .generate(UUID::randomUUID) - .map(UUID::toString) - .map("--"::concat) + .generate(Fixtures::someFlag) .limit(someInt(2, 4)) .collect(Collectors.toList()); } diff --git a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/assertions/JctCompilationAssertTest.java b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/assertions/JctCompilationAssertTest.java index 001088cf3..1fe8588bc 100644 --- a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/assertions/JctCompilationAssertTest.java +++ b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/assertions/JctCompilationAssertTest.java @@ -15,7 +15,9 @@ */ package io.github.ascopes.jct.tests.unit.assertions; +import static io.github.ascopes.jct.tests.helpers.Fixtures.someFlags; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -40,6 +42,42 @@ @DisplayName("JctCompilationAssert tests") class JctCompilationAssertTest { + @DisplayName("JctCompilationAssert#arguments tests") + @Nested + class ArgumentsTest { + + @DisplayName(".arguments() fails if the compilation is null") + @Test + void argumentsFailsIfCompilationIsNull() { + // Given + var assertions = new JctCompilationAssert(null); + + // Then + assertThatThrownBy(assertions::arguments) + .isInstanceOf(AssertionError.class); + } + + @DisplayName(".arguments() returns assertions on the arguments") + @Test + void argumentsReturnsAssertionsOnArguments() { + // Given + var compilation = mock(JctCompilation.class); + var arguments = someFlags(); + when(compilation.getArguments()) + .thenReturn(arguments); + + // When + var assertions = new JctCompilationAssert(compilation).arguments(); + + // Then + assertThatCode(() -> assertions.containsExactlyElementsOf(arguments)) + .doesNotThrowAnyException(); + + assertThatCode(() -> assertions.containsExactly("foo", "bar", "baz")) + .isInstanceOf(AssertionError.class); + } + } + @DisplayName("JctCompilationAssert#isSuccessful tests") @Nested class IsSuccessfulTest { diff --git a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/JctCompilationTest.java b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/JctCompilationTest.java index e41d51400..462320b99 100644 --- a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/JctCompilationTest.java +++ b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/JctCompilationTest.java @@ -16,6 +16,8 @@ package io.github.ascopes.jct.tests.unit.compilers; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.InstanceOfAssertFactories.STRING; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -25,9 +27,14 @@ import io.github.ascopes.jct.containers.OutputContainerGroup; import io.github.ascopes.jct.containers.PackageContainerGroup; import io.github.ascopes.jct.filemanagers.JctFileManager; +import java.lang.module.ModuleDescriptor.Version; import java.util.stream.Stream; import javax.tools.StandardLocation; +import org.assertj.core.api.ComparableAssert; +import org.assertj.core.api.GenericComparableAssert; +import org.assertj.core.api.InstanceOfAssertFactory; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -48,6 +55,29 @@ class JctCompilationTest { @Mock JctCompilation compilation; + @DisplayName("getArguments throws UnsupportedOperationException by default") + @Test + void getArgumentsThrowsUnsupportedOperationExceptionByDefault() { + // Given + when(compilation.getArguments()).thenCallRealMethod(); + + // Then + var version = JctCompilation.class.getModule().getDescriptor().version(); + + if (version.isPresent()) { + // We check this conditionally since IDEs won't always build the version in unless Maven + // has been called first. + assertThat(version) + .get(new InstanceOfAssertFactory<>(Version.class, GenericComparableAssert::new)) + .isLessThan(Version.parse("1.0.0")); + } + + assertThatThrownBy(compilation::getArguments) + .isInstanceOf(UnsupportedOperationException.class) + .hasMessage("This operation is not implemented, but will be mandatory in v1.0.0"); + } + + @DisplayName("isFailure() returns opposite of isSuccessful()") @ValueSource(booleans = {true, false}) @ParameterizedTest(name = "for isSuccessful() = {0}") diff --git a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/impl/JctCompilationFactoryImplTest.java b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/impl/JctCompilationFactoryImplTest.java index e2dc9673c..5d2fe1ac3 100644 --- a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/impl/JctCompilationFactoryImplTest.java +++ b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/impl/JctCompilationFactoryImplTest.java @@ -445,6 +445,25 @@ void correctlyConfiguredDiagnosticListenerIsUsedForCompilation( } } + @DisplayName("Arguments get placed in the compilation result") + @Test + void argumentsGetPlacedInTheCompilationResult() throws IOException { + // Given + var fileObjects = Set.of( + somePathFileObject("foo.bar.Baz"), + somePathFileObject("do.ray.Me"), + somePathFileObject("a.b.C") + ); + when(fileManager.list(any(), any(), any(), anyBoolean())) + .thenReturn(fileObjects); + + // When + var result = doCompile(null); + + // Then + assertThat(result.getArguments()).isEqualTo(flags); + } + @DisplayName("Diagnostics get placed in the compilation result") @Test @SuppressWarnings("rawtypes") diff --git a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/impl/JctCompilationImplTest.java b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/impl/JctCompilationImplTest.java index b61303ec8..1a1d9cc4b 100644 --- a/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/impl/JctCompilationImplTest.java +++ b/java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/impl/JctCompilationImplTest.java @@ -24,17 +24,16 @@ import io.github.ascopes.jct.diagnostics.TraceDiagnostic; import io.github.ascopes.jct.filemanagers.JctFileManager; import io.github.ascopes.jct.tests.helpers.Fixtures; +import io.github.ascopes.jct.utils.StringUtils; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; -import java.util.Random; import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.tools.JavaFileObject; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -48,10 +47,28 @@ */ @DisplayName("JctCompilationImpl tests") class JctCompilationImplTest { + + @DisplayName(".getArguments() returns the expected value") + @ValueSource(ints = {0, 1, 2, 3, 5, 10, 100}) + @ParameterizedTest(name = "for argumentCount = {0}") + void getArgumentsReturnsExpectedValue(int argumentCount) { + // Given + var arguments = Stream + .generate(Fixtures::someFlag) + .limit(argumentCount) + .collect(Collectors.toList()); - static Random RANDOM = new Random(); + var compilation = filledBuilder() + .arguments(arguments) + .build(); - @DisplayName("isSuccessful returns expected value") + // Then + assertThat(compilation.getArguments()) + .asInstanceOf(iterable(String.class)) + .containsExactlyElementsOf(arguments); + } + + @DisplayName(".isSuccessful() returns the expected value") @ValueSource(booleans = {true, false}) @ParameterizedTest(name = "for success = {0}") void isSuccessfulReturnsExpectedValue(boolean expected) { @@ -64,7 +81,7 @@ void isSuccessfulReturnsExpectedValue(boolean expected) { assertThat(compilation.isSuccessful()).isEqualTo(expected); } - @DisplayName("isFailOnWarnings returns expected value") + @DisplayName(".isFailOnWarnings() returns the expected value") @ValueSource(booleans = {true, false}) @ParameterizedTest(name = "for failOnWarnings = {0}") void isFailOnWarningsReturnsExpectedValue(boolean expected) { @@ -77,7 +94,7 @@ void isFailOnWarningsReturnsExpectedValue(boolean expected) { assertThat(compilation.isFailOnWarnings()).isEqualTo(expected); } - @DisplayName("getOutputLines returns expected value") + @DisplayName(".getOutputLines() returns the expected value") @ValueSource(ints = {0, 1, 2, 3, 5, 10, 100}) @ParameterizedTest(name = "for lineCount = {0}") void getOutputLinesReturnsExpectedValue(int lineCount) { @@ -97,7 +114,7 @@ void getOutputLinesReturnsExpectedValue(int lineCount) { .containsExactlyElementsOf(lines); } - @DisplayName("getCompilationUnits returns expected value") + @DisplayName(".getCompilationUnits() returns the expected value") @ValueSource(ints = {0, 1, 2, 3, 5, 10, 100}) @ParameterizedTest(name = "for compilationUnitCount = {0}") void getCompilationUnitsReturnsExpectedValue(int compilationUnitCount) { @@ -117,7 +134,7 @@ void getCompilationUnitsReturnsExpectedValue(int compilationUnitCount) { .containsExactlyElementsOf(compilationUnits); } - @DisplayName("getDiagnostics returns expected value") + @DisplayName(".getDiagnostics() returns the expected value") @ValueSource(ints = {0, 1, 2, 3, 5, 10, 100}) @ParameterizedTest(name = "for diagnosticCount = {0}") void getDiagnosticsReturnsExpectedValue(int diagnosticCount) { @@ -137,7 +154,7 @@ void getDiagnosticsReturnsExpectedValue(int diagnosticCount) { .containsExactlyElementsOf(diagnostics); } - @DisplayName("getFileManager returns expected value") + @DisplayName(".getFileManager() returns the expected value") @Test void getFileManagerReturnsExpectedValue() { // Given @@ -147,24 +164,35 @@ void getFileManagerReturnsExpectedValue() { .build(); // Then - Assertions.assertThat(compilation.getFileManager()).isEqualTo(fileManager); + assertThat(compilation.getFileManager()).isEqualTo(fileManager); } - @DisplayName("toString returns the expected value") + @DisplayName(".toString() returns the expected value") @Test void toStringReturnsExpectedValue() { // Given - var compilation = filledBuilder().build(); + var success = Fixtures.someBoolean(); + var failOnWarnings = Fixtures.someBoolean(); + var fileManager = mock(JctFileManager.class); + var arguments = Fixtures.someFlags(); + + var compilation = filledBuilder() + .success(success) + .failOnWarnings(failOnWarnings) + .fileManager(fileManager) + .arguments(arguments) + .build(); // Then assertThat(compilation) .asString() .as("compilation.toString()") .isEqualTo( - "JctCompilationImpl{success=%s, failOnWarnings=%s, fileManager=%s}", - compilation.isSuccessful(), - compilation.isFailOnWarnings(), - compilation.getFileManager() + "JctCompilationImpl{success=%s, failOnWarnings=%s, fileManager=%s, arguments=%s}", + success, + failOnWarnings, + fileManager, + StringUtils.quotedIterable(arguments) ); } @@ -172,17 +200,57 @@ void toStringReturnsExpectedValue() { @Nested class BuilderTest { + @DisplayName("Building without arguments raises a NullPointerException") + @Test + void buildingWithoutArgumentsRaisesNullPointerException() { + // Given + var builder = JctCompilationImpl + .builder() + .diagnostics(List.of()) + .fileManager(mock(JctFileManager.class)) + .outputLines(List.of()) + .compilationUnits(Set.of()) + .success(Fixtures.someBoolean()) + .failOnWarnings(Fixtures.someBoolean()); + + // Then + assertThatThrownBy(builder::build) + .isInstanceOf(NullPointerException.class) + .hasMessage("arguments"); + } + + @DisplayName("Building with null arguments raises a NullPointerException") + @Test + void buildingWithNullArgumentsRaisesNullPointerException() { + // Given + var builder = JctCompilationImpl + .builder() + .arguments(nullableListOf(Fixtures.someFlag(), null, Fixtures.someFlag())) + .fileManager(mock(JctFileManager.class)) + .outputLines(List.of()) + .compilationUnits(Set.of()) + .success(Fixtures.someBoolean()) + .failOnWarnings(Fixtures.someBoolean()) + .diagnostics(List.of()); + + // Then + assertThatThrownBy(builder::build) + .isInstanceOf(NullPointerException.class) + .hasMessage("arguments[1]"); + } + @DisplayName("Building without success set raises a NullPointerException") @Test void buildingWithoutSuccessSetRaisesNullPointerException() { // Given var builder = JctCompilationImpl .builder() + .arguments(List.of()) .fileManager(mock(JctFileManager.class)) .outputLines(List.of()) .diagnostics(List.of()) .compilationUnits(Set.of()) - .failOnWarnings(RANDOM.nextBoolean()); + .failOnWarnings(Fixtures.someBoolean()); // Then assertThatThrownBy(builder::build) @@ -196,11 +264,12 @@ void buildingWithoutFailOnWarningsSetRaisesNullPointerException() { // Given var builder = JctCompilationImpl .builder() + .arguments(List.of()) .fileManager(mock(JctFileManager.class)) .outputLines(List.of()) .diagnostics(List.of()) .compilationUnits(Set.of()) - .success(RANDOM.nextBoolean()); + .success(Fixtures.someBoolean()); // Then assertThatThrownBy(builder::build) @@ -226,11 +295,12 @@ void buildingWithoutCompilationUnitsRaisesNullPointerException() { // Given var builder = JctCompilationImpl .builder() + .arguments(List.of()) .fileManager(mock(JctFileManager.class)) .outputLines(List.of()) .diagnostics(List.of()) - .success(RANDOM.nextBoolean()) - .failOnWarnings(RANDOM.nextBoolean()); + .success(Fixtures.someBoolean()) + .failOnWarnings(Fixtures.someBoolean()); // Then assertThatThrownBy(builder::build) @@ -244,11 +314,12 @@ void buildingWithNullCompilationUnitsRaisesNullPointerException() { // Given var builder = JctCompilationImpl .builder() + .arguments(List.of()) .fileManager(mock(JctFileManager.class)) .outputLines(List.of()) .diagnostics(List.of()) - .success(RANDOM.nextBoolean()) - .failOnWarnings(RANDOM.nextBoolean()) + .success(Fixtures.someBoolean()) + .failOnWarnings(Fixtures.someBoolean()) .compilationUnits(nullableSetOf( mock(JavaFileObject.class), mock(JavaFileObject.class), @@ -281,11 +352,12 @@ void buildingWithoutDiagnosticsRaisesNullPointerException() { // Given var builder = JctCompilationImpl .builder() + .arguments(List.of()) .fileManager(mock(JctFileManager.class)) .outputLines(List.of()) .compilationUnits(Set.of()) - .success(RANDOM.nextBoolean()) - .failOnWarnings(RANDOM.nextBoolean()); + .success(Fixtures.someBoolean()) + .failOnWarnings(Fixtures.someBoolean()); // Then assertThatThrownBy(builder::build) @@ -299,11 +371,12 @@ void buildingWithNullDiagnosticsRaisesNullPointerException() { // Given var builder = JctCompilationImpl .builder() + .arguments(List.of()) .fileManager(mock(JctFileManager.class)) .outputLines(List.of()) .compilationUnits(Set.of()) - .success(RANDOM.nextBoolean()) - .failOnWarnings(RANDOM.nextBoolean()) + .success(Fixtures.someBoolean()) + .failOnWarnings(Fixtures.someBoolean()) .diagnostics(nullableListOf( mock(), null, @@ -334,11 +407,12 @@ void buildingWithoutFileManagerRaisesNullPointerException() { // Given var builder = JctCompilationImpl .builder() + .arguments(List.of()) .diagnostics(List.of()) .compilationUnits(Set.of()) .outputLines(List.of()) - .success(RANDOM.nextBoolean()) - .failOnWarnings(RANDOM.nextBoolean()); + .success(Fixtures.someBoolean()) + .failOnWarnings(Fixtures.someBoolean()); // Then assertThatThrownBy(builder::build) @@ -364,11 +438,12 @@ void buildingWithoutOutputLinesRaisesNullPointerException() { // Given var builder = JctCompilationImpl .builder() + .arguments(List.of()) .fileManager(mock(JctFileManager.class)) .diagnostics(List.of()) .compilationUnits(Set.of()) - .success(RANDOM.nextBoolean()) - .failOnWarnings(RANDOM.nextBoolean()); + .success(Fixtures.someBoolean()) + .failOnWarnings(Fixtures.someBoolean()); // Then assertThatThrownBy(builder::build) @@ -382,11 +457,12 @@ void buildingWithNullOutputLinesRaisesNullPointerException() { // Given var builder = JctCompilationImpl .builder() + .arguments(List.of()) .fileManager(mock(JctFileManager.class)) .outputLines(List.of()) .diagnostics(List.of()) - .success(RANDOM.nextBoolean()) - .failOnWarnings(RANDOM.nextBoolean()) + .success(Fixtures.someBoolean()) + .failOnWarnings(Fixtures.someBoolean()) .outputLines(nullableListOf("foo", "bar", "baz", "bork", null, "qux")); // Then @@ -409,11 +485,12 @@ static List nullableListOf(T... values) { static JctCompilationImpl.Builder filledBuilder() { return JctCompilationImpl .builder() + .arguments(List.of()) .compilationUnits(Set.of()) .diagnostics(List.of()) - .failOnWarnings(RANDOM.nextBoolean()) + .failOnWarnings(Fixtures.someBoolean()) .fileManager(mock(JctFileManager.class)) .outputLines(List.of()) - .success(RANDOM.nextBoolean()); + .success(Fixtures.someBoolean()); } } diff --git a/pom.xml b/pom.xml index a9031e851..2dc746df8 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ io.github.ascopes.jct java-compiler-testing-parent - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT pom Java Compiler Testing parent project