| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 if (parameter.isNamed) buffer.write(', named'); | 76 if (parameter.isNamed) buffer.write(', named'); |
| 77 // TODO(6490): dart2js always returns false for hasDefaultValue. | 77 // TODO(6490): dart2js always returns false for hasDefaultValue. |
| 78 if (parameter.hasDefaultValue) { | 78 if (parameter.hasDefaultValue) { |
| 79 buffer.write(', value = ${stringify(parameter.defaultValue)}'); | 79 buffer.write(', value = ${stringify(parameter.defaultValue)}'); |
| 80 } | 80 } |
| 81 // TODO(ahe): Move to writeVariableOn. | 81 // TODO(ahe): Move to writeVariableOn. |
| 82 buffer.write(', type = ${stringify(parameter.type)}'); | 82 buffer.write(', type = ${stringify(parameter.type)}'); |
| 83 return 'Parameter($buffer)'; | 83 return 'Parameter($buffer)'; |
| 84 } | 84 } |
| 85 | 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 |
| 86 stringifyType(TypeMirror type) { | 93 stringifyType(TypeMirror type) { |
| 87 var buffer = new StringBuffer(); | 94 var buffer = new StringBuffer(); |
| 88 writeDeclarationOn(type, buffer); | 95 writeDeclarationOn(type, buffer); |
| 89 return 'Type($buffer)'; | 96 return 'Type($buffer)'; |
| 90 } | 97 } |
| 91 | 98 |
| 92 stringifyClass(ClassMirror cls) { | 99 stringifyClass(ClassMirror cls) { |
| 93 var buffer = new StringBuffer(); | 100 var buffer = new StringBuffer(); |
| 94 writeDeclarationOn(cls, buffer); | 101 writeDeclarationOn(cls, buffer); |
| 95 return 'Class($buffer)'; | 102 return 'Class($buffer)'; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 109 if (value is Map) return stringifyMap(value); | 116 if (value is Map) return stringifyMap(value); |
| 110 if (value is Iterable) return stringifyIterable(value); | 117 if (value is Iterable) return stringifyIterable(value); |
| 111 if (value is InstanceMirror) return stringifyInstance(value); | 118 if (value is InstanceMirror) return stringifyInstance(value); |
| 112 if (value is ParameterMirror) return stringifyParameter(value); | 119 if (value is ParameterMirror) return stringifyParameter(value); |
| 113 if (value is VariableMirror) return stringifyVariable(value); | 120 if (value is VariableMirror) return stringifyVariable(value); |
| 114 if (value is MethodMirror) return stringifyMethod(value); | 121 if (value is MethodMirror) return stringifyMethod(value); |
| 115 if (value is num) return value.toString(); | 122 if (value is num) return value.toString(); |
| 116 if (value is String) return value; | 123 if (value is String) return value; |
| 117 if (value is Symbol) return stringifySymbol(value); | 124 if (value is Symbol) return stringifySymbol(value); |
| 118 if (value is ClassMirror) return stringifyClass(value); | 125 if (value is ClassMirror) return stringifyClass(value); |
| 126 if (value is TypeVariableMirror) return stringifyTypeVariable(value); |
| 119 if (value is TypeMirror) return stringifyType(value); | 127 if (value is TypeMirror) return stringifyType(value); |
| 120 if (value == null) return '<null>'; | 128 if (value == null) return '<null>'; |
| 121 throw 'Unexpected value: $value'; | 129 throw 'Unexpected value: $value'; |
| 122 } | 130 } |
| 123 | 131 |
| 124 expect(expected, actual, [String reason]) { | 132 expect(expected, actual, [String reason]) { |
| 125 Expect.stringEquals(expected, stringify(actual), reason); | 133 Expect.stringEquals(expected, stringify(actual), reason); |
| 126 } | 134 } |
| 127 | 135 |
| 128 compareSymbols(Symbol a, Symbol b) { | 136 compareSymbols(Symbol a, Symbol b) { |
| 129 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); | 137 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); |
| 130 } | 138 } |
| 131 | 139 |
| 132 simpleNames(Iterable<Mirror> i) => i.map((e) => e.simpleName); | 140 simpleNames(Iterable<Mirror> i) => i.map((e) => e.simpleName); |
| 133 | 141 |
| 134 sort(Iterable<Symbol> symbols) => symbols.toList()..sort(compareSymbols); | 142 sort(Iterable<Symbol> symbols) => symbols.toList()..sort(compareSymbols); |
| OLD | NEW |