| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Smoke test of the dart2js compiler API. | 5 // Smoke test of the dart2js compiler API. |
| 6 library analyze_only; | 6 library analyze_only; |
| 7 | 7 |
| 8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import "package:async_helper/async_helper.dart"; | 10 import "package:async_helper/async_helper.dart"; |
| 11 | 11 |
| 12 import '../../utils/dummy_compiler_test.dart' as dummy; | 12 import '../../utils/dummy_compiler_test.dart' as dummy; |
| 13 import 'package:compiler/compiler.dart'; | 13 import 'package:compiler/compiler.dart'; |
| 14 import 'package:compiler/src/commandline_options.dart'; | 14 import 'package:compiler/src/commandline_options.dart'; |
| 15 import 'package:compiler/src/diagnostics/messages.dart' | 15 import 'package:compiler/src/diagnostics/messages.dart' |
| 16 show MessageKind, MessageTemplate; | 16 show MessageKind, MessageTemplate; |
| 17 | 17 |
| 18 import 'output_collector.dart'; | 18 import 'output_collector.dart'; |
| 19 | 19 |
| 20 runCompiler(String main, List<String> options, | 20 runCompiler(String main, List<String> options, |
| 21 onValue(String code, List errors, List warnings)) { | 21 onValue(String code, List errors, List warnings)) { |
| 22 List errors = new List(); | 22 List errors = new List(); |
| 23 List warnings = new List(); | 23 List warnings = new List(); |
| 24 | 24 |
| 25 Future<String> localProvider(Uri uri) { | 25 Future<String> localProvider(Uri uri) { |
| 26 if (uri.scheme != 'main') return dummy.provider(uri); | 26 if (uri.scheme != 'main') return dummy.provider(uri); |
| 27 return new Future<String>.value(main); | 27 return new Future<String>.value(main); |
| 28 } | 28 } |
| 29 | 29 |
| 30 void localHandler(Uri uri, int begin, int end, | 30 void localHandler( |
| 31 String message, Diagnostic kind) { | 31 Uri uri, int begin, int end, String message, Diagnostic kind) { |
| 32 dummy.handler(uri, begin, end, message, kind); | 32 dummy.handler(uri, begin, end, message, kind); |
| 33 if (kind == Diagnostic.ERROR) { | 33 if (kind == Diagnostic.ERROR) { |
| 34 errors.add(message); | 34 errors.add(message); |
| 35 } else if (kind == Diagnostic.WARNING) { | 35 } else if (kind == Diagnostic.WARNING) { |
| 36 warnings.add(message); | 36 warnings.add(message); |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 print('-----------------------------------------------'); | 40 print('-----------------------------------------------'); |
| 41 print('main source:\n$main'); | 41 print('main source:\n$main'); |
| 42 print('options: $options\n'); | 42 print('options: $options\n'); |
| 43 asyncStart(); | 43 asyncStart(); |
| 44 OutputCollector outputCollector = new OutputCollector(); | 44 OutputCollector outputCollector = new OutputCollector(); |
| 45 Future<CompilationResult> result = | 45 Future<CompilationResult> result = compile( |
| 46 compile(new Uri(scheme: 'main'), | 46 new Uri(scheme: 'main'), |
| 47 new Uri(scheme: 'lib', path: '/'), | 47 new Uri(scheme: 'lib', path: '/'), |
| 48 new Uri(scheme: 'package', path: '/'), | 48 new Uri(scheme: 'package', path: '/'), |
| 49 localProvider, localHandler, options, outputCollector); | 49 localProvider, |
| 50 result.then((_) { | 50 localHandler, |
| 51 onValue(outputCollector.getOutput('', 'js'), errors, warnings); | 51 options, |
| 52 }, onError: (e, st) { | 52 outputCollector); |
| 53 throw 'Compilation failed: ${e} ${st}'; | 53 result |
| 54 }).then(asyncSuccess).catchError((error, stack) { | 54 .then((_) { |
| 55 print('\n\n-----------------------------------------------'); | 55 onValue(outputCollector.getOutput('', 'js'), errors, warnings); |
| 56 print('main source:\n$main'); | 56 }, onError: (e, st) { |
| 57 print('options: $options\n'); | 57 throw 'Compilation failed: ${e} ${st}'; |
| 58 print('threw:\n $error\n$stack'); | 58 }) |
| 59 print('-----------------------------------------------\n\n'); | 59 .then(asyncSuccess) |
| 60 throw error; | 60 .catchError((error, stack) { |
| 61 }); | 61 print('\n\n-----------------------------------------------'); |
| 62 print('main source:\n$main'); |
| 63 print('options: $options\n'); |
| 64 print('threw:\n $error\n$stack'); |
| 65 print('-----------------------------------------------\n\n'); |
| 66 throw error; |
| 67 }); |
| 62 } | 68 } |
| 63 | 69 |
| 64 main() { | 70 main() { |
| 65 runCompiler( | 71 runCompiler("", [Flags.generateCodeWithCompileTimeErrors], |
| 66 "", | 72 (String code, List errors, List warnings) { |
| 67 [Flags.generateCodeWithCompileTimeErrors], | 73 Expect.isNotNull(code); |
| 68 (String code, List errors, List warnings) { | 74 Expect.isTrue(errors.isEmpty, 'errors is not empty: $errors'); |
| 69 Expect.isNotNull(code); | 75 MessageTemplate template = |
| 70 Expect.isTrue(errors.isEmpty, 'errors is not empty: $errors'); | 76 MessageTemplate.TEMPLATES[MessageKind.MISSING_MAIN]; |
| 71 MessageTemplate template = | 77 Expect.equals("${template.message({'main': 'main'})}", warnings.single); |
| 72 MessageTemplate.TEMPLATES[MessageKind.MISSING_MAIN]; | 78 }); |
| 73 Expect.equals( | 79 |
| 74 "${template.message({'main': 'main'})}", | 80 runCompiler("main() {}", [Flags.generateCodeWithCompileTimeErrors], |
| 75 warnings.single); | 81 (String code, List errors, List warnings) { |
| 76 }); | 82 Expect.isNotNull(code); |
| 83 Expect.isTrue(errors.isEmpty); |
| 84 Expect.isTrue(warnings.isEmpty); |
| 85 }); |
| 86 |
| 87 runCompiler("", [Flags.analyzeOnly], |
| 88 (String code, List errors, List warnings) { |
| 89 Expect.isNull(code); |
| 90 Expect.isTrue(errors.isEmpty, 'errors is not empty: $errors'); |
| 91 MessageTemplate template = |
| 92 MessageTemplate.TEMPLATES[MessageKind.CONSIDER_ANALYZE_ALL]; |
| 93 Expect.equals("${template.message({'main': 'main'})}", warnings.single); |
| 94 }); |
| 95 |
| 96 runCompiler("main() {}", [Flags.analyzeOnly], |
| 97 (String code, List errors, List warnings) { |
| 98 Expect.isNull(code); |
| 99 Expect.isTrue(errors.isEmpty); |
| 100 Expect.isTrue(warnings.isEmpty); |
| 101 }); |
| 102 |
| 103 runCompiler("Foo foo; // Unresolved but not analyzed.", [Flags.analyzeOnly], |
| 104 (String code, List errors, List warnings) { |
| 105 Expect.isNull(code); |
| 106 Expect.isTrue(errors.isEmpty, 'errors is not empty: $errors'); |
| 107 MessageTemplate template = |
| 108 MessageTemplate.TEMPLATES[MessageKind.CONSIDER_ANALYZE_ALL]; |
| 109 Expect.equals("${template.message({'main': 'main'})}", warnings.single); |
| 110 }); |
| 77 | 111 |
| 78 runCompiler( | 112 runCompiler( |
| 79 "main() {}", | 113 """main() { |
| 80 [Flags.generateCodeWithCompileTimeErrors], | 114 Foo foo; // Unresolved and analyzed. |
| 81 (String code, List errors, List warnings) { | 115 }""", |
| 82 Expect.isNotNull(code); | 116 [Flags.analyzeOnly], (String code, List errors, List warnings) { |
| 83 Expect.isTrue(errors.isEmpty); | 117 Expect.isNull(code); |
| 84 Expect.isTrue(warnings.isEmpty); | 118 Expect.isTrue(errors.isEmpty); |
| 85 }); | 119 Expect.equals(1, warnings.length); |
| 120 Expect.equals("Cannot resolve type 'Foo'.", warnings[0].toString()); |
| 121 }); |
| 86 | 122 |
| 87 runCompiler( | 123 runCompiler( |
| 88 "", | 124 """main() { |
| 89 [Flags.analyzeOnly], | 125 Foo foo; // Unresolved and analyzed. |
| 90 (String code, List errors, List warnings) { | 126 }""", |
| 91 Expect.isNull(code); | 127 [Flags.analyzeOnly, Flags.analyzeSignaturesOnly], |
| 92 Expect.isTrue(errors.isEmpty, 'errors is not empty: $errors'); | 128 (String code, List errors, List warnings) { |
| 93 MessageTemplate template = | 129 Expect.isNull(code); |
| 94 MessageTemplate.TEMPLATES[MessageKind.CONSIDER_ANALYZE_ALL]; | 130 Expect.isTrue(errors.isEmpty); |
| 95 Expect.equals( | 131 Expect.isTrue(warnings.isEmpty); |
| 96 "${template.message({'main': 'main'})}", | 132 }); |
| 97 warnings.single); | 133 |
| 98 }); | 134 runCompiler("Foo foo; // Unresolved and analyzed.", [ |
| 135 Flags.analyzeOnly, |
| 136 Flags.analyzeAll |
| 137 ], (String code, List errors, List warnings) { |
| 138 Expect.isNull(code); |
| 139 Expect.isTrue(errors.isEmpty); |
| 140 Expect.equals("Cannot resolve type 'Foo'.", warnings[0].toString()); |
| 141 }); |
| 99 | 142 |
| 100 runCompiler( | 143 runCompiler( |
| 101 "main() {}", | 144 """Foo foo; // Unresolved and analyzed. |
| 102 [Flags.analyzeOnly], | 145 main() {}""", |
| 103 (String code, List errors, List warnings) { | 146 [Flags.analyzeOnly, Flags.analyzeAll], |
| 104 Expect.isNull(code); | 147 (String code, List errors, List warnings) { |
| 105 Expect.isTrue(errors.isEmpty); | 148 Expect.isNull(code); |
| 106 Expect.isTrue(warnings.isEmpty); | 149 Expect.isTrue(errors.isEmpty, 'Unexpected errors: $errors.'); |
| 107 }); | 150 Expect.equals(1, warnings.length, 'Unexpected warning count: $warnings.'); |
| 151 Expect.equals("Cannot resolve type 'Foo'.", warnings[0].toString()); |
| 152 }); |
| 108 | 153 |
| 109 runCompiler( | 154 runCompiler("", [Flags.analyzeOnly, Flags.analyzeAll], |
| 110 "Foo foo; // Unresolved but not analyzed.", | 155 (String code, List errors, List warnings) { |
| 111 [Flags.analyzeOnly], | 156 Expect.isNull(code); |
| 112 (String code, List errors, List warnings) { | 157 Expect.isTrue(errors.isEmpty); |
| 113 Expect.isNull(code); | 158 Expect.isTrue(warnings.isEmpty); |
| 114 Expect.isTrue(errors.isEmpty, 'errors is not empty: $errors'); | 159 }); |
| 115 MessageTemplate template = | |
| 116 MessageTemplate.TEMPLATES[MessageKind.CONSIDER_ANALYZE_ALL]; | |
| 117 Expect.equals( | |
| 118 "${template.message({'main': 'main'})}", | |
| 119 warnings.single); | |
| 120 }); | |
| 121 | |
| 122 runCompiler( | |
| 123 """main() { | |
| 124 Foo foo; // Unresolved and analyzed. | |
| 125 }""", | |
| 126 [Flags.analyzeOnly], | |
| 127 (String code, List errors, List warnings) { | |
| 128 Expect.isNull(code); | |
| 129 Expect.isTrue(errors.isEmpty); | |
| 130 Expect.equals(1, warnings.length); | |
| 131 Expect.equals( | |
| 132 "Cannot resolve type 'Foo'.", warnings[0].toString()); | |
| 133 }); | |
| 134 | |
| 135 runCompiler( | |
| 136 """main() { | |
| 137 Foo foo; // Unresolved and analyzed. | |
| 138 }""", | |
| 139 [Flags.analyzeOnly, Flags.analyzeSignaturesOnly], | |
| 140 (String code, List errors, List warnings) { | |
| 141 Expect.isNull(code); | |
| 142 Expect.isTrue(errors.isEmpty); | |
| 143 Expect.isTrue(warnings.isEmpty); | |
| 144 }); | |
| 145 | |
| 146 runCompiler( | |
| 147 "Foo foo; // Unresolved and analyzed.", | |
| 148 [Flags.analyzeOnly, Flags.analyzeAll], | |
| 149 (String code, List errors, List warnings) { | |
| 150 Expect.isNull(code); | |
| 151 Expect.isTrue(errors.isEmpty); | |
| 152 Expect.equals( | |
| 153 "Cannot resolve type 'Foo'.", warnings[0].toString()); | |
| 154 }); | |
| 155 | |
| 156 runCompiler( | |
| 157 """Foo foo; // Unresolved and analyzed. | |
| 158 main() {}""", | |
| 159 [Flags.analyzeOnly, Flags.analyzeAll], | |
| 160 (String code, List errors, List warnings) { | |
| 161 Expect.isNull(code); | |
| 162 Expect.isTrue(errors.isEmpty, 'Unexpected errors: $errors.'); | |
| 163 Expect.equals(1, warnings.length, 'Unexpected warning count: $warnings.'); | |
| 164 Expect.equals( | |
| 165 "Cannot resolve type 'Foo'.", warnings[0].toString()); | |
| 166 }); | |
| 167 | |
| 168 runCompiler( | |
| 169 "", | |
| 170 [Flags.analyzeOnly, Flags.analyzeAll], | |
| 171 (String code, List errors, List warnings) { | |
| 172 Expect.isNull(code); | |
| 173 Expect.isTrue(errors.isEmpty); | |
| 174 Expect.isTrue(warnings.isEmpty); | |
| 175 }); | |
| 176 | 160 |
| 177 // --analyze-signatures-only implies --analyze-only | 161 // --analyze-signatures-only implies --analyze-only |
| 178 runCompiler("", [Flags.analyzeSignaturesOnly, Flags.analyzeAll], | 162 runCompiler("", [Flags.analyzeSignaturesOnly, Flags.analyzeAll], |
| 179 (String code, List errors, List warnings) { | 163 (String code, List errors, List warnings) { |
| 180 Expect.isNull(code); | 164 Expect.isNull(code); |
| 181 Expect.isTrue(errors.isEmpty); | 165 Expect.isTrue(errors.isEmpty); |
| 182 Expect.isTrue(warnings.isEmpty); | 166 Expect.isTrue(warnings.isEmpty); |
| 183 }); | 167 }); |
| 184 | 168 |
| 185 // Test that --allow-native-extensions works. | 169 // Test that --allow-native-extensions works. |
| 186 runCompiler( | 170 runCompiler( |
| 187 """main() {} | 171 """main() {} |
| 188 foo() native 'foo';""", | 172 foo() native 'foo';""", |
| 189 [Flags.analyzeOnly, Flags.allowNativeExtensions], | 173 [Flags.analyzeOnly, Flags.allowNativeExtensions], |
| 190 (String code, List errors, List warnings) { | 174 (String code, List errors, List warnings) { |
| 191 Expect.isNull(code); | 175 Expect.isNull(code); |
| 192 Expect.isTrue(errors.isEmpty); | 176 Expect.isTrue(errors.isEmpty); |
| 193 Expect.isTrue(warnings.isEmpty); | 177 Expect.isTrue(warnings.isEmpty); |
| 194 }); | 178 }); |
| 195 runCompiler( | 179 runCompiler( |
| 196 """main() {} | 180 """main() {} |
| 197 foo() native 'foo';""", | 181 foo() native 'foo';""", |
| 198 [Flags.analyzeOnly], | 182 [Flags.analyzeOnly], (String code, List errors, List warnings) { |
| 199 (String code, List errors, List warnings) { | |
| 200 Expect.isNull(code); | 183 Expect.isNull(code); |
| 201 Expect.isTrue( | 184 Expect.isTrue( |
| 202 errors.single.startsWith("'native' modifier is not supported.")); | 185 errors.single.startsWith("'native' modifier is not supported.")); |
| 203 Expect.isTrue(warnings.isEmpty); | 186 Expect.isTrue(warnings.isEmpty); |
| 204 }); | 187 }); |
| 205 } | 188 } |
| OLD | NEW |