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