OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart Team. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in |
| 3 // the LICENSE file. |
| 4 |
| 5 // File being transformed by the reflectable transformer. |
| 6 // Part of the entry point 'reflectors_test.dart'. |
| 7 // |
| 8 // Independence: This library depends on the domain classes `M1`..`M3`, |
| 9 // `A`..`D`, `P`, and it dynamically uses the reflectors, but it does not |
| 10 // statically depend on 'meta_reflectors_definer.dart' nor on |
| 11 // 'meta_reflectors_domain_definer.dart'. |
| 12 |
| 13 library test_reflectable.test.meta_reflectors_user; |
| 14 |
| 15 import "package:reflectable/reflectable.dart"; |
| 16 import "package:unittest/unittest.dart"; |
| 17 import "meta_reflectors_meta.dart"; |
| 18 import "meta_reflectors_domain.dart"; |
| 19 |
| 20 testReflector(Reflectable reflector, String desc) { |
| 21 test("Mixin, $desc", () { |
| 22 ClassMirror aMirror = reflector.reflectType(A); |
| 23 ClassMirror bMirror = reflector.reflectType(B); |
| 24 ClassMirror cMirror = reflector.reflectType(C); |
| 25 ClassMirror dMirror = reflector.reflectType(D); |
| 26 ClassMirror m1Mirror = reflector.reflectType(M1); |
| 27 ClassMirror m2Mirror = reflector.reflectType(M2); |
| 28 ClassMirror m3Mirror = reflector.reflectType(M3); |
| 29 expect(aMirror.mixin, aMirror); |
| 30 expect(bMirror.mixin, bMirror); |
| 31 expect(cMirror.mixin, cMirror); |
| 32 expect(m1Mirror.mixin, m1Mirror); |
| 33 expect(m2Mirror.mixin, m2Mirror); |
| 34 expect(m3Mirror.mixin, m3Mirror); |
| 35 expect(bMirror.superclass.mixin, m1Mirror); |
| 36 expect(cMirror.superclass.superclass.mixin, m2Mirror); |
| 37 expect(cMirror.superclass.mixin, m3Mirror); |
| 38 expect(cMirror.superclass.superclass.superclass, bMirror); |
| 39 expect(dMirror.mixin, m1Mirror); |
| 40 expect(dMirror.superclass.mixin, aMirror); |
| 41 expect(bMirror.superclass.declarations["foo"].owner, m1Mirror); |
| 42 expect(bMirror.superclass.declarations["field"].owner, m1Mirror); |
| 43 expect(bMirror.superclass.declarations["staticBar"], null); |
| 44 expect(bMirror.superclass.hasReflectedType, true); |
| 45 expect(bMirror.superclass.reflectedType, const isInstanceOf<Type>()); |
| 46 expect(bMirror.superclass.superclass.reflectedType, |
| 47 const isInstanceOf<Type>()); |
| 48 }); |
| 49 } |
| 50 |
| 51 Matcher throwsANoSuchCapabilityException = |
| 52 throwsA(const isInstanceOf<NoSuchCapabilityError>()); |
| 53 |
| 54 Iterable<String> getNames(Iterable<Reflectable> reflectables) { |
| 55 return reflectables.map((Reflectable reflector) { |
| 56 String fullString = reflector.toString(); |
| 57 return fullString.substring(13, fullString.length - 1); |
| 58 }); |
| 59 } |
| 60 |
| 61 runTests() { |
| 62 List<Reflectable> reflectors = |
| 63 const AllReflectorsMetaReflector().reflectors.toList(); |
| 64 |
| 65 test("MetaReflector, set of reflectors", () { |
| 66 expect( |
| 67 getNames(reflectors).toSet(), |
| 68 [ |
| 69 "Reflector", |
| 70 "Reflector2", |
| 71 "ReflectorUpwardsClosed", |
| 72 "ReflectorUpwardsClosedToA", |
| 73 "ReflectorUpwardsClosedUntilA", |
| 74 "ScopeMetaReflector", |
| 75 "AllReflectorsMetaReflector" |
| 76 ].toSet()); |
| 77 expect(getNames(const ScopeMetaReflector().reflectablesOfScope("polymer")), |
| 78 ["Reflector", "ReflectorUpwardsClosed"].toSet()); |
| 79 expect(getNames(const ScopeMetaReflector().reflectablesOfScope("observe")), |
| 80 ["Reflector2", "ReflectorUpwardsClosed"].toSet()); |
| 81 }); |
| 82 |
| 83 const ScopeMetaReflector().reflectablesOfScope("polymer").forEach( |
| 84 (Reflectable reflector) => testReflector(reflector, "$reflector")); |
| 85 |
| 86 test("MetaReflector, select by name", () { |
| 87 var reflector2 = reflectors |
| 88 .firstWhere((Reflectable reflector) => "$reflector".contains("2")); |
| 89 ClassMirror bMirror = reflector2.reflectType(B); |
| 90 ClassMirror cMirror = reflector2.reflectType(C); |
| 91 ClassMirror dMirror = reflector2.reflectType(D); |
| 92 ClassMirror m1Mirror = reflector2.reflectType(M1); |
| 93 ClassMirror m2Mirror = reflector2.reflectType(M2); |
| 94 ClassMirror m3Mirror = reflector2.reflectType(M3); |
| 95 expect(bMirror.mixin, bMirror); |
| 96 expect(cMirror.mixin, cMirror); |
| 97 expect(dMirror.mixin, m1Mirror); |
| 98 expect(m1Mirror.mixin, m1Mirror); |
| 99 // Test that metadata is preserved. |
| 100 expect(m1Mirror.metadata, contains(const P())); |
| 101 expect(m2Mirror.mixin, m2Mirror); |
| 102 expect(m3Mirror.mixin, m3Mirror); |
| 103 expect(bMirror.superclass.mixin, m1Mirror); |
| 104 // Test that the mixin-application does not inherit the metadata from its |
| 105 // mixin. |
| 106 expect(bMirror.superclass.metadata, isEmpty); |
| 107 expect( |
| 108 () => bMirror.superclass.superclass, throwsANoSuchCapabilityException); |
| 109 expect(cMirror.superclass.superclass.mixin, m2Mirror); |
| 110 expect(cMirror.superclass.mixin, m3Mirror); |
| 111 expect(cMirror.superclass.superclass.superclass, bMirror); |
| 112 expect(() => dMirror.superclass, throwsANoSuchCapabilityException); |
| 113 }); |
| 114 |
| 115 test("MetaReflector, select by capability", () { |
| 116 var reflector = reflectors.firstWhere((Reflectable reflector) { |
| 117 return (reflector.capabilities.any((ReflectCapability capability) => |
| 118 capability is SuperclassQuantifyCapability && |
| 119 capability.upperBound == A && |
| 120 !capability.excludeUpperBound)); |
| 121 }); |
| 122 ClassMirror aMirror = reflector.reflectType(A); |
| 123 ClassMirror bMirror = reflector.reflectType(B); |
| 124 ClassMirror cMirror = reflector.reflectType(C); |
| 125 ClassMirror dMirror = reflector.reflectType(D); |
| 126 expect(() => reflector.reflectType(M1), throwsANoSuchCapabilityException); |
| 127 expect(() => reflector.reflectType(M2), throwsANoSuchCapabilityException); |
| 128 expect(() => reflector.reflectType(M3), throwsANoSuchCapabilityException); |
| 129 expect(() => bMirror.superclass.mixin, throwsANoSuchCapabilityException); |
| 130 expect(bMirror.superclass.superclass, aMirror); |
| 131 expect(() => cMirror.superclass.mixin, throwsANoSuchCapabilityException); |
| 132 expect(() => cMirror.superclass.superclass.mixin, |
| 133 throwsANoSuchCapabilityException); |
| 134 expect(cMirror.superclass.superclass.superclass, bMirror); |
| 135 expect(() => dMirror.mixin, throwsANoSuchCapabilityException); |
| 136 expect(dMirror.superclass, aMirror); |
| 137 }); |
| 138 } |
OLD | NEW |