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 ddc.test.codegen_test; | 8 library ddc.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:analyzer/src/generated/engine.dart' show AnalysisEngine, Logger; | 12 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine, Logger; |
12 import 'package:analyzer/src/generated/java_engine.dart' show CaughtException; | 13 import 'package:analyzer/src/generated/java_engine.dart' show CaughtException; |
13 import 'package:args/args.dart'; | 14 import 'package:args/args.dart'; |
14 import 'package:cli_util/cli_util.dart' show getSdkDir; | 15 import 'package:cli_util/cli_util.dart' show getSdkDir; |
15 import 'package:dev_compiler/devc.dart'; | 16 import 'package:dev_compiler/devc.dart'; |
16 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver; | |
17 import 'package:dev_compiler/src/options.dart'; | 17 import 'package:dev_compiler/src/options.dart'; |
18 import 'package:logging/logging.dart' show Level; | 18 import 'package:logging/logging.dart' show Level; |
19 import 'package:path/path.dart' as path; | 19 import 'package:path/path.dart' as path; |
20 import 'package:unittest/unittest.dart'; | 20 import 'package:unittest/unittest.dart'; |
21 | 21 |
22 final ArgParser argParser = new ArgParser() | 22 final ArgParser argParser = new ArgParser() |
23 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) | 23 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) |
24 ..addFlag( | 24 ..addFlag( |
25 'dart-gen', abbr: 'd', help: 'Generate dart output', defaultsTo: false); | 25 'dart-gen', abbr: 'd', help: 'Generate dart output', defaultsTo: false); |
26 | 26 |
(...skipping 22 matching lines...) Expand all Loading... |
49 var inputDir = dartGen | 49 var inputDir = dartGen |
50 ? path.join(testDir, 'dart_codegen') | 50 ? path.join(testDir, 'dart_codegen') |
51 : path.join(testDir, 'codegen'); | 51 : path.join(testDir, 'codegen'); |
52 var actualDir = path.join(inputDir, 'actual'); | 52 var actualDir = path.join(inputDir, 'actual'); |
53 var paths = new Directory(inputDir) | 53 var paths = new Directory(inputDir) |
54 .listSync() | 54 .listSync() |
55 .where((f) => f is File) | 55 .where((f) => f is File) |
56 .map((f) => f.path) | 56 .map((f) => f.path) |
57 .where((p) => p.endsWith('.dart') && filePattern.hasMatch(p)); | 57 .where((p) => p.endsWith('.dart') && filePattern.hasMatch(p)); |
58 | 58 |
59 var options = new CompilerOptions( | 59 compile(String entryPoint, String sdkPath, [bool checkSdk = false]) { |
60 outputDir: actualDir, | 60 var options = new CompilerOptions( |
61 useColors: false, | 61 outputDir: actualDir, |
62 outputDart: dartGen, | 62 useColors: false, |
63 formatOutput: dartGen, | 63 outputDart: dartGen, |
64 emitSourceMaps: false); | 64 formatOutput: dartGen, |
65 var realSdk = new TypeResolver.fromDir(getSdkDir(arguments).path, options); | 65 emitSourceMaps: false, |
| 66 forceCompile: checkSdk, |
| 67 cheapTestFormat: checkSdk, |
| 68 checkSdk: checkSdk, |
| 69 entryPointFile: entryPoint, |
| 70 dartSdkPath: sdkPath); |
| 71 return new Compiler(options).run(); |
| 72 } |
| 73 var realSdk = getSdkDir(arguments).path; |
66 | 74 |
67 // Validate that old output is gone before running. | 75 // Validate that old output is gone before running. |
68 // TODO(jmesserly): it'd be nice to do all cleanup here, including removing | 76 // TODO(jmesserly): it'd be nice to do all cleanup here, including removing |
69 // pub's 'packages' symlinks which mess up the diff. That way this test | 77 // pub's 'packages' symlinks which mess up the diff. That way this test |
70 // can be self contained instead of depending on a shell script. | 78 // can be self contained instead of depending on a shell script. |
71 if (new Directory(actualDir).existsSync()) { | 79 if (new Directory(actualDir).existsSync()) { |
72 throw 'Old compiler output should be cleaned up first. Use ./test/test.sh'; | 80 throw 'Old compiler output should be cleaned up first. Use ./test/test.sh'; |
73 } | 81 } |
74 | 82 |
75 for (var filePath in paths) { | 83 for (var filePath in paths) { |
76 var filename = path.basenameWithoutExtension(filePath); | 84 var filename = path.basenameWithoutExtension(filePath); |
77 | 85 |
78 test('devc $filename.dart', () { | 86 test('devc $filename.dart', () { |
79 compilerMessages.writeln('// Messages from compiling $filename.dart'); | 87 compilerMessages.writeln('// Messages from compiling $filename.dart'); |
80 | 88 |
81 var result = compile(filePath, realSdk, options); | 89 var result = compile(filePath, realSdk); |
82 var success = !result.failure; | 90 var success = !result.failure; |
83 | 91 |
84 // Write compiler messages to disk. | 92 // Write compiler messages to disk. |
85 new File(path.join(actualDir, '$filename.txt')) | 93 new File(path.join(actualDir, '$filename.txt')) |
86 .writeAsStringSync(compilerMessages.toString()); | 94 .writeAsStringSync(compilerMessages.toString()); |
87 | 95 |
88 var outFile = dartGen | 96 var outFile = dartGen |
89 ? new File(path.join(actualDir, '$filename/$filename.dart')) | 97 ? new File(path.join(actualDir, '$filename/$filename.dart')) |
90 : new File(path.join(actualDir, '$filename.js')); | 98 : new File(path.join(actualDir, '$filename.js')); |
91 expect(outFile.existsSync(), success, | 99 expect(outFile.existsSync(), success, |
(...skipping 17 matching lines...) Expand all Loading... |
109 }); | 117 }); |
110 tearDown(() { | 118 tearDown(() { |
111 AnalysisEngine.instance.logger = savedLogger; | 119 AnalysisEngine.instance.logger = savedLogger; |
112 }); | 120 }); |
113 | 121 |
114 test('devc dart:core', () { | 122 test('devc dart:core', () { |
115 // Get the test SDK. We use a checked in copy so test expectations can be | 123 // Get the test SDK. We use a checked in copy so test expectations can be |
116 // generated against a specific SDK version. | 124 // generated against a specific SDK version. |
117 // TODO(jmesserly): eventually we should track compiler messages. | 125 // TODO(jmesserly): eventually we should track compiler messages. |
118 // For now we're just trying to get decent code generation. | 126 // For now we're just trying to get decent code generation. |
119 var options = new CompilerOptions( | 127 var testSdk = dartGen |
120 outputDir: actualDir, | |
121 checkSdk: true, | |
122 forceCompile: true, | |
123 outputDart: dartGen, | |
124 formatOutput: dartGen, | |
125 cheapTestFormat: true, | |
126 emitSourceMaps: false); | |
127 var sdkPath = dartGen | |
128 ? path.join(testDir, '..', 'tool', 'input_sdk') | 128 ? path.join(testDir, '..', 'tool', 'input_sdk') |
129 : path.join(testDir, 'generated_sdk'); | 129 : path.join(testDir, 'generated_sdk'); |
130 var testSdk = new TypeResolver.fromDir(sdkPath, options); | 130 var result = compile('dart:core', testSdk, true); |
131 compile('dart:core', testSdk, options); | 131 var outputDir = new Directory(path.join(actualDir, 'core')); |
132 var outFile = dartGen | 132 var outFile = dartGen |
133 ? new File(path.join(actualDir, 'core/core')) | 133 ? new File(path.join(actualDir, 'core/core')) |
134 : new File(path.join(actualDir, 'dart/core.js')); | 134 : new File(path.join(actualDir, 'dart/core.js')); |
135 expect(outFile.existsSync(), true, | 135 expect(outFile.existsSync(), true, |
136 reason: '${outFile.path} was created for dart:core'); | 136 reason: '${outFile.path} was created for dart:core'); |
137 }); | 137 }); |
138 }); | 138 }); |
139 | 139 |
140 if (!dartGen) { | 140 if (!dartGen) { |
141 test('devc jscodegen html_input.html', () { | 141 test('devc jscodegen html_input.html', () { |
142 var filePath = path.join(inputDir, 'html_input.html'); | 142 var filePath = path.join(inputDir, 'html_input.html'); |
143 compilerMessages.writeln('// Messages from compiling html_input.html'); | 143 compilerMessages.writeln('// Messages from compiling html_input.html'); |
144 | 144 |
145 var result = compile(filePath, realSdk, options); | 145 var result = compile(filePath, realSdk); |
146 var success = !result.failure; | 146 var success = !result.failure; |
147 | 147 |
148 // Write compiler messages to disk. | 148 // Write compiler messages to disk. |
149 new File(path.join(actualDir, 'html_input.txt')) | 149 new File(path.join(actualDir, 'html_input.txt')) |
150 .writeAsStringSync(compilerMessages.toString()); | 150 .writeAsStringSync(compilerMessages.toString()); |
151 | 151 |
152 var outFile = new File(path.join(actualDir, 'html_input.html')); | 152 var outFile = new File(path.join(actualDir, 'html_input.html')); |
153 expect(outFile.existsSync(), success, | 153 expect(outFile.existsSync(), success, |
154 reason: '${outFile.path} was created iff compilation succeeds'); | 154 reason: '${outFile.path} was created iff compilation succeeds'); |
155 }); | 155 }); |
156 } | 156 } |
157 } | 157 } |
158 | 158 |
159 /// An implementation of analysis engine's [Logger] that prints. | 159 /// An implementation of analysis engine's [Logger] that prints. |
160 class PrintLogger implements Logger { | 160 class PrintLogger implements Logger { |
161 @override void logError(String message, [CaughtException exception]) { | 161 @override void logError(String message, [CaughtException exception]) { |
162 print('[AnalysisEngine] error $message $exception'); | 162 print('[AnalysisEngine] error $message $exception'); |
163 } | 163 } |
164 | 164 |
165 @override void logError2(String message, Object exception) { | 165 @override void logError2(String message, Object exception) { |
166 print('[AnalysisEngine] error $message $exception'); | 166 print('[AnalysisEngine] error $message $exception'); |
167 } | 167 } |
168 | 168 |
169 void logInformation(String message, [CaughtException exception]) {} | 169 void logInformation(String message, [CaughtException exception]) {} |
170 void logInformation2(String message, Object exception) {} | 170 void logInformation2(String message, Object exception) {} |
171 } | 171 } |
OLD | NEW |