Skip to content

Commit

Permalink
Added tests for parsing switch expressions in return statements, and …
Browse files Browse the repository at this point in the history
…got them passing.
  • Loading branch information
neilccbrown committed May 11, 2024
1 parent 3c275df commit bc42a60
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
3 changes: 1 addition & 2 deletions bluej/src/main/java/bluej/parser/JavaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
47 changes: 47 additions & 0 deletions bluej/src/test/java/bluej/parser/BasicParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bc42a60

Please sign in to comment.