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 /// Uses public features of an instance of a private class in a different |
| 7 /// library. This illustrates that there is (very limited) support for access |
| 8 /// to private features. |
| 9 library test_reflectable.test.private_class_test; |
| 10 |
| 11 @GlobalQuantifyCapability("PublicClass", privacyReflectable) |
| 12 import 'package:reflectable/reflectable.dart'; |
| 13 import 'package:unittest/unittest.dart'; |
| 14 import 'private_class_library.dart'; |
| 15 |
| 16 class PrivacyReflectable extends Reflectable { |
| 17 const PrivacyReflectable() |
| 18 : super( |
| 19 subtypeQuantifyCapability, |
| 20 reflectedTypeCapability, |
| 21 instanceInvokeCapability, |
| 22 declarationsCapability, |
| 23 libraryCapability); |
| 24 } |
| 25 |
| 26 const privacyReflectable = const PrivacyReflectable(); |
| 27 |
| 28 final Set<String> libraryClassNames = <String>[ |
| 29 "PublicClass", |
| 30 "_PrivateClass1", |
| 31 "_PrivateClass2", |
| 32 "PublicSubclass1", |
| 33 "PublicSubclass2" |
| 34 ].toSet(); |
| 35 |
| 36 void testPrivacyViolation(PublicClass object, String description, |
| 37 {bool doReflect: true}) { |
| 38 test("Privacy, $description", () { |
| 39 bool canReflect = privacyReflectable.canReflect(object); |
| 40 expect(canReflect, doReflect); |
| 41 if (canReflect) { |
| 42 // Check that we can reflect upon [object], and that its class |
| 43 // is among the expected ones. |
| 44 InstanceMirror instanceMirror = privacyReflectable.reflect(object); |
| 45 ClassMirror classMirror = instanceMirror.type; |
| 46 expect(libraryClassNames.contains(classMirror.simpleName), true); |
| 47 |
| 48 // Browse [object] and call a method declared in a private class. |
| 49 classMirror.declarations.values.forEach((DeclarationMirror declaration) { |
| 50 expect(declaration is MethodMirror, true); |
| 51 MethodMirror method = |
| 52 declaration; // Declaration needed, no type propagation. |
| 53 expect(method.reflectedReturnType, int); |
| 54 expect(method.parameters.length, 0); |
| 55 expect( |
| 56 <String>[ |
| 57 "publicMethod", |
| 58 "supposedlyPrivate", |
| 59 "supposedlyPrivateToo" |
| 60 ].contains(method.simpleName), |
| 61 true); |
| 62 if (method.simpleName != "publicMethod") { |
| 63 expect(instanceMirror.invoke(declaration.simpleName, []), |
| 64 -object.publicMethod()); |
| 65 } |
| 66 }); |
| 67 } |
| 68 }); |
| 69 } |
| 70 |
| 71 main() { |
| 72 test("Privacy, libraries", () { |
| 73 // Check that we can browse libraries. |
| 74 Map<Uri, LibraryMirror> libraries = privacyReflectable.libraries; |
| 75 Uri libraryUri = libraries.keys.firstWhere( |
| 76 (Uri uri) => uri.toString().contains("private_class_library")); |
| 77 LibraryMirror library = libraries[libraryUri]; |
| 78 expect(library.declarations.keys, libraryClassNames); |
| 79 }); |
| 80 |
| 81 testPrivacyViolation(func1(), "private subclass", doReflect: false); |
| 82 testPrivacyViolation(func2(), "private subtype", doReflect: false); |
| 83 testPrivacyViolation(func3(), "public subclass of private subclass"); |
| 84 testPrivacyViolation(func4(), "public subclass of private subtype"); |
| 85 } |
OLD | NEW |