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