1 /*
   2  * @test /nodynamiccopyright/
   3  * @bug 8206986
   4  * @summary Verify cases with multiple labels work properly.
   5  * @compile/fail/ref=MultipleLabelsExpression-old.out -source 9 -Xlint:-options -XDrawDiagnostics MultipleLabelsExpression.java
   6  * @compile --enable-preview -source 12 MultipleLabelsExpression.java
   7  * @run main/othervm --enable-preview MultipleLabelsExpression
   8  */
   9 
  10 import java.util.Objects;
  11 import java.util.function.Function;
  12 
  13 public class MultipleLabelsExpression {
  14     public static void main(String... args) {
  15         new MultipleLabelsExpression().run();
  16     }
  17 
  18     private void run() {
  19         runTest(this::expression1);
  20     }
  21 
  22     private void runTest(Function<T, String> print) {
  23         check(T.A,  print, "A");
  24         check(T.B,  print, "B-C");
  25         check(T.C,  print, "B-C");
  26         check(T.D,  print, "D");
  27         check(T.E,  print, "other");
  28     }
  29 
  30     private String expression1(T t) {
  31         return switch (t) {
  32             case A -> "A";
  33             case B, C -> { break "B-C"; }
  34             case D -> "D";
  35             default -> "other";
  36         };
  37     }
  38 
  39     private void check(T t, Function<T, String> print, String expected) {
  40         String result = print.apply(t);
  41         if (!Objects.equals(result, expected)) {
  42             throw new AssertionError("Unexpected result: " + result);
  43         }
  44     }
  45 
  46     enum T {
  47         A, B, C, D, E;
  48     }
  49 }