Skip to content

Commit 18a943d

Browse files
Limit RemoveIsMatcher to invocations within assertThat (#374)
* started adding more unit tests to test the issue * is matcher now also checks if is invocation is directly inside assertThat before making a change * Update src/test/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcherTest.java Co-authored-by: Tim te Beek <[email protected]> * Update src/main/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcher.java Co-authored-by: Tim te Beek <[email protected]> * Update src/test/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcherTest.java Co-authored-by: Tim te Beek <[email protected]> * Update src/test/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcherTest.java Co-authored-by: Tim te Beek <[email protected]> * Update src/test/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcherTest.java Co-authored-by: Tim te Beek <[email protected]> --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 91bbd4d commit 18a943d

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

src/main/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcher.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*/
1616
package org.openrewrite.java.testing.hamcrest;
1717

18-
import org.openrewrite.ExecutionContext;
19-
import org.openrewrite.Recipe;
20-
import org.openrewrite.TreeVisitor;
18+
import org.openrewrite.*;
2119
import org.openrewrite.java.JavaVisitor;
2220
import org.openrewrite.java.MethodMatcher;
21+
import org.openrewrite.java.TreeVisitingPrinter;
2322
import org.openrewrite.java.tree.J;
2423

24+
@SuppressWarnings("NullableProblems")
2525
public class RemoveIsMatcher extends Recipe {
2626
@Override
2727
public String getDisplayName() {
@@ -34,13 +34,16 @@ public String getDescription() {
3434
}
3535

3636
static final MethodMatcher IS_MATCHER = new MethodMatcher("org.hamcrest.Matchers is(org.hamcrest.Matcher)");
37+
static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.hamcrest.MatcherAssert assertThat(..)");
3738

3839
@Override
3940
public TreeVisitor<?, ExecutionContext> getVisitor() {
4041
return new JavaVisitor<ExecutionContext>() {
4142
@Override
4243
public J visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) {
43-
if (IS_MATCHER.matches(mi)) {
44+
if (ASSERT_THAT_MATCHER.matches(mi)) {
45+
getCursor().putMessage("ASSERT_THAT", mi);
46+
} else if (IS_MATCHER.matches(mi) && getCursor().pollNearestMessage("ASSERT_THAT") != null) {
4447
maybeRemoveImport("org.hamcrest.Matchers.is");
4548
return mi.getArguments().get(0).withPrefix(mi.getPrefix());
4649
}

src/test/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcherTest.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,90 @@ void testEquals() {
6868
"""));
6969
}
7070

71+
@Test
72+
void isNotCalledInAssertThat() {
73+
//language=java
74+
rewriteRun(
75+
java(
76+
"""
77+
import org.junit.jupiter.api.Test;
78+
import static org.hamcrest.Matchers.is;
79+
import static org.hamcrest.Matchers.equalTo;
80+
81+
class ATest {
82+
@Test
83+
void testMethod() {
84+
String str1 = "Hello world!";
85+
Matcher<String> x = is(equalTo(str1));
86+
}
87+
}
88+
"""
89+
)
90+
);
91+
}
92+
93+
@Test
94+
void isNotDirectlyInAssertThat() {
95+
//language=java
96+
rewriteRun(
97+
java(
98+
"""
99+
import org.junit.jupiter.api.Test;
100+
import static org.hamcrest.MatcherAssert.assertThat;
101+
import static org.hamcrest.Matchers.*;
102+
103+
class ATest {
104+
@Test
105+
void testMethod() {
106+
String str1 = "Hello world!";
107+
String str2 = "Hello world!";
108+
assertThat(str1, not(is(equalTo(str2))));
109+
}
110+
}
111+
""",
112+
"""
113+
import org.junit.jupiter.api.Test;
114+
import static org.hamcrest.MatcherAssert.assertThat;
115+
import static org.hamcrest.Matchers.not;
116+
import static org.hamcrest.Matchers.equalTo;
117+
118+
class ATest {
119+
@Test
120+
void testMethod() {
121+
String str1 = "Hello world!";
122+
String str2 = "Hello world!";
123+
assertThat(str1, not(equalTo(str2)));
124+
}
125+
}
126+
"""
127+
)
128+
);
129+
}
130+
131+
@Test
132+
void noReplacementForOtherMethodInvocations() {
133+
//language=java
134+
rewriteRun(
135+
java(
136+
"""
137+
import org.junit.jupiter.api.Test;
138+
import static org.hamcrest.MatcherAssert.assertThat;
139+
import static org.hamcrest.Matcher;
140+
import static org.hamcrest.Matchers.*;
141+
142+
class ATest {
143+
@Test
144+
void testMethod() {
145+
String str1 = "Hello world!";
146+
String str2 = "Hello world!";
147+
foo(is(equalTo(str2)));
148+
}
149+
150+
void foo(Matcher<String> matcher) {
151+
}
152+
}
153+
"""
154+
)
155+
);
156+
}
71157
}

0 commit comments

Comments
 (0)