| 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 import "dart:mirrors"; | 5 import "dart:mirrors"; |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 | 8 |
| 9 class WannabeFunction { | 9 class WannabeFunction { |
| 10 int call(int a, int b) => a + b; | 10 int call(int a, int b) => a + b; |
| 11 method(x) => x * x; | 11 method(x) => x * x; |
| 12 } | 12 } |
| 13 | 13 |
| 14 main() { | 14 main() { |
| 15 Expect.isTrue(new WannabeFunction() is Function); | 15 Expect.isTrue(new WannabeFunction() is Function); |
| 16 | 16 |
| 17 ClosureMirror cm = reflect(new WannabeFunction()); | 17 ClosureMirror cm = reflect(new WannabeFunction()); |
| 18 Expect.equals(7, cm.invoke(const Symbol("call"), [3,4]).reflectee); | 18 Expect.equals(7, cm.invoke(#call, [3,4]).reflectee); |
| 19 Expect.throws(() => cm.invoke(const Symbol("call"), [3]), | 19 Expect.throws(() => cm.invoke(#call, [3]), |
| 20 (e) => e is NoSuchMethodError, | 20 (e) => e is NoSuchMethodError, |
| 21 "Wrong arity"); | 21 "Wrong arity"); |
| 22 Expect.equals(49, cm.invoke(const Symbol("method"), [7]).reflectee); | 22 Expect.equals(49, cm.invoke(#method, [7]).reflectee); |
| 23 Expect.throws(() => cm.invoke(const Symbol("method"), [3, 4]), | 23 Expect.throws(() => cm.invoke(#method, [3, 4]), |
| 24 (e) => e is NoSuchMethodError, | 24 (e) => e is NoSuchMethodError, |
| 25 "Wrong arity"); | 25 "Wrong arity"); |
| 26 Expect.equals(7, cm.apply([3,4]).reflectee); | 26 Expect.equals(7, cm.apply([3,4]).reflectee); |
| 27 Expect.throws(() => cm.apply([3]), | 27 Expect.throws(() => cm.apply([3]), |
| 28 (e) => e is NoSuchMethodError, | 28 (e) => e is NoSuchMethodError, |
| 29 "Wrong arity"); | 29 "Wrong arity"); |
| 30 | 30 |
| 31 MethodMirror mm = cm.function; | 31 MethodMirror mm = cm.function; |
| 32 Expect.equals(const Symbol("call"), mm.simpleName); | 32 Expect.equals(#call, mm.simpleName); |
| 33 Expect.equals(reflectClass(WannabeFunction), | 33 Expect.equals(reflectClass(WannabeFunction), mm.owner); |
| 34 mm.owner); | |
| 35 Expect.isTrue(mm.isRegularMethod); | 34 Expect.isTrue(mm.isRegularMethod); |
| 36 Expect.equals(const Symbol("int"), mm.returnType.simpleName); | 35 Expect.equals(#int, mm.returnType.simpleName); |
| 37 Expect.equals(const Symbol("int"), mm.parameters[0].type.simpleName); | 36 Expect.equals(#int, mm.parameters[0].type.simpleName); |
| 38 Expect.equals(const Symbol("int"), mm.parameters[1].type.simpleName); | 37 Expect.equals(#int, mm.parameters[1].type.simpleName); |
| 39 | 38 |
| 40 ClassMirror km = cm.type; | 39 ClassMirror km = cm.type; |
| 41 Expect.equals(reflectClass(WannabeFunction), km); | 40 Expect.equals(reflectClass(WannabeFunction), km); |
| 42 Expect.equals(const Symbol("WannabeFunction"), km.simpleName); | 41 Expect.equals(#WannabeFunction, km.simpleName); |
| 43 Expect.equals(mm, km.members[const Symbol("call")]); | 42 Expect.equals(mm, km.members[#call]); |
| 44 Expect.setEquals([const Symbol("call"), const Symbol("method")], | 43 Expect.setEquals([#call, #method], km.members.keys); |
| 45 km.members.keys); | |
| 46 } | 44 } |
| OLD | NEW |