| 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 // Tests some basic functionality of instance mirrors and class mirrors. | 5 // Tests some basic functionality of instance mirrors and class mirrors. |
| 6 | 6 |
| 7 library test_reflectable.test.basic_test; | 7 library test_reflectable.test.basic_test; |
| 8 | 8 |
| 9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 10 import 'package:reflectable/reflectable.dart' as r; | 10 import 'package:reflectable/reflectable.dart' as r; |
| 11 import 'package:reflectable/capability.dart'; | 11 import 'package:reflectable/capability.dart'; |
| 12 | 12 |
| 13 class MyReflectable extends r.Reflectable { | 13 class MyReflectable extends r.Reflectable { |
| 14 const MyReflectable() | 14 const MyReflectable() |
| 15 : super(instanceInvokeCapability, newInstanceCapability); | 15 : super(instanceInvokeCapability, newInstanceCapability, |
| 16 declarationsCapability); |
| 16 } | 17 } |
| 17 | 18 |
| 18 @MyReflectable() | 19 @MyReflectable() |
| 19 class A { | 20 class A { |
| 20 var foo; | 21 var foo; |
| 21 instanceMethod(x) => 'A:instanceMethod($x)'; | 22 instanceMethod(x) => 'A:instanceMethod($x)'; |
| 22 get accessor => 'A:get accessor'; | 23 get accessor => 'A:get accessor'; |
| 23 set accessor(x) { | 24 set accessor(x) { |
| 24 accessorBackingStorageA = x; | 25 accessorBackingStorageA = x; |
| 25 } | 26 } |
| 27 |
| 26 aMethod() => 'aMethod'; | 28 aMethod() => 'aMethod'; |
| 27 } | 29 } |
| 28 | 30 |
| 29 @MyReflectable() | 31 @MyReflectable() |
| 30 class B extends A { | 32 class B extends A { |
| 31 get foo => 'B:get field'; | 33 get foo => 'B:get field'; |
| 32 instanceMethod(x) => 'B:instanceMethod($x)'; | 34 instanceMethod(x) => 'B:instanceMethod($x)'; |
| 33 get accessor => 'B:get accessor'; | 35 get accessor => 'B:get accessor'; |
| 34 set accessor(x) { | 36 set accessor(x) { |
| 35 accessorBackingStorageB = x; | 37 accessorBackingStorageB = x; |
| 36 } | 38 } |
| 39 |
| 37 bMethod() => 'bMethod'; | 40 bMethod() => 'bMethod'; |
| 38 } | 41 } |
| 39 | 42 |
| 40 @MyReflectable() | 43 @MyReflectable() |
| 41 class C extends B { | 44 class C extends B { |
| 42 set foo(x) { | 45 set foo(x) { |
| 43 fooBackingStorageC = x; | 46 fooBackingStorageC = x; |
| 44 } | 47 } |
| 48 |
| 45 instanceMethod(x) => 'C:instanceMethod($x)'; | 49 instanceMethod(x) => 'C:instanceMethod($x)'; |
| 46 get accessor => 'C:get accessor'; | 50 get accessor => 'C:get accessor'; |
| 47 set accessor(x) { | 51 set accessor(x) { |
| 48 accessorBackingStorageC = x; | 52 accessorBackingStorageC = x; |
| 49 } | 53 } |
| 54 |
| 50 cMethod() => 'cMethod'; | 55 cMethod() => 'cMethod'; |
| 51 } | 56 } |
| 52 | 57 |
| 53 List<r.DeclarationMirror> filteredDeclarationsOf(r.ClassMirror cm, predicate) { | 58 List<r.DeclarationMirror> filteredDeclarationsOf(r.ClassMirror cm, predicate) { |
| 54 var result = new List(); | 59 var result = new List(); |
| 55 cm.declarations.forEach((k, v) { | 60 cm.declarations.forEach((k, v) { |
| 56 if (predicate(v)) { | 61 if (predicate(v)) { |
| 57 result.add(v); | 62 result.add(v); |
| 58 } | 63 } |
| 59 }); | 64 }); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 74 | 79 |
| 75 List<r.MethodMirror> gettersOf(r.ClassMirror cm) { | 80 List<r.MethodMirror> gettersOf(r.ClassMirror cm) { |
| 76 return filteredDeclarationsOf(cm, (v) => v is r.MethodMirror && v.isGetter); | 81 return filteredDeclarationsOf(cm, (v) => v is r.MethodMirror && v.isGetter); |
| 77 } | 82 } |
| 78 | 83 |
| 79 List<r.MethodMirror> settersOf(r.ClassMirror cm) { | 84 List<r.MethodMirror> settersOf(r.ClassMirror cm) { |
| 80 return filteredDeclarationsOf(cm, (v) => v is r.MethodMirror && v.isSetter); | 85 return filteredDeclarationsOf(cm, (v) => v is r.MethodMirror && v.isSetter); |
| 81 } | 86 } |
| 82 | 87 |
| 83 List<r.MethodMirror> methodsOf(r.ClassMirror cm) { | 88 List<r.MethodMirror> methodsOf(r.ClassMirror cm) { |
| 84 return filteredDeclarationsOf(cm, | 89 return filteredDeclarationsOf( |
| 85 (v) => v is r.MethodMirror && v.isRegularMethod); | 90 cm, (v) => v is r.MethodMirror && v.isRegularMethod); |
| 86 } | 91 } |
| 87 | 92 |
| 88 Matcher throwsNoSuchCapabilityError = throwsA(isNoSuchCapabilityError); | 93 Matcher throwsNoSuchCapabilityError = throwsA(isNoSuchCapabilityError); |
| 89 Matcher throwsNoSuchMethodError = throwsA(isNoSuchMethodError); | 94 Matcher throwsNoSuchMethodError = throwsA(isNoSuchMethodError); |
| 90 Matcher isNoSuchCapabilityError = new isInstanceOf<NoSuchCapabilityError>(); | 95 Matcher isNoSuchCapabilityError = new isInstanceOf<NoSuchCapabilityError>(); |
| 91 Matcher isNoSuchMethodError = new isInstanceOf<NoSuchMethodError>(); | 96 Matcher isNoSuchMethodError = new isInstanceOf<NoSuchMethodError>(); |
| 92 | 97 |
| 93 main() { | 98 main() { |
| 94 var unnamed = ''; | 99 var unnamed = ''; |
| 95 var foo = 'foo'; | 100 var foo = 'foo'; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 expect(() => aM.invoke(cMethod, []), throwsNoSuchMethodError); | 176 expect(() => aM.invoke(cMethod, []), throwsNoSuchMethodError); |
| 172 expect(() => bM.invoke(cMethod, []), throwsNoSuchMethodError); | 177 expect(() => bM.invoke(cMethod, []), throwsNoSuchMethodError); |
| 173 expect(cM.invoke(cMethod, []), "cMethod"); | 178 expect(cM.invoke(cMethod, []), "cMethod"); |
| 174 }); | 179 }); |
| 175 | 180 |
| 176 test('getters and setters', () { | 181 test('getters and setters', () { |
| 177 expect(gettersOf(aClass).map((x) => x.simpleName), ["accessor"]); | 182 expect(gettersOf(aClass).map((x) => x.simpleName), ["accessor"]); |
| 178 expect(gettersOf(bClass).map((x) => x.simpleName), | 183 expect(gettersOf(bClass).map((x) => x.simpleName), |
| 179 ["accessor", "foo"].toSet()); | 184 ["accessor", "foo"].toSet()); |
| 180 expect(gettersOf(cClass).map((x) => x.simpleName), ["accessor"]); | 185 expect(gettersOf(cClass).map((x) => x.simpleName), ["accessor"]); |
| 181 expect(settersOf(aClass).map((x) => x.simpleName), | 186 expect(settersOf(aClass).map((x) => x.simpleName), ["accessor="]); |
| 182 ["accessor="]); | 187 expect(settersOf(bClass).map((x) => x.simpleName), ["accessor="].toSet()); |
| 183 expect(settersOf(bClass).map((x) => x.simpleName), | |
| 184 ["accessor="].toSet()); | |
| 185 expect(settersOf(cClass).map((x) => x.simpleName), | 188 expect(settersOf(cClass).map((x) => x.simpleName), |
| 186 ["accessor=", "foo="].toSet()); | 189 ["accessor=", "foo="].toSet()); |
| 187 expect(methodsOf(aClass).map((x) => x.simpleName), | 190 expect(methodsOf(aClass).map((x) => x.simpleName), |
| 188 ["instanceMethod", "aMethod"].toSet()); | 191 ["instanceMethod", "aMethod"].toSet()); |
| 189 expect(methodsOf(bClass).map((x) => x.simpleName), | 192 expect(methodsOf(bClass).map((x) => x.simpleName), |
| 190 ["instanceMethod", "bMethod"].toSet()); | 193 ["instanceMethod", "bMethod"].toSet()); |
| 191 expect(methodsOf(cClass).map((x) => x.simpleName), | 194 expect(methodsOf(cClass).map((x) => x.simpleName), |
| 192 ["instanceMethod", "cMethod"].toSet()); | 195 ["instanceMethod", "cMethod"].toSet()); |
| 193 }); | 196 }); |
| 194 } | 197 } |
| OLD | NEW |