From 6fd76b92dc4b679b48345c9035aaa34ad594e596 Mon Sep 17 00:00:00 2001 From: "johnniwinther@google.com" Date: Fri, 7 Feb 2014 09:26:15 +0000 Subject: [PATCH] Add mirrors_reader_test which tests that everything reachable from a [MirrorSystem] can be accessed. BUG= R=floitsch@google.com, rmacnak@google.com Review URL: https://codereview.chromium.org//153553007 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@32415 260f80e4-7a28-3924-810f-c04153c831b5 --- .../compiler/implementation/compiler.dart | 12 ++-- .../dart2js/mirrors/mirrors_reader_test.dart | 25 +++++++- tests/lib/mirrors/mirrors_reader.dart | 58 ++++++++++--------- tests/lib/mirrors/mirrors_reader_test.dart | 14 +++-- 4 files changed, 71 insertions(+), 38 deletions(-) diff --git a/sdk/lib/_internal/compiler/implementation/compiler.dart b/sdk/lib/_internal/compiler/implementation/compiler.dart index d13da550c6b..2551bf87be7 100644 --- a/sdk/lib/_internal/compiler/implementation/compiler.dart +++ b/sdk/lib/_internal/compiler/implementation/compiler.dart @@ -499,10 +499,7 @@ abstract class Compiler implements DiagnosticListener { return f(); } on SpannableAssertionFailure catch (ex) { if (!hasCrashed) { - String message = (ex.message != null) ? tryToString(ex.message) - : tryToString(ex); - SourceSpan span = spanFromSpannable(ex.node); - reportError(ex.node, MessageKind.GENERIC, {'text': message}); + reportAssertionFailure(ex); pleaseReportCrash(); } hasCrashed = true; @@ -1397,6 +1394,13 @@ abstract class Compiler implements DiagnosticListener { void reportDiagnostic(SourceSpan span, String message, api.Diagnostic kind); + void reportAssertionFailure(SpannableAssertionFailure ex) { + String message = (ex.message != null) ? tryToString(ex.message) + : tryToString(ex); + SourceSpan span = spanFromSpannable(ex.node); + reportError(ex.node, MessageKind.GENERIC, {'text': message}); + } + SourceSpan spanFromTokens(Token begin, Token end, [Uri uri]) { if (begin == null || end == null) { // TODO(ahe): We can almost always do better. Often it is only diff --git a/tests/compiler/dart2js/mirrors/mirrors_reader_test.dart b/tests/compiler/dart2js/mirrors/mirrors_reader_test.dart index 4645ec28e08..53c9cebb586 100644 --- a/tests/compiler/dart2js/mirrors/mirrors_reader_test.dart +++ b/tests/compiler/dart2js/mirrors/mirrors_reader_test.dart @@ -12,9 +12,26 @@ import "package:async_helper/async_helper.dart"; import "mirrors_test_helper.dart"; import "../../../lib/mirrors/mirrors_reader.dart"; +import "../../../../sdk/lib/_internal/compiler/implementation/util/util.dart"; +import "../../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirrors.dart"; import "../../../../sdk/lib/_internal/compiler/implementation/mirrors/source_mirrors.dart"; class SourceMirrorsReader extends MirrorsReader { + final Dart2JsMirrorSystem mirrorSystem; + + SourceMirrorsReader(this.mirrorSystem, + {bool verbose: false, bool includeStackTrace: false}) + : super(verbose: verbose, includeStackTrace: includeStackTrace); + + evaluate(f()) { + try { + return f(); + } on SpannableAssertionFailure catch (e) { + mirrorSystem.compiler.reportAssertionFailure(e); + rethrow; + } + } + visitMirror(Mirror mirror) { if (mirror is CombinatorMirror) { visitCombinatorMirror(mirror); @@ -113,10 +130,12 @@ class SourceMirrorsReader extends MirrorsReader { } } -main() { +main(List arguments) { asyncTest(() => analyzeUri(Uri.parse('dart:core')). then((MirrorSystem mirrors) { - MirrorsReader reader = new SourceMirrorsReader(); - readMirrorSystem(reader, mirrors); + MirrorsReader reader = new SourceMirrorsReader(mirrors, + verbose: arguments.contains('-v'), + includeStackTrace: arguments.contains('-s')); + reader.checkMirrorSystem(mirrors); })); } diff --git a/tests/lib/mirrors/mirrors_reader.dart b/tests/lib/mirrors/mirrors_reader.dart index 136896ff81e..9b16f81a34c 100644 --- a/tests/lib/mirrors/mirrors_reader.dart +++ b/tests/lib/mirrors/mirrors_reader.dart @@ -7,29 +7,6 @@ library mirrors.reader; import 'dart:mirrors'; import 'mirrors_visitor.dart'; -/// Produce verbose output. -bool VERBOSE = false; -/// Include stack trace in the error report. -bool INCLUDE_STACK_TRACE = false; - -void readMirrorSystem(MirrorsReader reader, - MirrorSystem mirrorSystem) { - reader.visitMirrorSystem(mirrorSystem); - if (!reader.errors.isEmpty) { - Set errors = new Set(); - for (ReadError error in reader.errors) { - String text = 'Mirrors read error: ${error.tag}=${error.exception}'; - if (INCLUDE_STACK_TRACE) { - text = '$text\n${error.stackTrace}'; - } - if (errors.add(text)) { - print(text); - } - } - throw 'Unexpected errors occurred reading mirrors.'; - } -} - class ReadError { final String tag; final exception; @@ -39,6 +16,11 @@ class ReadError { } class MirrorsReader extends MirrorsVisitor { + /// Produce verbose output. + final bool verbose; + /// Include stack trace in the error report. + final bool includeStackTrace; + bool fatalError = false; Set visited = new Set(); Set declarations = new Set(); @@ -46,6 +28,25 @@ class MirrorsReader extends MirrorsVisitor { List errors = []; List queue = []; + MirrorsReader({this.verbose: false, this.includeStackTrace: false}); + + void checkMirrorSystem(MirrorSystem mirrorSystem) { + visitMirrorSystem(mirrorSystem); + if (!errors.isEmpty) { + Set errorMessages = new Set(); + for (ReadError error in errors) { + String text = 'Mirrors read error: ${error.tag}=${error.exception}'; + if (includeStackTrace) { + text = '$text\n${error.stackTrace}'; + } + if (errorMessages.add(text)) { + print(text); + } + } + throw 'Unexpected errors occurred reading mirrors.'; + } + } + // Skip mirrors so that each mirror is only visited once. bool skipMirror(Mirror mirror) { if (fatalError) return true; @@ -69,7 +70,7 @@ class MirrorsReader extends MirrorsVisitor { visitUnsupported(var receiver, String tag, UnsupportedError exception, StackTrace stackTrace) { - if (VERBOSE) print('visitUnsupported:$receiver.$tag:$exception'); + if (verbose) print('visitUnsupported:$receiver.$tag:$exception'); if (!expectUnsupported(receiver, tag, exception) && !allowUnsupported(receiver, tag, exception)) { reportError(receiver, tag, exception, stackTrace); @@ -84,10 +85,14 @@ class MirrorsReader extends MirrorsVisitor { bool allowUnsupported(var receiver, String tag, UnsupportedError exception) => false; + /// Evaluates the function [f]. Subclasses can override this to handle + /// specific exceptions. + evaluate(f()) => f(); + visit(var receiver, String tag, var value) { if (value is Function) { try { - var result = value(); + var result = evaluate(value); if (expectUnsupported(receiver, tag, null)) { reportError(receiver, tag, 'Expected UnsupportedError.', null); } @@ -106,7 +111,7 @@ class MirrorsReader extends MirrorsVisitor { } else { if (value is Mirror) { if (!skipMirror(value)) { - if (VERBOSE) print('visit:$receiver.$tag=$value'); + if (verbose) print('visit:$receiver.$tag=$value'); bool drain = queue.isEmpty; queue.add(value); if (drain) { @@ -120,6 +125,7 @@ class MirrorsReader extends MirrorsVisitor { } else if (value is SourceLocation) { visitSourceLocation(value); } else if (value is Iterable) { + // TODO(johnniwinther): Merge with `immutable_collections_test.dart`. value.forEach((e) { visit(receiver, tag, e); }); diff --git a/tests/lib/mirrors/mirrors_reader_test.dart b/tests/lib/mirrors/mirrors_reader_test.dart index 637b7bb250c..099d043ef68 100644 --- a/tests/lib/mirrors/mirrors_reader_test.dart +++ b/tests/lib/mirrors/mirrors_reader_test.dart @@ -12,8 +12,10 @@ import 'mirrors_reader.dart'; class RuntimeMirrorsReader extends MirrorsReader { final String mirrorSystemType; - RuntimeMirrorsReader(MirrorSystem mirrorSystem) - : this.mirrorSystemType = '${mirrorSystem.runtimeType}'; + RuntimeMirrorsReader(MirrorSystem mirrorSystem, + {bool verbose: false, bool includeStackTrace: false}) + : this.mirrorSystemType = '${mirrorSystem.runtimeType}', + super(verbose: verbose, includeStackTrace: includeStackTrace); bool allowUnsupported(var receiver, String tag, UnsupportedError exception) { if (mirrorSystemType == '_LocalMirrorSystem') { @@ -42,8 +44,10 @@ class RuntimeMirrorsReader extends MirrorsReader { } } -void main() { +void main(List arguments) { MirrorSystem mirrors = currentMirrorSystem(); - MirrorsReader reader = new RuntimeMirrorsReader(mirrors); - readMirrorSystem(reader, mirrors); + MirrorsReader reader = new RuntimeMirrorsReader(mirrors, + verbose: arguments.contains('-v'), + includeStackTrace: arguments.contains('-s')); + reader.checkMirrorSystem(mirrors); }