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 /// Helper methods for converting a [Mirror] to a [String]. | |
| 6 library test.stringify; | |
| 7 | |
| 8 import 'dart:mirrors'; | |
| 9 | |
| 10 import 'package:expect/expect.dart'; | |
| 11 | |
| 12 name(DeclarationMirror mirror) { | |
| 13 return (mirror == null) ? '<null>' : stringify(mirror.simpleName); | |
| 14 } | |
| 15 | |
| 16 stringifyMap(Map map) { | |
| 17 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.
| |
| 18 bool first = true; | |
| 19 for (String key in map.keys.map(MirrorSystem.getName).toList()..sort()) { | |
| 20 if (!first) buffer.write(', '); | |
| 21 first = false; | |
| 22 buffer.write(key); | |
| 23 buffer.write(': '); | |
| 24 buffer.write(stringify(map[new Symbol(key)])); | |
| 25 } | |
| 26 return (buffer..write('}')).toString(); | |
| 27 } | |
| 28 | |
| 29 stringifyList(List list) { | |
| 30 var buffer = new StringBuffer('['); | |
| 31 bool first = true; | |
| 32 for (String value in list.map(stringify)) { | |
| 33 if (!first) buffer.write(', '); | |
| 34 first = false; | |
| 35 buffer.write(value); | |
| 36 } | |
| 37 return (buffer..write(']')).toString(); | |
| 38 } | |
| 39 | |
| 40 stringifySymbol(Symbol symbol) => 's(${MirrorSystem.getName(symbol)})'; | |
| 41 | |
| 42 writeDeclarationOn(DeclarationMirror mirror, StringBuffer buffer) { | |
| 43 buffer.write(stringify(mirror.simpleName)); | |
| 44 if (mirror.owner != null) { | |
| 45 buffer.write(' in '); | |
| 46 buffer.write(name(mirror.owner)); | |
| 47 } | |
| 48 if (mirror.isPrivate) buffer.write(', private'); | |
| 49 if (mirror.isTopLevel) buffer.write(', top-level'); | |
| 50 } | |
| 51 | |
| 52 writeVariableOn(VariableMirror variable, StringBuffer buffer) { | |
| 53 writeDeclarationOn(variable, buffer); | |
| 54 if (variable.isStatic) buffer.write(', static'); | |
| 55 if (variable.isFinal) buffer.write(', final'); | |
| 56 } | |
| 57 | |
| 58 stringifyVariable(VariableMirror variable) { | |
| 59 var buffer = new StringBuffer('Variable('); | |
| 60 writeVariableOn(variable, buffer); | |
| 61 return (buffer..write(')')).toString(); | |
| 62 } | |
| 63 | |
| 64 stringifyParameter(ParameterMirror parameter) { | |
| 65 var buffer = new StringBuffer('Parameter('); | |
| 66 writeVariableOn(parameter, buffer); | |
| 67 if (parameter.isOptional) buffer.write(', optional'); | |
| 68 if (parameter.isNamed) buffer.write(', named'); | |
| 69 if (parameter.hasDefaultValue) { | |
| 70 buffer.write(', value = ${stringify(parameter.defaultValue)}'); | |
| 71 } | |
| 72 // TODO(ahe): Move to writeVariableOn. | |
| 73 buffer.write(', type = ${stringifyType(parameter.type)}'); | |
| 74 return (buffer..write(')')).toString(); | |
| 75 } | |
| 76 | |
| 77 stringifyType(TypeMirror type) { | |
| 78 var buffer = new StringBuffer('Type('); | |
| 79 writeDeclarationOn(type, buffer); | |
| 80 return (buffer..write(')')).toString(); | |
| 81 } | |
| 82 | |
| 83 stringifyMethod(MethodMirror method) { | |
| 84 var buffer = new StringBuffer('Method('); | |
| 85 writeDeclarationOn(method, buffer); | |
| 86 if (method.isStatic) buffer.write(', static'); | |
| 87 if (method.isGetter) buffer.write(', getter'); | |
| 88 if (method.isSetter) buffer.write(', setter'); | |
| 89 if (method.isConstructor) buffer.write(', constructor'); | |
| 90 return (buffer..write(')')).toString(); | |
| 91 } | |
| 92 | |
| 93 stringify(value) { | |
| 94 if (value is Map) return stringifyMap(value); | |
| 95 if (value is List) return stringifyList(value); | |
| 96 if (value is ParameterMirror) return stringifyParameter(value); | |
| 97 if (value is VariableMirror) return stringifyVariable(value); | |
| 98 if (value is MethodMirror) return stringifyMethod(value); | |
| 99 if (value is Symbol) return stringifySymbol(value); | |
| 100 if (value is TypeMirror) return stringifyType(value); | |
| 101 if (value == null) return '<null>'; | |
| 102 throw 'Unexpected value: $value'; | |
| 103 } | |
| 104 | |
| 105 expect(expected, actual) => Expect.stringEquals(expected, stringify(actual)); | |
| OLD | NEW |