-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PSF-9432 Introduce JUnit Arguments Refaster Rule
- Loading branch information
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ntrib/src/main/java/tech/picnic/errorprone/refastertemplates/JUnitArgumentsTemplates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 which replaces argument creation for parametrized JUnit tests using {@link | ||
* Arguments#of} with statically imported {@link Arguments#arguments} calls. | ||
*/ | ||
final class JUnitArgumentsTemplates { | ||
private JUnitArgumentsTemplates() {} | ||
|
||
/** Prefer statically imported {@link Arguments#arguments} over {@link Arguments#of} calls. */ | ||
static final class ArgumentsReplace<T> { | ||
@BeforeTemplate | ||
Arguments before(@Repeated T objs) { | ||
return Arguments.of(objs); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) | ||
Arguments after(@Repeated T objs) { | ||
return arguments(objs); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...c/test/resources/tech/picnic/errorprone/bugpatterns/JUnitArgumentsTemplatesTestInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
|
||
final class JUnitTemplatesTest implements RefasterTemplateTestCase { | ||
Arguments testSingle() { | ||
return Arguments.of("foo"); | ||
} | ||
|
||
Arguments testMultiple() { | ||
return Arguments.of(1, "foo", 2, "bar"); | ||
} | ||
|
||
Stream<Arguments> testSingleInStream() { | ||
return Stream.of(Arguments.of("foo")); | ||
} | ||
|
||
Stream<Arguments> testMultipleInStream() { | ||
return Stream.of(Arguments.of(1, "foo", 2, "bar")); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.../test/resources/tech/picnic/errorprone/bugpatterns/JUnitArgumentsTemplatesTestOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
|
||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
|
||
final class JUnitTemplatesTest implements RefasterTemplateTestCase { | ||
Arguments testSingle() { | ||
return arguments("foo"); | ||
} | ||
|
||
Arguments testMultiple() { | ||
return arguments(1, "foo", 2, "bar"); | ||
} | ||
|
||
Stream<Arguments> testSingleInStream() { | ||
return Stream.of(arguments("foo")); | ||
} | ||
|
||
Stream<Arguments> testMultipleInStream() { | ||
return Stream.of(arguments(1, "foo", 2, "bar")); | ||
} | ||
} |