Skip to content

Commit 7c2106a

Browse files
authored
More Hamcrest Migrations for Number Matchers (#379)
* removed containsInRelativeOrder from yaml list * initial commit * first test passes * changed name * updating file name * updates * updated tests and added support for the BigDecimal type with closeTo() * removed dedicated recipe, realized it could be generalized * fixed bug and added another test
1 parent a428327 commit 7c2106a

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private class MigrateToAssertJVisitor extends JavaIsoVisitor<ExecutionContext> {
7070
private final MethodMatcher matchersMatcher = new MethodMatcher("org.hamcrest.Matchers " + matcher + "(..)");
7171
private final MethodMatcher subMatcher = new MethodMatcher("org.hamcrest.Matchers *(org.hamcrest.Matcher)");
7272

73+
7374
@Override
7475
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
7576
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
@@ -97,12 +98,15 @@ private J.MethodInvocation handleTwoArgumentCase(J.MethodInvocation mi, Executio
9798
String argumentsTemplate = originalArguments.stream()
9899
.map(a -> typeToIndicator(a.getType()))
99100
.collect(Collectors.joining(", "));
101+
argumentsTemplate = applySpecialCases((J.MethodInvocation) matcherArgument, argumentsTemplate);
102+
100103
JavaTemplate template = JavaTemplate.builder(String.format("assertThat(%s).%s(%s)",
101104
actual, assertion, argumentsTemplate))
102105
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3.24"))
103-
.staticImports("org.assertj.core.api.Assertions.assertThat")
106+
.staticImports("org.assertj.core.api.Assertions.assertThat", "org.assertj.core.api.Assertions.within")
104107
.build();
105108
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
109+
maybeAddImport("org.assertj.core.api.Assertions", "within");
106110
maybeRemoveImport("org.hamcrest.Matchers." + matcher);
107111
maybeRemoveImport("org.hamcrest.MatcherAssert.assertThat");
108112

@@ -154,5 +158,24 @@ private String typeToIndicator(JavaType type) {
154158
return String.format("#{any(%s)}", str);
155159
}
156160
}
161+
162+
private String applySpecialCases(J.MethodInvocation mi, String template) {
163+
final MethodMatcher CLOSE_TO_MATCHER = new MethodMatcher("org.hamcrest.Matchers closeTo(..)");
164+
String[] splitTemplate = template.split(", ");
165+
166+
if (CLOSE_TO_MATCHER.matches(mi)) {
167+
List<String> newTemplateArr = new ArrayList<>();
168+
for (int i = 0; i<splitTemplate.length; i++) {
169+
// within needs to placed on the second argument of isCloseTo
170+
if (i == 1) {
171+
newTemplateArr.add(String.format("within(%s)", splitTemplate[i]));
172+
continue;
173+
}
174+
newTemplateArr.add(splitTemplate[i]);
175+
}
176+
return String.join(", ", newTemplateArr);
177+
}
178+
return template;
179+
}
157180
}
158181
}

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,82 @@ void test() {
366366
""")
367367
);
368368
}
369+
370+
@Test
371+
void closeToTest() {
372+
rewriteRun(
373+
spec -> spec.recipe(new HamcrestMatcherToAssertJ("closeTo", "isCloseTo")),
374+
//language=java
375+
java("""
376+
import org.junit.jupiter.api.Test;
377+
378+
import static org.hamcrest.MatcherAssert.assertThat;
379+
import static org.hamcrest.Matchers.closeTo;
380+
381+
class ATest {
382+
@Test
383+
void replaceCloseTo() {
384+
assertThat(1.0, closeTo(2.0, 1.0));
385+
}
386+
}
387+
""",
388+
"""
389+
import org.junit.jupiter.api.Test;
390+
391+
import static org.assertj.core.api.Assertions.assertThat;
392+
import static org.assertj.core.api.Assertions.within;
393+
394+
class ATest {
395+
@Test
396+
void replaceCloseTo() {
397+
assertThat(1.0).isCloseTo(2.0, within(1.0));
398+
}
399+
}
400+
""")
401+
);
402+
}
403+
404+
@Test
405+
void closeToWorksWithBigDecimal() {
406+
rewriteRun(
407+
spec -> spec.recipe(new HamcrestMatcherToAssertJ("closeTo", "isCloseTo")),
408+
//language=java
409+
java("""
410+
import org.junit.jupiter.api.Test;
411+
import java.math.BigDecimal;
412+
413+
import static org.hamcrest.MatcherAssert.assertThat;
414+
import static org.hamcrest.Matchers.closeTo;
415+
416+
class ATest {
417+
@Test
418+
void replaceCloseTo() {
419+
BigDecimal x = new BigDecimal("1.000005");
420+
BigDecimal y = new BigDecimal("2.0");
421+
BigDecimal z = new BigDecimal("0.0005");
422+
assertThat(x, closeTo(y, z));
423+
}
424+
}
425+
""",
426+
"""
427+
import org.junit.jupiter.api.Test;
428+
import java.math.BigDecimal;
429+
430+
import static org.assertj.core.api.Assertions.assertThat;
431+
import static org.assertj.core.api.Assertions.within;
432+
433+
class ATest {
434+
@Test
435+
void replaceCloseTo() {
436+
BigDecimal x = new BigDecimal("1.000005");
437+
BigDecimal y = new BigDecimal("2.0");
438+
BigDecimal z = new BigDecimal("0.0005");
439+
assertThat(x).isCloseTo(y, within(z));
440+
}
441+
}
442+
""")
443+
);
444+
}
369445
}
370446

371447
@Nested

0 commit comments

Comments
 (0)