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