Skip to content

Commit

Permalink
Closes #72: Switch case action supports if/unless expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
jaccomoc committed Dec 12, 2024
1 parent c6a644d commit 5ecf45a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/io/jactl/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2605,7 +2605,7 @@ Expr.Switch switchExpr() {
patterns = switchPatterns();
}
expect(ARROW);
Expr expr = expression();
Expr expr = ifUnlessExpr(false);
if (expr instanceof Expr.Closure) {
expr = convertClosureToBlockExpr(expr);
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/io/jactl/SwitchTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public class SwitchTests extends BaseTest {
test("def it = [1,2,3] as int[]; switch{ [1,2,3] -> 2; default -> 0 }", 2);
test("def it = [1,2,3] as int[]; switch\n{\n [\n1,\n2,\n3\n]\n ->\n 2\ndefault\n ->\n 0\n }", 2);
testError("int x = 2\nswitch (x) {\n 1,2 -> x\n 'abc' -> x\n}", "can never match type string");
test("def x = [1,2,3]\nswitch (x) {\n [a,b,c] -> a if a < 3\n}", 1);
test("def it = [1,2,3] as int[]; switch{ [1,2,3] -> 2 if it.size() > 2; default -> 0 }", 2);
test("def it = [1,2,3] as int[]; switch{ [1,2,3] -> 2 unless it.size() < 2; default -> 0 }", 2);
}

@Test public void switchLiterals() {
Expand Down

0 comments on commit 5ecf45a

Please sign in to comment.