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(); |
| 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}'; |
| 27 } |
| 28 |
| 29 stringifyIterable(Iterable 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]'; |
| 38 } |
| 39 |
| 40 stringifyInstance(InstanceMirror instance) { |
| 41 var buffer = new StringBuffer(); |
| 42 if (instance.hasReflectee) { |
| 43 buffer.write('value = ${stringify(instance.reflectee)}'); |
| 44 } |
| 45 return 'Instance(${buffer})'; |
| 46 } |
| 47 |
| 48 stringifySymbol(Symbol symbol) => 's(${MirrorSystem.getName(symbol)})'; |
| 49 |
| 50 writeDeclarationOn(DeclarationMirror mirror, StringBuffer buffer) { |
| 51 buffer.write(stringify(mirror.simpleName)); |
| 52 if (mirror.owner != null) { |
| 53 buffer.write(' in '); |
| 54 buffer.write(name(mirror.owner)); |
| 55 } |
| 56 if (mirror.isPrivate) buffer.write(', private'); |
| 57 if (mirror.isTopLevel) buffer.write(', top-level'); |
| 58 } |
| 59 |
| 60 writeVariableOn(VariableMirror variable, StringBuffer buffer) { |
| 61 writeDeclarationOn(variable, buffer); |
| 62 if (variable.isStatic) buffer.write(', static'); |
| 63 if (variable.isFinal) buffer.write(', final'); |
| 64 } |
| 65 |
| 66 stringifyVariable(VariableMirror variable) { |
| 67 var buffer = new StringBuffer(); |
| 68 writeVariableOn(variable, buffer); |
| 69 return 'Variable($buffer)'; |
| 70 } |
| 71 |
| 72 stringifyParameter(ParameterMirror parameter) { |
| 73 var buffer = new StringBuffer(); |
| 74 writeVariableOn(parameter, buffer); |
| 75 if (parameter.isOptional) buffer.write(', optional'); |
| 76 if (parameter.isNamed) buffer.write(', named'); |
| 77 // TODO(6490): dart2js always returns false for hasDefaultValue. |
| 78 if (parameter.hasDefaultValue) { |
| 79 buffer.write(', value = ${stringify(parameter.defaultValue)}'); |
| 80 } |
| 81 // TODO(ahe): Move to writeVariableOn. |
| 82 buffer.write(', type = ${stringify(parameter.type)}'); |
| 83 return 'Parameter($buffer)'; |
| 84 } |
| 85 |
| 86 stringifyTypeVariable(TypeVariableMirror typeVariable) { |
| 87 var buffer = new StringBuffer(); |
| 88 writeDeclarationOn(typeVariable, buffer); |
| 89 buffer.write(', upperBound = ${stringify(typeVariable.upperBound)}'); |
| 90 return 'TypeVariable($buffer)'; |
| 91 } |
| 92 |
| 93 stringifyType(TypeMirror type) { |
| 94 var buffer = new StringBuffer(); |
| 95 writeDeclarationOn(type, buffer); |
| 96 return 'Type($buffer)'; |
| 97 } |
| 98 |
| 99 stringifyClass(ClassMirror cls) { |
| 100 var buffer = new StringBuffer(); |
| 101 writeDeclarationOn(cls, buffer); |
| 102 return 'Class($buffer)'; |
| 103 } |
| 104 |
| 105 stringifyMethod(MethodMirror method) { |
| 106 var buffer = new StringBuffer(); |
| 107 writeDeclarationOn(method, buffer); |
| 108 if (method.isAbstract) buffer.write(', abstract'); |
| 109 if (method.isSynthetic) buffer.write(', synthetic'); |
| 110 if (method.isStatic) buffer.write(', static'); |
| 111 if (method.isGetter) buffer.write(', getter'); |
| 112 if (method.isSetter) buffer.write(', setter'); |
| 113 if (method.isConstructor) buffer.write(', constructor'); |
| 114 return 'Method($buffer)'; |
| 115 } |
| 116 |
| 117 stringifyDependencies(LibraryMirror l) { |
| 118 n(s) => s is Symbol ? MirrorSystem.getName(s) : s; |
| 119 compareDep(a, b) { |
| 120 if (a.targetLibrary == b.targetLibrary) { |
| 121 if ((a.prefix != null) && (b.prefix != null)) { |
| 122 return n(a.prefix).compareTo(n(b.prefix)); |
| 123 } |
| 124 return a.prefix == null ? 1 : -1; |
| 125 } |
| 126 return n(a.targetLibrary.simpleName) |
| 127 .compareTo(n(b.targetLibrary.simpleName)); |
| 128 } |
| 129 |
| 130 compareCom(a, b) => n(a.identifier).compareTo(n(b.identifier)); |
| 131 compareFirst(a, b) => a[0].compareTo(b[0]); |
| 132 sortBy(c, p) => new List.from(c)..sort(p); |
| 133 |
| 134 var buffer = new StringBuffer(); |
| 135 sortBy(l.libraryDependencies, compareDep).forEach((dep) { |
| 136 if (dep.isImport) buffer.write('import '); |
| 137 if (dep.isExport) buffer.write('export '); |
| 138 buffer.write(n(dep.targetLibrary.simpleName)); |
| 139 if (dep.isDeferred) buffer.write(' deferred'); |
| 140 if (dep.prefix != null) buffer.write(' as ${n(dep.prefix)}'); |
| 141 buffer.write('\n'); |
| 142 |
| 143 List flattenedCombinators = new List(); |
| 144 dep.combinators.forEach((com) { |
| 145 com.identifiers.forEach((ident) { |
| 146 flattenedCombinators.add([n(ident), com.isShow, com.isHide]); |
| 147 }); |
| 148 }); |
| 149 sortBy(flattenedCombinators, compareFirst).forEach((triple) { |
| 150 buffer.write(' '); |
| 151 if (triple[1]) buffer.write('show '); |
| 152 if (triple[2]) buffer.write('hide '); |
| 153 buffer.write(triple[0]); |
| 154 buffer.write('\n'); |
| 155 }); |
| 156 }); |
| 157 return buffer.toString(); |
| 158 } |
| 159 |
| 160 String stringify(value) { |
| 161 if (value is Map) return stringifyMap(value); |
| 162 if (value is Iterable) return stringifyIterable(value); |
| 163 if (value is InstanceMirror) return stringifyInstance(value); |
| 164 if (value is ParameterMirror) return stringifyParameter(value); |
| 165 if (value is VariableMirror) return stringifyVariable(value); |
| 166 if (value is MethodMirror) return stringifyMethod(value); |
| 167 if (value is num) return value.toString(); |
| 168 if (value is String) return value; |
| 169 if (value is Symbol) return stringifySymbol(value); |
| 170 if (value is ClassMirror) return stringifyClass(value); |
| 171 if (value is TypeVariableMirror) return stringifyTypeVariable(value); |
| 172 if (value is TypeMirror) return stringifyType(value); |
| 173 if (value == null) return '<null>'; |
| 174 throw 'Unexpected value: $value'; |
| 175 } |
| 176 |
| 177 expect(expected, actual, [String reason]) { |
| 178 Expect.stringEquals(expected, stringify(actual), reason); |
| 179 } |
| 180 |
| 181 int compareSymbols(Symbol a, Symbol b) { |
| 182 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); |
| 183 } |
| 184 |
| 185 simpleNames(Iterable<Mirror> i) => |
| 186 i.map((e) => (e as DeclarationMirror).simpleName); |
| 187 |
| 188 sort(Iterable<Symbol> symbols) => symbols.toList()..sort(compareSymbols); |
OLD | NEW |