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(instanceInvokeCapability); | 13 const Reflector() : super(instanceInvokeCapability); |
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 "getter2", |
| 57 "setter1=", |
| 58 "+", |
| 59 "A", |
| 60 "A.redirecting", |
| 61 "A.factory", |
| 62 "A.redirectingFactory", |
| 63 "A.c" |
| 64 ])); |
| 65 MethodMirror foo = declarationsA["foo"] as MethodMirror; |
| 66 expect(foo.isRegularMethod, isTrue); |
| 67 expect(foo.isStatic, isFalse); |
| 68 expect(foo.isGetter, isFalse); |
| 69 expect(foo.isSetter, isFalse); |
| 70 expect(foo.isPrivate, isFalse); |
| 71 expect(foo.isAbstract, isFalse); |
| 72 expect(foo.isConstructor, isFalse); |
| 73 expect(foo.isGenerativeConstructor, isFalse); |
| 74 expect(foo.isFactoryConstructor, isFalse); |
| 75 expect(foo.isConstConstructor, isFalse); |
| 76 expect(foo.isRedirectingConstructor, isFalse); |
| 77 expect(foo.isOperator, isFalse); |
| 78 expect(foo.isSynthetic, isFalse); |
| 79 expect(foo.isTopLevel, isFalse); |
| 80 MethodMirror getter2A = declarationsA["getter2"] as MethodMirror; |
| 81 expect(getter2A.isRegularMethod, isFalse); |
| 82 expect(getter2A.isStatic, isFalse); |
| 83 expect(getter2A.isGetter, isTrue); |
| 84 expect(getter2A.isSetter, isFalse); |
| 85 expect(getter2A.isPrivate, isFalse); |
| 86 expect(getter2A.isAbstract, isTrue); |
| 87 expect(getter2A.isConstructor, isFalse); |
| 88 expect(getter2A.isGenerativeConstructor, isFalse); |
| 89 expect(getter2A.isFactoryConstructor, isFalse); |
| 90 expect(getter2A.isConstConstructor, isFalse); |
| 91 expect(getter2A.isRedirectingConstructor, isFalse); |
| 92 expect(getter2A.isOperator, isFalse); |
| 93 expect(getter2A.isSynthetic, isFalse); |
| 94 expect(getter2A.isTopLevel, isFalse); |
| 95 MethodMirror setter1 = declarationsA["setter1="] as MethodMirror; |
| 96 expect(setter1.isRegularMethod, isFalse); |
| 97 expect(setter1.isStatic, isFalse); |
| 98 expect(setter1.isGetter, isFalse); |
| 99 expect(setter1.isSetter, isTrue); |
| 100 expect(setter1.isPrivate, isFalse); |
| 101 expect(setter1.isAbstract, isFalse); |
| 102 expect(setter1.isOperator, isFalse); |
| 103 expect(setter1.isSynthetic, isFalse); |
| 104 MethodMirror operatorPlus = declarationsA["+"] as MethodMirror; |
| 105 expect(operatorPlus.isRegularMethod, isTrue); |
| 106 expect(operatorPlus.isStatic, isFalse); |
| 107 expect(operatorPlus.isGetter, isFalse); |
| 108 expect(operatorPlus.isSetter, isFalse); |
| 109 expect(operatorPlus.isPrivate, isFalse); |
| 110 expect(operatorPlus.isAbstract, isFalse); |
| 111 expect(operatorPlus.isConstructor, isFalse); |
| 112 expect(operatorPlus.isOperator, isTrue); |
| 113 MethodMirror constructorA = declarationsA["A"] as MethodMirror; |
| 114 expect(constructorA.isRegularMethod, isFalse); |
| 115 expect(constructorA.isStatic, isFalse); |
| 116 expect(constructorA.isGetter, isFalse); |
| 117 expect(constructorA.isSetter, isFalse); |
| 118 expect(constructorA.isPrivate, isFalse); |
| 119 expect(constructorA.isAbstract, isFalse); |
| 120 expect(constructorA.isConstructor, isTrue); |
| 121 expect(constructorA.isGenerativeConstructor, isTrue); |
| 122 expect(constructorA.isFactoryConstructor, isFalse); |
| 123 expect(constructorA.isConstConstructor, isFalse); |
| 124 expect(constructorA.isRedirectingConstructor, isFalse); |
| 125 expect(constructorA.isOperator, isFalse); |
| 126 expect(constructorA.isSynthetic, isFalse); |
| 127 expect(constructorA.isTopLevel, isFalse); |
| 128 MethodMirror redirectingConstructorA = |
| 129 declarationsA["A.redirecting"] as MethodMirror; |
| 130 expect(redirectingConstructorA.isConstructor, isTrue); |
| 131 expect(redirectingConstructorA.isGenerativeConstructor, isTrue); |
| 132 expect(redirectingConstructorA.isFactoryConstructor, isFalse); |
| 133 expect(redirectingConstructorA.isConstConstructor, isFalse); |
| 134 expect(redirectingConstructorA.isRedirectingConstructor, isTrue); |
| 135 MethodMirror factoryConstructorA = |
| 136 declarationsA["A.factory"] as MethodMirror; |
| 137 expect(factoryConstructorA.isConstructor, isTrue); |
| 138 expect(factoryConstructorA.isGenerativeConstructor, isFalse); |
| 139 expect(factoryConstructorA.isFactoryConstructor, isTrue); |
| 140 expect(factoryConstructorA.isConstConstructor, isFalse); |
| 141 expect(factoryConstructorA.isRedirectingConstructor, isFalse); |
| 142 MethodMirror constConstructorA = declarationsA["A.c"] as MethodMirror; |
| 143 expect(constConstructorA.isConstructor, isTrue); |
| 144 expect(constConstructorA.isGenerativeConstructor, isTrue); |
| 145 expect(constConstructorA.isFactoryConstructor, isFalse); |
| 146 expect(constConstructorA.isConstConstructor, isTrue); |
| 147 expect(constConstructorA.isRedirectingConstructor, isFalse); |
| 148 MethodMirror redirectingFactoryConstructorA = |
| 149 declarationsA["A.redirectingFactory"] as MethodMirror; |
| 150 expect(redirectingFactoryConstructorA.isConstructor, isTrue); |
| 151 expect(redirectingFactoryConstructorA.isGenerativeConstructor, isFalse); |
| 152 expect(redirectingFactoryConstructorA.isFactoryConstructor, isTrue); |
| 153 expect(redirectingFactoryConstructorA.isConstConstructor, isTrue); |
| 154 expect(redirectingFactoryConstructorA.isRedirectingConstructor, isTrue); |
| 155 var declarationsB = reflector.reflectType(B).declarations; |
| 156 expect(declarationsB.values |
| 157 .where((DeclarationMirror x) => x is MethodMirror) |
| 158 .map((x) => x.simpleName), |
| 159 new Set.from(["bar", "getter1", "getter2", "setter2=", "B"])); |
35 }); | 160 }); |
36 } | 161 } |
OLD | NEW |