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'; |
11 | 11 |
12 name(DeclarationMirror mirror) { | 12 name(DeclarationMirror mirror) { |
13 return (mirror == null) ? '<null>' : stringify(mirror.simpleName); | 13 return (mirror == null) ? '<null>' : stringify(mirror.simpleName); |
14 } | 14 } |
15 | 15 |
16 stringifyMap(Map map) { | 16 stringifyMap(Map map) { |
17 var buffer = new StringBuffer(); | 17 var buffer = new StringBuffer(); |
18 bool first = true; | 18 bool first = true; |
19 for (String key in map.keys.map(MirrorSystem.getName).toList()..sort()) { | 19 for (String key in map.keys.map(MirrorSystem.getName).toList()..sort()) { |
20 if (!first) buffer.write(', '); | 20 if (!first) buffer.write(', '); |
21 first = false; | 21 first = false; |
22 buffer.write(key); | 22 buffer.write(key); |
23 buffer.write(': '); | 23 buffer.write(': '); |
24 buffer.write(stringify(map[new Symbol(key)])); | 24 buffer.write(stringify(map[new Symbol(key)])); |
25 } | 25 } |
26 return '{$buffer}'; | 26 return '{$buffer}'; |
27 } | 27 } |
28 | 28 |
29 stringifyList(List list) { | 29 stringifyIterable(Iterable list) { |
30 var buffer = new StringBuffer(); | 30 var buffer = new StringBuffer(); |
31 bool first = true; | 31 bool first = true; |
32 for (String value in list.map(stringify)) { | 32 for (String value in list.map(stringify)) { |
33 if (!first) buffer.write(', '); | 33 if (!first) buffer.write(', '); |
34 first = false; | 34 first = false; |
35 buffer.write(value); | 35 buffer.write(value); |
36 } | 36 } |
37 return '[$buffer]'; | 37 return '[$buffer]'; |
38 } | 38 } |
39 | 39 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 writeDeclarationOn(method, buffer); | 93 writeDeclarationOn(method, buffer); |
94 if (method.isStatic) buffer.write(', static'); | 94 if (method.isStatic) buffer.write(', static'); |
95 if (method.isGetter) buffer.write(', getter'); | 95 if (method.isGetter) buffer.write(', getter'); |
96 if (method.isSetter) buffer.write(', setter'); | 96 if (method.isSetter) buffer.write(', setter'); |
97 if (method.isConstructor) buffer.write(', constructor'); | 97 if (method.isConstructor) buffer.write(', constructor'); |
98 return 'Method($buffer)'; | 98 return 'Method($buffer)'; |
99 } | 99 } |
100 | 100 |
101 stringify(value) { | 101 stringify(value) { |
102 if (value is Map) return stringifyMap(value); | 102 if (value is Map) return stringifyMap(value); |
103 if (value is List) return stringifyList(value); | 103 if (value is Iterable) return stringifyIterable(value); |
104 if (value is ParameterMirror) return stringifyParameter(value); | 104 if (value is ParameterMirror) return stringifyParameter(value); |
105 if (value is VariableMirror) return stringifyVariable(value); | 105 if (value is VariableMirror) return stringifyVariable(value); |
106 if (value is MethodMirror) return stringifyMethod(value); | 106 if (value is MethodMirror) return stringifyMethod(value); |
107 if (value is Symbol) return stringifySymbol(value); | 107 if (value is Symbol) return stringifySymbol(value); |
108 if (value is ClassMirror) return stringifyClass(value); | 108 if (value is ClassMirror) return stringifyClass(value); |
109 if (value is TypeMirror) return stringifyType(value); | 109 if (value is TypeMirror) return stringifyType(value); |
110 if (value == null) return '<null>'; | 110 if (value == null) return '<null>'; |
111 throw 'Unexpected value: $value'; | 111 throw 'Unexpected value: $value'; |
112 } | 112 } |
113 | 113 |
114 expect(expected, actual) => Expect.stringEquals(expected, stringify(actual)); | 114 expect(expected, actual, [String reason]) { |
| 115 Expect.stringEquals(expected, stringify(actual), reason); |
| 116 } |
115 | 117 |
116 compareSymbols(Symbol a, Symbol b) { | 118 compareSymbols(Symbol a, Symbol b) { |
117 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); | 119 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); |
118 } | 120 } |
| 121 |
| 122 simpleNames(Iterable<Mirror> i) => i.map((e) => e.simpleName); |
| 123 |
| 124 sort(Iterable<Symbol> symbols) => symbols.toList()..sort(compareSymbols); |
OLD | NEW |