Skip to content

Commit

Permalink
[UnusedMethod] exempt @MethodSource with qualified method names
Browse files Browse the repository at this point in the history
As mentioned in [the JUnit 5 docs](https://junit.org/junit5/docs/current/api/org.junit.jupiter.params/org/junit/jupiter/params/provider/MethodSource.html), the value within `@MethodSource` can be a fully-qualified name. This updates the code to handle this fact and adds some tests to try to flex it. The particular case we were hitting is the one with the `@Nested` test class using a parameter method in its parent.

Fixes #2411

COPYBARA_INTEGRATE_REVIEW=#2411 from HubSpot:qualified-method-source-upstream 19ce7fb
PiperOrigin-RevId: 381974583
  • Loading branch information
stevegutz authored and Error Prone Team committed Jun 28, 2021
1 parent dca7e79 commit 9b4ffa0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
Expand Up @@ -254,14 +254,21 @@ private void handleMethodSource(MethodTree tree) {
.findAny()
// get the annotation value array as a set of Names
.flatMap(a -> getAnnotationValue(a, "value"))
.map(y -> asStrings(y).map(state::getName).collect(toImmutableSet()))
// remove all potentially unused methods whose simple name is referenced by the
// @MethodSource
.map(
y -> asStrings(y).map(state::getName).map(Name::toString).collect(toImmutableSet()))
// remove all potentially unused methods referenced by the @MethodSource
.ifPresent(
referencedNames ->
unusedMethods
.entrySet()
.removeIf(e -> referencedNames.contains(e.getKey().getSimpleName())));
.removeIf(
e -> {
Symbol unusedSym = e.getKey();
String simpleName = unusedSym.getSimpleName().toString();
return referencedNames.contains(simpleName)
|| referencedNames.contains(
unusedSym.owner.getQualifiedName() + "#" + simpleName);
}));
}
}

Expand Down
Expand Up @@ -223,4 +223,56 @@ public void methodSource() {
"}")
.doTest();
}

@Test
public void qualifiedMethodSource() {
helper
.addSourceLines(
"MethodSource.java",
"package org.junit.jupiter.params.provider;",
"public @interface MethodSource {",
" String[] value();",
"}")
.addSourceLines(
"Test.java",
"import java.util.stream.Stream;",
"import org.junit.jupiter.params.provider.MethodSource;",
"class Test {",
" @MethodSource(\"Test#parameters\")",
" void test() {}",
"",
"",
" private static Stream<String> parameters() {",
" return Stream.of();",
" }",
"}")
.doTest();
}

@Test
public void nestedQualifiedMethodSource() {
helper
.addSourceLines(
"MethodSource.java",
"package org.junit.jupiter.params.provider;",
"public @interface MethodSource {",
" String[] value();",
"}")
.addSourceLines(
"Test.java",
"import java.util.stream.Stream;",
"import org.junit.jupiter.params.provider.MethodSource;",
"class Test {",
" // @Nested",
" public class NestedTest {",
" @MethodSource(\"Test#parameters\")",
" void test() {}",
" }",
"",
" private static Stream<String> parameters() {",
" return Stream.of();",
" }",
"}")
.doTest();
}
}

0 comments on commit 9b4ffa0

Please sign in to comment.