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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<String> 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);
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -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<String> errors = new Set<String>();
|
||||
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<Mirror> visited = new Set<Mirror>();
|
||||
Set<TypeMirror> declarations = new Set<TypeMirror>();
|
||||
@@ -46,6 +28,25 @@ class MirrorsReader extends MirrorsVisitor {
|
||||
List<ReadError> errors = <ReadError>[];
|
||||
List<Mirror> queue = <Mirror>[];
|
||||
|
||||
MirrorsReader({this.verbose: false, this.includeStackTrace: false});
|
||||
|
||||
void checkMirrorSystem(MirrorSystem mirrorSystem) {
|
||||
visitMirrorSystem(mirrorSystem);
|
||||
if (!errors.isEmpty) {
|
||||
Set<String> errorMessages = new Set<String>();
|
||||
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);
|
||||
});
|
||||
|
||||
@@ -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<String> 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user