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