OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Test the exit code of dart2js in case of exceptions, errors, warnings, etc. | 5 // Test the exit code of dart2js in case of exceptions, errors, warnings, etc. |
6 | 6 |
7 | |
8 import 'dart:async'; | 7 import 'dart:async'; |
9 import 'dart:io' show Platform; | 8 import 'dart:io' show Platform; |
10 | 9 |
11 import 'package:async_helper/async_helper.dart'; | 10 import 'package:async_helper/async_helper.dart'; |
12 import 'package:expect/expect.dart'; | 11 import 'package:expect/expect.dart'; |
13 | 12 |
14 import 'package:compiler/compiler_new.dart' as api; | 13 import 'package:compiler/compiler_new.dart' as api; |
15 import 'package:compiler/src/common/codegen.dart'; | 14 import 'package:compiler/src/common/codegen.dart'; |
16 import 'package:compiler/src/compile_time_constants.dart'; | 15 import 'package:compiler/src/compile_time_constants.dart'; |
17 import 'package:compiler/src/compiler.dart'; | 16 import 'package:compiler/src/compiler.dart'; |
(...skipping 12 matching lines...) Expand all Loading... |
30 import 'package:compiler/src/scanner/scanner_task.dart'; | 29 import 'package:compiler/src/scanner/scanner_task.dart'; |
31 import 'package:compiler/src/universe/world_impact.dart'; | 30 import 'package:compiler/src/universe/world_impact.dart'; |
32 import 'diagnostic_reporter_helper.dart'; | 31 import 'diagnostic_reporter_helper.dart'; |
33 | 32 |
34 class TestCompiler extends apiimpl.CompilerImpl { | 33 class TestCompiler extends apiimpl.CompilerImpl { |
35 final String testMarker; | 34 final String testMarker; |
36 final String testType; | 35 final String testType; |
37 final Function onTest; | 36 final Function onTest; |
38 TestDiagnosticReporter reporter; | 37 TestDiagnosticReporter reporter; |
39 | 38 |
40 TestCompiler(api.CompilerInput inputProvider, | 39 TestCompiler( |
41 api.CompilerOutput outputProvider, | 40 api.CompilerInput inputProvider, |
42 api.CompilerDiagnostics handler, | 41 api.CompilerOutput outputProvider, |
43 CompilerOptions options, | 42 api.CompilerDiagnostics handler, |
44 String this.testMarker, | 43 CompilerOptions options, |
45 String this.testType, | 44 String this.testMarker, |
46 Function this.onTest) | 45 String this.testType, |
| 46 Function this.onTest) |
47 : reporter = new TestDiagnosticReporter(), | 47 : reporter = new TestDiagnosticReporter(), |
48 super(inputProvider, outputProvider, handler, options) { | 48 super(inputProvider, outputProvider, handler, options) { |
49 reporter.compiler = this; | 49 reporter.compiler = this; |
50 reporter.reporter = super.reporter; | 50 reporter.reporter = super.reporter; |
51 test('Compiler'); | 51 test('Compiler'); |
52 } | 52 } |
53 | 53 |
54 @override | 54 @override |
55 ScannerTask createScannerTask() => new TestScanner(this); | 55 ScannerTask createScannerTask() => new TestScanner(this); |
56 | 56 |
(...skipping 23 matching lines...) Expand all Loading... |
80 } | 80 } |
81 | 81 |
82 WorldImpact codegen(CodegenWorkItem work, Enqueuer world) { | 82 WorldImpact codegen(CodegenWorkItem work, Enqueuer world) { |
83 test('Compiler.codegen'); | 83 test('Compiler.codegen'); |
84 return super.codegen(work, world); | 84 return super.codegen(work, world); |
85 } | 85 } |
86 | 86 |
87 test(String marker) { | 87 test(String marker) { |
88 if (marker == testMarker) { | 88 if (marker == testMarker) { |
89 switch (testType) { | 89 switch (testType) { |
90 case 'assert': | 90 case 'assert': |
91 onTest(testMarker, testType); | 91 onTest(testMarker, testType); |
92 assert(false); | 92 assert(false); |
93 break; | 93 break; |
94 case 'invariant': | 94 case 'invariant': |
95 onTest(testMarker, testType); | 95 onTest(testMarker, testType); |
96 invariant(NO_LOCATION_SPANNABLE, false, message: marker); | 96 invariant(NO_LOCATION_SPANNABLE, false, message: marker); |
97 break; | 97 break; |
98 case 'warning': | 98 case 'warning': |
99 onTest(testMarker, testType); | 99 onTest(testMarker, testType); |
100 reporter.reportWarningMessage( | 100 reporter.reportWarningMessage( |
101 NO_LOCATION_SPANNABLE, | 101 NO_LOCATION_SPANNABLE, MessageKind.GENERIC, {'text': marker}); |
102 MessageKind.GENERIC, {'text': marker}); | 102 break; |
103 break; | 103 case 'error': |
104 case 'error': | 104 onTest(testMarker, testType); |
105 onTest(testMarker, testType); | 105 reporter.reportErrorMessage( |
106 reporter.reportErrorMessage( | 106 NO_LOCATION_SPANNABLE, MessageKind.GENERIC, {'text': marker}); |
107 NO_LOCATION_SPANNABLE, | 107 break; |
108 MessageKind.GENERIC, {'text': marker}); | 108 case 'internalError': |
109 break; | 109 onTest(testMarker, testType); |
110 case 'internalError': | 110 reporter.internalError(NO_LOCATION_SPANNABLE, marker); |
111 onTest(testMarker, testType); | 111 break; |
112 reporter.internalError(NO_LOCATION_SPANNABLE, marker); | 112 case 'NoSuchMethodError': |
113 break; | 113 onTest(testMarker, testType); |
114 case 'NoSuchMethodError': | 114 null.foo; |
115 onTest(testMarker, testType); | 115 break; |
116 null.foo; | 116 case '': |
117 break; | 117 onTest(testMarker, testType); |
118 case '': | 118 break; |
119 onTest(testMarker, testType); | |
120 break; | |
121 } | 119 } |
122 } | 120 } |
123 } | 121 } |
124 } | 122 } |
125 | 123 |
126 class TestDiagnosticReporter extends DiagnosticReporterWrapper { | 124 class TestDiagnosticReporter extends DiagnosticReporterWrapper { |
127 TestCompiler compiler; | 125 TestCompiler compiler; |
128 DiagnosticReporter reporter; | 126 DiagnosticReporter reporter; |
129 | 127 |
130 @override | 128 @override |
(...skipping 16 matching lines...) Expand all Loading... |
147 compiler.test('ScannerTask.scanElements'); | 145 compiler.test('ScannerTask.scanElements'); |
148 super.scanElements(compilationUnit); | 146 super.scanElements(compilationUnit); |
149 } | 147 } |
150 } | 148 } |
151 | 149 |
152 class TestResolver extends ResolverTask { | 150 class TestResolver extends ResolverTask { |
153 final TestCompiler compiler; | 151 final TestCompiler compiler; |
154 | 152 |
155 TestResolver(TestCompiler compiler, ConstantCompiler constantCompiler) | 153 TestResolver(TestCompiler compiler, ConstantCompiler constantCompiler) |
156 : this.compiler = compiler, | 154 : this.compiler = compiler, |
157 super( | 155 super(compiler.resolution, constantCompiler, compiler.world, |
158 compiler.resolution, | |
159 constantCompiler, | |
160 compiler.world, | |
161 compiler.measurer); | 156 compiler.measurer); |
162 | 157 |
163 void computeClassMembers(ClassElement element) { | 158 void computeClassMembers(ClassElement element) { |
164 compiler.test('ResolverTask.computeClassMembers'); | 159 compiler.test('ResolverTask.computeClassMembers'); |
165 super.computeClassMembers(element); | 160 super.computeClassMembers(element); |
166 } | 161 } |
167 } | 162 } |
168 | 163 |
169 int checkedResults = 0; | 164 int checkedResults = 0; |
170 | 165 |
171 Future testExitCode( | 166 Future testExitCode( |
172 String marker, String type, int expectedExitCode, List options) { | 167 String marker, String type, int expectedExitCode, List options) { |
173 bool testOccurred = false; | 168 bool testOccurred = false; |
174 | 169 |
175 void onTest(String testMarker, String testType) { | 170 void onTest(String testMarker, String testType) { |
176 if (testMarker == marker && testType == type) { | 171 if (testMarker == marker && testType == type) { |
177 testOccurred = true; | 172 testOccurred = true; |
178 } | 173 } |
179 } | 174 } |
| 175 |
180 return new Future(() { | 176 return new Future(() { |
181 Future<api.CompilationResult> compile( | 177 Future<api.CompilationResult> compile( |
182 CompilerOptions compilerOptions, | 178 CompilerOptions compilerOptions, |
183 api.CompilerInput compilerInput, | 179 api.CompilerInput compilerInput, |
184 api.CompilerDiagnostics compilerDiagnostics, | 180 api.CompilerDiagnostics compilerDiagnostics, |
185 api.CompilerOutput compilerOutput) { | 181 api.CompilerOutput compilerOutput) { |
186 compilerOutput = const NullCompilerOutput(); | 182 compilerOutput = const NullCompilerOutput(); |
187 // Use this to silence the test when debugging: | 183 // Use this to silence the test when debugging: |
188 // handler = (uri, begin, end, message, kind) {}; | 184 // handler = (uri, begin, end, message, kind) {}; |
189 Compiler compiler = new TestCompiler( | 185 Compiler compiler = new TestCompiler(compilerInput, compilerOutput, |
190 compilerInput, | 186 compilerDiagnostics, compilerOptions, marker, type, onTest); |
191 compilerOutput, | |
192 compilerDiagnostics, | |
193 compilerOptions, | |
194 marker, | |
195 type, | |
196 onTest); | |
197 return compiler.run(compilerOptions.entryPoint).then((bool success) { | 187 return compiler.run(compilerOptions.entryPoint).then((bool success) { |
198 return new api.CompilationResult(compiler, isSuccess: success); | 188 return new api.CompilationResult(compiler, isSuccess: success); |
199 }); | 189 }); |
200 } | 190 } |
201 | 191 |
202 int foundExitCode; | 192 int foundExitCode; |
203 | 193 |
204 checkResult() { | 194 checkResult() { |
205 Expect.isTrue(testOccurred, 'testExitCode($marker, $type) did not occur'); | 195 Expect.isTrue(testOccurred, 'testExitCode($marker, $type) did not occur'); |
206 if (foundExitCode == null) foundExitCode = 0; | 196 if (foundExitCode == null) foundExitCode = 0; |
207 print('testExitCode($marker, $type) ' | 197 print('testExitCode($marker, $type) ' |
208 'exitCode=$foundExitCode expected=$expectedExitCode'); | 198 'exitCode=$foundExitCode expected=$expectedExitCode'); |
209 Expect.equals(expectedExitCode, foundExitCode, | 199 Expect.equals( |
| 200 expectedExitCode, |
| 201 foundExitCode, |
210 'testExitCode($marker, $type) ' | 202 'testExitCode($marker, $type) ' |
211 'exitCode=$foundExitCode expected=${expectedExitCode}'); | 203 'exitCode=$foundExitCode expected=${expectedExitCode}'); |
212 checkedResults++; | 204 checkedResults++; |
213 } | 205 } |
214 | 206 |
215 void exit(exitCode) { | 207 void exit(exitCode) { |
216 if (foundExitCode == null) { | 208 if (foundExitCode == null) { |
217 foundExitCode = exitCode; | 209 foundExitCode = exitCode; |
218 } | 210 } |
219 }; | 211 } |
| 212 |
| 213 ; |
220 | 214 |
221 entry.exitFunc = exit; | 215 entry.exitFunc = exit; |
222 entry.compileFunc = compile; | 216 entry.compileFunc = compile; |
223 | 217 |
224 List<String> args = new List<String>.from(options) | 218 List<String> args = new List<String>.from(options) |
225 ..add("--library-root=${Platform.script.resolve('../../../sdk/')}") | 219 ..add("--library-root=${Platform.script.resolve('../../../sdk/')}") |
226 ..add("tests/compiler/dart2js/data/exit_code_helper.dart"); | 220 ..add("tests/compiler/dart2js/data/exit_code_helper.dart"); |
227 Future result = entry.internalMain(args); | 221 Future result = entry.internalMain(args); |
228 return result.catchError((e, s) { | 222 return result.catchError((e, s) { |
229 // Capture crashes. | 223 // Capture crashes. |
230 }).whenComplete(checkResult); | 224 }).whenComplete(checkResult); |
231 }); | 225 }); |
232 } | 226 } |
233 | 227 |
234 Future testExitCodes( | 228 Future testExitCodes( |
235 String marker, Map<String,int> expectedExitCodes, List<String> options) { | 229 String marker, Map<String, int> expectedExitCodes, List<String> options) { |
236 return Future.forEach(expectedExitCodes.keys, (String type) { | 230 return Future.forEach(expectedExitCodes.keys, (String type) { |
237 return testExitCode(marker, type, expectedExitCodes[type], options); | 231 return testExitCode(marker, type, expectedExitCodes[type], options); |
238 }); | 232 }); |
239 } | 233 } |
240 | 234 |
241 void main() { | 235 void main() { |
242 bool isCheckedMode = false; | 236 bool isCheckedMode = false; |
243 assert((isCheckedMode = true)); | 237 assert((isCheckedMode = true)); |
244 | 238 |
245 Map _expectedExitCode({bool beforeRun: false, bool fatalWarnings: false}) { | 239 Map _expectedExitCode({bool beforeRun: false, bool fatalWarnings: false}) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 'ResolverTask.computeClassMembers': duringRun, | 272 'ResolverTask.computeClassMembers': duringRun, |
279 }; | 273 }; |
280 int totalExpectedErrors = 0; | 274 int totalExpectedErrors = 0; |
281 | 275 |
282 asyncTest(() async { | 276 asyncTest(() async { |
283 for (String marker in tests.keys) { | 277 for (String marker in tests.keys) { |
284 var expected = _expectedExitCode(beforeRun: tests[marker]); | 278 var expected = _expectedExitCode(beforeRun: tests[marker]); |
285 totalExpectedErrors += expected.length; | 279 totalExpectedErrors += expected.length; |
286 await testExitCodes(marker, expected, []); | 280 await testExitCodes(marker, expected, []); |
287 | 281 |
288 expected = _expectedExitCode( | 282 expected = |
289 beforeRun: tests[marker], fatalWarnings: true); | 283 _expectedExitCode(beforeRun: tests[marker], fatalWarnings: true); |
290 totalExpectedErrors += expected.length; | 284 totalExpectedErrors += expected.length; |
291 await testExitCodes(marker, expected, ['--fatal-warnings']); | 285 await testExitCodes(marker, expected, ['--fatal-warnings']); |
292 } | 286 } |
293 | 287 |
294 Expect.equals(totalExpectedErrors, checkedResults); | 288 Expect.equals(totalExpectedErrors, checkedResults); |
295 }); | 289 }); |
296 } | 290 } |
OLD | NEW |