Skip to content

Commit

Permalink
Add tests for nested switch expressions.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 697895020
  • Loading branch information
rluble authored and copybara-github committed Nov 19, 2024
1 parent 6e38366 commit 9635353
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static void main(String... args) {
testSwitchWithErasureCast();
testStringSwitch();
testThrow();
testNestedSwitch();
testAutoboxing();
}

Expand Down Expand Up @@ -211,6 +212,21 @@ class ExceptionForSwitchExpression extends Exception {}
}
}

private static void testNestedSwitch() {
int i = 0;
int j = 1;
int result =
switch (i) {
case 0 ->
switch (j) {
case 0 -> 1;
default -> 2;
};
default -> 3;
};
assertEquals(2, result);
}

private static void testAutoboxing() {
// Having some yields boxed and some unboxed ensures that autoboxing in yield is occurring.
assertTrue(0 == getYieldWithMixedBoxedAndUnboxedValue(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,10 @@ private static void testBasicSwitchExpressions() {

private static void testSwitchExpressionsWithComplexControlFlow() {
int a = 0;
// This it a switch expression featuring all the combinations in the spec per Java 14.
// Note that the different branches of the switch yield different types that are also different
// from the type that is assigned.
long i =
switch (3) {
// Simple case with implicit yield.
case 1 -> 5;
// Case with 2 symbols that just throws.
case 3, 4 -> throw new RuntimeException();
// Case with a block and and explicit yield.
default -> {
Short j = (short) a++;
while (j < 3) {
Expand All @@ -59,4 +53,22 @@ private static void testSwitchExpressionsWithComplexControlFlow() {
}
};
}

private static void testNestedSwitchExpressions() {
int a = 0;
long i =
switch (3) {
// Simple case with implicit yield.
case 1 ->
switch (5) {
case 1 -> 10;
default ->
switch (6) {
case 1 -> throw new RuntimeException();
default -> 5;
};
};
default -> a;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ class SwitchExpression extends j_l_Object {
})();
}
/** @nodts */
static m_testNestedSwitchExpressions__void() {
let a = 0;
let i = (() =>{
switch (3) {
case 1:
return (() =>{
switch (5) {
case 1:
return $Long.fromInt(10);
default:
return (() =>{
switch (6) {
case 1:
throw $Exceptions.toJs(RuntimeException.$create__());
default:
return $Long.fromInt(5);
}
})();
}
})();
default:
return $Primitives.widenIntToLong(a);
}
})();
}
/** @nodts */
static $clinit() {
SwitchExpression.$clinit = () =>{};
SwitchExpression.$loadModules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,38 @@ open class SwitchExpression {
}
}
}

@JvmStatic
private fun testNestedSwitchExpressions() {
val a: Int = 0
val i: Long = run {
when (3) {
1 -> {
return@run run {
when (5) {
1 -> {
return@run 10L
}
else -> {
return@run run {
when (6) {
1 -> {
throw RuntimeException()
}
else -> {
return@run 5L
}
}
}
}
}
}
}
else -> {
return@run a.toLong()
}
}
}
}
}
}

0 comments on commit 9635353

Please sign in to comment.