| 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 library test.reflect_model_test; | 5 library test.reflect_model_test; |
| 6 | 6 |
| 7 import 'dart:mirrors'; | 7 import 'dart:mirrors'; |
| 8 | 8 |
| 9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
| 10 | 10 |
| 11 import 'model.dart'; | 11 import 'model.dart'; |
| 12 import 'stringify.dart'; |
| 12 | 13 |
| 13 isNoSuchMethodError(e) => e is NoSuchMethodError; | 14 isNoSuchMethodError(e) => e is NoSuchMethodError; |
| 14 | 15 |
| 15 name(DeclarationMirror mirror) { | |
| 16 return (mirror == null) ? '<null>' : stringify(mirror.simpleName); | |
| 17 } | |
| 18 | |
| 19 stringifyMap(Map map) { | |
| 20 var buffer = new StringBuffer('{'); | |
| 21 bool first = true; | |
| 22 for (String key in map.keys.map(MirrorSystem.getName).toList()..sort()) { | |
| 23 if (!first) buffer.write(', '); | |
| 24 first = false; | |
| 25 buffer.write(key); | |
| 26 buffer.write(': '); | |
| 27 buffer.write(stringify(map[new Symbol(key)])); | |
| 28 } | |
| 29 return (buffer..write('}')).toString(); | |
| 30 } | |
| 31 | |
| 32 stringifySymbol(Symbol symbol) => 's(${MirrorSystem.getName(symbol)})'; | |
| 33 | |
| 34 writeDeclarationOn(DeclarationMirror mirror, StringBuffer buffer) { | |
| 35 buffer.write(stringify(mirror.simpleName)); | |
| 36 if (mirror.owner != null) { | |
| 37 buffer.write(' in '); | |
| 38 buffer.write(name(mirror.owner)); | |
| 39 } | |
| 40 if (mirror.isPrivate) buffer.write(', private'); | |
| 41 if (mirror.isTopLevel) buffer.write(', top-level'); | |
| 42 } | |
| 43 | |
| 44 stringifyVariable(VariableMirror variable) { | |
| 45 var buffer = new StringBuffer('Variable('); | |
| 46 writeDeclarationOn(variable, buffer); | |
| 47 if (variable.isStatic) buffer.write(', static'); | |
| 48 if (variable.isFinal) buffer.write(', final'); | |
| 49 return (buffer..write(')')).toString(); | |
| 50 } | |
| 51 | |
| 52 stringifyMethod(MethodMirror method) { | |
| 53 var buffer = new StringBuffer('Method('); | |
| 54 writeDeclarationOn(method, buffer); | |
| 55 if (method.isStatic) buffer.write(', static'); | |
| 56 if (method.isGetter) buffer.write(', getter'); | |
| 57 if (method.isSetter) buffer.write(', setter'); | |
| 58 if (method.isConstructor) buffer.write(', constructor'); | |
| 59 return (buffer..write(')')).toString(); | |
| 60 } | |
| 61 | |
| 62 stringify(value) { | |
| 63 if (value is Map) return stringifyMap(value); | |
| 64 if (value is VariableMirror) return stringifyVariable(value); | |
| 65 if (value is MethodMirror) return stringifyMethod(value); | |
| 66 if (value is Symbol) return stringifySymbol(value); | |
| 67 if (value == null) return '<null>'; | |
| 68 throw 'Unexpected value: $value'; | |
| 69 } | |
| 70 | |
| 71 expect(expected, actual) => Expect.stringEquals(expected, stringify(actual)); | |
| 72 | |
| 73 main() { | 16 main() { |
| 74 var unnamed = new Symbol(''); | 17 var unnamed = new Symbol(''); |
| 75 var field = new Symbol('field'); | 18 var field = new Symbol('field'); |
| 76 var instanceMethod = new Symbol('instanceMethod'); | 19 var instanceMethod = new Symbol('instanceMethod'); |
| 77 var accessor = new Symbol('accessor'); | 20 var accessor = new Symbol('accessor'); |
| 78 var aMethod = new Symbol('aMethod'); | 21 var aMethod = new Symbol('aMethod'); |
| 79 var bMethod = new Symbol('bMethod'); | 22 var bMethod = new Symbol('bMethod'); |
| 80 var cMethod = new Symbol('cMethod'); | 23 var cMethod = new Symbol('cMethod'); |
| 81 | 24 |
| 82 var aClass = reflectClass(A); | 25 var aClass = reflectClass(A); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 Expect.equals('aMethod', c.invoke(aMethod, []).reflectee); | 104 Expect.equals('aMethod', c.invoke(aMethod, []).reflectee); |
| 162 | 105 |
| 163 Expect.throws(() { a.invoke(bMethod, []); }, isNoSuchMethodError); | 106 Expect.throws(() { a.invoke(bMethod, []); }, isNoSuchMethodError); |
| 164 Expect.equals('bMethod', b.invoke(bMethod, []).reflectee); | 107 Expect.equals('bMethod', b.invoke(bMethod, []).reflectee); |
| 165 Expect.equals('bMethod', c.invoke(bMethod, []).reflectee); | 108 Expect.equals('bMethod', c.invoke(bMethod, []).reflectee); |
| 166 | 109 |
| 167 Expect.throws(() { a.invoke(cMethod, []); }, isNoSuchMethodError); | 110 Expect.throws(() { a.invoke(cMethod, []); }, isNoSuchMethodError); |
| 168 Expect.throws(() { b.invoke(cMethod, []); }, isNoSuchMethodError); | 111 Expect.throws(() { b.invoke(cMethod, []); }, isNoSuchMethodError); |
| 169 Expect.equals('cMethod', c.invoke(cMethod, []).reflectee); | 112 Expect.equals('cMethod', c.invoke(cMethod, []).reflectee); |
| 170 } | 113 } |
| OLD | NEW |