From 01f3c4e4eca97dc4707f9ff9aad703fa3aa04527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kautler?= Date: Thu, 26 Dec 2024 17:05:11 +0100 Subject: [PATCH] Fix compile error with single explicit assert in switch expression branch (#2033) Fixes #1845 --- docs/release_notes.adoc | 3 ++- .../compiler/AbstractDeepBlockRewriter.java | 10 ++++++++++ .../smoke/condition/ConditionG4Spec.groovy | 10 ++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/release_notes.adoc b/docs/release_notes.adoc index 7f78d7aa30..6f3cd71548 100644 --- a/docs/release_notes.adoc +++ b/docs/release_notes.adoc @@ -28,8 +28,9 @@ include::include.adoc[] * Fix mocking issue with the ByteBuddy MockMaker when using multiple classloaders in Java 21 spockIssue:2017[] * Fix mocking of final classes via `@SpringBean` and `@SpringSpy` spockIssue:1960[] * Size of data providers is no longer calculated multiple times but only once -* Fix exception when using `@RepeatUntilFailure` with a data provider with unknown iteration amount. spockPull:2031[] +* Fix exception when using `@RepeatUntilFailure` with a data provider with unknown iteration amount spockPull:2031[] * Clarified documentation about data providers and `size()` calls spockIssue:2022[] +* Fix compile error with single explicit assert in switch expression branch spockIssue:1845[] == 2.4-M4 (2024-03-21) 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..9a8c1ba184 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,16 @@ public final void visitClosureExpression(ClosureExpression expr) { currSpecialMethodCall = NoSpecialMethodCall.INSTANCE; // unrelated closure terminates currSpecialMethodCall scope } try { + Statement code = expr.getCode(); + // the code of a closure is not necessarily a block statement but could for example + // also be a single assert statement in cases like `case 3 -> assert 1 == 1` + // to uniformly treat the closure code as block statement and later be able to + // add statements to it, wrap non-block statements in a block statement here + 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 + } + } }