21 * questions.
22 */
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import org.testng.ITestResult;
28 import org.testng.annotations.AfterMethod;
29 import org.testng.annotations.Test;
30 import tools.javac.combo.JavacTemplateTestBase;
31
32 import static java.util.stream.Collectors.toList;
33
34 @Test
35 public class ExpSwitchNestingTest extends JavacTemplateTestBase {
36 private static final String RUNNABLE = "Runnable r = () -> { # };";
37 private static final String INT_FN = "java.util.function.IntSupplier r = () -> { # };";
38 private static final String LABEL = "label: #";
39 private static final String DEF_LABEL_VAR = "int label = 0; { # }";
40 private static final String FOR = "for (int i=0; i<10; i++) { # }";
41 private static final String WHILE = "while (cond) { # }";
42 private static final String DO = "do { # } while (cond);";
43 private static final String SSWITCH = "switch (x) { case 0: # };";
44 private static final String ESWITCH_Z = "int res = switch (x) { case 0 -> { # } default -> 0; };";
45 private static final String ESWITCH_S = "String res_string = switch (x) { case 0 -> { # } default -> \"default\"; };";
46 private static final String INT_FN_ESWITCH = "java.util.function.IntSupplier r = switch (x) { case 0 -> { # } default -> null; };";
47 private static final String INT_ESWITCH_DEFAULT = "int res = switch (x) { default -> { # } };";
48 private static final String IF = "if (cond) { # }";
49 private static final String BLOCK = "{ # }";
50 private static final String BREAK_Z = "break 0;";
51 private static final String BREAK_S = "break \"hello world\";";
52 private static final String BREAK_INT_FN = "break () -> 0 ;";
53 private static final String BREAK_N = "break;";
54 private static final String BREAK_L = "break label;";
55 private static final String RETURN_Z = "return 0;";
56 private static final String RETURN_N = "return;";
57 private static final String RETURN_S = "return \"Hello\";";
58 private static final String CONTINUE_N = "continue;";
59 private static final String CONTINUE_L = "continue label;";
60 private static final String NOTHING = "System.out.println();";
220 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, SSWITCH, IF, BREAK_Z);
221 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, FOR, IF, BREAK_Z);
222 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, WHILE, IF, BREAK_Z);
223 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, DO, IF, BREAK_Z);
224 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, SSWITCH, IF, BREAK_Z);
225 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, FOR, IF, BREAK_Z);
226 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, WHILE, IF, BREAK_Z);
227 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, DO, IF, BREAK_Z);
228 }
229
230 public void testBreakExpressionLabelDisambiguation() {
231 assertOK(DEF_LABEL_VAR, ESWITCH_Z, BREAK_L);
232 assertFail("compiler.err.break.ambiguous.target", LABEL, FOR, BLOCK, DEF_LABEL_VAR, ESWITCH_Z, BREAK_L);
233 assertFail("compiler.err.break.ambiguous.target", DEF_LABEL_VAR, ESWITCH_Z, LABEL, FOR, BREAK_L); //label break
234 assertFail("compiler.err.break.ambiguous.target", DEF_LABEL_VAR, LABEL, BLOCK, ESWITCH_Z, BREAK_L); //expression break
235 //
236 }
237
238 public void testFunReturningSwitchExp() {
239 assertOK(INT_FN_ESWITCH, BREAK_INT_FN);
240 }
241 }
|
21 * questions.
22 */
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import org.testng.ITestResult;
28 import org.testng.annotations.AfterMethod;
29 import org.testng.annotations.Test;
30 import tools.javac.combo.JavacTemplateTestBase;
31
32 import static java.util.stream.Collectors.toList;
33
34 @Test
35 public class ExpSwitchNestingTest extends JavacTemplateTestBase {
36 private static final String RUNNABLE = "Runnable r = () -> { # };";
37 private static final String INT_FN = "java.util.function.IntSupplier r = () -> { # };";
38 private static final String LABEL = "label: #";
39 private static final String DEF_LABEL_VAR = "int label = 0; { # }";
40 private static final String FOR = "for (int i=0; i<10; i++) { # }";
41 private static final String FOR_EACH = "for (int i : new int[] {}) { # }";
42 private static final String WHILE = "while (cond) { # }";
43 private static final String DO = "do { # } while (cond);";
44 private static final String SSWITCH = "switch (x) { case 0: # };";
45 private static final String ESWITCH_Z = "int res = switch (x) { case 0 -> { # } default -> 0; };";
46 private static final String ESWITCH_S = "String res_string = switch (x) { case 0 -> { # } default -> \"default\"; };";
47 private static final String INT_FN_ESWITCH = "java.util.function.IntSupplier r = switch (x) { case 0 -> { # } default -> null; };";
48 private static final String INT_ESWITCH_DEFAULT = "int res = switch (x) { default -> { # } };";
49 private static final String IF = "if (cond) { # }";
50 private static final String BLOCK = "{ # }";
51 private static final String BREAK_Z = "break 0;";
52 private static final String BREAK_S = "break \"hello world\";";
53 private static final String BREAK_INT_FN = "break () -> 0 ;";
54 private static final String BREAK_N = "break;";
55 private static final String BREAK_L = "break label;";
56 private static final String RETURN_Z = "return 0;";
57 private static final String RETURN_N = "return;";
58 private static final String RETURN_S = "return \"Hello\";";
59 private static final String CONTINUE_N = "continue;";
60 private static final String CONTINUE_L = "continue label;";
61 private static final String NOTHING = "System.out.println();";
221 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, SSWITCH, IF, BREAK_Z);
222 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, FOR, IF, BREAK_Z);
223 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, WHILE, IF, BREAK_Z);
224 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, DO, IF, BREAK_Z);
225 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, SSWITCH, IF, BREAK_Z);
226 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, FOR, IF, BREAK_Z);
227 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, WHILE, IF, BREAK_Z);
228 assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, DO, IF, BREAK_Z);
229 }
230
231 public void testBreakExpressionLabelDisambiguation() {
232 assertOK(DEF_LABEL_VAR, ESWITCH_Z, BREAK_L);
233 assertFail("compiler.err.break.ambiguous.target", LABEL, FOR, BLOCK, DEF_LABEL_VAR, ESWITCH_Z, BREAK_L);
234 assertFail("compiler.err.break.ambiguous.target", DEF_LABEL_VAR, ESWITCH_Z, LABEL, FOR, BREAK_L); //label break
235 assertFail("compiler.err.break.ambiguous.target", DEF_LABEL_VAR, LABEL, BLOCK, ESWITCH_Z, BREAK_L); //expression break
236 //
237 }
238
239 public void testFunReturningSwitchExp() {
240 assertOK(INT_FN_ESWITCH, BREAK_INT_FN);
241 }
242
243 public void testContinueLoops() {
244 assertOK(LABEL, FOR, CONTINUE_L);
245 assertOK(LABEL, FOR_EACH, CONTINUE_L);
246 assertOK(LABEL, WHILE, CONTINUE_L);
247 assertOK(LABEL, DO, CONTINUE_L);
248 assertFail("compiler.err.not.loop.label", LABEL, CONTINUE_L);
249 }
250 }
|