OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 reflectable_test.declarations_test; | 5 library reflectable_test.declarations_test; |
6 | 6 |
7 import "package:reflectable/reflectable.dart"; | 7 import "package:reflectable/reflectable.dart"; |
8 import "package:unittest/unittest.dart"; | 8 import "package:unittest/unittest.dart"; |
9 | 9 |
10 class Reflector extends Reflectable { | 10 class Reflector extends Reflectable { |
| 11 // TODO(sigurdm): Include capability to reflect on constructors when |
| 12 // available. |
11 const Reflector() : super(invokeMembersCapability); | 13 const Reflector() : super(invokeMembersCapability); |
12 } | 14 } |
13 | 15 |
| 16 // TODO(sigurdm): Adapt this test when we support fields. |
| 17 |
14 @Reflector() | 18 @Reflector() |
15 class A { | 19 abstract class A { |
| 20 A(); |
| 21 A.redirecting() : this(); |
| 22 factory A.factory() { |
| 23 return new B(); |
| 24 } |
| 25 const factory A.redirectingFactory() = A.c; |
| 26 const A.c(); |
16 foo() {} | 27 foo() {} |
| 28 int get getter1 => 10; |
| 29 int get getter2; |
| 30 set setter1(x) => null; |
| 31 operator +(Object other) { |
| 32 return this; |
| 33 } |
17 } | 34 } |
18 | 35 |
19 @Reflector() | 36 @Reflector() |
20 class B extends A { | 37 class B extends A { |
21 bar() {} | 38 bar() {} |
| 39 set setter2(x) => null; |
| 40 @override |
| 41 int get getter1 => 11; |
| 42 @override |
| 43 int get getter2 => 12; |
22 } | 44 } |
23 | 45 |
24 main() { | 46 main() { |
25 const reflector = const Reflector(); | 47 const reflector = const Reflector(); |
26 test("Test declarations", () { | 48 Map<String, DeclarationMirror> declarationsA = |
27 // TODO(sigurdm): Adapt this test when we support constructors, fields, | 49 reflector.reflectType(A).declarations; |
28 // getters and setters in declarations. | 50 Map<String, DeclarationMirror> declarationsB = |
29 expect(reflector.reflectType(A).declarations.values | 51 reflector.reflectType(B).declarations; |
30 .where((DeclarationMirror x) => x is MethodMirror && x.isRegularMethod) | 52 |
31 .map((x) => x.simpleName), ["foo"]); | 53 test("declarations", () { |
32 expect(reflector.reflectType(B).declarations.values | 54 expect(declarationsA.values.map((x) => x.simpleName), new Set.from([ |
33 .where((DeclarationMirror x) => x is MethodMirror && x.isRegularMethod) | 55 "foo", |
34 .map((x) => x.simpleName), ["bar"]); | 56 "getter1", |
| 57 "setter1=", |
| 58 "+", |
| 59 "A", |
| 60 "A.redirecting", |
| 61 "A.factory", |
| 62 "A.redirectingFactory", |
| 63 "A.c" |
| 64 ])); |
| 65 |
| 66 expect(declarationsB.values.map((x) => x.simpleName), |
| 67 new Set.from(["bar", "getter1", "getter2", "setter2=", "B"])); |
| 68 }); |
| 69 |
| 70 test("MethodMirror properties", () { |
| 71 MethodMirror foo = declarationsA["foo"] as MethodMirror; |
| 72 expect(foo.isRegularMethod, isTrue); |
| 73 expect(foo.isStatic, isFalse); |
| 74 expect(foo.isGetter, isFalse); |
| 75 expect(foo.isSetter, isFalse); |
| 76 expect(foo.isPrivate, isFalse); |
| 77 expect(foo.isAbstract, isFalse); |
| 78 expect(foo.isConstructor, isFalse); |
| 79 expect(foo.isGenerativeConstructor, isFalse); |
| 80 expect(foo.isFactoryConstructor, isFalse); |
| 81 expect(foo.isConstConstructor, isFalse); |
| 82 expect(foo.isRedirectingConstructor, isFalse); |
| 83 expect(foo.isOperator, isFalse); |
| 84 expect(foo.isSynthetic, isFalse); |
| 85 expect(foo.isTopLevel, isFalse); |
| 86 MethodMirror setter1 = declarationsA["setter1="] as MethodMirror; |
| 87 expect(setter1.isRegularMethod, isFalse); |
| 88 expect(setter1.isStatic, isFalse); |
| 89 expect(setter1.isGetter, isFalse); |
| 90 expect(setter1.isSetter, isTrue); |
| 91 expect(setter1.isPrivate, isFalse); |
| 92 expect(setter1.isAbstract, isFalse); |
| 93 expect(setter1.isOperator, isFalse); |
| 94 expect(setter1.isSynthetic, isFalse); |
| 95 MethodMirror operatorPlus = declarationsA["+"] as MethodMirror; |
| 96 expect(operatorPlus.isRegularMethod, isTrue); |
| 97 expect(operatorPlus.isStatic, isFalse); |
| 98 expect(operatorPlus.isGetter, isFalse); |
| 99 expect(operatorPlus.isSetter, isFalse); |
| 100 expect(operatorPlus.isPrivate, isFalse); |
| 101 expect(operatorPlus.isAbstract, isFalse); |
| 102 expect(operatorPlus.isConstructor, isFalse); |
| 103 expect(operatorPlus.isOperator, isTrue); |
| 104 MethodMirror constructorA = declarationsA["A"] as MethodMirror; |
| 105 expect(constructorA.isRegularMethod, isFalse); |
| 106 expect(constructorA.isStatic, isFalse); |
| 107 expect(constructorA.isGetter, isFalse); |
| 108 expect(constructorA.isSetter, isFalse); |
| 109 expect(constructorA.isPrivate, isFalse); |
| 110 expect(constructorA.isAbstract, isFalse); |
| 111 expect(constructorA.isConstructor, isTrue); |
| 112 expect(constructorA.isGenerativeConstructor, isTrue); |
| 113 expect(constructorA.isFactoryConstructor, isFalse); |
| 114 expect(constructorA.isConstConstructor, isFalse); |
| 115 expect(constructorA.isRedirectingConstructor, isFalse); |
| 116 expect(constructorA.isOperator, isFalse); |
| 117 expect(constructorA.isSynthetic, isFalse); |
| 118 expect(constructorA.isTopLevel, isFalse); |
| 119 MethodMirror redirectingConstructorA = |
| 120 declarationsA["A.redirecting"] as MethodMirror; |
| 121 expect(redirectingConstructorA.isConstructor, isTrue); |
| 122 expect(redirectingConstructorA.isGenerativeConstructor, isTrue); |
| 123 expect(redirectingConstructorA.isFactoryConstructor, isFalse); |
| 124 expect(redirectingConstructorA.isConstConstructor, isFalse); |
| 125 expect(redirectingConstructorA.isRedirectingConstructor, isTrue); |
| 126 MethodMirror factoryConstructorA = |
| 127 declarationsA["A.factory"] as MethodMirror; |
| 128 expect(factoryConstructorA.isConstructor, isTrue); |
| 129 expect(factoryConstructorA.isGenerativeConstructor, isFalse); |
| 130 expect(factoryConstructorA.isFactoryConstructor, isTrue); |
| 131 expect(factoryConstructorA.isConstConstructor, isFalse); |
| 132 expect(factoryConstructorA.isRedirectingConstructor, isFalse); |
| 133 MethodMirror constConstructorA = declarationsA["A.c"] as MethodMirror; |
| 134 expect(constConstructorA.isConstructor, isTrue); |
| 135 expect(constConstructorA.isGenerativeConstructor, isTrue); |
| 136 expect(constConstructorA.isFactoryConstructor, isFalse); |
| 137 expect(constConstructorA.isConstConstructor, isTrue); |
| 138 expect(constConstructorA.isRedirectingConstructor, isFalse); |
| 139 MethodMirror redirectingFactoryConstructorA = |
| 140 declarationsA["A.redirectingFactory"] as MethodMirror; |
| 141 expect(redirectingFactoryConstructorA.isConstructor, isTrue); |
| 142 expect(redirectingFactoryConstructorA.isGenerativeConstructor, isFalse); |
| 143 expect(redirectingFactoryConstructorA.isFactoryConstructor, isTrue); |
| 144 expect(redirectingFactoryConstructorA.isConstConstructor, isTrue); |
| 145 expect(redirectingFactoryConstructorA.isRedirectingConstructor, isTrue); |
| 146 }); |
| 147 |
| 148 test("instanceMethods", () { |
| 149 Map<String, DeclarationMirror> instanceMembersA = |
| 150 reflector.reflectType(A).instanceMembers; |
| 151 expect(instanceMembersA.values.map((x) => x.simpleName), new Set.from([ |
| 152 "toString", |
| 153 "hashCode", |
| 154 "==", |
| 155 "noSuchMethod", |
| 156 "runtimeType", |
| 157 "foo", |
| 158 "getter1", |
| 159 "setter1=", |
| 160 "+" |
| 161 ])); |
| 162 Map<String, DeclarationMirror> instanceMembersB = |
| 163 reflector.reflectType(B).instanceMembers; |
| 164 expect(instanceMembersB.values.map((x) => x.simpleName), new Set.from([ |
| 165 "toString", |
| 166 "hashCode", |
| 167 "==", |
| 168 "noSuchMethod", |
| 169 "runtimeType", |
| 170 "foo", |
| 171 "bar", |
| 172 "getter1", |
| 173 "getter2", |
| 174 "setter1=", |
| 175 "setter2=", |
| 176 "+" |
| 177 ])); |
35 }); | 178 }); |
36 } | 179 } |
OLD | NEW |