Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static java.util.Collections.singleton;
Expand Down Expand Up @@ -170,7 +169,7 @@ private String getStringTemplateAndAppendArguments(J.MethodInvocation assertThat
}

// If either argument is empty, we choose which one to add to the arguments list, and optionally extract the select
arguments.add(extractEitherArgument(assertThatArgumentIsEmpty, assertThatArgument, methodToReplaceArgument));
arguments.add(assertThatArgumentIsEmpty ? methodToReplaceArgument : assertThatArgument);

// Special case for Path.of() assertions
if ("java.nio.file.Path".equals(requiredType) && dedicatedAssertion.contains("Raw") &&
Expand All @@ -181,17 +180,6 @@ private String getStringTemplateAndAppendArguments(J.MethodInvocation assertThat

return "assertThat(#{any()}).%s(#{any()})";
}

private Expression extractEitherArgument(boolean assertThatArgumentIsEmpty, Expression assertThatArgument, Expression methodToReplaceArgument) {
if (assertThatArgumentIsEmpty) {
return methodToReplaceArgument;
}
// Only on the assertThat argument do we possibly replace the argument with the select; such as list.size() -> list
if (chainedAssertMatcher.matches(assertThatArgument)) {
return Objects.requireNonNull(((J.MethodInvocation) assertThatArgument).getSelect());
}
return assertThatArgument;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,55 @@ void biscuits() {
}
}

@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/496")
@Test
void assertTrueContainsWithMethodCallArgPreservesMethodCall() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5")),
//language=java
java(
"""
import java.util.Locale;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertTrue;

class MyTest {
static class UnderTest {
Locale asLocale() {
return Locale.US;
}
}

void test(Set<Locale> localeSet) {
UnderTest underTest = new UnderTest();
assertTrue(localeSet.contains(underTest.asLocale()));
}
}
""",
"""
import java.util.Locale;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

class MyTest {
static class UnderTest {
Locale asLocale() {
return Locale.US;
}
}

void test(Set<Locale> localeSet) {
UnderTest underTest = new UnderTest();
assertThat(localeSet).contains(underTest.asLocale());
}
}
"""
)
);
}

/**
* Chained AssertJ assertions should be simplified to the corresponding dedicated assertion, as
* per <a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,34 @@ void testMethod(Map<String, ?> map) {
);
}

@Test
void argumentMatchingChainedAssertionIsNotStripped() {
rewriteRun(
spec -> spec.recipe(new SimplifyChainedAssertJAssertion("equals", "isTrue", "isEqualTo", "java.lang.Object")),
//language=java
java(
"""
import static org.assertj.core.api.Assertions.assertThat;

class MyTest {
void testMethod(Object obj1, Object obj2, Object obj3) {
assertThat(obj1.equals(obj2.equals(obj3))).isTrue();
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;

class MyTest {
void testMethod(Object obj1, Object obj2, Object obj3) {
assertThat(obj1).isEqualTo(obj2.equals(obj3));
}
}
"""
)
);
}

@Test
void sizeIsEqualToLongLiteralIsNotConverted() {
rewriteRun(
Expand Down
Loading