OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 import 'package:async_helper/async_helper.dart'; |
| 7 import 'package:expect/expect.dart'; |
| 8 import 'package:compiler/compiler_new.dart'; |
| 9 import 'memory_compiler.dart'; |
| 10 |
| 11 final EXCEPTION = 'Crash'; |
| 12 |
| 13 main() { |
| 14 asyncTest(() async { |
| 15 test('Empty program', await run()); |
| 16 test('Crash diagnostics', |
| 17 await run(diagnostics: new CrashingDiagnostics()), |
| 18 expectedLines: [ |
| 19 'Uncaught exception in diagnostic handler: $EXCEPTION', |
| 20 null /* Stack trace*/], |
| 21 expectedExceptions: [EXCEPTION]); |
| 22 test('Throw in package discovery', |
| 23 await run(packagesDiscoveryProvider: (_) { throw EXCEPTION; }), |
| 24 expectedLines: [ |
| 25 'Uncaught exception in package discovery: $EXCEPTION', |
| 26 null /* Stack trace*/], |
| 27 expectedExceptions: [EXCEPTION]); |
| 28 test('new Future.error in package discovery', |
| 29 await run(packagesDiscoveryProvider: |
| 30 (_) => new Future.error(EXCEPTION)), |
| 31 expectedExceptions: [EXCEPTION]); |
| 32 test('Throw in input provider', |
| 33 await run(memorySourceFiles: new CrashingMap()), |
| 34 expectedLines: [ |
| 35 'Uncaught exception in input provider: $EXCEPTION', |
| 36 null, // Stack trace |
| 37 'memory:main.dart:\nError: $EXCEPTION' /* READ_SELF_ERROR */]); |
| 38 }); |
| 39 } |
| 40 |
| 41 void test(String title, RunResult result, |
| 42 {List expectedLines: const [], |
| 43 List expectedExceptions: const []}) { |
| 44 print('--------------------------------------------------------------------'); |
| 45 print('Running $title'); |
| 46 print('--------------------------------------------------------------------'); |
| 47 print('lines:'); |
| 48 result.lines.forEach(print); |
| 49 print('exceptions:'); |
| 50 result.exceptions.forEach(print); |
| 51 Expect.equals(expectedLines.length, result.lines.length, |
| 52 "Unexpected number of calls to print."); |
| 53 Expect.equals(expectedExceptions.length, result.exceptions.length, |
| 54 "Unexpected number of exceptions."); |
| 55 for (int i = 0; i < expectedLines.length; i++) { |
| 56 if (expectedLines[i] != null) { |
| 57 Expect.equals(expectedLines[i], result.lines[i]); |
| 58 } |
| 59 } |
| 60 } |
| 61 |
| 62 Future<RunResult> run( |
| 63 {Map<String, String> memorySourceFiles: const {'main.dart': 'main() {}'}, |
| 64 CompilerDiagnostics diagnostics, |
| 65 PackagesDiscoveryProvider packagesDiscoveryProvider}) async { |
| 66 RunResult result = new RunResult(); |
| 67 await runZoned(() async { |
| 68 try { |
| 69 await runCompiler( |
| 70 entryPoint: Uri.parse('memory:main.dart'), |
| 71 memorySourceFiles: memorySourceFiles, |
| 72 diagnosticHandler: diagnostics, |
| 73 packagesDiscoveryProvider: packagesDiscoveryProvider); |
| 74 } catch (e) { |
| 75 result.exceptions.add(e); |
| 76 } |
| 77 |
| 78 }, |
| 79 zoneSpecification: new ZoneSpecification(print: |
| 80 (Zone self, ZoneDelegate parent, Zone zone, String line) { |
| 81 result.lines.add(line); |
| 82 })); |
| 83 return result; |
| 84 } |
| 85 |
| 86 class RunResult { |
| 87 List<String> lines = <String>[]; |
| 88 List exceptions = []; |
| 89 } |
| 90 |
| 91 class CrashingDiagnostics extends DiagnosticCollector { |
| 92 @override |
| 93 void report(code, Uri uri, int begin, int end, String text, Diagnostic kind) { |
| 94 throw EXCEPTION; |
| 95 } |
| 96 } |
| 97 |
| 98 class CrashingMap implements Map<String, String> { |
| 99 operator [](_) => throw EXCEPTION; |
| 100 |
| 101 noSuchMethod(_) => null; |
| 102 } |
OLD | NEW |