diff --git a/spock-core/src/main/java/org/spockframework/compiler/AbstractDeepBlockRewriter.java b/spock-core/src/main/java/org/spockframework/compiler/AbstractDeepBlockRewriter.java index f4129615f3..f1cc5f13a1 100644 --- a/spock-core/src/main/java/org/spockframework/compiler/AbstractDeepBlockRewriter.java +++ b/spock-core/src/main/java/org/spockframework/compiler/AbstractDeepBlockRewriter.java @@ -149,6 +149,12 @@ public final void visitClosureExpression(ClosureExpression expr) { currSpecialMethodCall = NoSpecialMethodCall.INSTANCE; // unrelated closure terminates currSpecialMethodCall scope } try { + Statement code = expr.getCode(); + if (!(code instanceof BlockStatement)) { + BlockStatement block = new BlockStatement(); + block.addStatement(code); + expr.setCode(block); + } doVisitClosureExpression(expr); } finally { currClosure = oldClosure; diff --git a/spock-specs/src/test-groovy-ge-4.0/groovy/org/spockframework/smoke/condition/ConditionG4Spec.groovy b/spock-specs/src/test-groovy-ge-4.0/groovy/org/spockframework/smoke/condition/ConditionG4Spec.groovy index b598d8a54e..71a228a4cc 100644 --- a/spock-specs/src/test-groovy-ge-4.0/groovy/org/spockframework/smoke/condition/ConditionG4Spec.groovy +++ b/spock-specs/src/test-groovy-ge-4.0/groovy/org/spockframework/smoke/condition/ConditionG4Spec.groovy @@ -13,4 +13,14 @@ class ConditionG4Spec extends Specification { (0..<5) == [0, 1, 2, 3, 4] (0<..<5) == [1, 2, 3, 4] } + + @Issue("https://github.com/spockframework/spock/issues/1845") + def "explicit assert in switch expression"() { + expect: + def b = 3 + !!switch (b) { + case 3 -> assert 1 == 1 + default -> assert 1 == 1 + } + } }