-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce JUnitNullaryParameterizedTestDeclaration
check
#817
Merged
+251
−7
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
91 changes: 91 additions & 0 deletions
91
...ain/java/tech/picnic/errorprone/bugpatterns/JUnitNullaryParameterizedTestDeclaration.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,91 @@ | ||
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.anyOf; | ||
import static com.google.errorprone.matchers.Matchers.isType; | ||
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; | ||
import static tech.picnic.errorprone.bugpatterns.util.MoreMatchers.hasMetaAnnotation; | ||
|
||
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.fixes.SuggestedFix; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.matchers.MultiMatcher; | ||
import com.google.errorprone.matchers.MultiMatcher.MultiMatchResult; | ||
import com.sun.source.tree.AnnotationTree; | ||
import com.sun.source.tree.MethodTree; | ||
import tech.picnic.errorprone.bugpatterns.util.SourceCode; | ||
|
||
/** | ||
* A {@link BugChecker} that flags nullary {@link | ||
* org.junit.jupiter.params.ParameterizedTest @ParameterizedTest} test methods. | ||
* | ||
* <p>Such tests are unnecessarily executed more than necessary. This checker suggests annotating | ||
* the method with {@link org.junit.jupiter.api.Test @Test}, and to drop all declared {@link | ||
* org.junit.jupiter.params.provider.ArgumentsSource argument sources}. | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
summary = "Nullary JUnit test methods should not be parameterized", | ||
link = BUG_PATTERNS_BASE_URL + "JUnitNullaryParameterizedTestDeclaration", | ||
linkType = CUSTOM, | ||
severity = SUGGESTION, | ||
tags = SIMPLIFICATION) | ||
public final class JUnitNullaryParameterizedTestDeclaration extends BugChecker | ||
implements MethodTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final MultiMatcher<MethodTree, AnnotationTree> IS_PARAMETERIZED_TEST = | ||
annotations(AT_LEAST_ONE, isType("org.junit.jupiter.params.ParameterizedTest")); | ||
private static final Matcher<AnnotationTree> IS_ARGUMENT_SOURCE = | ||
anyOf( | ||
isType("org.junit.jupiter.params.provider.ArgumentsSource"), | ||
isType("org.junit.jupiter.params.provider.ArgumentsSources"), | ||
hasMetaAnnotation("org.junit.jupiter.params.provider.ArgumentsSource")); | ||
|
||
/** Instantiates a new {@link JUnitNullaryParameterizedTestDeclaration} instance. */ | ||
public JUnitNullaryParameterizedTestDeclaration() {} | ||
|
||
@Override | ||
public Description matchMethod(MethodTree tree, VisitorState state) { | ||
if (!tree.getParameters().isEmpty()) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
MultiMatchResult<AnnotationTree> isParameterizedTest = | ||
IS_PARAMETERIZED_TEST.multiMatchResult(tree, state); | ||
if (!isParameterizedTest.matches()) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
/* | ||
* This method is vacuously parameterized. Suggest replacing `@ParameterizedTest` with `@Test`. | ||
* (As each method is checked independently, we cannot in general determine whether this | ||
* suggestion makes a `ParameterizedTest` type import obsolete; that task is left to Error | ||
* Prone's `RemoveUnusedImports` check.) | ||
*/ | ||
SuggestedFix.Builder fix = SuggestedFix.builder(); | ||
fix.merge( | ||
SuggestedFix.replace( | ||
isParameterizedTest.onlyMatchingNode(), | ||
'@' + SuggestedFixes.qualifyType(state, fix, "org.junit.jupiter.api.Test"))); | ||
|
||
/* | ||
* Also suggest dropping all (explicit and implicit) `@ArgumentsSource`s. No attempt is made to | ||
* assess whether a dropped `@MethodSource` also makes the referenced factory method(s) unused. | ||
*/ | ||
tree.getModifiers().getAnnotations().stream() | ||
.filter(a -> IS_ARGUMENT_SOURCE.matches(a, state)) | ||
.forEach(a -> fix.merge(SourceCode.deleteWithTrailingWhitespace(a, state))); | ||
|
||
return describeMatch(tree, fix.build()); | ||
} | ||
} |
155 changes: 155 additions & 0 deletions
155
...java/tech/picnic/errorprone/bugpatterns/JUnitNullaryParameterizedTestDeclarationTest.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,155 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import com.google.errorprone.BugCheckerRefactoringTestHelper; | ||
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class JUnitNullaryParameterizedTestDeclarationTest { | ||
@Test | ||
void identification() { | ||
CompilationTestHelper.newInstance(JUnitNullaryParameterizedTestDeclaration.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import org.junit.jupiter.api.Test;", | ||
"import org.junit.jupiter.params.ParameterizedTest;", | ||
"import org.junit.jupiter.params.provider.ValueSource;", | ||
"", | ||
"class A {", | ||
" void nonTest() {}", | ||
"", | ||
" @Test", | ||
" void nonParameterizedTest() {}", | ||
"", | ||
" @ParameterizedTest", | ||
" @ValueSource(ints = {0, 1})", | ||
" void goodParameterizedTest(int someInt) {}", | ||
"", | ||
" @ParameterizedTest", | ||
" @ValueSource(ints = {0, 1})", | ||
" // BUG: Diagnostic contains:", | ||
" void nullaryParameterizedTest() {}", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
void replacement() { | ||
BugCheckerRefactoringTestHelper.newInstance( | ||
JUnitNullaryParameterizedTestDeclaration.class, getClass()) | ||
.addInputLines( | ||
"A.java", | ||
"import org.junit.jupiter.params.ParameterizedTest;", | ||
"import org.junit.jupiter.params.provider.ArgumentsProvider;", | ||
"import org.junit.jupiter.params.provider.ArgumentsSource;", | ||
"import org.junit.jupiter.params.provider.ArgumentsSources;", | ||
"import org.junit.jupiter.params.provider.MethodSource;", | ||
"import org.junit.jupiter.params.provider.ValueSource;", | ||
"", | ||
"class A {", | ||
" @ParameterizedTest", | ||
" void withoutArgumentSource() {}", | ||
"", | ||
" @ParameterizedTest", | ||
" @ArgumentsSource(ArgumentsProvider.class)", | ||
" void withCustomArgumentSource() {}", | ||
"", | ||
" @ParameterizedTest", | ||
" @ArgumentsSources({", | ||
" @ArgumentsSource(ArgumentsProvider.class),", | ||
" @ArgumentsSource(ArgumentsProvider.class)", | ||
" })", | ||
" void withCustomerArgumentSources() {}", | ||
"", | ||
" /** Foo. */", | ||
" @ParameterizedTest", | ||
" @ValueSource(ints = {0, 1})", | ||
" void withValueSourceAndJavadoc() {}", | ||
"", | ||
" @ParameterizedTest", | ||
" @MethodSource(\"nonexistentMethod\")", | ||
" @SuppressWarnings(\"foo\")", | ||
" void withMethodSourceAndUnrelatedAnnotation() {}", | ||
"", | ||
" @org.junit.jupiter.params.ParameterizedTest", | ||
" @ArgumentsSource(ArgumentsProvider.class)", | ||
" @ValueSource(ints = {0, 1})", | ||
" @MethodSource(\"nonexistentMethod\")", | ||
" void withMultipleArgumentSourcesAndFullyQualifiedImport() {}", | ||
"", | ||
" class NestedWithTestAnnotationFirst {", | ||
" @ParameterizedTest", | ||
" @ValueSource(ints = {0, 1})", | ||
" void withValueSource() {}", | ||
" }", | ||
"", | ||
" class NestedWithTestAnnotationSecond {", | ||
" @ValueSource(ints = {0, 1})", | ||
" @ParameterizedTest", | ||
" void withValueSource() {}", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"A.java", | ||
"import org.junit.jupiter.api.Test;", | ||
"import org.junit.jupiter.params.ParameterizedTest;", | ||
"import org.junit.jupiter.params.provider.ArgumentsProvider;", | ||
"import org.junit.jupiter.params.provider.ArgumentsSource;", | ||
"import org.junit.jupiter.params.provider.ArgumentsSources;", | ||
"import org.junit.jupiter.params.provider.MethodSource;", | ||
"import org.junit.jupiter.params.provider.ValueSource;", | ||
"", | ||
"class A {", | ||
" @Test", | ||
" void withoutArgumentSource() {}", | ||
"", | ||
" @Test", | ||
" void withCustomArgumentSource() {}", | ||
"", | ||
" @Test", | ||
" void withCustomerArgumentSources() {}", | ||
"", | ||
" /** Foo. */", | ||
" @Test", | ||
" void withValueSourceAndJavadoc() {}", | ||
"", | ||
" @Test", | ||
" @SuppressWarnings(\"foo\")", | ||
" void withMethodSourceAndUnrelatedAnnotation() {}", | ||
"", | ||
" @Test", | ||
" void withMultipleArgumentSourcesAndFullyQualifiedImport() {}", | ||
"", | ||
" class NestedWithTestAnnotationFirst {", | ||
" @Test", | ||
" void withValueSource() {}", | ||
" }", | ||
"", | ||
" class NestedWithTestAnnotationSecond {", | ||
" @Test", | ||
" void withValueSource() {}", | ||
" }", | ||
"}") | ||
.addInputLines( | ||
"B.java", | ||
"import org.junit.jupiter.params.ParameterizedTest;", | ||
"", | ||
"class B {", | ||
" @ParameterizedTest", | ||
" void scopeInWhichIdentifierTestIsAlreadyDeclared() {}", | ||
"", | ||
" class Test {}", | ||
"}") | ||
.addOutputLines( | ||
"B.java", | ||
"import org.junit.jupiter.params.ParameterizedTest;", | ||
"", | ||
"class B {", | ||
" @org.junit.jupiter.api.Test", | ||
" void scopeInWhichIdentifierTestIsAlreadyDeclared() {}", | ||
"", | ||
" class Test {}", | ||
"}") | ||
.doTest(TestMode.TEXT_MATCH); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes me think of the
MoreJUnitMatchers#HAS_METHOD_SOURCE
(link) but this usage is slightly different is fine as-is and we don't need to consider any changes right now. Anyway, wanted to highlight it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, fine to move such things only when a second use case comes up 👍