From bc42a602cd33f01f6a6ebc4bf3ecbf9e5a47738c Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Sat, 11 May 2024 07:59:08 +0100 Subject: [PATCH] Added tests for parsing switch expressions in return statements, and got them passing. --- .../main/java/bluej/parser/JavaParser.java | 3 +- .../java/bluej/parser/BasicParseTest.java | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/bluej/src/main/java/bluej/parser/JavaParser.java b/bluej/src/main/java/bluej/parser/JavaParser.java index 58b2c7ec3..2be22d578 100644 --- a/bluej/src/main/java/bluej/parser/JavaParser.java +++ b/bluej/src/main/java/bluej/parser/JavaParser.java @@ -2899,8 +2899,7 @@ else if (tt1 == JavaTokenTypes.IDENT && tt2 == JavaTokenTypes.COMMA) { case 31: // LITERAL_switch // Switch expression parseSwitchExpression(token); - token = nextToken(); - continue exprLoop; + break; default: tokenStream.pushBack(token); error("Invalid expression token: " + token.getText()); diff --git a/bluej/src/test/java/bluej/parser/BasicParseTest.java b/bluej/src/test/java/bluej/parser/BasicParseTest.java index cfd87c786..d45797e7a 100644 --- a/bluej/src/test/java/bluej/parser/BasicParseTest.java +++ b/bluej/src/test/java/bluej/parser/BasicParseTest.java @@ -853,6 +853,53 @@ class Foo assertFalse(info.hadParseError()); } + @Test + public void testSwitchExpression1() + { + String aSrc = + """ + class Foo + { + public int bar() + { + return switch ("hi") { + case "bye" -> -1; + default -> 0; + }; + } + } + """; + + ClassInfo info = InfoParser.parse(new StringReader(aSrc), new ClassLoaderResolver(getClass().getClassLoader()), null); + assertNotNull(info); + assertFalse(info.hadParseError()); + } + + @Test + public void testSwitchExpression2() + { + String aSrc = + """ + class Foo + { + public int bar() + { + return switch ("hi") { + case "bye" -> -1; + default -> 0; + } + switch ("bye") { + case "hi" -> -1; + default -> 0; + }; + } + } + """; + + ClassInfo info = InfoParser.parse(new StringReader(aSrc), new ClassLoaderResolver(getClass().getClassLoader()), null); + assertNotNull(info); + assertFalse(info.hadParseError()); + } + // Examples mostly from https://openjdk.org/jeps/409 @Test