OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Helper methods for converting a [Mirror] to a [String]. | 5 /// Helper methods for converting a [Mirror] to a [String]. |
6 library test.stringify; | 6 library test.stringify; |
7 | 7 |
8 import 'dart:mirrors'; | 8 import 'dart:mirrors'; |
9 | 9 |
10 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 writeDeclarationOn(method, buffer); | 107 writeDeclarationOn(method, buffer); |
108 if (method.isAbstract) buffer.write(', abstract'); | 108 if (method.isAbstract) buffer.write(', abstract'); |
109 if (method.isSynthetic) buffer.write(', synthetic'); | 109 if (method.isSynthetic) buffer.write(', synthetic'); |
110 if (method.isStatic) buffer.write(', static'); | 110 if (method.isStatic) buffer.write(', static'); |
111 if (method.isGetter) buffer.write(', getter'); | 111 if (method.isGetter) buffer.write(', getter'); |
112 if (method.isSetter) buffer.write(', setter'); | 112 if (method.isSetter) buffer.write(', setter'); |
113 if (method.isConstructor) buffer.write(', constructor'); | 113 if (method.isConstructor) buffer.write(', constructor'); |
114 return 'Method($buffer)'; | 114 return 'Method($buffer)'; |
115 } | 115 } |
116 | 116 |
117 stringifyDependencies(LibraryMirror l) { | |
118 n(s) => s is Symbol ? MirrorSystem.getName(s) : s; | |
119 compareDep(a, b) => | |
120 n(a.targetLibrary.simpleName).compareTo(n(b.targetLibrary.simpleName)); | |
121 compareCom(a, b) => n(a.identifier).compareTo(n(b.identifier)); | |
122 compareFirst(a, b) => a[0].compareTo(b[0]); | |
123 sortBy(c, p) => new List.from(c)..sort(p); | |
124 | |
125 var buffer = new StringBuffer(); | |
126 sortBy(l.libraryDependencies, compareDep).forEach((dep) { | |
127 if (dep.isImport) buffer.write('import '); | |
128 if (dep.isExport) buffer.write('export '); | |
129 buffer.write(n(dep.targetLibrary.simpleName)); | |
130 if (dep.prefix != null) buffer.write(' as ${n(dep.prefix)}'); | |
131 buffer.write('\n'); | |
132 | |
133 List flattenedCombinators = new List(); | |
134 dep.combinators.forEach((com) { | |
135 com.identifiers.forEach((ident) { | |
136 flattenedCombinators.add([n(ident), com.isShow, com.isHide]); | |
137 }); | |
138 }); | |
139 sortBy(flattenedCombinators, compareFirst).forEach((triple) { | |
140 buffer.write(' '); | |
141 if (triple[1]) buffer.write('show '); | |
142 if (triple[2]) buffer.write('hide '); | |
143 buffer.write(triple[0]); | |
144 buffer.write('\n'); | |
145 }); | |
146 }); | |
147 return buffer.toString(); | |
148 } | |
149 | |
150 stringify(value) { | 117 stringify(value) { |
151 if (value is Map) return stringifyMap(value); | 118 if (value is Map) return stringifyMap(value); |
152 if (value is Iterable) return stringifyIterable(value); | 119 if (value is Iterable) return stringifyIterable(value); |
153 if (value is InstanceMirror) return stringifyInstance(value); | 120 if (value is InstanceMirror) return stringifyInstance(value); |
154 if (value is ParameterMirror) return stringifyParameter(value); | 121 if (value is ParameterMirror) return stringifyParameter(value); |
155 if (value is VariableMirror) return stringifyVariable(value); | 122 if (value is VariableMirror) return stringifyVariable(value); |
156 if (value is MethodMirror) return stringifyMethod(value); | 123 if (value is MethodMirror) return stringifyMethod(value); |
157 if (value is num) return value.toString(); | 124 if (value is num) return value.toString(); |
158 if (value is String) return value; | 125 if (value is String) return value; |
159 if (value is Symbol) return stringifySymbol(value); | 126 if (value is Symbol) return stringifySymbol(value); |
160 if (value is ClassMirror) return stringifyClass(value); | 127 if (value is ClassMirror) return stringifyClass(value); |
161 if (value is TypeVariableMirror) return stringifyTypeVariable(value); | 128 if (value is TypeVariableMirror) return stringifyTypeVariable(value); |
162 if (value is TypeMirror) return stringifyType(value); | 129 if (value is TypeMirror) return stringifyType(value); |
163 if (value == null) return '<null>'; | 130 if (value == null) return '<null>'; |
164 throw 'Unexpected value: $value'; | 131 throw 'Unexpected value: $value'; |
165 } | 132 } |
166 | 133 |
167 expect(expected, actual, [String reason]) { | 134 expect(expected, actual, [String reason]) { |
168 Expect.stringEquals(expected, stringify(actual), reason); | 135 Expect.stringEquals(expected, stringify(actual), reason); |
169 } | 136 } |
170 | 137 |
171 compareSymbols(Symbol a, Symbol b) { | 138 compareSymbols(Symbol a, Symbol b) { |
172 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); | 139 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); |
173 } | 140 } |
174 | 141 |
175 simpleNames(Iterable<Mirror> i) => i.map((e) => e.simpleName); | 142 simpleNames(Iterable<Mirror> i) => i.map((e) => e.simpleName); |
176 | 143 |
177 sort(Iterable<Symbol> symbols) => symbols.toList()..sort(compareSymbols); | 144 sort(Iterable<Symbol> symbols) => symbols.toList()..sort(compareSymbols); |
OLD | NEW |