OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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.constructor_test; |
| 6 |
| 7 @MirrorsUsed(targets: const [A]) |
| 8 import 'dart:mirrors'; |
| 9 import 'package:expect/expect.dart'; |
| 10 |
| 11 class A { |
| 12 factory A([x, y]) = B; |
| 13 factory A.more([x, y]) = B.more; |
| 14 factory A.oneMore(x, [y]) = B.more; |
| 15 } |
| 16 |
| 17 class B implements A { |
| 18 final _x, _y, _z; |
| 19 |
| 20 B([x = 'x', y = 'y']) : _x = x, _y = y; |
| 21 |
| 22 B.more([x = 'x', y = 'y', z = 'z']) : _x = x, _y = y, _z = z; |
| 23 |
| 24 toString() => 'B(x=$_x, y=$_y, z=$_z)'; |
| 25 } |
| 26 |
| 27 |
| 28 main() { |
| 29 var d1 = new A(1); |
| 30 Expect.equals('B(x=1, y=y, z=null)', '$d1', 'direct 1'); |
| 31 |
| 32 var d2 = new A.more(1); |
| 33 Expect.equals('B(x=1, y=y, z=z)', '$d2', 'direct 2'); |
| 34 |
| 35 ClassMirror cm = reflectClass(A); |
| 36 |
| 37 var v1 = cm.newInstance(const Symbol(''), []).reflectee; |
| 38 var v2 = cm.newInstance(const Symbol(''), [1]).reflectee; |
| 39 var v3 = cm.newInstance(const Symbol(''), [2, 3]).reflectee; |
| 40 |
| 41 Expect.equals('B(x=x, y=y, z=null)', '$v1', 'unnamed 1'); |
| 42 Expect.equals('B(x=1, y=y, z=null)', '$v2', 'unnamed 2'); |
| 43 Expect.equals('B(x=2, y=3, z=null)', '$v3', 'unnamed 3'); |
| 44 |
| 45 var m1 = cm.newInstance(const Symbol('more'), []).reflectee; |
| 46 var m2 = cm.newInstance(const Symbol('more'), [1]).reflectee; |
| 47 var m3 = cm.newInstance(const Symbol('more'), [2, 3]).reflectee; |
| 48 |
| 49 Expect.equals('B(x=x, y=y, z=z)', '$m1', 'more 1'); |
| 50 Expect.equals('B(x=1, y=y, z=z)', '$m2', 'more 2'); |
| 51 Expect.equals('B(x=2, y=3, z=z)', '$m3', 'more 3'); |
| 52 |
| 53 var o1 = cm.newInstance(const Symbol('oneMore'), [1]).reflectee; |
| 54 var o2 = cm.newInstance(const Symbol('oneMore'), [2, 3]).reflectee; |
| 55 |
| 56 Expect.equals('B(x=1, y=y, z=z)', '$o1', 'oneMore one arg'); |
| 57 Expect.equals('B(x=2, y=3, z=z)', '$o2', 'oneMore two args'); |
| 58 } |
OLD | NEW |