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();"; 62 63 // containers that do not require exhaustiveness 64 private static final List<String> CONTAINERS 65 = List.of(RUNNABLE, FOR, WHILE, DO, SSWITCH, IF, BLOCK); 66 // containers that do not require exhaustiveness that are statements 67 private static final List<String> CONTAINER_STATEMENTS 68 = List.of(FOR, WHILE, DO, SSWITCH, IF, BLOCK); 69 70 @AfterMethod 71 public void dumpTemplateIfError(ITestResult result) { 72 // Make sure offending template ends up in log file on failure 73 if (!result.isSuccess()) { 74 System.err.printf("Diagnostics: %s%nTemplate: %s%n", diags.errorKeys(), sourceFiles.stream().map(p -> p.snd).collect(toList())); 75 } 76 } 77 78 private void program(String... constructs) { 79 String s = "class C { static boolean cond = false; static int x = 0; void m() { # } }"; 80 for (String c : constructs) 81 s = s.replace("#", c); 82 addSourceFile("C.java", new StringTemplate(s)); 83 } 84 85 private void assertOK(String... constructs) { 86 reset(); 87 addCompileOptions("--enable-preview", "-source", "12"); 88 program(constructs); 89 try { 90 compile(); 91 } 92 catch (IOException e) { 93 throw new RuntimeException(e); 94 } 95 assertCompileSucceeded(); 96 } 97 98 private void assertOKWithWarning(String warning, String... constructs) { 99 reset(); 100 addCompileOptions("--enable-preview", "-source", "12"); 101 program(constructs); 102 try { 103 compile(); 104 } 105 catch (IOException e) { 106 throw new RuntimeException(e); 107 } 108 assertCompileSucceededWithWarning(warning); 109 } 110 111 private void assertFail(String expectedDiag, String... constructs) { 112 reset(); 113 addCompileOptions("--enable-preview", "-source", "12"); 114 program(constructs); 115 try { 116 compile(); 117 } 118 catch (IOException e) { 119 throw new RuntimeException(e); 120 } 121 assertCompileFailed(expectedDiag); 122 } 123 124 public void testReallySimpleCases() { 125 for (String s : CONTAINERS) 126 assertOK(s, NOTHING); 127 for (String s : CONTAINER_STATEMENTS) 128 assertOK(LABEL, s, NOTHING); 129 } 130 131 public void testLambda() { 132 assertOK(RUNNABLE, RETURN_N); 133 assertOK(RUNNABLE, NOTHING); | 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();"; 62 63 // containers that do not require exhaustiveness 64 private static final List<String> CONTAINERS 65 = List.of(RUNNABLE, FOR, WHILE, DO, SSWITCH, IF, BLOCK); 66 // containers that do not require exhaustiveness that are statements 67 private static final List<String> CONTAINER_STATEMENTS 68 = List.of(FOR, WHILE, DO, SSWITCH, IF, BLOCK); 69 70 @AfterMethod 71 public void dumpTemplateIfError(ITestResult result) { 72 // Make sure offending template ends up in log file on failure 73 if (!result.isSuccess()) { 74 System.err.printf("Diagnostics: %s%nTemplate: %s%n", diags.errorKeys(), sourceFiles.stream().map(p -> p.snd).collect(toList())); 75 } 76 } 77 78 private static String[] PREVIEW_OPTIONS = {"--enable-preview", "-source", "13"}; 79 80 private void program(String... constructs) { 81 String s = "class C { static boolean cond = false; static int x = 0; void m() { # } }"; 82 for (String c : constructs) 83 s = s.replace("#", c); 84 addSourceFile("C.java", new StringTemplate(s)); 85 } 86 87 private void assertOK(String... constructs) { 88 reset(); 89 addCompileOptions(PREVIEW_OPTIONS); 90 program(constructs); 91 try { 92 compile(); 93 } 94 catch (IOException e) { 95 throw new RuntimeException(e); 96 } 97 assertCompileSucceeded(); 98 } 99 100 private void assertOKWithWarning(String warning, String... constructs) { 101 reset(); 102 addCompileOptions(PREVIEW_OPTIONS); 103 program(constructs); 104 try { 105 compile(); 106 } 107 catch (IOException e) { 108 throw new RuntimeException(e); 109 } 110 assertCompileSucceededWithWarning(warning); 111 } 112 113 private void assertFail(String expectedDiag, String... constructs) { 114 reset(); 115 addCompileOptions(PREVIEW_OPTIONS); 116 program(constructs); 117 try { 118 compile(); 119 } 120 catch (IOException e) { 121 throw new RuntimeException(e); 122 } 123 assertCompileFailed(expectedDiag); 124 } 125 126 public void testReallySimpleCases() { 127 for (String s : CONTAINERS) 128 assertOK(s, NOTHING); 129 for (String s : CONTAINER_STATEMENTS) 130 assertOK(LABEL, s, NOTHING); 131 } 132 133 public void testLambda() { 134 assertOK(RUNNABLE, RETURN_N); 135 assertOK(RUNNABLE, NOTHING); |