OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "dart:mirrors"; |
| 6 |
| 7 import "package:expect/expect.dart"; |
| 8 |
| 9 membersOf(ClassMirror cm) { |
| 10 var result = new Map(); |
| 11 cm.declarations.forEach((k,v) { |
| 12 if(v is MethodMirror && !v.isConstructor) result[k] = v; |
| 13 if(v is VariableMirror) result[k] = v; |
| 14 }); |
| 15 return result; |
| 16 } |
| 17 |
| 18 class WannabeFunction { |
| 19 int call(int a, int b) => a + b; |
| 20 method(x) => x * x; |
| 21 } |
| 22 |
| 23 main() { |
| 24 Expect.isTrue(new WannabeFunction() is Function); |
| 25 |
| 26 ClosureMirror cm = reflect(new WannabeFunction()); |
| 27 Expect.equals(7, cm.invoke(#call, [3,4]).reflectee); |
| 28 Expect.throws(() => cm.invoke(#call, [3]), |
| 29 (e) => e is NoSuchMethodError, |
| 30 "Wrong arity"); |
| 31 Expect.equals(49, cm.invoke(#method, [7]).reflectee); |
| 32 Expect.throws(() => cm.invoke(#method, [3, 4]), |
| 33 (e) => e is NoSuchMethodError, |
| 34 "Wrong arity"); |
| 35 Expect.equals(7, cm.apply([3,4]).reflectee); |
| 36 Expect.throws(() => cm.apply([3]), |
| 37 (e) => e is NoSuchMethodError, |
| 38 "Wrong arity"); |
| 39 |
| 40 MethodMirror mm = cm.function; |
| 41 Expect.equals(#call, mm.simpleName); |
| 42 Expect.equals(reflectClass(WannabeFunction), mm.owner); |
| 43 Expect.isTrue(mm.isRegularMethod); |
| 44 Expect.equals(#int, mm.returnType.simpleName); |
| 45 Expect.equals(#int, mm.parameters[0].type.simpleName); |
| 46 Expect.equals(#int, mm.parameters[1].type.simpleName); |
| 47 |
| 48 ClassMirror km = cm.type; |
| 49 Expect.equals(reflectClass(WannabeFunction), km); |
| 50 Expect.equals(#WannabeFunction, km.simpleName); |
| 51 Expect.equals(mm, km.declarations[#call]); |
| 52 Expect.setEquals([#call, #method], membersOf(km).keys); |
| 53 } |
OLD | NEW |