test/langtools/tools/javac/expswitch/ExpSwitchNestingTest.java
author jlahoda
Tue, 12 Nov 2019 06:32:13 +0000
changeset 59021 cfc7bb9a5a92
parent 55306 ea43db53de91
permissions -rw-r--r--
8232684: Make switch expressions final Reviewed-by: alanb, mcimadamore, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     1
/*
53878
4584d0331318 8219254: Update explicit uses of latest source/target in langtools tests to a property
darcy
parents: 53023
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     4
 *
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     8
 *
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    14
 *
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    18
 *
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    21
 * questions.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    22
 */
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    23
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    24
import java.io.IOException;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    25
import java.util.List;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    26
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    27
import org.testng.ITestResult;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    28
import org.testng.annotations.AfterMethod;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    29
import org.testng.annotations.Test;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    30
import tools.javac.combo.JavacTemplateTestBase;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    31
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    32
import static java.util.stream.Collectors.toList;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    33
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    34
@Test
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    35
public class ExpSwitchNestingTest extends JavacTemplateTestBase {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    36
    private static final String RUNNABLE = "Runnable r = () -> { # };";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    37
    private static final String INT_FN = "java.util.function.IntSupplier r = () -> { # };";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    38
    private static final String LABEL = "label: #";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    39
    private static final String DEF_LABEL_VAR = "int label = 0; { # }";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    40
    private static final String FOR = "for (int i=0; i<10; i++) { # }";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    41
    private static final String FOR_EACH = "for (int i : new int[] {}) { # }";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    42
    private static final String WHILE = "while (cond) { # }";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    43
    private static final String DO = "do { # } while (cond);";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    44
    private static final String SSWITCH = "switch (x) { case 0: # };";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    45
    private static final String ESWITCH_Z = "int res = switch (x) { case 0 -> { # } default -> 0; };";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    46
    private static final String ESWITCH_S = "String res_string = switch (x) { case 0 -> { # } default -> \"default\"; };";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    47
    private static final String INT_FN_ESWITCH = "java.util.function.IntSupplier r = switch (x) { case 0 -> { # } default -> null; };";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    48
    private static final String INT_ESWITCH_DEFAULT = "int res = switch (x) { default -> { # } };";
52635
6938c8ef179a 8212982: Rule cases in switch expression accepted even if complete normally
jlahoda
parents: 51563
diff changeset
    49
    private static final String IF = "if (cond) { # } else throw new RuntimeException();";
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    50
    private static final String BLOCK = "{ # }";
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
    51
    private static final String YIELD_Z = "yield 0;";
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
    52
    private static final String YIELD_S = "yield \"hello world\";";
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
    53
    private static final String YIELD_INT_FN = "yield () -> 0 ;";
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    54
    private static final String BREAK_N = "break;";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    55
    private static final String BREAK_L = "break label;";
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
    56
    private static final String YIELD_L = "yield label;";
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    57
    private static final String RETURN_Z = "return 0;";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    58
    private static final String RETURN_N = "return;";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    59
    private static final String RETURN_S = "return \"Hello\";";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    60
    private static final String CONTINUE_N = "continue;";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    61
    private static final String CONTINUE_L = "continue label;";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    62
    private static final String NOTHING = "System.out.println();";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    63
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    64
    // containers that do not require exhaustiveness
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    65
    private static final List<String> CONTAINERS
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    66
            = List.of(RUNNABLE, FOR, WHILE, DO, SSWITCH, IF, BLOCK);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    67
    // containers that do not require exhaustiveness that are statements
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    68
    private static final List<String> CONTAINER_STATEMENTS
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    69
            = List.of(FOR, WHILE, DO, SSWITCH, IF, BLOCK);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    70
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    71
    @AfterMethod
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    72
    public void dumpTemplateIfError(ITestResult result) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    73
        // Make sure offending template ends up in log file on failure
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    74
        if (!result.isSuccess()) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    75
            System.err.printf("Diagnostics: %s%nTemplate: %s%n", diags.errorKeys(), sourceFiles.stream().map(p -> p.snd).collect(toList()));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    76
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    77
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    78
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    79
    private void program(String... constructs) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    80
        String s = "class C { static boolean cond = false; static int x = 0; void m() { # } }";
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    81
        for (String c : constructs)
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    82
            s = s.replace("#", c);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    83
        addSourceFile("C.java", new StringTemplate(s));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    84
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    85
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    86
    private void assertOK(String... constructs) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    87
        reset();
59021
cfc7bb9a5a92 8232684: Make switch expressions final
jlahoda
parents: 55306
diff changeset
    88
        addCompileOptions();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    89
        program(constructs);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    90
        try {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    91
            compile();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    92
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    93
        catch (IOException e) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    94
            throw new RuntimeException(e);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    95
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    96
        assertCompileSucceeded();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    97
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    98
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    99
    private void assertOKWithWarning(String warning, String... constructs) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   100
        reset();
59021
cfc7bb9a5a92 8232684: Make switch expressions final
jlahoda
parents: 55306
diff changeset
   101
        addCompileOptions();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   102
        program(constructs);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   103
        try {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   104
            compile();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   105
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   106
        catch (IOException e) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   107
            throw new RuntimeException(e);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   108
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   109
        assertCompileSucceededWithWarning(warning);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   110
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   111
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   112
    private void assertFail(String expectedDiag, String... constructs) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   113
        reset();
59021
cfc7bb9a5a92 8232684: Make switch expressions final
jlahoda
parents: 55306
diff changeset
   114
        addCompileOptions();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   115
        program(constructs);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   116
        try {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   117
            compile();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   118
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   119
        catch (IOException e) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   120
            throw new RuntimeException(e);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   121
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   122
        assertCompileFailed(expectedDiag);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   123
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   124
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   125
    public void testReallySimpleCases() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   126
        for (String s : CONTAINERS)
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   127
            assertOK(s, NOTHING);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   128
        for (String s : CONTAINER_STATEMENTS)
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   129
            assertOK(LABEL, s, NOTHING);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   130
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   131
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   132
    public void testLambda() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   133
        assertOK(RUNNABLE, RETURN_N);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   134
        assertOK(RUNNABLE, NOTHING);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   135
        assertOK(INT_FN, RETURN_Z);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   136
        assertFail("compiler.err.break.outside.switch.loop", RUNNABLE, BREAK_N);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   137
        assertFail("compiler.err.no.switch.expression", RUNNABLE, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   138
        assertFail("compiler.err.no.switch.expression", RUNNABLE, YIELD_S);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   139
        assertFail("compiler.err.break.outside.switch.loop", INT_FN, BREAK_N);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   140
        assertFail("compiler.err.no.switch.expression", INT_FN, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   141
        assertFail("compiler.err.no.switch.expression", INT_FN, YIELD_S);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   142
        assertFail("compiler.err.cont.outside.loop", RUNNABLE, CONTINUE_N);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   143
        assertFail("compiler.err.undef.label", RUNNABLE, BREAK_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   144
        assertFail("compiler.err.undef.label", RUNNABLE, CONTINUE_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   145
        assertFail("compiler.err.cont.outside.loop", INT_FN, CONTINUE_N);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   146
        assertFail("compiler.err.undef.label", INT_FN, BREAK_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   147
        assertFail("compiler.err.undef.label", INT_FN, CONTINUE_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   148
        assertFail("compiler.err.undef.label", LABEL, BLOCK, RUNNABLE, BREAK_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   149
        assertFail("compiler.err.undef.label", LABEL, BLOCK, RUNNABLE, CONTINUE_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   150
        assertFail("compiler.err.undef.label", LABEL, BLOCK, INT_FN, BREAK_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   151
        assertFail("compiler.err.undef.label", LABEL, BLOCK, INT_FN, CONTINUE_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   152
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   153
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   154
    public void testEswitch() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   155
        //Int-valued switch expressions
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   156
        assertOK(ESWITCH_Z, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   157
        assertOK(LABEL, BLOCK, ESWITCH_Z, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   158
        assertFail("compiler.err.break.outside.switch.expression", ESWITCH_Z, BREAK_N);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   159
        assertFail("compiler.err.prob.found.req", ESWITCH_Z, YIELD_S);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   160
        assertFail("compiler.err.undef.label", ESWITCH_Z, BREAK_L);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   161
        assertFail("compiler.err.break.outside.switch.expression", LABEL, BLOCK, ESWITCH_Z, BREAK_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   162
        assertFail("compiler.err.undef.label", ESWITCH_Z, CONTINUE_L);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   163
        assertFail("compiler.err.continue.outside.switch.expression", ESWITCH_Z, CONTINUE_N);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   164
        assertFail("compiler.err.return.outside.switch.expression", ESWITCH_Z, RETURN_N);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   165
        assertFail("compiler.err.return.outside.switch.expression", ESWITCH_Z, RETURN_Z);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   166
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   167
        assertOK(INT_ESWITCH_DEFAULT, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   168
        assertFail("compiler.err.break.outside.switch.expression", INT_ESWITCH_DEFAULT, BREAK_N);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   169
        assertFail("compiler.err.prob.found.req", INT_ESWITCH_DEFAULT, YIELD_S);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   170
        assertFail("compiler.err.undef.label", INT_ESWITCH_DEFAULT, BREAK_L);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   171
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   172
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   173
        // String-valued switch expressions
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   174
        assertOK(ESWITCH_S, YIELD_S);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   175
        assertOK(LABEL, BLOCK, ESWITCH_S, YIELD_S);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   176
        assertFail("compiler.err.break.outside.switch.expression", ESWITCH_S, BREAK_N);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   177
        assertFail("compiler.err.prob.found.req", ESWITCH_S, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   178
        assertFail("compiler.err.undef.label", ESWITCH_S, BREAK_L);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   179
        assertFail("compiler.err.break.outside.switch.expression", LABEL, BLOCK, ESWITCH_S, BREAK_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   180
        assertFail("compiler.err.undef.label", ESWITCH_S, CONTINUE_L);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   181
        assertFail("compiler.err.continue.outside.switch.expression", ESWITCH_S, CONTINUE_N);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   182
        assertFail("compiler.err.return.outside.switch.expression", ESWITCH_S, RETURN_N);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   183
        assertFail("compiler.err.return.outside.switch.expression", ESWITCH_S, RETURN_S);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   184
        // Function-valued switch expression
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   185
        assertOK(INT_FN_ESWITCH, YIELD_INT_FN);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   186
        assertFail("compiler.err.break.outside.switch.expression", INT_FN_ESWITCH, BREAK_N);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   187
        assertFail("compiler.err.prob.found.req", INT_FN_ESWITCH, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   188
        assertFail("compiler.err.prob.found.req", INT_FN_ESWITCH, YIELD_S);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   189
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   190
        assertFail("compiler.err.undef.label", INT_FN_ESWITCH, BREAK_L);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   191
        assertFail("compiler.err.break.outside.switch.expression", LABEL, BLOCK, INT_FN_ESWITCH, BREAK_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   192
        assertFail("compiler.err.undef.label", INT_FN_ESWITCH, CONTINUE_L);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   193
        assertFail("compiler.err.continue.outside.switch.expression", INT_FN_ESWITCH, CONTINUE_N);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   194
        assertFail("compiler.err.return.outside.switch.expression", INT_FN_ESWITCH, RETURN_N);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   195
        assertFail("compiler.err.return.outside.switch.expression", INT_FN_ESWITCH, RETURN_S);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   196
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   197
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   198
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   199
    public void testNestedInExpSwitch() {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   200
        assertOK(ESWITCH_Z, IF,     YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   201
        assertOK(ESWITCH_Z, BLOCK,  YIELD_Z);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   202
        //
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   203
        assertOK(ESWITCH_Z, IF,     IF,     YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   204
        assertOK(ESWITCH_Z, IF,     BLOCK,  YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   205
        assertOK(ESWITCH_Z, BLOCK,  IF,     YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   206
        assertOK(ESWITCH_Z, BLOCK,  BLOCK,  YIELD_Z);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   207
        //
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   208
        assertOK(ESWITCH_Z, IF,     IF,     IF,     YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   209
        assertOK(ESWITCH_Z, IF,     IF,     BLOCK,  YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   210
        assertOK(ESWITCH_Z, IF,     BLOCK,  IF,     YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   211
        assertOK(ESWITCH_Z, IF,     BLOCK,  BLOCK,  YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   212
        assertOK(ESWITCH_Z, BLOCK,  IF,     IF,     YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   213
        assertOK(ESWITCH_Z, BLOCK,  IF,     BLOCK,  YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   214
        assertOK(ESWITCH_Z, BLOCK,  BLOCK,  IF,     YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   215
        assertOK(ESWITCH_Z, BLOCK,  BLOCK,  BLOCK,  YIELD_Z);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   216
        //
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   217
        assertOK(ESWITCH_Z, YIELD_Z, SSWITCH, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   218
        assertOK(ESWITCH_Z, YIELD_Z, FOR, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   219
        assertOK(ESWITCH_Z, YIELD_Z, WHILE, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   220
        assertOK(ESWITCH_Z, YIELD_Z, DO, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   221
        assertFail("compiler.err.no.switch.expression", ESWITCH_Z, INT_FN, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   222
        assertOK(ESWITCH_Z, YIELD_Z, SSWITCH, IF, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   223
        assertOK(ESWITCH_Z, YIELD_Z, FOR, IF, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   224
        assertOK(ESWITCH_Z, YIELD_Z, WHILE, IF, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   225
        assertOK(ESWITCH_Z, YIELD_Z, DO, IF, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   226
        assertOK(ESWITCH_Z, YIELD_Z, BLOCK, SSWITCH, IF, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   227
        assertOK(ESWITCH_Z, YIELD_Z, BLOCK, FOR, IF, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   228
        assertOK(ESWITCH_Z, YIELD_Z, BLOCK, WHILE, IF, YIELD_Z);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   229
        assertOK(ESWITCH_Z, YIELD_Z, BLOCK, DO, IF, YIELD_Z);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   230
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   231
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   232
    public void testBreakExpressionLabelDisambiguation() {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   233
        assertOK(DEF_LABEL_VAR, ESWITCH_Z, YIELD_L);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   234
        assertFail("compiler.err.undef.label", DEF_LABEL_VAR, ESWITCH_Z, BREAK_L);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   235
        assertFail("compiler.err.break.outside.switch.expression", LABEL, FOR, BLOCK, DEF_LABEL_VAR, ESWITCH_Z, BREAK_L);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   236
        assertOK(DEF_LABEL_VAR, ESWITCH_Z, YIELD_Z, LABEL, FOR, BREAK_L); //label break
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   237
        assertFail("compiler.err.break.outside.switch.expression", DEF_LABEL_VAR, LABEL, BLOCK, ESWITCH_Z, BREAK_L);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   238
        assertOK(DEF_LABEL_VAR, LABEL, BLOCK, ESWITCH_Z, YIELD_L); //expression break
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   239
        //
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   240
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   241
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   242
    public void testFunReturningSwitchExp() {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
   243
        assertOK(INT_FN_ESWITCH, YIELD_INT_FN);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   244
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   245
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   246
    public void testContinueLoops() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   247
        assertOK(LABEL, FOR, CONTINUE_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   248
        assertOK(LABEL, FOR_EACH, CONTINUE_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   249
        assertOK(LABEL, WHILE, CONTINUE_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   250
        assertOK(LABEL, DO, CONTINUE_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   251
        assertFail("compiler.err.not.loop.label", LABEL, CONTINUE_L);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   252
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   253
}