| 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 /// Test of [ParameterMirror]. | 5 /// Test of [ParameterMirror]. |
| 6 library test.parameter_test; | 6 library test.parameter_test; |
| 7 | 7 |
| 8 @MirrorsUsed(targets: 'test.parameter_test', override: '*') | 8 @MirrorsUsed(targets: 'test.parameter_test', override: '*') |
| 9 import 'dart:mirrors'; | 9 import 'dart:mirrors'; |
| 10 | 10 |
| 11 import 'package:expect/expect.dart'; |
| 11 import 'stringify.dart'; | 12 import 'stringify.dart'; |
| 12 | 13 |
| 13 class B { | 14 class B { |
| 14 B(); | 15 B(); |
| 15 B.foo(int x); | 16 B.foo(int x); |
| 16 B.bar(int z, x); | 17 B.bar(int z, x); |
| 18 |
| 19 // TODO(6490): Currently only supported by the VM. |
| 20 B.baz(final int x, int y, final int z); |
| 21 B.qux(int x, [int y= 3 + 1]); |
| 22 B.quux(int x, {String str: "foo"}); |
| 23 B.corge({int x: 3 * 17, String str: "bar"}); |
| 24 |
| 25 var _x; |
| 26 get x => _x; |
| 27 set x(final value) { _x = value; } |
| 28 |
| 29 grault([int x]); |
| 30 garply({int y}); |
| 31 waldo(int z); |
| 17 } | 32 } |
| 18 | 33 |
| 19 main() { | 34 main() { |
| 20 var constructors = reflectClass(B).constructors; | 35 ClassMirror cm = reflectClass(B); |
| 36 Map<Symbol, MethodMirror> constructors = cm.constructors; |
| 21 | 37 |
| 22 expect('{B: Method(s(B) in s(B), constructor), ' | 38 List<Symbol> constructorKeys = [ |
| 23 'B.bar: Method(s(B.bar) in s(B), constructor), ' | 39 const Symbol('B'), const Symbol('B.bar'), const Symbol('B.baz'), |
| 24 'B.foo: Method(s(B.foo) in s(B), constructor)}', | 40 const Symbol('B.foo'), const Symbol('B.quux'), const Symbol('B.qux'), |
| 25 constructors); | 41 const Symbol('B.corge')]; |
| 42 Expect.setEquals(constructorKeys, constructors.keys); |
| 26 | 43 |
| 27 var unnamedConstructor = constructors[new Symbol('B')]; | 44 MethodMirror unnamedConstructor = constructors[const Symbol('B')]; |
| 28 | 45 expect('Method(s(B) in s(B), constructor)', unnamedConstructor); |
| 29 expect('[]', unnamedConstructor.parameters); | 46 expect('[]', unnamedConstructor.parameters); |
| 30 expect('Class(s(B) in s(test.parameter_test), top-level)', | 47 expect('Class(s(B) in s(test.parameter_test), top-level)', |
| 31 unnamedConstructor.returnType); | 48 unnamedConstructor.returnType); |
| 32 | 49 |
| 33 var fooConstructor = constructors[new Symbol('B.foo')]; | 50 MethodMirror fooConstructor = constructors[const Symbol('B.foo')]; |
| 51 expect('Method(s(B.foo) in s(B), constructor)', fooConstructor); |
| 34 expect('[Parameter(s(x) in s(B.foo),' | 52 expect('[Parameter(s(x) in s(B.foo),' |
| 35 ' type = Class(s(int) in s(dart.core), top-level))]', | 53 ' type = Class(s(int) in s(dart.core), top-level))]', |
| 36 fooConstructor.parameters); | 54 fooConstructor.parameters); |
| 37 expect('Class(s(B) in s(test.parameter_test), top-level)', | 55 expect('Class(s(B) in s(test.parameter_test), top-level)', |
| 38 fooConstructor.returnType); | 56 fooConstructor.returnType); |
| 39 | 57 |
| 40 var barConstructor = constructors[new Symbol('B.bar')]; | 58 MethodMirror barConstructor = constructors[const Symbol('B.bar')]; |
| 59 expect('Method(s(B.bar) in s(B), constructor)', barConstructor); |
| 41 expect('[Parameter(s(z) in s(B.bar),' | 60 expect('[Parameter(s(z) in s(B.bar),' |
| 42 ' type = Class(s(int) in s(dart.core), top-level)), ' | 61 ' type = Class(s(int) in s(dart.core), top-level)), ' |
| 43 'Parameter(s(x) in s(B.bar),' | 62 'Parameter(s(x) in s(B.bar),' |
| 44 ' type = Type(s(dynamic), top-level))]', | 63 ' type = Type(s(dynamic), top-level))]', |
| 45 barConstructor.parameters); | 64 barConstructor.parameters); |
| 46 expect('Class(s(B) in s(test.parameter_test), top-level)', | 65 expect('Class(s(B) in s(test.parameter_test), top-level)', |
| 47 barConstructor.returnType); | 66 barConstructor.returnType); |
| 48 | 67 |
| 49 print(constructors); | 68 MethodMirror bazConstructor = constructors[const Symbol('B.baz')]; |
| 69 expect('Method(s(B.baz) in s(B), constructor)', bazConstructor); |
| 70 expect('[Parameter(s(x) in s(B.baz), final,' |
| 71 ' type = Class(s(int) in s(dart.core), top-level)), ' |
| 72 'Parameter(s(y) in s(B.baz),' |
| 73 ' type = Class(s(int) in s(dart.core), top-level)), ' |
| 74 'Parameter(s(z) in s(B.baz), final,' |
| 75 ' type = Class(s(int) in s(dart.core), top-level))]', |
| 76 bazConstructor.parameters); |
| 77 expect('Class(s(B) in s(test.parameter_test), top-level)', |
| 78 bazConstructor.returnType); |
| 79 |
| 80 MethodMirror quxConstructor = constructors[const Symbol('B.qux')]; |
| 81 expect('Method(s(B.qux) in s(B), constructor)', quxConstructor); |
| 82 expect('[Parameter(s(x) in s(B.qux),' |
| 83 ' type = Class(s(int) in s(dart.core), top-level)), ' |
| 84 'Parameter(s(y) in s(B.qux), optional,' |
| 85 ' value = Instance(value = 4),' |
| 86 ' type = Class(s(int) in s(dart.core), top-level))]', |
| 87 quxConstructor.parameters); |
| 88 expect('Class(s(B) in s(test.parameter_test), top-level)', |
| 89 quxConstructor.returnType); |
| 90 |
| 91 MethodMirror quuxConstructor = constructors[const Symbol('B.quux')]; |
| 92 expect('Method(s(B.quux) in s(B), constructor)', quuxConstructor); |
| 93 expect('[Parameter(s(x) in s(B.quux),' |
| 94 ' type = Class(s(int) in s(dart.core), top-level)), ' |
| 95 'Parameter(s(str) in s(B.quux), optional, named,' |
| 96 ' value = Instance(value = foo),' |
| 97 ' type = Class(s(String) in s(dart.core), top-level))]', |
| 98 quuxConstructor.parameters); |
| 99 expect('Class(s(B) in s(test.parameter_test), top-level)', |
| 100 quuxConstructor.returnType); |
| 101 |
| 102 MethodMirror corgeConstructor = constructors[const Symbol('B.corge')]; |
| 103 expect('Method(s(B.corge) in s(B), constructor)', corgeConstructor); |
| 104 expect('[Parameter(s(x) in s(B.corge), optional, named,' |
| 105 ' value = Instance(value = 51),' |
| 106 ' type = Class(s(int) in s(dart.core), top-level)), ' |
| 107 'Parameter(s(str) in s(B.corge), optional, named,' |
| 108 ' value = Instance(value = bar),' |
| 109 ' type = Class(s(String) in s(dart.core), top-level))]', |
| 110 corgeConstructor.parameters); |
| 111 expect('Class(s(B) in s(test.parameter_test), top-level)', |
| 112 corgeConstructor.returnType); |
| 113 |
| 114 MethodMirror xGetter = cm.getters[const Symbol('x')]; |
| 115 expect('Method(s(x) in s(B), getter)', xGetter); |
| 116 expect('[]', xGetter.parameters); |
| 117 |
| 118 MethodMirror xSetter = cm.setters[const Symbol('x=')]; |
| 119 expect('Method(s(x=) in s(B), setter)', xSetter); |
| 120 expect('[Parameter(s(value) in s(x=), final,' |
| 121 ' type = Type(s(dynamic), top-level))]', |
| 122 xSetter.parameters); |
| 123 |
| 124 MethodMirror grault = cm.members[const Symbol("grault")]; |
| 125 expect('Method(s(grault) in s(B))', grault); |
| 126 expect('[Parameter(s(x) in s(grault), optional,' |
| 127 ' type = Class(s(int) in s(dart.core), top-level))]', |
| 128 grault.parameters); |
| 129 expect('Instance(value = <null>)', grault.parameters[0].defaultValue); |
| 130 |
| 131 MethodMirror garply = cm.members[const Symbol("garply")]; |
| 132 expect('Method(s(garply) in s(B))', garply); |
| 133 expect('[Parameter(s(y) in s(garply), optional, named,' |
| 134 ' type = Class(s(int) in s(dart.core), top-level))]', |
| 135 garply.parameters); |
| 136 expect('Instance(value = <null>)', garply.parameters[0].defaultValue); |
| 137 |
| 138 MethodMirror waldo = cm.members[const Symbol("waldo")]; |
| 139 expect('Method(s(waldo) in s(B))', waldo); |
| 140 expect('[Parameter(s(z) in s(waldo),' |
| 141 ' type = Class(s(int) in s(dart.core), top-level))]', |
| 142 waldo.parameters); |
| 143 expect('<null>', waldo.parameters[0].defaultValue); |
| 50 } | 144 } |
| OLD | NEW |