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 |
14 @Reflector() | 16 @Reflector() |
15 class A { | 17 abstract class A { |
| 18 A(); |
| 19 A.redirecting() : this(); |
| 20 factory A.factory() { |
| 21 return new B(); |
| 22 } |
| 23 const factory A.redirectingFactory() = A.c; |
| 24 const A.c(); |
16 foo() {} | 25 foo() {} |
| 26 int get getter1 => 10; |
| 27 int get getter2; |
| 28 set setter1(x) => null; |
| 29 operator +(Object other) { |
| 30 return this; |
| 31 } |
17 } | 32 } |
18 | 33 |
19 @Reflector() | 34 @Reflector() |
20 class B extends A { | 35 class B extends A { |
21 bar() {} | 36 bar() {} |
| 37 set setter2(x) => null; |
| 38 @override |
| 39 int get getter1 => 11; |
| 40 @override |
| 41 int get getter2 => 12; |
22 } | 42 } |
23 | 43 |
24 main() { | 44 main() { |
25 const reflector = const Reflector(); | 45 const reflector = const Reflector(); |
26 test("Test declarations", () { | 46 test("Test declarations", () { |
27 // TODO(sigurdm): Adapt this test when we support constructors, fields, | 47 // TODO(sigurdm): Adapt this test when we support constructors, fields, |
28 // getters and setters in declarations. | 48 // in declarations. |
29 expect(reflector.reflectType(A).declarations.values | 49 Map<String, DeclarationMirror> declarationsA = |
30 .where((DeclarationMirror x) => x is MethodMirror && x.isRegularMethod) | 50 reflector.reflectType(A).declarations; |
31 .map((x) => x.simpleName), ["foo"]); | 51 expect(declarationsA.values |
32 expect(reflector.reflectType(B).declarations.values | 52 .where((DeclarationMirror x) => x is MethodMirror) |
33 .where((DeclarationMirror x) => x is MethodMirror && x.isRegularMethod) | 53 .map((x) => x.simpleName), new Set.from([ |
34 .map((x) => x.simpleName), ["bar"]); | 54 "foo", |
| 55 "getter1", |
| 56 "setter1=", |
| 57 "+", |
| 58 "A", |
| 59 "A.redirecting", |
| 60 "A.factory", |
| 61 "A.redirectingFactory", |
| 62 "A.c" |
| 63 ])); |
| 64 MethodMirror foo = declarationsA["foo"] as MethodMirror; |
| 65 expect(foo.isRegularMethod, isTrue); |
| 66 expect(foo.isStatic, isFalse); |
| 67 expect(foo.isGetter, isFalse); |
| 68 expect(foo.isSetter, isFalse); |
| 69 expect(foo.isPrivate, isFalse); |
| 70 expect(foo.isAbstract, isFalse); |
| 71 expect(foo.isConstructor, isFalse); |
| 72 expect(foo.isGenerativeConstructor, isFalse); |
| 73 expect(foo.isFactoryConstructor, isFalse); |
| 74 expect(foo.isConstConstructor, isFalse); |
| 75 expect(foo.isRedirectingConstructor, isFalse); |
| 76 expect(foo.isOperator, isFalse); |
| 77 expect(foo.isSynthetic, isFalse); |
| 78 expect(foo.isTopLevel, isFalse); |
| 79 MethodMirror setter1 = declarationsA["setter1="] as MethodMirror; |
| 80 expect(setter1.isRegularMethod, isFalse); |
| 81 expect(setter1.isStatic, isFalse); |
| 82 expect(setter1.isGetter, isFalse); |
| 83 expect(setter1.isSetter, isTrue); |
| 84 expect(setter1.isPrivate, isFalse); |
| 85 expect(setter1.isAbstract, isFalse); |
| 86 expect(setter1.isOperator, isFalse); |
| 87 expect(setter1.isSynthetic, isFalse); |
| 88 MethodMirror operatorPlus = declarationsA["+"] as MethodMirror; |
| 89 expect(operatorPlus.isRegularMethod, isTrue); |
| 90 expect(operatorPlus.isStatic, isFalse); |
| 91 expect(operatorPlus.isGetter, isFalse); |
| 92 expect(operatorPlus.isSetter, isFalse); |
| 93 expect(operatorPlus.isPrivate, isFalse); |
| 94 expect(operatorPlus.isAbstract, isFalse); |
| 95 expect(operatorPlus.isConstructor, isFalse); |
| 96 expect(operatorPlus.isOperator, isTrue); |
| 97 MethodMirror constructorA = declarationsA["A"] as MethodMirror; |
| 98 expect(constructorA.isRegularMethod, isFalse); |
| 99 expect(constructorA.isStatic, isFalse); |
| 100 expect(constructorA.isGetter, isFalse); |
| 101 expect(constructorA.isSetter, isFalse); |
| 102 expect(constructorA.isPrivate, isFalse); |
| 103 expect(constructorA.isAbstract, isFalse); |
| 104 expect(constructorA.isConstructor, isTrue); |
| 105 expect(constructorA.isGenerativeConstructor, isTrue); |
| 106 expect(constructorA.isFactoryConstructor, isFalse); |
| 107 expect(constructorA.isConstConstructor, isFalse); |
| 108 expect(constructorA.isRedirectingConstructor, isFalse); |
| 109 expect(constructorA.isOperator, isFalse); |
| 110 expect(constructorA.isSynthetic, isFalse); |
| 111 expect(constructorA.isTopLevel, isFalse); |
| 112 MethodMirror redirectingConstructorA = |
| 113 declarationsA["A.redirecting"] as MethodMirror; |
| 114 expect(redirectingConstructorA.isConstructor, isTrue); |
| 115 expect(redirectingConstructorA.isGenerativeConstructor, isTrue); |
| 116 expect(redirectingConstructorA.isFactoryConstructor, isFalse); |
| 117 expect(redirectingConstructorA.isConstConstructor, isFalse); |
| 118 expect(redirectingConstructorA.isRedirectingConstructor, isTrue); |
| 119 MethodMirror factoryConstructorA = |
| 120 declarationsA["A.factory"] as MethodMirror; |
| 121 expect(factoryConstructorA.isConstructor, isTrue); |
| 122 expect(factoryConstructorA.isGenerativeConstructor, isFalse); |
| 123 expect(factoryConstructorA.isFactoryConstructor, isTrue); |
| 124 expect(factoryConstructorA.isConstConstructor, isFalse); |
| 125 expect(factoryConstructorA.isRedirectingConstructor, isFalse); |
| 126 MethodMirror constConstructorA = declarationsA["A.c"] as MethodMirror; |
| 127 expect(constConstructorA.isConstructor, isTrue); |
| 128 expect(constConstructorA.isGenerativeConstructor, isTrue); |
| 129 expect(constConstructorA.isFactoryConstructor, isFalse); |
| 130 expect(constConstructorA.isConstConstructor, isTrue); |
| 131 expect(constConstructorA.isRedirectingConstructor, isFalse); |
| 132 MethodMirror redirectingFactoryConstructorA = |
| 133 declarationsA["A.redirectingFactory"] as MethodMirror; |
| 134 expect(redirectingFactoryConstructorA.isConstructor, isTrue); |
| 135 expect(redirectingFactoryConstructorA.isGenerativeConstructor, isFalse); |
| 136 expect(redirectingFactoryConstructorA.isFactoryConstructor, isTrue); |
| 137 expect(redirectingFactoryConstructorA.isConstConstructor, isTrue); |
| 138 expect(redirectingFactoryConstructorA.isRedirectingConstructor, isTrue); |
| 139 var declarationsB = reflector.reflectType(B).declarations; |
| 140 expect(declarationsB.values |
| 141 .where((DeclarationMirror x) => x is MethodMirror) |
| 142 .map((x) => x.simpleName), |
| 143 new Set.from(["bar", "getter1", "getter2", "setter2=", "B"])); |
35 }); | 144 }); |
36 } | 145 } |
OLD | NEW |