Skip to content

Commit

Permalink
Merge pull request #41 from HubSpot/method-source-improvements
Browse files Browse the repository at this point in the history
[UnusedMethod] exempt @MethodSource with qualified method names
  • Loading branch information
stevegutz committed Jun 25, 2021
2 parents 4804d1d + 2c8cc67 commit c6bf02d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,20 @@ 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
Original file line number Diff line number Diff line change
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 c6bf02d

Please sign in to comment.