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 19 matching lines...) Expand all Loading... |
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 |
| 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 |
40 stringifySymbol(Symbol symbol) => 's(${MirrorSystem.getName(symbol)})'; | 48 stringifySymbol(Symbol symbol) => 's(${MirrorSystem.getName(symbol)})'; |
41 | 49 |
42 writeDeclarationOn(DeclarationMirror mirror, StringBuffer buffer) { | 50 writeDeclarationOn(DeclarationMirror mirror, StringBuffer buffer) { |
43 buffer.write(stringify(mirror.simpleName)); | 51 buffer.write(stringify(mirror.simpleName)); |
44 if (mirror.owner != null) { | 52 if (mirror.owner != null) { |
45 buffer.write(' in '); | 53 buffer.write(' in '); |
46 buffer.write(name(mirror.owner)); | 54 buffer.write(name(mirror.owner)); |
47 } | 55 } |
48 if (mirror.isPrivate) buffer.write(', private'); | 56 if (mirror.isPrivate) buffer.write(', private'); |
49 if (mirror.isTopLevel) buffer.write(', top-level'); | 57 if (mirror.isTopLevel) buffer.write(', top-level'); |
50 } | 58 } |
51 | 59 |
52 writeVariableOn(VariableMirror variable, StringBuffer buffer) { | 60 writeVariableOn(VariableMirror variable, StringBuffer buffer) { |
53 writeDeclarationOn(variable, buffer); | 61 writeDeclarationOn(variable, buffer); |
54 if (variable.isStatic) buffer.write(', static'); | 62 if (variable.isStatic) buffer.write(', static'); |
55 if (variable.isFinal) buffer.write(', final'); | 63 if (variable.isFinal) buffer.write(', final'); |
56 } | 64 } |
57 | 65 |
58 stringifyVariable(VariableMirror variable) { | 66 stringifyVariable(VariableMirror variable) { |
59 var buffer = new StringBuffer(); | 67 var buffer = new StringBuffer(); |
60 writeVariableOn(variable, buffer); | 68 writeVariableOn(variable, buffer); |
61 return 'Variable($buffer)'; | 69 return 'Variable($buffer)'; |
62 } | 70 } |
63 | 71 |
64 stringifyParameter(ParameterMirror parameter) { | 72 stringifyParameter(ParameterMirror parameter) { |
65 var buffer = new StringBuffer(); | 73 var buffer = new StringBuffer(); |
66 writeVariableOn(parameter, buffer); | 74 writeVariableOn(parameter, buffer); |
67 if (parameter.isOptional) buffer.write(', optional'); | 75 if (parameter.isOptional) buffer.write(', optional'); |
68 if (parameter.isNamed) buffer.write(', named'); | 76 if (parameter.isNamed) buffer.write(', named'); |
69 // TODO(6490,12430): Add this check as soon as it's properly implemented in | 77 // TODO(6490): dart2js always returns false for hasDefaultValue. |
70 // the VM and dart2js. | 78 if (parameter.hasDefaultValue) { |
71 // if (parameter.hasDefaultValue) { | 79 buffer.write(', value = ${stringify(parameter.defaultValue)}'); |
72 // buffer.write(', value = ${stringify(parameter.defaultValue)}'); | 80 } |
73 // } | |
74 // TODO(ahe): Move to writeVariableOn. | 81 // TODO(ahe): Move to writeVariableOn. |
75 buffer.write(', type = ${stringify(parameter.type)}'); | 82 buffer.write(', type = ${stringify(parameter.type)}'); |
76 return 'Parameter($buffer)'; | 83 return 'Parameter($buffer)'; |
77 } | 84 } |
78 | 85 |
79 stringifyType(TypeMirror type) { | 86 stringifyType(TypeMirror type) { |
80 var buffer = new StringBuffer(); | 87 var buffer = new StringBuffer(); |
81 writeDeclarationOn(type, buffer); | 88 writeDeclarationOn(type, buffer); |
82 return 'Type($buffer)'; | 89 return 'Type($buffer)'; |
83 } | 90 } |
(...skipping 10 matching lines...) Expand all Loading... |
94 if (method.isStatic) buffer.write(', static'); | 101 if (method.isStatic) buffer.write(', static'); |
95 if (method.isGetter) buffer.write(', getter'); | 102 if (method.isGetter) buffer.write(', getter'); |
96 if (method.isSetter) buffer.write(', setter'); | 103 if (method.isSetter) buffer.write(', setter'); |
97 if (method.isConstructor) buffer.write(', constructor'); | 104 if (method.isConstructor) buffer.write(', constructor'); |
98 return 'Method($buffer)'; | 105 return 'Method($buffer)'; |
99 } | 106 } |
100 | 107 |
101 stringify(value) { | 108 stringify(value) { |
102 if (value is Map) return stringifyMap(value); | 109 if (value is Map) return stringifyMap(value); |
103 if (value is Iterable) return stringifyIterable(value); | 110 if (value is Iterable) return stringifyIterable(value); |
| 111 if (value is InstanceMirror) return stringifyInstance(value); |
104 if (value is ParameterMirror) return stringifyParameter(value); | 112 if (value is ParameterMirror) return stringifyParameter(value); |
105 if (value is VariableMirror) return stringifyVariable(value); | 113 if (value is VariableMirror) return stringifyVariable(value); |
106 if (value is MethodMirror) return stringifyMethod(value); | 114 if (value is MethodMirror) return stringifyMethod(value); |
| 115 if (value is num) return value.toString(); |
| 116 if (value is String) return value; |
107 if (value is Symbol) return stringifySymbol(value); | 117 if (value is Symbol) return stringifySymbol(value); |
108 if (value is ClassMirror) return stringifyClass(value); | 118 if (value is ClassMirror) return stringifyClass(value); |
109 if (value is TypeMirror) return stringifyType(value); | 119 if (value is TypeMirror) return stringifyType(value); |
110 if (value == null) return '<null>'; | 120 if (value == null) return '<null>'; |
111 throw 'Unexpected value: $value'; | 121 throw 'Unexpected value: $value'; |
112 } | 122 } |
113 | 123 |
114 expect(expected, actual, [String reason]) { | 124 expect(expected, actual, [String reason]) { |
115 Expect.stringEquals(expected, stringify(actual), reason); | 125 Expect.stringEquals(expected, stringify(actual), reason); |
116 } | 126 } |
117 | 127 |
118 compareSymbols(Symbol a, Symbol b) { | 128 compareSymbols(Symbol a, Symbol b) { |
119 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); | 129 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); |
120 } | 130 } |
121 | 131 |
122 simpleNames(Iterable<Mirror> i) => i.map((e) => e.simpleName); | 132 simpleNames(Iterable<Mirror> i) => i.map((e) => e.simpleName); |
123 | 133 |
124 sort(Iterable<Symbol> symbols) => symbols.toList()..sort(compareSymbols); | 134 sort(Iterable<Symbol> symbols) => symbols.toList()..sort(compareSymbols); |
OLD | NEW |