Skip to content

Commit 01f3c4e

Browse files
authored
Fix compile error with single explicit assert in switch expression branch (#2033)
Fixes #1845
1 parent da17c4d commit 01f3c4e

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

docs/release_notes.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ include::include.adoc[]
2828
* Fix mocking issue with the ByteBuddy MockMaker when using multiple classloaders in Java 21 spockIssue:2017[]
2929
* Fix mocking of final classes via `@SpringBean` and `@SpringSpy` spockIssue:1960[]
3030
* Size of data providers is no longer calculated multiple times but only once
31-
* Fix exception when using `@RepeatUntilFailure` with a data provider with unknown iteration amount. spockPull:2031[]
31+
* Fix exception when using `@RepeatUntilFailure` with a data provider with unknown iteration amount spockPull:2031[]
3232
* Clarified documentation about data providers and `size()` calls spockIssue:2022[]
33+
* Fix compile error with single explicit assert in switch expression branch spockIssue:1845[]
3334

3435
== 2.4-M4 (2024-03-21)
3536

spock-core/src/main/java/org/spockframework/compiler/AbstractDeepBlockRewriter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ public final void visitClosureExpression(ClosureExpression expr) {
149149
currSpecialMethodCall = NoSpecialMethodCall.INSTANCE; // unrelated closure terminates currSpecialMethodCall scope
150150
}
151151
try {
152+
Statement code = expr.getCode();
153+
// the code of a closure is not necessarily a block statement but could for example
154+
// also be a single assert statement in cases like `case 3 -> assert 1 == 1`
155+
// to uniformly treat the closure code as block statement and later be able to
156+
// add statements to it, wrap non-block statements in a block statement here
157+
if (!(code instanceof BlockStatement)) {
158+
BlockStatement block = new BlockStatement();
159+
block.addStatement(code);
160+
expr.setCode(block);
161+
}
152162
doVisitClosureExpression(expr);
153163
} finally {
154164
currClosure = oldClosure;

spock-specs/src/test-groovy-ge-4.0/groovy/org/spockframework/smoke/condition/ConditionG4Spec.groovy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ class ConditionG4Spec extends Specification {
1313
(0..<5) == [0, 1, 2, 3, 4]
1414
(0<..<5) == [1, 2, 3, 4]
1515
}
16+
17+
@Issue("https://github.com/spockframework/spock/issues/1845")
18+
def "explicit assert in switch expression"() {
19+
expect:
20+
def b = 3
21+
!!switch (b) {
22+
case 3 -> assert 1 == 1
23+
default -> assert 1 == 1
24+
}
25+
}
1626
}

0 commit comments

Comments
 (0)