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

Side by Side Diff: tests/compiler/dart2js/exit_code_test.dart

Issue 80793002: Check compiler exitCode. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 1 month 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
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
5 // Test the exit code of dart2js in case of exceptions, fatal errors, errors,
6 // warnings, etc.
7
8
9 import 'dart:async';
10 import 'dart:io' show Platform;
11
12 import 'package:async_helper/async_helper.dart';
13 import 'package:expect/expect.dart';
14
15 import '../../../sdk/lib/_internal/compiler/compiler.dart' as api;
16 import '../../../sdk/lib/_internal/compiler/implementation/dart2js.dart' as entr y;
17 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart';
18 import '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart' as apii mpl;
19 import '../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar t';
20 import '../../../sdk/lib/_internal/compiler/implementation/scanner/scannerlib.da rt';
21 import '../../../sdk/lib/_internal/compiler/implementation/util/util.dart';
22
23 class TestCompiler extends apiimpl.Compiler {
24 final String testMarker;
25 final String testType;
26 final Function onTest;
27
28 TestCompiler(api.CompilerInputProvider inputProvider,
29 api.CompilerOutputProvider outputProvider,
30 api.DiagnosticHandler handler,
31 Uri libraryRoot,
32 Uri packageRoot,
33 List<String> options,
34 Map<String, dynamic> environment,
35 String this.testMarker,
36 String this.testType,
37 Function this.onTest)
38 : super(inputProvider, outputProvider, handler, libraryRoot,
39 packageRoot, options, environment) {
40 scanner = new TestScanner(this);
41 test('Compiler');
42 }
43
44 Future<bool> run(Uri uri) {
45 test('Compiler.run');
46 return super.run(uri);
47 }
48
49 TreeElements analyzeElement(Element element) {
50 test('Compiler.analyzeElement');
51 return super.analyzeElement(element);
52 }
53
54 void codegen(CodegenWorkItem work, CodegenEnqueuer world) {
55 test('Compiler.codegen');
56 super.codegen(work, world);
57 }
58
59 withCurrentElement(Element element, f()) {
60 return super.withCurrentElement(element, () {
61 test('Compiler.withCurrentElement');
62 return f();
63 });
64 }
65
66 test(String marker) {
67 if (marker == testMarker) {
68 switch (testType) {
69 case 'assert':
70 onTest(testMarker, testType);
71 assert(false);
72 break;
73 case 'invariant':
74 onTest(testMarker, testType);
75 invariant(CURRENT_ELEMENT_SPANNABLE, false, message: marker);
76 break;
77 case 'warning':
78 onTest(testMarker, testType);
79 reportWarningCode(CURRENT_ELEMENT_SPANNABLE,
80 MessageKind.GENERIC, {'text': marker});
81 break;
82 case 'error':
83 onTest(testMarker, testType);
84 reportError(CURRENT_ELEMENT_SPANNABLE,
85 MessageKind.GENERIC, {'text': marker});
86 break;
87 case 'fatalError':
88 onTest(testMarker, testType);
89 reportFatalError(CURRENT_ELEMENT_SPANNABLE,
90 MessageKind.GENERIC, {'text': marker});
91 break;
92 case 'NoSuchMethodError':
93 onTest(testMarker, testType);
94 null.foo;
95 break;
96 case '':
97 onTest(testMarker, testType);
98 break;
99 }
100 }
101 }
102 }
103
104 class TestScanner extends ScannerTask {
105 TestScanner(TestCompiler compiler) : super(compiler);
106
107 TestCompiler get compiler => super.compiler;
108
109 void scanElements(CompilationUnitElement compilationUnit) {
110 compiler.test('ScannerTask.scanElements');
111 super.scanElements(compilationUnit);
112 }
113
114 }
115
116 Future testExitCode(String marker, String type, int expectedExitCode) {
117 bool testOccurred = false;
118 void onTest(String testMarker, String testType) {
119 if (testMarker == marker && testType == type) {
120 testOccurred = true;
121 }
122 }
123 return new Future(() {
124 Future<String> compile(Uri script,
125 Uri libraryRoot,
126 Uri packageRoot,
127 api.CompilerInputProvider inputProvider,
128 api.DiagnosticHandler handler,
129 [List<String> options = const [],
130 api.CompilerOutputProvider outputProvider,
131 Map<String, dynamic> environment = const {}]) {
132 libraryRoot = Platform.script.resolve('../../../sdk/');
133 outputProvider = NullSink.outputProvider;
134 handler = (uri, begin, end, message, kind) {};
135 Compiler compiler = new TestCompiler(inputProvider,
136 outputProvider,
137 handler,
138 libraryRoot,
139 packageRoot,
140 options,
141 environment,
142 marker,
143 type,
144 onTest);
145 return compiler.run(script).then((_) {
146 String code = compiler.assembledCode;
147 if (code != null && outputProvider != null) {
148 String outputType = 'js';
149 if (options.contains('--output-type=dart')) {
150 outputType = 'dart';
151 }
152 outputProvider('', outputType)
153 ..add(code)
154 ..close();
155 code = ''; // Non-null signals success.
156 }
157 return code;
158 });
159 }
160
161 int foundExitCode;
162
163 checkResult() {
164 Expect.isTrue(testOccurred, 'testExitCode($marker, $type) did not occur');
165 if (foundExitCode == null) foundExitCode = 0;
166 print('testExitCode($marker, $type) '
167 'exitCode=$foundExitCode expected=$expectedExitCode');
168 Expect.equals(expectedExitCode, foundExitCode,
169 'testExitCode($marker, $type) '
170 'exitCode=$foundExitCode expected=${expectedExitCode}');
171 }
172
173 void exit(exitCode) {
174 if (foundExitCode == null) {
175 foundExitCode = exitCode;
176 }
177 };
178
179 Future result = entry.internalMain(
180 exit, compile,
181 ["tests/compiler/dart2js/exit_code_helper.dart"]);
182 return result.whenComplete(checkResult);
183 });
184 }
185
186 Future testExitCodes(String marker, Map<String,int> expectedExitCodes) {
187 return Future.forEach(expectedExitCodes.keys, (String type) {
188 return testExitCode(marker, type, expectedExitCodes[type]);
189 });
190 }
191
192 void main() {
193 const beforeRun = const {
194 '': 0,
195 'NoSuchMethodError': 253,
196 'assert': 253,
197 'invariant': 253
198 };
199
200 const duringRun = const {
201 '': 0,
202 'NoSuchMethodError': 253,
203 'assert': 253,
204 'invariant': 253,
205 'warning': 0,
206 'error': 1,
207 'fatalError': 1,
208 };
209
210 const tests = const {
211 'Compiler': beforeRun,
212 'Compiler.run': beforeRun,
213 'ScannerTask.scanElements': duringRun,
214 'Compiler.withCurrentElement': duringRun,
215 'Compiler.analyzeElement': duringRun,
216 'Compiler.codegen': duringRun,
217 };
218
219 asyncTest(() => Future.forEach(tests.keys, (marker) {
220 return testExitCodes(marker, tests[marker]);
221 }));
222 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698