Skip to content

Commit

Permalink
RemoveTestPrefix should skip implied MethodSource
Browse files Browse the repository at this point in the history
For #462
  • Loading branch information
timtebeek committed Jan 16, 2024
1 parent 216983b commit a426e40
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
Expand Down Expand Up @@ -70,6 +71,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}

private static class RemoveTestPrefixVisitor extends JavaIsoVisitor<ExecutionContext> {

private static final AnnotationMatcher ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.params.provider.MethodSource");

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
ExecutionContext ctx) {
Expand All @@ -93,19 +97,29 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
return m;
}

// Rename method
// Avoid reserved keywords
String newMethodName = snakecase
? Character.toLowerCase(simpleName.charAt(5)) + simpleName.substring(6)
: Character.toLowerCase(simpleName.charAt(4)) + simpleName.substring(5);
if (RESERVED_KEYWORDS.contains(newMethodName)) {
return m;
}

// Prevent conflicts with existing methods
JavaType.Method type = m.getMethodType();
if (type == null || methodExists(type, newMethodName)) {
return m;
}

// Skip implied methodSource
for (J.Annotation annotation : method.getLeadingAnnotations()) {
if (ANNOTATION_MATCHER.matches(annotation) &&
(annotation.getArguments() == null || annotation.getArguments().isEmpty())) {
return m;
}
}

// Rename method and return
type = type.withName(newMethodName);
return m.withName(m.getName().withSimpleName(newMethodName).withType(type))
.withMethodType(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class RemoveTestPrefixTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9"))
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),
"junit-jupiter-api-5.9", "junit-jupiter-params-5.9"))
.recipe(new RemoveTestPrefix());
}

Expand Down Expand Up @@ -265,4 +266,30 @@ void myDoSomethingLogic() {}
)
);
}

@Test
void skipImpliedMethodSource() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
class ATest {
@Test
@MethodSource
void testMyDoSomethingLogic(Arguments args) {
}
static Stream<Arguments> testMyDoSomethingLogic() {
return Stream.empty();
}
}
"""
)
);
}
}

0 comments on commit a426e40

Please sign in to comment.