| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 that the compiler can generates code with compile time error according | 5 // Test that the compiler can generates code with compile time error according |
| 6 // to the compiler options. | 6 // to the compiler options. |
| 7 | 7 |
| 8 library dart2js.test.generate_code_with_compile_time_errors; | 8 library dart2js.test.generate_code_with_compile_time_errors; |
| 9 | 9 |
| 10 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
| 11 import 'package:async_helper/async_helper.dart'; | 11 import 'package:async_helper/async_helper.dart'; |
| 12 import 'package:compiler/src/compiler.dart'; | 12 import 'package:compiler/src/compiler.dart'; |
| 13 import 'package:compiler/src/js_backend/js_backend.dart'; | 13 import 'package:compiler/src/js_backend/js_backend.dart'; |
| 14 import 'memory_compiler.dart'; | 14 import 'memory_compiler.dart'; |
| 15 import 'output_collector.dart'; | 15 import 'output_collector.dart'; |
| 16 | 16 |
| 17 const MEMORY_SOURCE_FILES = const { | 17 const MEMORY_SOURCE_FILES = const { |
| 18 'main.dart': ''' | 18 'main.dart': ''' |
| 19 foo() { | 19 foo() { |
| 20 const list = []; | 20 const list = []; |
| 21 } | 21 } |
| 22 | 22 |
| 23 main() { | 23 main() { |
| 24 foo(); | 24 foo(); |
| 25 } | 25 } |
| 26 ''', | 26 ''', |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 test(List<String> options, | 29 test(List<String> options, |
| 30 {bool expectedOutput, | 30 {bool expectedOutput, |
| 31 bool expectedCodeGenerated, | 31 bool expectedCodeGenerated, |
| 32 bool expectHint: false}) async { | 32 bool expectHint: false}) async { |
| 33 DiagnosticCollector collector = new DiagnosticCollector(); | 33 DiagnosticCollector collector = new DiagnosticCollector(); |
| 34 OutputCollector outputCollector = new OutputCollector(); | 34 OutputCollector outputCollector = new OutputCollector(); |
| 35 CompilationResult result = await runCompiler( | 35 CompilationResult result = await runCompiler( |
| 36 memorySourceFiles: MEMORY_SOURCE_FILES, | 36 memorySourceFiles: MEMORY_SOURCE_FILES, |
| 37 diagnosticHandler: collector, | 37 diagnosticHandler: collector, |
| 38 outputProvider: outputCollector, | 38 outputProvider: outputCollector, |
| 39 options: options); | 39 options: options); |
| 40 Compiler compiler = result.compiler; | 40 Compiler compiler = result.compiler; |
| 41 Expect.isFalse( | 41 Expect.isFalse(result.isSuccess, "Expected compilation failure."); |
| 42 result.isSuccess, | |
| 43 "Expected compilation failure."); | |
| 44 Expect.isTrue( | 42 Expect.isTrue( |
| 45 collector.warnings.isEmpty, | 43 collector.warnings.isEmpty, "Unexpected warnings: ${collector.warnings}"); |
| 46 "Unexpected warnings: ${collector.warnings}"); | 44 Expect.isFalse(collector.errors.isEmpty, "Expected compile-time errors."); |
| 47 Expect.isFalse( | 45 Expect.equals(expectHint, collector.hints.isNotEmpty, |
| 48 collector.errors.isEmpty, | |
| 49 "Expected compile-time errors."); | |
| 50 Expect.equals( | |
| 51 expectHint, | |
| 52 collector.hints.isNotEmpty, | |
| 53 "Unexpected hints: ${collector.warnings}"); | 46 "Unexpected hints: ${collector.warnings}"); |
| 54 | 47 |
| 55 JavaScriptBackend backend = compiler.backend; | 48 JavaScriptBackend backend = compiler.backend; |
| 56 bool isCodeGenerated = backend.generatedCode.isNotEmpty; | 49 bool isCodeGenerated = backend.generatedCode.isNotEmpty; |
| 57 Expect.equals( | 50 Expect.equals( |
| 58 expectedCodeGenerated, | 51 expectedCodeGenerated, |
| 59 isCodeGenerated, | 52 isCodeGenerated, |
| 60 expectedCodeGenerated | 53 expectedCodeGenerated |
| 61 ? "Expected generated code for options $options." | 54 ? "Expected generated code for options $options." |
| 62 : "Expected no code generated for options $options."); | 55 : "Expected no code generated for options $options."); |
| 63 Expect.equals( | 56 Expect.equals( |
| 64 expectedOutput, | 57 expectedOutput, |
| 65 outputCollector.outputMap.isNotEmpty, | 58 outputCollector.outputMap.isNotEmpty, |
| 66 expectedOutput | 59 expectedOutput |
| 67 ? "Expected output for options $options." | 60 ? "Expected output for options $options." |
| 68 : "Expected no output for options $options."); | 61 : "Expected no output for options $options."); |
| 69 } | 62 } |
| 70 | 63 |
| 71 void main() { | 64 void main() { |
| 72 asyncTest(() async { | 65 asyncTest(() async { |
| 73 await test( | 66 await test([], expectedCodeGenerated: false, expectedOutput: false); |
| 74 [], | 67 await test(['--test-mode'], |
| 75 expectedCodeGenerated: false, | 68 expectedCodeGenerated: false, expectedOutput: false); |
| 76 expectedOutput: false); | 69 await test(['--generate-code-with-compile-time-errors'], |
| 77 await test( | 70 expectedCodeGenerated: true, expectedOutput: true); |
| 78 ['--test-mode'], | 71 await test(['--generate-code-with-compile-time-errors', '--test-mode'], |
| 79 expectedCodeGenerated: false, | 72 expectedCodeGenerated: true, expectedOutput: false); |
| 80 expectedOutput: false); | |
| 81 await test( | |
| 82 ['--generate-code-with-compile-time-errors'], | |
| 83 expectedCodeGenerated: true, | |
| 84 expectedOutput: true); | |
| 85 await test( | |
| 86 ['--generate-code-with-compile-time-errors', '--test-mode'], | |
| 87 expectedCodeGenerated: true, | |
| 88 expectedOutput: false); | |
| 89 }); | 73 }); |
| 90 } | 74 } |
| OLD | NEW |