| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 63 |
| 64 stringifyParameter(ParameterMirror parameter) { | 64 stringifyParameter(ParameterMirror parameter) { |
| 65 var buffer = new StringBuffer(); | 65 var buffer = new StringBuffer(); |
| 66 writeVariableOn(parameter, buffer); | 66 writeVariableOn(parameter, buffer); |
| 67 if (parameter.isOptional) buffer.write(', optional'); | 67 if (parameter.isOptional) buffer.write(', optional'); |
| 68 if (parameter.isNamed) buffer.write(', named'); | 68 if (parameter.isNamed) buffer.write(', named'); |
| 69 if (parameter.hasDefaultValue) { | 69 if (parameter.hasDefaultValue) { |
| 70 buffer.write(', value = ${stringify(parameter.defaultValue)}'); | 70 buffer.write(', value = ${stringify(parameter.defaultValue)}'); |
| 71 } | 71 } |
| 72 // TODO(ahe): Move to writeVariableOn. | 72 // TODO(ahe): Move to writeVariableOn. |
| 73 buffer.write(', type = ${stringifyType(parameter.type)}'); | 73 buffer.write(', type = ${stringify(parameter.type)}'); |
| 74 return 'Parameter($buffer)'; | 74 return 'Parameter($buffer)'; |
| 75 } | 75 } |
| 76 | 76 |
| 77 stringifyType(TypeMirror type) { | 77 stringifyType(TypeMirror type) { |
| 78 var buffer = new StringBuffer(); | 78 var buffer = new StringBuffer(); |
| 79 writeDeclarationOn(type, buffer); | 79 writeDeclarationOn(type, buffer); |
| 80 return 'Type($buffer)'; | 80 return 'Type($buffer)'; |
| 81 } | 81 } |
| 82 | 82 |
| 83 stringifyClass(ClassMirror cls) { |
| 84 var buffer = new StringBuffer(); |
| 85 writeDeclarationOn(cls, buffer); |
| 86 return 'Class($buffer)'; |
| 87 } |
| 88 |
| 83 stringifyMethod(MethodMirror method) { | 89 stringifyMethod(MethodMirror method) { |
| 84 var buffer = new StringBuffer(); | 90 var buffer = new StringBuffer(); |
| 85 writeDeclarationOn(method, buffer); | 91 writeDeclarationOn(method, buffer); |
| 86 if (method.isStatic) buffer.write(', static'); | 92 if (method.isStatic) buffer.write(', static'); |
| 87 if (method.isGetter) buffer.write(', getter'); | 93 if (method.isGetter) buffer.write(', getter'); |
| 88 if (method.isSetter) buffer.write(', setter'); | 94 if (method.isSetter) buffer.write(', setter'); |
| 89 if (method.isConstructor) buffer.write(', constructor'); | 95 if (method.isConstructor) buffer.write(', constructor'); |
| 90 return 'Method($buffer)'; | 96 return 'Method($buffer)'; |
| 91 } | 97 } |
| 92 | 98 |
| 93 stringify(value) { | 99 stringify(value) { |
| 94 if (value is Map) return stringifyMap(value); | 100 if (value is Map) return stringifyMap(value); |
| 95 if (value is List) return stringifyList(value); | 101 if (value is List) return stringifyList(value); |
| 96 if (value is ParameterMirror) return stringifyParameter(value); | 102 if (value is ParameterMirror) return stringifyParameter(value); |
| 97 if (value is VariableMirror) return stringifyVariable(value); | 103 if (value is VariableMirror) return stringifyVariable(value); |
| 98 if (value is MethodMirror) return stringifyMethod(value); | 104 if (value is MethodMirror) return stringifyMethod(value); |
| 99 if (value is Symbol) return stringifySymbol(value); | 105 if (value is Symbol) return stringifySymbol(value); |
| 106 if (value is ClassMirror) return stringifyClass(value); |
| 100 if (value is TypeMirror) return stringifyType(value); | 107 if (value is TypeMirror) return stringifyType(value); |
| 101 if (value == null) return '<null>'; | 108 if (value == null) return '<null>'; |
| 102 throw 'Unexpected value: $value'; | 109 throw 'Unexpected value: $value'; |
| 103 } | 110 } |
| 104 | 111 |
| 105 expect(expected, actual) => Expect.stringEquals(expected, stringify(actual)); | 112 expect(expected, actual) => Expect.stringEquals(expected, stringify(actual)); |
| 106 | 113 |
| 107 compareSymbols(Symbol a, Symbol b) { | 114 compareSymbols(Symbol a, Symbol b) { |
| 108 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); | 115 return MirrorSystem.getName(a).compareTo(MirrorSystem.getName(b)); |
| 109 } | 116 } |
| OLD | NEW |