-
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
JUnitParameterizedMethodDeclaration
bug checker
- Loading branch information
1 parent
605d045
commit 9950be0
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...src/main/java/tech/picnic/errorprone/bugpatterns/JUnitParameterizedMethodDeclaration.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,48 @@ | ||
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.ChildMultiMatcher.MatchType.AT_LEAST_ONE; | ||
import static com.google.errorprone.matchers.Matchers.annotations; | ||
import static com.google.errorprone.matchers.Matchers.isType; | ||
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.MethodTreeMatcher; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.sun.source.tree.MethodTree; | ||
|
||
/** | ||
* A {@link BugChecker} that flags test methods using {@link | ||
* org.junit.jupiter.params.ParameterizedTest} without actually having any arguments. | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
summary = "JUnit parameterized test used without arguments", | ||
link = BUG_PATTERNS_BASE_URL + "JUnitParameterizedMethodDeclaration", | ||
linkType = CUSTOM, | ||
severity = SUGGESTION, | ||
tags = SIMPLIFICATION) | ||
public final class JUnitParameterizedMethodDeclaration extends BugChecker | ||
implements MethodTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final Matcher<MethodTree> IS_PARAMETERIZED_TEST = | ||
annotations(AT_LEAST_ONE, isType("org.junit.jupiter.params.ParameterizedTest")); | ||
|
||
/** Instantiates a new {@link JUnitParameterizedMethodDeclaration} instance. */ | ||
public JUnitParameterizedMethodDeclaration() {} | ||
|
||
@Override | ||
public Description matchMethod(MethodTree tree, VisitorState state) { | ||
if (IS_PARAMETERIZED_TEST.matches(tree, state) && tree.getParameters().isEmpty()) { | ||
return describeMatch(tree); | ||
} | ||
|
||
return Description.NO_MATCH; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...test/java/tech/picnic/errorprone/bugpatterns/JUnitParameterizedMethodDeclarationTest.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,30 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class JUnitParameterizedMethodDeclarationTest { | ||
@Test | ||
void identification() { | ||
CompilationTestHelper.newInstance(JUnitParameterizedMethodDeclaration.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import org.junit.jupiter.api.Test;", | ||
"import org.junit.jupiter.params.ParameterizedTest;", | ||
"", | ||
"class A {", | ||
" @Test", | ||
" void test() {}", | ||
"", | ||
" @ParameterizedTest", | ||
" // BUG: Diagnostic contains:", | ||
" void badParameterizedTest() {}", | ||
"", | ||
" @ParameterizedTest", | ||
" void goodParameterizedTest(Object someArgument) {}", | ||
"", | ||
" void nonTest() {}", | ||
"}") | ||
.doTest(); | ||
} | ||
} |