-
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.
Introduce
JUnitSingleArguments
bug checker
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitSingleArguments.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,50 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static com.google.errorprone.BugPattern.LinkType.CUSTOM; | ||
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; | ||
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION; | ||
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; | ||
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import tech.picnic.errorprone.bugpatterns.util.SourceCode; | ||
|
||
/** | ||
* A {@link BugChecker} that flags uses of {@link | ||
* org.junit.jupiter.params.provider.Arguments#arguments(Object...)} with a single (or no) argument; | ||
* in such cases the use of {@link org.junit.jupiter.params.provider.Arguments} is not required as a | ||
* {@link java.util.stream.Stream} of objects can be directly provided to a {@link | ||
* org.junit.jupiter.params.provider.MethodSource}. | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
summary = "JUnit arguments wrapping a single object are redundant", | ||
link = BUG_PATTERNS_BASE_URL + "JUnitSingleArguments", | ||
linkType = CUSTOM, | ||
severity = SUGGESTION, | ||
tags = SIMPLIFICATION) | ||
public final class JUnitSingleArguments extends BugChecker implements MethodInvocationTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final Matcher<ExpressionTree> ARGUMENTS_ARGUMENTS = | ||
staticMethod().onClass("org.junit.jupiter.params.provider.Arguments").named("arguments"); | ||
|
||
/** Instantiates a new {@link JUnitSingleArguments} instance. */ | ||
public JUnitSingleArguments() {} | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
if (ARGUMENTS_ARGUMENTS.matches(tree, state) && tree.getArguments().size() <= 1) { | ||
Check warning on line 44 in error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitSingleArguments.java GitHub Actions / pitestA change can be made to line 44 without causing a test to fail
|
||
return describeMatch(tree, SourceCode.unwrapMethodInvocation(tree, state)); | ||
} | ||
|
||
return Description.NO_MATCH; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ne-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/JUnitSingleArgumentsTest.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,26 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class JUnitSingleArgumentsTest { | ||
@Test | ||
void identification() { | ||
CompilationTestHelper.newInstance(JUnitSingleArguments.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import static org.junit.jupiter.params.provider.Arguments.arguments;", | ||
"import java.util.stream.Stream;", | ||
"", | ||
"class A {", | ||
" void m() {", | ||
" // BUG: Diagnostic contains:", | ||
" arguments();", | ||
" // BUG: Diagnostic contains:", | ||
" arguments(1);", | ||
" arguments(1,2);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |