1 /**
   2  * @test /nodynamiccopyright/
   3  * @bug 8206986
   4  * @compile/fail/ref=ExpressionSwitch-old.out -source 9 -Xlint:-options -XDrawDiagnostics ExpressionSwitch.java
   5  * @compile --enable-preview -source 12 ExpressionSwitch.java
   6  * @run main/othervm --enable-preview ExpressionSwitch
   7  */
   8 
   9 import java.util.Objects;
  10 
  11 public class ExpressionSwitch {
  12     public static void main(String... args) {
  13         new ExpressionSwitch().run();
  14     }
  15 
  16     private void run() {
  17         check(T.A, "A");
  18         check(T.B, "B");
  19         check(T.C, "other");
  20         exhaustive1(T.C);
  21     }
  22 
  23     private String print(T t) {
  24         return switch (t) {
  25             case A -> "A";
  26             case B -> { break "B"; }
  27             default -> { break "other"; }
  28         };
  29     }
  30 
  31     private String exhaustive1(T t) {
  32         return switch (t) {
  33             case A -> "A";
  34             case B -> { break "B"; }
  35             case C -> "C";
  36             case D -> "D";
  37         };
  38     }
  39 
  40     private String exhaustive2(T t) {
  41         return switch (t) {
  42             case A -> "A";
  43             case B -> "B";
  44             case C -> "C";
  45             case D -> "D";
  46         };
  47     }
  48 
  49     private void check(T t, String expected) {
  50         String result = print(t);
  51         if (!Objects.equals(result, expected)) {
  52             throw new AssertionError("Unexpected result: " + result);
  53         }
  54     }
  55 
  56     enum T {
  57         A, B, C, D;
  58     }
  59     void t() {
  60         Runnable r = () -> {};
  61         r.run();
  62     }
  63 }