Skip to content

Commit 39dc67c

Browse files
committed
AssertToAssertions maintains whitespace of arguments
Fixes: #423
1 parent 64651c2 commit 39dc67c

File tree

2 files changed

+74
-13
lines changed

2 files changed

+74
-13
lines changed

src/main/java/org/openrewrite/java/testing/junit5/AssertToAssertions.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.openrewrite.Preconditions;
2020
import org.openrewrite.Recipe;
2121
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.internal.ListUtils;
2223
import org.openrewrite.internal.lang.Nullable;
2324
import org.openrewrite.java.ChangeMethodTargetToStatic;
2425
import org.openrewrite.java.JavaIsoVisitor;
@@ -29,6 +30,8 @@
2930
import java.util.Arrays;
3031
import java.util.List;
3132

33+
import static java.util.Collections.emptyList;
34+
3235
public class AssertToAssertions extends Recipe {
3336

3437
@Override
@@ -82,8 +85,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
8285
"org.junit.jupiter.api.Assertions", null, null, true)
8386
.getVisitor());
8487

85-
List<Expression> args = m.getArguments();
86-
Expression firstArg = args.get(0);
88+
List<JRightPadded<Expression>> args = m.getPadding().getArguments().getPadding().getElements();
89+
Expression firstArg = args.get(0).getElement();
8790
// Suppress arg-switching for Assertions.assertEquals(String, String)
8891
if (args.size() == 2) {
8992
if ("assertSame".equals(m.getSimpleName()) ||
@@ -97,17 +100,29 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
97100
if (TypeUtils.isString(firstArg.getType())) {
98101
// Move the first arg to be the last argument
99102

100-
List<Expression> newArgs = new ArrayList<>(args.size());
101-
for (int i = 1; i < args.size(); i++) {
102-
if (i == 1) {
103-
newArgs.add(args.get(i).withPrefix(firstArg.getPrefix()));
104-
} else {
105-
newArgs.add(args.get(i));
106-
}
107-
}
108-
newArgs.add(firstArg.withPrefix(args.get(args.size() - 1).getPrefix()));
109-
110-
m = m.withArguments(newArgs);
103+
List<JRightPadded<Expression>> newArgs = new ArrayList<>(args);
104+
JRightPadded<Expression> first = newArgs.remove(0);
105+
JRightPadded<Expression> lastArg = args.get(args.size() - 1);
106+
boolean lastArgComments = !lastArg.getAfter().getComments().isEmpty();
107+
108+
newArgs = ListUtils.mapFirst(newArgs, e -> e.withElement(e.getElement().withPrefix(first.getElement().getPrefix())));
109+
newArgs = ListUtils.mapLast(newArgs, e -> e.withAfter(Space.EMPTY));
110+
newArgs.add(first
111+
.withElement(first.getElement()
112+
.withPrefix(lastArgComments ?
113+
lastArg.getAfter().withComments(ListUtils.mapLast(
114+
lastArg.getAfter().getComments(),
115+
c -> c.withSuffix(lastArg.getElement().getPrefix().getWhitespace()))
116+
) :
117+
lastArg.getElement().getPrefix()
118+
)
119+
)
120+
.withAfter(lastArgComments ? Space.build(lastArg.getAfter().getLastWhitespace(), emptyList()) : lastArg.getAfter())
121+
);
122+
123+
m = m.getPadding().withArguments(
124+
m.getPadding().getArguments().getPadding().withElements(newArgs)
125+
);
111126
}
112127

113128
return m;

src/test/java/org/openrewrite/java/testing/junit5/AssertToAssertionsTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,52 @@ void testNestedPartitionStepStepReference() {
404404
);
405405
}
406406

407+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/423")
408+
@Test
409+
void assertThrows() {
410+
//language=java
411+
rewriteRun(
412+
java(
413+
"""
414+
import static org.junit.Assert.assertThrows;
415+
416+
class Test {
417+
void test(Runnable run) {
418+
assertThrows(
419+
"Exception from cleanable.clean() should be rethrown",
420+
IllegalStateException.class,
421+
run::run
422+
);
423+
assertThrows(
424+
"Exception from cleanable.clean() should be rethrown",
425+
IllegalStateException.class,
426+
run::run // do not remove
427+
);
428+
}
429+
}
430+
""",
431+
"""
432+
import static org.junit.jupiter.api.Assertions.assertThrows;
433+
434+
class Test {
435+
void test(Runnable run) {
436+
assertThrows(
437+
IllegalStateException.class,
438+
run::run,
439+
"Exception from cleanable.clean() should be rethrown"
440+
);
441+
assertThrows(
442+
IllegalStateException.class,
443+
run::run, // do not remove
444+
"Exception from cleanable.clean() should be rethrown"
445+
);
446+
}
447+
}
448+
"""
449+
)
450+
);
451+
}
452+
407453
@Test
408454
void missingTypeInfo() {
409455
//language=java

0 commit comments

Comments
 (0)