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 /// Tests code generation. | 5 /// Tests code generation. |
6 /// Runs Dart Dev Compiler on all input in the `codegen` directory and checks | 6 /// Runs Dart Dev Compiler on all input in the `codegen` directory and checks |
7 /// that the output is what we expected. | 7 /// that the output is what we expected. |
8 library dev_compiler.test.codegen_test; | 8 library dev_compiler.test.codegen_test; |
9 | 9 |
10 import 'dart:io'; | 10 import 'dart:io'; |
11 import 'package:cli_util/cli_util.dart' show getSdkDir; | 11 import 'package:cli_util/cli_util.dart' show getSdkDir; |
12 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine, Logger; | 12 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine, Logger; |
13 import 'package:analyzer/src/generated/java_engine.dart' show CaughtException; | 13 import 'package:analyzer/src/generated/java_engine.dart' show CaughtException; |
14 import 'package:args/args.dart'; | 14 import 'package:args/args.dart'; |
15 import 'package:cli_util/cli_util.dart' show getSdkDir; | |
16 import 'package:dev_compiler/devc.dart'; | |
17 import 'package:dev_compiler/src/options.dart'; | |
18 import 'package:logging/logging.dart' show Level; | 15 import 'package:logging/logging.dart' show Level; |
19 import 'package:path/path.dart' as path; | 16 import 'package:path/path.dart' as path; |
20 import 'package:unittest/unittest.dart'; | 17 import 'package:unittest/unittest.dart'; |
21 | 18 |
| 19 import 'package:dev_compiler/devc.dart'; |
| 20 import 'package:dev_compiler/src/options.dart'; |
| 21 import 'package:dev_compiler/src/dependency_graph.dart' |
| 22 show defaultRuntimeFiles; |
| 23 |
22 final ArgParser argParser = new ArgParser() | 24 final ArgParser argParser = new ArgParser() |
23 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) | 25 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) |
24 ..addFlag('dart-gen', | 26 ..addFlag('dart-gen', |
25 abbr: 'd', help: 'Generate dart output', defaultsTo: false); | 27 abbr: 'd', help: 'Generate dart output', defaultsTo: false); |
26 | 28 |
27 main(arguments) { | 29 main(arguments) { |
28 if (arguments == null) arguments = []; | 30 if (arguments == null) arguments = []; |
29 ArgResults args = argParser.parse(arguments); | 31 ArgResults args = argParser.parse(arguments); |
30 var script = Platform.script.path; | 32 var script = Platform.script.path; |
31 var dartGen = args['dart-gen']; | 33 var dartGen = args['dart-gen']; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 ? new File(path.join(actualDir, '$filename/$filename.dart')) | 109 ? new File(path.join(actualDir, '$filename/$filename.dart')) |
108 : new File(path.join(actualDir, '$filename.js')); | 110 : new File(path.join(actualDir, '$filename.js')); |
109 expect(outFile.existsSync(), success, | 111 expect(outFile.existsSync(), success, |
110 reason: '${outFile.path} was created iff compilation succeeds'); | 112 reason: '${outFile.path} was created iff compilation succeeds'); |
111 | 113 |
112 // TODO(jmesserly): ideally we'd diff the output here. For now it | 114 // TODO(jmesserly): ideally we'd diff the output here. For now it |
113 // happens in the containing shell script. | 115 // happens in the containing shell script. |
114 }); | 116 }); |
115 } | 117 } |
116 | 118 |
117 group('sdk', () { | 119 if (dartGen) { |
118 // The analyzer does not bubble exception messages for certain internal | 120 group('sdk', () { |
119 // dart:* library failures, such as failing to find | 121 // The analyzer does not bubble exception messages for certain internal |
120 // "_internal/libraries.dart". Instead it produces an opaque "failed to | 122 // dart:* library failures, such as failing to find |
121 // instantiate dart:core" message. To remedy this we hook up an analysis | 123 // "_internal/libraries.dart". Instead it produces an opaque "failed to |
122 // logger that prints these messages. | 124 // instantiate dart:core" message. To remedy this we hook up an analysis |
123 var savedLogger; | 125 // logger that prints these messages. |
124 setUp(() { | 126 var savedLogger; |
125 savedLogger = AnalysisEngine.instance.logger; | 127 setUp(() { |
126 AnalysisEngine.instance.logger = new PrintLogger(); | 128 savedLogger = AnalysisEngine.instance.logger; |
| 129 AnalysisEngine.instance.logger = new PrintLogger(); |
| 130 }); |
| 131 tearDown(() { |
| 132 AnalysisEngine.instance.logger = savedLogger; |
| 133 }); |
| 134 |
| 135 test('devc dart:core', () { |
| 136 // Get the test SDK. We use a checked in copy so test expectations can |
| 137 // be generated against a specific SDK version. |
| 138 var testSdk = path.join(testDir, '..', 'tool', 'input_sdk'); |
| 139 var result = compile('dart:core', testSdk, checkSdk: true); |
| 140 var outputDir = new Directory(path.join(actualDir, 'core')); |
| 141 var outFile = new File(path.join(actualDir, 'core/core')); |
| 142 expect(outFile.existsSync(), true, |
| 143 reason: '${outFile.path} was created for dart:core'); |
| 144 }); |
127 }); | 145 }); |
128 tearDown(() { | 146 } |
129 AnalysisEngine.instance.logger = savedLogger; | |
130 }); | |
131 | 147 |
132 test('devc dart:core', () { | 148 var expectedRuntime = |
133 // Get the test SDK. We use a checked in copy so test expectations can be | 149 defaultRuntimeFiles.map((f) => 'dev_compiler/runtime/$f'); |
134 // generated against a specific SDK version. | |
135 // TODO(jmesserly): eventually we should track compiler messages. | |
136 // For now we're just trying to get decent code generation. | |
137 var testSdk = dartGen | |
138 ? path.join(testDir, '..', 'tool', 'input_sdk') | |
139 : path.join(testDir, 'generated_sdk'); | |
140 var result = compile('dart:core', testSdk, checkSdk: true); | |
141 var outputDir = new Directory(path.join(actualDir, 'core')); | |
142 var outFile = dartGen | |
143 ? new File(path.join(actualDir, 'core/core')) | |
144 : new File(path.join(actualDir, 'dart/core.js')); | |
145 expect(outFile.existsSync(), true, | |
146 reason: '${outFile.path} was created for dart:core'); | |
147 }); | |
148 }); | |
149 | 150 |
150 if (!dartGen) { | 151 if (!dartGen) { |
151 test('devc jscodegen sunflower.html', () { | 152 test('devc jscodegen sunflower.html', () { |
152 var filePath = path.join(inputDir, 'sunflower', 'sunflower.html'); | 153 var filePath = path.join(inputDir, 'sunflower', 'sunflower.html'); |
153 compilerMessages.writeln('// Messages from compiling sunflower.html'); | 154 compilerMessages.writeln('// Messages from compiling sunflower.html'); |
154 | 155 |
155 var result = compile(filePath, realSdk, subDir: 'sunflower'); | 156 var result = compile(filePath, realSdk, subDir: 'sunflower'); |
156 var success = !result.failure; | 157 var success = !result.failure; |
157 | 158 |
158 // Write compiler messages to disk. | 159 // Write compiler messages to disk. |
159 new File(path.join(actualDir, 'sunflower', 'sunflower.txt')) | 160 new File(path.join(actualDir, 'sunflower', 'sunflower.txt')) |
160 .writeAsStringSync(compilerMessages.toString()); | 161 .writeAsStringSync(compilerMessages.toString()); |
161 | 162 |
162 var expectedFiles = [ | 163 var expectedFiles = [ |
163 'sunflower.html', | 164 'sunflower.html', |
164 'sunflower.js', | 165 'sunflower.js', |
165 'sunflower.css', | 166 'sunflower.css', |
166 'math.png', | 167 'math.png', |
167 'dev_compiler/runtime/dart_core.js', | 168 ]..addAll(expectedRuntime); |
168 'dev_compiler/runtime/dart_runtime.js', | 169 |
169 'dev_compiler/runtime/harmony_feature_check.js', | |
170 ]; | |
171 for (var filepath in expectedFiles) { | 170 for (var filepath in expectedFiles) { |
172 var outFile = new File(path.join(actualDir, 'sunflower', filepath)); | 171 var outFile = new File(path.join(actualDir, 'sunflower', filepath)); |
173 expect(outFile.existsSync(), success, | 172 expect(outFile.existsSync(), success, |
174 reason: '${outFile.path} was created iff compilation succeeds'); | 173 reason: '${outFile.path} was created iff compilation succeeds'); |
175 } | 174 } |
176 }); | 175 }); |
177 | 176 |
178 test('devc jscodegen html_input.html', () { | 177 test('devc jscodegen html_input.html', () { |
179 var filePath = path.join(inputDir, 'html_input.html'); | 178 var filePath = path.join(inputDir, 'html_input.html'); |
180 compilerMessages.writeln('// Messages from compiling html_input.html'); | 179 compilerMessages.writeln('// Messages from compiling html_input.html'); |
181 | 180 |
182 var result = compile(filePath, realSdk); | 181 var result = compile(filePath, realSdk); |
183 var success = !result.failure; | 182 var success = !result.failure; |
184 | 183 |
185 // Write compiler messages to disk. | 184 // Write compiler messages to disk. |
186 new File(path.join(actualDir, 'html_input.txt')) | 185 new File(path.join(actualDir, 'html_input.txt')) |
187 .writeAsStringSync(compilerMessages.toString()); | 186 .writeAsStringSync(compilerMessages.toString()); |
188 | 187 |
189 var expectedFiles = [ | 188 var expectedFiles = [ |
190 'html_input.html', | 189 'html_input.html', |
191 'dir/html_input_a.js', | 190 'dir/html_input_a.js', |
192 'dir/html_input_b.js', | 191 'dir/html_input_b.js', |
193 'dir/html_input_c.js', | 192 'dir/html_input_c.js', |
194 'dir/html_input_d.js', | 193 'dir/html_input_d.js', |
195 'dir/html_input_e.js', | 194 'dir/html_input_e.js' |
196 'dev_compiler/runtime/dart_core.js', | 195 ]..addAll(expectedRuntime); |
197 'dev_compiler/runtime/dart_runtime.js', | 196 |
198 'dev_compiler/runtime/harmony_feature_check.js', | |
199 ]; | |
200 for (var filepath in expectedFiles) { | 197 for (var filepath in expectedFiles) { |
201 var outFile = new File(path.join(actualDir, filepath)); | 198 var outFile = new File(path.join(actualDir, filepath)); |
202 expect(outFile.existsSync(), success, | 199 expect(outFile.existsSync(), success, |
203 reason: '${outFile.path} was created iff compilation succeeds'); | 200 reason: '${outFile.path} was created iff compilation succeeds'); |
204 } | 201 } |
205 | 202 |
206 var notExpectedFiles = [ | 203 var notExpectedFiles = [ |
207 'dev_compiler/runtime/messages_widget.js', | 204 'dev_compiler/runtime/messages_widget.js', |
208 'dev_compiler/runtime/messages.css' | 205 'dev_compiler/runtime/messages.css' |
209 ]; | 206 ]; |
(...skipping 16 matching lines...) Expand all Loading... |
226 new File(path.join(actualDir, 'server_mode', 'html_input.txt')) | 223 new File(path.join(actualDir, 'server_mode', 'html_input.txt')) |
227 .writeAsStringSync(compilerMessages.toString()); | 224 .writeAsStringSync(compilerMessages.toString()); |
228 | 225 |
229 var expectedFiles = [ | 226 var expectedFiles = [ |
230 'html_input.html', | 227 'html_input.html', |
231 'dir/html_input_a.js', | 228 'dir/html_input_a.js', |
232 'dir/html_input_b.js', | 229 'dir/html_input_b.js', |
233 'dir/html_input_c.js', | 230 'dir/html_input_c.js', |
234 'dir/html_input_d.js', | 231 'dir/html_input_d.js', |
235 'dir/html_input_e.js', | 232 'dir/html_input_e.js', |
236 'dev_compiler/runtime/dart_core.js', | |
237 'dev_compiler/runtime/dart_runtime.js', | |
238 'dev_compiler/runtime/harmony_feature_check.js', | |
239 'dev_compiler/runtime/messages_widget.js', | 233 'dev_compiler/runtime/messages_widget.js', |
240 'dev_compiler/runtime/messages.css' | 234 'dev_compiler/runtime/messages.css' |
241 ]; | 235 ]..addAll(expectedRuntime); |
| 236 |
242 for (var filepath in expectedFiles) { | 237 for (var filepath in expectedFiles) { |
243 var outFile = new File(path.join(actualDir, 'server_mode', filepath)); | 238 var outFile = new File(path.join(actualDir, 'server_mode', filepath)); |
244 expect(outFile.existsSync(), success, | 239 expect(outFile.existsSync(), success, |
245 reason: '${outFile.path} was created iff compilation succeeds'); | 240 reason: '${outFile.path} was created iff compilation succeeds'); |
246 } | 241 } |
247 }); | 242 }); |
248 } | 243 } |
249 } | 244 } |
250 | 245 |
251 /// An implementation of analysis engine's [Logger] that prints. | 246 /// An implementation of analysis engine's [Logger] that prints. |
252 class PrintLogger implements Logger { | 247 class PrintLogger implements Logger { |
253 @override void logError(String message, [CaughtException exception]) { | 248 @override void logError(String message, [CaughtException exception]) { |
254 print('[AnalysisEngine] error $message $exception'); | 249 print('[AnalysisEngine] error $message $exception'); |
255 } | 250 } |
256 | 251 |
257 @override void logError2(String message, Object exception) { | 252 @override void logError2(String message, Object exception) { |
258 print('[AnalysisEngine] error $message $exception'); | 253 print('[AnalysisEngine] error $message $exception'); |
259 } | 254 } |
260 | 255 |
261 void logInformation(String message, [CaughtException exception]) {} | 256 void logInformation(String message, [CaughtException exception]) {} |
262 void logInformation2(String message, Object exception) {} | 257 void logInformation2(String message, Object exception) {} |
263 } | 258 } |
OLD | NEW |