diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index f7a2f1b8b2..ef8aeec521 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -130,7 +130,7 @@ org.junit.jupiter junit-jupiter-params - test + provided org.mockito diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/JUnitTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/JUnitTemplates.java new file mode 100644 index 0000000000..393b5843c7 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/JUnitTemplates.java @@ -0,0 +1,29 @@ +package tech.picnic.errorprone.refastertemplates; + +import static org.junit.jupiter.params.provider.Arguments.arguments; + +import com.google.errorprone.refaster.ImportPolicy; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.Repeated; +import com.google.errorprone.refaster.annotation.UseImportPolicy; +import org.junit.jupiter.params.provider.Arguments; + +/** Refaster templates related to JUnit expressions and statements. */ +final class JUnitTemplates { + private JUnitTemplates() {} + + /** Prefer statically imported {@link Arguments#arguments} over {@link Arguments#of} calls. */ + static final class ArgumentsEnumeration { + @BeforeTemplate + Arguments before(@Repeated T objects) { + return Arguments.of(objects); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + Arguments after(@Repeated T objects) { + return arguments(objects); + } + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java index a4b0a1a968..c14f3d1905 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java @@ -57,6 +57,7 @@ public final class RefasterCheckTest { "ImmutableSortedMultiset", "ImmutableSortedSet", "IntStream", + "JUnit", "LongStream", "MapEntry", "Mockito", diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/JUnitTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/JUnitTemplatesTestInput.java new file mode 100644 index 0000000000..4deb1df3ae --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/JUnitTemplatesTestInput.java @@ -0,0 +1,11 @@ +package tech.picnic.errorprone.bugpatterns; + +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.params.provider.Arguments; + +final class JUnitTemplatesTest implements RefasterTemplateTestCase { + ImmutableSet testArgumentsEnumeration() { + return ImmutableSet.of( + Arguments.of("foo"), Arguments.of(1, "foo", 2, "bar"), Arguments.of(new Object())); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/JUnitTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/JUnitTemplatesTestOutput.java new file mode 100644 index 0000000000..a392979d3a --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/JUnitTemplatesTestOutput.java @@ -0,0 +1,13 @@ +package tech.picnic.errorprone.bugpatterns; + +import static org.junit.jupiter.params.provider.Arguments.arguments; + +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.params.provider.Arguments; + +final class JUnitTemplatesTest implements RefasterTemplateTestCase { + ImmutableSet testArgumentsEnumeration() { + return ImmutableSet.of( + arguments("foo"), arguments(1, "foo", 2, "bar"), arguments(new Object())); + } +}