Skip to content

Commit

Permalink
Skip RemoveTestPrefix when calling a similarly named method (#543)
Browse files Browse the repository at this point in the history
* Skip RemoveTestPrefix when calling a similarly named method

#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>
  • Loading branch information
timtebeek and github-actions[bot] authored Jul 3, 2024
1 parent 2096894 commit 30f35fe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<AtomicBoolean>() {
@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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void testMethod() {
@Test
void test_snake_case() {
}
@Test
void testRTFCharacters() {
}
Expand Down Expand Up @@ -85,7 +85,7 @@ void method() {
@Test
void snake_case() {
}
@Test
void rtfCharacters() {
}
Expand Down Expand Up @@ -139,7 +139,7 @@ void ignoreOverriddenMethod() {
abstract class AbstractTest {
public abstract void testMethod();
}
class BTest extends AbstractTest {
@Test
@Override
Expand Down Expand Up @@ -268,7 +268,7 @@ class ATest {
@Test
void testMyDoSomethingLogic() {
}
void myDoSomethingLogic() {}
}
"""
Expand All @@ -292,7 +292,7 @@ class ATest {
@MethodSource
void testMyDoSomethingLogic(Arguments args) {
}
static Stream<Arguments> testMyDoSomethingLogic() {
return Stream.empty();
}
Expand Down Expand Up @@ -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();
}
}
"""
)
);
}
}

0 comments on commit 30f35fe

Please sign in to comment.