From 30f35fe919b8166c19ddf22e2f1ea1c0d918487a Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 3 Jul 2024 15:27:56 +0200 Subject: [PATCH] Skip RemoveTestPrefix when calling a similarly named method (#543) * Skip RemoveTestPrefix when calling a similarly named method https://github.com/openrewrite/rewrite-testing-frameworks/issues/258 * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../testing/cleanup/RemoveTestPrefix.java | 16 ++++++++++ .../testing/cleanup/RemoveTestPrefixTest.java | 31 ++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java b/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java index f16fe570d..2cb18b734 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java @@ -31,6 +31,7 @@ import java.time.Duration; import java.util.Arrays; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; public class RemoveTestPrefix extends Recipe { @@ -121,6 +122,21 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, } } + // Skip when calling a similarly named method + AtomicBoolean skip = new AtomicBoolean(false); + new JavaIsoVisitor() { + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) { + if (method.getName().getSimpleName().equals(newMethodName)) { + skip.set(true); + } + return super.visitMethodInvocation(method, atomicBoolean); + } + }.visitMethodDeclaration(m, skip); + if (skip.get()) { + return m; + } + // Rename method and return type = type.withName(newMethodName); return m.withName(m.getName().withSimpleName(newMethodName).withType(type)) diff --git a/src/test/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefixTest.java b/src/test/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefixTest.java index 00be3cd29..6cdeae91a 100644 --- a/src/test/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefixTest.java +++ b/src/test/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefixTest.java @@ -53,7 +53,7 @@ void testMethod() { @Test void test_snake_case() { } - + @Test void testRTFCharacters() { } @@ -85,7 +85,7 @@ void method() { @Test void snake_case() { } - + @Test void rtfCharacters() { } @@ -139,7 +139,7 @@ void ignoreOverriddenMethod() { abstract class AbstractTest { public abstract void testMethod(); } - + class BTest extends AbstractTest { @Test @Override @@ -268,7 +268,7 @@ class ATest { @Test void testMyDoSomethingLogic() { } - + void myDoSomethingLogic() {} } """ @@ -292,7 +292,7 @@ class ATest { @MethodSource void testMyDoSomethingLogic(Arguments args) { } - + static Stream testMyDoSomethingLogic() { return Stream.empty(); } @@ -324,4 +324,25 @@ void tests() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/258") + void removeTestPrefixWithClashingMethod() { + rewriteRun( + //language=java + java( + """ + import org.junit.jupiter.api.Test; + import static java.util.List.of; + + class FooTest { + @Test + void testOf() { + of(); + } + } + """ + ) + ); + } }