Skip to content

Commit

Permalink
Adds org.mockito.ArgumentMatchers to StringFormatStringValidation sin…
Browse files Browse the repository at this point in the history
…ce newer versions of Mockito moved those methods.

RELNOTES: FormatStringAnnotation checker will recognize invocations to org.mockito.ArgumentMatchers members as substitutions for constant format strings.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=243826694
  • Loading branch information
yanglinw authored and ronshapiro committed Apr 18, 2019
1 parent 958807d commit 12ba9de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@
*/
public class StrictFormatStringValidation {

private static final Matcher<ExpressionTree> MOCKITO_ARGUMENT_MATCHER =
staticMethod().onClass("org.mockito.Matchers");
private static final Matcher<ExpressionTree> MOCKITO_MATCHERS =
staticMethod().onClassAny("org.mockito.Matchers", "org.mockito.ArgumentMatchers");

@Nullable
public static ValidationResult validate(
ExpressionTree formatStringTree, List<? extends ExpressionTree> args, VisitorState state) {
if (MOCKITO_ARGUMENT_MATCHER.matches(formatStringTree, state)) {
if (MOCKITO_MATCHERS.matches(formatStringTree, state)) {
// Mockito matchers do not pass standard @FormatString requirements, but we allow them so
// that people can verify @FormatMethod methods.
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,24 @@ public void testMatches_succeedsForMockitoMatchers() {
"}")
.doTest();
}

@Test
public void testMatches_succeedsForMockitoArgumentMatchers() {
compilationHelper
.addSourceLines(
"test/FormatStringTestCase.java",
"package test;",
"import static org.mockito.ArgumentMatchers.any;",
"import static org.mockito.ArgumentMatchers.eq;",
"import static org.mockito.Mockito.verify;",
"import com.google.errorprone.annotations.FormatMethod;",
"import com.google.errorprone.annotations.FormatString;",
"public class FormatStringTestCase {",
" @FormatMethod public void log(@FormatString String s, Object... args) {}",
" public void callLog(String s, Object... args) {",
" verify(this).log(any(String.class), eq(args));",
" }",
"}")
.doTest();
}
}

0 comments on commit 12ba9de

Please sign in to comment.