-
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 BugPattern for removing duplicate Mockito.verifyNoInteractions()
calls
#476
base: master
Are you sure you want to change the base?
Changes from all commits
26a04b9
63d351c
88cabab
83da373
301ec17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
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.Matchers.staticMethod; | ||
import static java.util.stream.Collectors.joining; | ||
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; | ||
import static tech.picnic.errorprone.bugpatterns.util.MoreJUnitMatchers.TEST_METHOD; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.common.collect.ImmutableList; | ||
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.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.source.tree.MethodTree; | ||
import com.sun.source.util.TreeScanner; | ||
import java.util.List; | ||
import org.jspecify.annotations.Nullable; | ||
import org.mockito.Mockito; | ||
import tech.picnic.errorprone.bugpatterns.util.SourceCode; | ||
|
||
/** | ||
* A {@link BugChecker} that flags multiple usages of {@link Mockito#verifyNoInteractions} in favor | ||
* of one call with varargs. | ||
* | ||
* <p>Multiple calls of {@link Mockito#verifyNoInteractions} can make the code more verbose than | ||
* necessary. Instead of multiple calls, because {@link Mockito#verifyNoInteractions} accepts | ||
* varargs, one call should be preferred. | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
summary = "Prefer one call to `verifyNoInteractions(varargs...)` over multiple calls", | ||
link = BUG_PATTERNS_BASE_URL + "MockitoVerifyNoInteractionsUsage", | ||
linkType = CUSTOM, | ||
severity = SUGGESTION, | ||
tags = SIMPLIFICATION) | ||
public final class MockitoVerifyNoInteractionsUsage extends BugChecker | ||
implements MethodTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final Matcher<ExpressionTree> VERIFY_NO_INTERACTIONS = | ||
staticMethod().onClass("org.mockito.Mockito").named("verifyNoInteractions"); | ||
|
||
/** Instantiates a new {@link MockitoVerifyNoInteractionsUsage} instance. */ | ||
public MockitoVerifyNoInteractionsUsage() {} | ||
|
||
@Override | ||
public Description matchMethod(MethodTree tree, VisitorState state) { | ||
if (!TEST_METHOD.matches(tree, state)) { | ||
return Description.NO_MATCH; | ||
} | ||
ImmutableList<MethodInvocationTree> verifyNoInteractionsInvocations = | ||
getVerifyNoInteractionsInvocations(tree, state); | ||
if (verifyNoInteractionsInvocations.size() < 2) { | ||
return Description.NO_MATCH; | ||
} | ||
String combinedArgument = | ||
verifyNoInteractionsInvocations.stream() | ||
.map(MethodInvocationTree::getArguments) | ||
.flatMap(List::stream) | ||
.map(Object::toString) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 looks like we're converting a |
||
.collect(joining(", ")); | ||
|
||
SuggestedFix.Builder fixBuilder = SuggestedFix.builder(); | ||
MethodInvocationTree lastInvocation = | ||
verifyNoInteractionsInvocations.get(verifyNoInteractionsInvocations.size() - 1); | ||
verifyNoInteractionsInvocations.forEach( | ||
invocationTree -> { | ||
if (!invocationTree.equals(lastInvocation)) { | ||
fixBuilder.replace( | ||
ASTHelpers.getStartPosition(invocationTree), | ||
state.getEndPosition(invocationTree) + 1, | ||
""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This replacement leaves an empty line behind, I couldn't figure out how to collapse lines 😞 or perhaps it should be taken care of by formatting and it's not a big deal? |
||
} | ||
}); | ||
|
||
String callAsString = SourceCode.treeToString(lastInvocation, state); | ||
fixBuilder.replace( | ||
lastInvocation, | ||
callAsString.startsWith("Mockito.") | ||
? "Mockito.verifyNoInteractions(" + combinedArgument + ")" | ||
: "verifyNoInteractions(" + combinedArgument + ")"); | ||
|
||
return describeMatch(tree, fixBuilder.build()); | ||
Kamil-Gabaydullin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private static ImmutableList<MethodInvocationTree> getVerifyNoInteractionsInvocations( | ||
MethodTree methodTree, VisitorState state) { | ||
ImmutableList.Builder<MethodInvocationTree> invocationTreeBuilder = ImmutableList.builder(); | ||
|
||
new TreeScanner<@Nullable Void, @Nullable Void>() { | ||
@Override | ||
public @Nullable Void visitMethodInvocation( | ||
MethodInvocationTree node, @Nullable Void unused) { | ||
if (VERIFY_NO_INTERACTIONS.matches(node, state)) { | ||
invocationTreeBuilder.add(node); | ||
} | ||
return super.visitMethodInvocation(node, unused); | ||
} | ||
}.scan(methodTree, null); | ||
|
||
return invocationTreeBuilder.build(); | ||
} | ||
} |
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.
If there is only one call to
verifyNoInteractions()
we don't want to do anything too