Skip to content

Commit

Permalink
Only convert when actual is of type boolean too
Browse files Browse the repository at this point in the history
Fixes #587
  • Loading branch information
timtebeek committed Aug 28, 2024
1 parent 8145e8b commit cb3c8c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (ASSERT_EQUALS.matches(mi) && isBooleanLiteral(mi)) {
if (ASSERT_EQUALS.matches(mi) && isBooleanLiteral(mi) &&
JavaType.Primitive.Boolean.equals(mi.getArguments().get(1).getType())) {
StringBuilder sb = new StringBuilder();
String assertMethod = Boolean.parseBoolean(((J.Literal) mi.getArguments().get(0)).getValueSource())
? "assertTrue" : "assertFalse";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,23 @@ void test() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/587")
void assertTrueWithNonBoolean() {
rewriteRun(
spec -> spec.recipe(new AssertEqualsBooleanToAssertBoolean()),
// language=java
java(
"""
import static org.junit.jupiter.api.Assertions.assertEquals;
class Main {
void foo() {
assertEquals(true, new Object());
}
}
"""
)
);
}
}

0 comments on commit cb3c8c0

Please sign in to comment.