Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile error with single explicit assert in switch expression branch (#1845) #2033

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/release_notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@
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)) {
Vampire marked this conversation as resolved.
Show resolved Hide resolved
BlockStatement block = new BlockStatement();
block.addStatement(code);
expr.setCode(block);

Check warning on line 160 in spock-core/src/main/java/org/spockframework/compiler/AbstractDeepBlockRewriter.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L158 - L160 were not covered by tests
Vampire marked this conversation as resolved.
Show resolved Hide resolved
}
doVisitClosureExpression(expr);
} finally {
currClosure = oldClosure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding examples for the other assertions switch variants?

expect:
def b = 3
!!switch (b) {
case 3 -> assert 1 == 1
default -> assert 1 == 1
}
}
}
Loading