Chromium Code Reviews| Index: dart/tests/lib/mirrors/stringify.dart |
| diff --git a/dart/tests/lib/mirrors/stringify.dart b/dart/tests/lib/mirrors/stringify.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4316c420ce86fbfd42faa7269be70c0c89faf7b5 |
| --- /dev/null |
| +++ b/dart/tests/lib/mirrors/stringify.dart |
| @@ -0,0 +1,105 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/// Helper methods for converting a [Mirror] to a [String]. |
| +library test.stringify; |
| + |
| +import 'dart:mirrors'; |
| + |
| +import 'package:expect/expect.dart'; |
| + |
| +name(DeclarationMirror mirror) { |
| + return (mirror == null) ? '<null>' : stringify(mirror.simpleName); |
| +} |
| + |
| +stringifyMap(Map map) { |
| + var buffer = new StringBuffer('{'); |
|
kasperl
2013/06/28 10:18:09
Nit: Consider using an empty string buffer and end
ahe
2013/06/28 11:07:12
Done.
|
| + bool first = true; |
| + for (String key in map.keys.map(MirrorSystem.getName).toList()..sort()) { |
| + if (!first) buffer.write(', '); |
| + first = false; |
| + buffer.write(key); |
| + buffer.write(': '); |
| + buffer.write(stringify(map[new Symbol(key)])); |
| + } |
| + return (buffer..write('}')).toString(); |
| +} |
| + |
| +stringifyList(List list) { |
| + var buffer = new StringBuffer('['); |
| + bool first = true; |
| + for (String value in list.map(stringify)) { |
| + if (!first) buffer.write(', '); |
| + first = false; |
| + buffer.write(value); |
| + } |
| + return (buffer..write(']')).toString(); |
| +} |
| + |
| +stringifySymbol(Symbol symbol) => 's(${MirrorSystem.getName(symbol)})'; |
| + |
| +writeDeclarationOn(DeclarationMirror mirror, StringBuffer buffer) { |
| + buffer.write(stringify(mirror.simpleName)); |
| + if (mirror.owner != null) { |
| + buffer.write(' in '); |
| + buffer.write(name(mirror.owner)); |
| + } |
| + if (mirror.isPrivate) buffer.write(', private'); |
| + if (mirror.isTopLevel) buffer.write(', top-level'); |
| +} |
| + |
| +writeVariableOn(VariableMirror variable, StringBuffer buffer) { |
| + writeDeclarationOn(variable, buffer); |
| + if (variable.isStatic) buffer.write(', static'); |
| + if (variable.isFinal) buffer.write(', final'); |
| +} |
| + |
| +stringifyVariable(VariableMirror variable) { |
| + var buffer = new StringBuffer('Variable('); |
| + writeVariableOn(variable, buffer); |
| + return (buffer..write(')')).toString(); |
| +} |
| + |
| +stringifyParameter(ParameterMirror parameter) { |
| + var buffer = new StringBuffer('Parameter('); |
| + writeVariableOn(parameter, buffer); |
| + if (parameter.isOptional) buffer.write(', optional'); |
| + if (parameter.isNamed) buffer.write(', named'); |
| + if (parameter.hasDefaultValue) { |
| + buffer.write(', value = ${stringify(parameter.defaultValue)}'); |
| + } |
| + // TODO(ahe): Move to writeVariableOn. |
| + buffer.write(', type = ${stringifyType(parameter.type)}'); |
| + return (buffer..write(')')).toString(); |
| +} |
| + |
| +stringifyType(TypeMirror type) { |
| + var buffer = new StringBuffer('Type('); |
| + writeDeclarationOn(type, buffer); |
| + return (buffer..write(')')).toString(); |
| +} |
| + |
| +stringifyMethod(MethodMirror method) { |
| + var buffer = new StringBuffer('Method('); |
| + writeDeclarationOn(method, buffer); |
| + if (method.isStatic) buffer.write(', static'); |
| + if (method.isGetter) buffer.write(', getter'); |
| + if (method.isSetter) buffer.write(', setter'); |
| + if (method.isConstructor) buffer.write(', constructor'); |
| + return (buffer..write(')')).toString(); |
| +} |
| + |
| +stringify(value) { |
| + if (value is Map) return stringifyMap(value); |
| + if (value is List) return stringifyList(value); |
| + if (value is ParameterMirror) return stringifyParameter(value); |
| + if (value is VariableMirror) return stringifyVariable(value); |
| + if (value is MethodMirror) return stringifyMethod(value); |
| + if (value is Symbol) return stringifySymbol(value); |
| + if (value is TypeMirror) return stringifyType(value); |
| + if (value == null) return '<null>'; |
| + throw 'Unexpected value: $value'; |
| +} |
| + |
| +expect(expected, actual) => Expect.stringEquals(expected, stringify(actual)); |