Skip to content

Commit 2a3c45c

Browse files
Groovy: handle comma as an assert separator (#7532)
1 parent a133d77 commit 2a3c45c

4 files changed

Lines changed: 65 additions & 2 deletions

File tree

rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,12 +1518,18 @@ public void visitAssertStatement(AssertStatement statement) {
15181518
skip("assert");
15191519
Expression condition = doVisit(statement.getBooleanExpression());
15201520
JLeftPadded<Expression> message = null;
1521+
Markers markers = Markers.EMPTY;
15211522
if (!(statement.getMessageExpression() instanceof ConstantExpression) || !((ConstantExpression) statement.getMessageExpression()).isNullExpression()) {
15221523
Space messagePrefix = whitespace();
1523-
skip(":");
1524+
if (cursor < source.length() && source.charAt(cursor) == ',') {
1525+
skip(",");
1526+
markers = markers.addIfAbsent(new AssertMessageComma(randomId()));
1527+
} else {
1528+
skip(":");
1529+
}
15241530
message = padLeft(messagePrefix, doVisit(statement.getMessageExpression()));
15251531
}
1526-
return new J.Assert(randomId(), prefix, Markers.EMPTY, condition, message);
1532+
return new J.Assert(randomId(), prefix, markers, condition, message);
15271533
}));
15281534
}
15291535

rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyPrinter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,19 @@ public J visitClassDeclaration(J.ClassDeclaration classDecl, PrintOutputCapture<
314314
return classDecl;
315315
}
316316

317+
@Override
318+
public J visitAssert(J.Assert assert_, PrintOutputCapture<P> p) {
319+
if (!assert_.getMarkers().findFirst(AssertMessageComma.class).isPresent()) {
320+
return super.visitAssert(assert_, p);
321+
}
322+
beforeSyntax(assert_, Space.Location.ASSERT_PREFIX, p);
323+
p.append("assert");
324+
visit(assert_.getCondition(), p);
325+
visitLeftPadded(",", assert_.getDetail(), JLeftPadded.Location.ASSERT_DETAIL, p);
326+
afterSyntax(assert_, p);
327+
return assert_;
328+
}
329+
317330
@Override
318331
public J visitTypeCast(J.TypeCast t, PrintOutputCapture<P> p) {
319332
if (!t.getMarkers().findFirst(AsStyleTypeCast.class).isPresent()) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.groovy.marker;
17+
18+
import lombok.Value;
19+
import lombok.With;
20+
import org.openrewrite.marker.Marker;
21+
22+
import java.util.UUID;
23+
24+
/**
25+
* Indicates that a Groovy {@code assert} statement uses {@code ,} (rather than {@code :})
26+
* to separate the condition from the message.
27+
*/
28+
@Value
29+
@With
30+
public class AssertMessageComma implements Marker {
31+
UUID id;
32+
}

rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/AssertTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ void withMessage() {
4949
);
5050
}
5151

52+
@Test
53+
void withGStringMessageAfterComma() {
54+
rewriteRun(
55+
groovy(
56+
"""
57+
def x = 1
58+
assert x == 2, "value is ${x}"
59+
"""
60+
)
61+
);
62+
}
63+
5264
@Issue("https://github.com/openrewrite/rewrite/issues/6374")
5365
@Test
5466
void withMessageAndSpaceBeforeColon() {

0 commit comments

Comments
 (0)