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.constructors_test; | 5 library test.constructors_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 'stringify.dart'; | 11 import 'stringify.dart'; |
12 | 12 |
| 13 constructorsOf(ClassMirror cm) { |
| 14 var result = new Map(); |
| 15 cm.declarations.forEach((k,v) { |
| 16 if(v is MethodMirror && v.isConstructor) result[k] = v; |
| 17 }); |
| 18 return result; |
| 19 } |
| 20 |
13 class Foo { | 21 class Foo { |
14 } | 22 } |
15 | 23 |
16 class Bar { | 24 class Bar { |
17 Bar(); | 25 Bar(); |
18 } | 26 } |
19 | 27 |
20 class Baz { | 28 class Baz { |
21 Baz.named(); | 29 Baz.named(); |
22 } | 30 } |
23 | 31 |
24 class Biz { | 32 class Biz { |
25 Biz(); | 33 Biz(); |
26 Biz.named(); | 34 Biz.named(); |
27 } | 35 } |
28 | 36 |
29 main() { | 37 main() { |
30 ClassMirror fooMirror = reflectClass(Foo); | 38 ClassMirror fooMirror = reflectClass(Foo); |
31 Map<Symbol, MethodMirror> fooConstructors = fooMirror.constructors; | 39 Map<Symbol, MethodMirror> fooConstructors = constructorsOf(fooMirror); |
32 ClassMirror barMirror = reflectClass(Bar); | 40 ClassMirror barMirror = reflectClass(Bar); |
33 Map<Symbol, MethodMirror> barConstructors = barMirror.constructors; | 41 Map<Symbol, MethodMirror> barConstructors = constructorsOf(barMirror); |
34 ClassMirror bazMirror = reflectClass(Baz); | 42 ClassMirror bazMirror = reflectClass(Baz); |
35 Map<Symbol, MethodMirror> bazConstructors = bazMirror.constructors; | 43 Map<Symbol, MethodMirror> bazConstructors = constructorsOf(bazMirror); |
36 ClassMirror bizMirror = reflectClass(Biz); | 44 ClassMirror bizMirror = reflectClass(Biz); |
37 Map<Symbol, MethodMirror> bizConstructors = bizMirror.constructors; | 45 Map<Symbol, MethodMirror> bizConstructors = constructorsOf(bizMirror); |
38 | 46 |
39 expect('{Foo: Method(s(Foo) in s(Foo), constructor)}', fooConstructors); | 47 expect('{Foo: Method(s(Foo) in s(Foo), constructor)}', fooConstructors); |
40 expect('{Bar: Method(s(Bar) in s(Bar), constructor)}', barConstructors); | 48 expect('{Bar: Method(s(Bar) in s(Bar), constructor)}', barConstructors); |
41 expect('{Baz.named: Method(s(Baz.named) in s(Baz), constructor)}', | 49 expect('{Baz.named: Method(s(Baz.named) in s(Baz), constructor)}', |
42 bazConstructors); | 50 bazConstructors); |
43 expect('{Biz: Method(s(Biz) in s(Biz), constructor),' | 51 expect('{Biz: Method(s(Biz) in s(Biz), constructor),' |
44 ' Biz.named: Method(s(Biz.named) in s(Biz), constructor)}', | 52 ' Biz.named: Method(s(Biz.named) in s(Biz), constructor)}', |
45 bizConstructors); | 53 bizConstructors); |
46 print(bizConstructors); | 54 print(bizConstructors); |
47 | 55 |
48 expect('[]', fooConstructors.values.single.parameters); | 56 expect('[]', fooConstructors.values.single.parameters); |
49 expect('[]', barConstructors.values.single.parameters); | 57 expect('[]', barConstructors.values.single.parameters); |
50 expect('[]', bazConstructors.values.single.parameters); | 58 expect('[]', bazConstructors.values.single.parameters); |
51 for (var constructor in bizConstructors.values) { | 59 for (var constructor in bizConstructors.values) { |
52 expect('[]', constructor.parameters); | 60 expect('[]', constructor.parameters); |
53 } | 61 } |
54 | 62 |
55 expect('[s()]', | 63 expect('[s()]', |
56 fooConstructors.values.map((m) => m.constructorName).toList()); | 64 fooConstructors.values.map((m) => m.constructorName).toList()); |
57 expect('[s()]', | 65 expect('[s()]', |
58 barConstructors.values.map((m) => m.constructorName).toList()); | 66 barConstructors.values.map((m) => m.constructorName).toList()); |
59 expect('[s(named)]', | 67 expect('[s(named)]', |
60 bazConstructors.values.map((m) => m.constructorName).toList()); | 68 bazConstructors.values.map((m) => m.constructorName).toList()); |
61 expect('[s(), s(named)]', | 69 expect('[s(), s(named)]', |
62 bizConstructors.values.map((m) => m.constructorName).toList() | 70 bizConstructors.values.map((m) => m.constructorName).toList() |
63 ..sort(compareSymbols)); | 71 ..sort(compareSymbols)); |
64 } | 72 } |
OLD | NEW |