Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
|
ahe
2013/11/26 11:18:25
Extra line.
Johnni Winther
2013/11/27 07:58:04
Done.
| |
| 114 } | |
| 115 | |
| 116 Future testExitCode(String marker, String type, int expectedExitCode) { | |
| 117 bool testOccurred = false; | |
| 118 void onTest(String testMarker, String testType) { | |
|
ahe
2013/11/26 11:18:25
Add lines around function.
Johnni Winther
2013/11/27 07:58:04
Done.
| |
| 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 entry.exitFunc = exit; | |
| 180 entry.compileFunc = compile; | |
| 181 | |
| 182 Future result = entry.internalMain( | |
| 183 ["tests/compiler/dart2js/exit_code_helper.dart"]); | |
| 184 return result.whenComplete(checkResult); | |
| 185 }); | |
| 186 } | |
| 187 | |
| 188 Future testExitCodes(String marker, Map<String,int> expectedExitCodes) { | |
| 189 return Future.forEach(expectedExitCodes.keys, (String type) { | |
| 190 return testExitCode(marker, type, expectedExitCodes[type]); | |
| 191 }); | |
| 192 } | |
| 193 | |
| 194 void main() { | |
| 195 const beforeRun = const { | |
| 196 '': 0, | |
| 197 'NoSuchMethodError': 253, | |
| 198 'assert': 253, | |
| 199 'invariant': 253 | |
| 200 }; | |
| 201 | |
| 202 const duringRun = const { | |
| 203 '': 0, | |
| 204 'NoSuchMethodError': 253, | |
| 205 'assert': 253, | |
| 206 'invariant': 253, | |
| 207 'warning': 0, | |
| 208 'error': 1, | |
| 209 'fatalError': 1, | |
| 210 }; | |
| 211 | |
| 212 const tests = const { | |
| 213 'Compiler': beforeRun, | |
| 214 'Compiler.run': beforeRun, | |
| 215 'ScannerTask.scanElements': duringRun, | |
| 216 'Compiler.withCurrentElement': duringRun, | |
| 217 'Compiler.analyzeElement': duringRun, | |
| 218 'Compiler.codegen': duringRun, | |
| 219 }; | |
| 220 | |
| 221 asyncTest(() => Future.forEach(tests.keys, (marker) { | |
| 222 return testExitCodes(marker, tests[marker]); | |
| 223 })); | |
|
ahe
2013/11/26 11:18:25
Can we add a then() section here that checks that
Johnni Winther
2013/11/27 07:58:04
Done.
| |
| 224 } | |
| OLD | NEW |