Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: test/codegen_test.dart

Issue 2158173003: fix #603, support mock objects (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: add test Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 /// 6 ///
7 /// Runs Dart Dev Compiler on all input in the `codegen` directory and checks 7 /// Runs Dart Dev Compiler on all input in the `codegen` directory and checks
8 /// that the output is what we expected. 8 /// that the output is what we expected.
9 library dev_compiler.test.codegen_test; 9 library dev_compiler.test.codegen_test;
10 10
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 // TODO(jmesserly): switch this to a .packages file. 59 // TODO(jmesserly): switch this to a .packages file.
60 final packageUrlMappings = { 60 final packageUrlMappings = {
61 'package:expect/expect.dart': path.join(codegenDir, 'expect.dart'), 61 'package:expect/expect.dart': path.join(codegenDir, 'expect.dart'),
62 'package:async_helper/async_helper.dart': 62 'package:async_helper/async_helper.dart':
63 path.join(codegenDir, 'async_helper.dart'), 63 path.join(codegenDir, 'async_helper.dart'),
64 'package:js/js.dart': path.join(codegenDir, 'packages', 'js', 'js.dart') 64 'package:js/js.dart': path.join(codegenDir, 'packages', 'js', 'js.dart')
65 }; 65 };
66 66
67 final codeCoverage = Platform.environment.containsKey('COVERALLS_TOKEN'); 67 final codeCoverage = Platform.environment.containsKey('COVERALLS_TOKEN');
68 68
69 RegExp filePattern;
70
69 main(List<String> arguments) { 71 main(List<String> arguments) {
70 if (arguments == null) arguments = []; 72 if (arguments == null) arguments = [];
71 ArgResults args = argParser.parse(arguments); 73 ArgResults args = argParser.parse(arguments);
72 var filePattern = new RegExp(args.rest.length > 0 ? args.rest[0] : '.'); 74 filePattern = new RegExp(args.rest.length > 0 ? args.rest[0] : '.');
73 75
74 var sdkDir = path.join(repoDirectory, 'gen', 'patched_sdk'); 76 var sdkDir = path.join(repoDirectory, 'gen', 'patched_sdk');
75 var sdkSummaryFile = 77 var sdkSummaryFile =
76 path.join(testDirectory, '..', 'lib', 'runtime', 'dart_sdk.sum'); 78 path.join(testDirectory, '..', 'lib', 'runtime', 'dart_sdk.sum');
77 var analyzerOptions = new AnalyzerOptions( 79 var analyzerOptions = new AnalyzerOptions(
78 customUrlMappings: packageUrlMappings, 80 customUrlMappings: packageUrlMappings,
79 dartSdkSummaryPath: sdkSummaryFile); 81 dartSdkSummaryPath: sdkSummaryFile);
80 var compiler = new ModuleCompiler(analyzerOptions); 82 var compiler = new ModuleCompiler(analyzerOptions);
81 83
82 // Build packages tests depend on. 84 // Build packages tests depend on.
83 _buildAllPackages(compiler); 85 _buildAllPackages(compiler);
84 86
85 var testDirs = [ 87 var testDirs = [
86 'language', 88 'language',
87 'corelib', 89 'corelib',
88 path.join('lib', 'convert'), 90 path.join('lib', 'convert'),
89 path.join('lib', 'html'), 91 path.join('lib', 'html'),
90 path.join('lib', 'math'), 92 path.join('lib', 'math'),
91 path.join('lib', 'typed_data'), 93 path.join('lib', 'typed_data'),
92 ]; 94 ];
93 95
94 // Copy all of the test files and expanded multitest files to 96 // Copy all of the test files and expanded multitest files to
95 // gen/codegen_tests. We'll compile from there. 97 // gen/codegen_tests. We'll compile from there.
96 var testFiles = _setUpTests(testDirs, filePattern); 98 var testFiles = _setUpTests(testDirs);
97 99
98 // Our default compiler options. Individual tests can override these. 100 // Our default compiler options. Individual tests can override these.
99 var defaultOptions = ['--no-source-map', '--no-summarize']; 101 var defaultOptions = ['--no-source-map', '--no-summarize'];
100 var compilerArgParser = CompilerOptions.addArguments(new ArgParser()); 102 var compilerArgParser = CompilerOptions.addArguments(new ArgParser());
101 103
102 // Compile each test file to JS and put the result in gen/codegen_output. 104 // Compile each test file to JS and put the result in gen/codegen_output.
103 for (var testFile in testFiles) { 105 for (var testFile in testFiles) {
104 var relativePath = path.relative(testFile, from: codegenTestDir); 106 var relativePath = path.relative(testFile, from: codegenTestDir);
105 107
106 // Only compile the top-level files for generating coverage. 108 // Only compile the top-level files for generating coverage.
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 expectFile.writeAsStringSync("//FAILED TO COMPILE"); 206 expectFile.writeAsStringSync("//FAILED TO COMPILE");
205 } 207 }
206 } 208 }
207 209
208 void _buildAllPackages(ModuleCompiler compiler) { 210 void _buildAllPackages(ModuleCompiler compiler) {
209 group('dartdevc package', () { 211 group('dartdevc package', () {
210 _buildPackages(compiler, codegenOutputDir, codegenExpectDir); 212 _buildPackages(compiler, codegenOutputDir, codegenExpectDir);
211 213
212 var packages = ['matcher', 'path', 'stack_trace']; 214 var packages = ['matcher', 'path', 'stack_trace'];
213 for (var package in packages) { 215 for (var package in packages) {
216 if (!filePattern.hasMatch(package)) continue;
214 test(package, () { 217 test(package, () {
215 _buildPackage(compiler, codegenOutputDir, codegenExpectDir, package); 218 _buildPackage(compiler, codegenOutputDir, codegenExpectDir, package);
216 }); 219 });
217 } 220 }
218 221
222 if (!filePattern.hasMatch('unittest')) return;
223
219 test('unittest', () { 224 test('unittest', () {
220 // Only build files applicable to the web - html_*.dart and its 225 // Only build files applicable to the web - html_*.dart and its
221 // internal dependences. 226 // internal dependences.
222 _buildPackage(compiler, codegenOutputDir, codegenExpectDir, "unittest", 227 _buildPackage(compiler, codegenOutputDir, codegenExpectDir, "unittest",
223 packageFiles: [ 228 packageFiles: [
224 'unittest.dart', 229 'unittest.dart',
225 'html_config.dart', 230 'html_config.dart',
226 'html_individual_config.dart', 231 'html_individual_config.dart',
227 'html_enhanced_config.dart' 232 'html_enhanced_config.dart'
228 ]); 233 ]);
229 }); 234 });
230 }); 235 });
231 236
237 if (!filePattern.hasMatch('sunflower')) return;
238
232 test('dartdevc sunflower', () { 239 test('dartdevc sunflower', () {
233 _buildSunflower(compiler, codegenOutputDir, codegenExpectDir); 240 _buildSunflower(compiler, codegenOutputDir, codegenExpectDir);
234 }); 241 });
235 } 242 }
236 243
237 void _buildSunflower( 244 void _buildSunflower(
238 ModuleCompiler compiler, String outputDir, String expectDir) { 245 ModuleCompiler compiler, String outputDir, String expectDir) {
239 var baseDir = path.join(codegenDir, 'sunflower'); 246 var baseDir = path.join(codegenDir, 'sunflower');
240 var files = ['sunflower', 'circle', 'painter'] 247 var files = ['sunflower', 'circle', 'painter']
241 .map((f) => path.join(baseDir, '$f.dart')) 248 .map((f) => path.join(baseDir, '$f.dart'))
242 .toList(); 249 .toList();
243 var input = new BuildUnit('sunflower', baseDir, files, _moduleForLibrary); 250 var input = new BuildUnit('sunflower', baseDir, files, _moduleForLibrary);
244 var options = new CompilerOptions(summarizeApi: false); 251 var options = new CompilerOptions(summarizeApi: false);
245 252
246 var built = compiler.compile(input, options); 253 var built = compiler.compile(input, options);
247 _writeModule(path.join(outputDir, 'sunflower', 'sunflower'), 254 _writeModule(path.join(outputDir, 'sunflower', 'sunflower'),
248 path.join(expectDir, 'sunflower', 'sunflower'), built); 255 path.join(expectDir, 'sunflower', 'sunflower'), built);
249 } 256 }
250 257
251 void _buildPackages( 258 void _buildPackages(
252 ModuleCompiler compiler, String outputDir, String expectDir) { 259 ModuleCompiler compiler, String outputDir, String expectDir) {
253 // Note: we don't summarize these, as we're going to rely on our in-memory 260 // Note: we don't summarize these, as we're going to rely on our in-memory
254 // shared analysis context for caching, and `_moduleForLibrary` below 261 // shared analysis context for caching, and `_moduleForLibrary` below
255 // understands these are from other modules. 262 // understands these are from other modules.
256 var options = new CompilerOptions(sourceMap: false, summarizeApi: false); 263 var options = new CompilerOptions(sourceMap: false, summarizeApi: false);
257 264
258 for (var uri in packageUrlMappings.keys) { 265 for (var uri in packageUrlMappings.keys) {
266 if (!filePattern.hasMatch(uri)) return;
267
259 assert(uri.startsWith('package:')); 268 assert(uri.startsWith('package:'));
260 var uriPath = uri.substring('package:'.length); 269 var uriPath = uri.substring('package:'.length);
261 var name = path.basenameWithoutExtension(uriPath); 270 var name = path.basenameWithoutExtension(uriPath);
262 test(name, () { 271 test(name, () {
263 var input = new BuildUnit(name, codegenDir, [uri], _moduleForLibrary); 272 var input = new BuildUnit(name, codegenDir, [uri], _moduleForLibrary);
264 var built = compiler.compile(input, options); 273 var built = compiler.compile(input, options);
265 274
266 var outPath = path.join(outputDir, path.withoutExtension(uriPath)); 275 var outPath = path.join(outputDir, path.withoutExtension(uriPath));
267 var expectPath = path.join(expectDir, path.withoutExtension(uriPath)); 276 var expectPath = path.join(expectDir, path.withoutExtension(uriPath));
268 _writeModule(outPath, expectPath, built); 277 _writeModule(outPath, expectPath, built);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 } 315 }
307 316
308 String _moduleForLibrary(Source source) { 317 String _moduleForLibrary(Source source) {
309 var scheme = source.uri.scheme; 318 var scheme = source.uri.scheme;
310 if (scheme == 'package') { 319 if (scheme == 'package') {
311 return source.uri.pathSegments.first; 320 return source.uri.pathSegments.first;
312 } 321 }
313 throw new Exception('Module not found for library "${source.fullName}"'); 322 throw new Exception('Module not found for library "${source.fullName}"');
314 } 323 }
315 324
316 List<String> _setUpTests(List<String> testDirs, RegExp filePattern) { 325 List<String> _setUpTests(List<String> testDirs) {
317 var testFiles = <String>[]; 326 var testFiles = <String>[];
318 327
319 for (var testDir in testDirs) { 328 for (var testDir in testDirs) {
320 for (var file in _listFiles(path.join(codegenDir, testDir), filePattern, 329 for (var file
321 recursive: true)) { 330 in _listFiles(path.join(codegenDir, testDir), recursive: true)) {
322 var relativePath = path.relative(file, from: codegenDir); 331 var relativePath = path.relative(file, from: codegenDir);
323 var outputPath = path.join(codegenTestDir, relativePath); 332 var outputPath = path.join(codegenTestDir, relativePath);
324 333
325 _ensureDirectory(path.dirname(outputPath)); 334 _ensureDirectory(path.dirname(outputPath));
326 335
327 // Copy it over. We do this even for multitests because import_self_test 336 // Copy it over. We do this even for multitests because import_self_test
328 // is a multitest, yet imports its own unexpanded form (!). 337 // is a multitest, yet imports its own unexpanded form (!).
329 new File(file).copySync(outputPath); 338 new File(file).copySync(outputPath);
330 339
331 if (file.endsWith("_test.dart")) { 340 if (file.endsWith("_test.dart")) {
(...skipping 16 matching lines...) Expand all
348 }); 357 });
349 } else { 358 } else {
350 // It's a single test suite. 359 // It's a single test suite.
351 testFiles.add(outputPath); 360 testFiles.add(outputPath);
352 } 361 }
353 } 362 }
354 } 363 }
355 } 364 }
356 365
357 // Also include the other special files that live at the top level directory. 366 // Also include the other special files that live at the top level directory.
358 for (var file in _listFiles(codegenDir, filePattern)) { 367 for (var file in _listFiles(codegenDir)) {
359 var relativePath = path.relative(file, from: codegenDir); 368 var relativePath = path.relative(file, from: codegenDir);
360 var outputPath = path.join(codegenTestDir, relativePath); 369 var outputPath = path.join(codegenTestDir, relativePath);
361 370
362 new File(file).copySync(outputPath); 371 new File(file).copySync(outputPath);
363 if (file.endsWith(".dart")) { 372 if (file.endsWith(".dart")) {
364 testFiles.add(outputPath); 373 testFiles.add(outputPath);
365 } 374 }
366 } 375 }
367 376
368 return testFiles; 377 return testFiles;
369 } 378 }
370 379
371 /// Recursively creates [dir] if it doesn't exist. 380 /// Recursively creates [dir] if it doesn't exist.
372 void _ensureDirectory(String dir) { 381 void _ensureDirectory(String dir) {
373 new Directory(dir).createSync(recursive: true); 382 new Directory(dir).createSync(recursive: true);
374 } 383 }
375 384
376 /// Lists all of the files within [dir] that match [filePattern]. 385 /// Lists all of the files within [dir] that match [filePattern].
377 Iterable<String> _listFiles(String dir, RegExp filePattern, 386 Iterable<String> _listFiles(String dir, {bool recursive: false}) {
378 {bool recursive: false}) {
379 return new Directory(dir) 387 return new Directory(dir)
380 .listSync(recursive: recursive, followLinks: false) 388 .listSync(recursive: recursive, followLinks: false)
381 .where((entry) { 389 .where((entry) {
382 if (entry is! File) return false; 390 if (entry is! File) return false;
383 391
384 var filePath = entry.path; 392 var filePath = entry.path;
385 if (!filePattern.hasMatch(filePath)) return false; 393 if (!filePattern.hasMatch(filePath)) return false;
386 394
387 return true; 395 return true;
388 }).map((file) => file.path); 396 }).map((file) => file.path);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 /// Simplified from ParseDartTask.resolveDirective. 430 /// Simplified from ParseDartTask.resolveDirective.
423 String _resolveDirective(UriBasedDirective directive) { 431 String _resolveDirective(UriBasedDirective directive) {
424 StringLiteral uriLiteral = directive.uri; 432 StringLiteral uriLiteral = directive.uri;
425 String uriContent = uriLiteral.stringValue; 433 String uriContent = uriLiteral.stringValue;
426 if (uriContent != null) { 434 if (uriContent != null) {
427 uriContent = uriContent.trim(); 435 uriContent = uriContent.trim();
428 directive.uriContent = uriContent; 436 directive.uriContent = uriContent;
429 } 437 }
430 return directive.validate() == null ? uriContent : null; 438 return directive.validate() == null ? uriContent : null;
431 } 439 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698