Chromium Code Reviews| Index: test_reflectable/test/private_class_test.dart |
| diff --git a/test_reflectable/test/private_class_test.dart b/test_reflectable/test/private_class_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b3ffe3c00922aad97590d92bb09b9e9289a95fe1 |
| --- /dev/null |
| +++ b/test_reflectable/test/private_class_test.dart |
| @@ -0,0 +1,69 @@ |
| +// Copyright (c) 2015, the Dart Team. All rights reserved. Use of this |
| +// source code is governed by a BSD-style license that can be found in |
| +// the LICENSE file. |
| + |
| +/// File being transformed by the reflectable transformer. |
| +/// Uses public features of an instance of a private class in a different |
| +/// library. This illustrates that there is (very limited) support for access |
| +/// to private features. |
| +library test_reflectable.test.private_class_test; |
| + |
| +@GlobalQuantifyCapability("PublicClass", privacyReflectable) |
| +import 'package:reflectable/reflectable.dart'; |
| +import 'package:unittest/unittest.dart'; |
| +import 'private_class_library.dart'; |
| + |
| +class PrivacyReflectable extends Reflectable { |
| + const PrivacyReflectable() |
| + : super( |
| + subtypeQuantifyCapability, |
| + reflectedTypeCapability, |
| + instanceInvokeCapability, |
| + declarationsCapability, |
| + libraryCapability); |
| +} |
| + |
| +const privacyReflectable = const PrivacyReflectable(); |
| + |
| +void testPrivacyViolation(PublicClass object, String description) { |
| + test("Privacy violation, $description", () { |
| + Map<Uri, LibraryMirror> libraries = privacyReflectable.libraries; |
| + Uri libraryUri = libraries.keys.firstWhere( |
| + (Uri uri) => uri.toString().contains("private_class_library")); |
| + LibraryMirror library = libraries[libraryUri]; |
| + expect( |
| + library.declarations.keys, |
| + <String>[ |
| + "PublicClass", |
| + "_PrivateClass1", |
| + "_PrivateClass2", |
| + "PublicSubclass1", |
| + "PublicSubclass2" |
| + ].toSet()); |
| + |
| + InstanceMirror instanceMirror = privacyReflectable.reflect(object); |
|
sigurdm
2015/10/15 12:30:58
I think the right way to allow reflection on priva
sigurdm
2015/10/15 12:30:58
What about other actions on private class-mirrors?
eernst
2015/10/15 13:46:11
For now, I've changed the test to check `canReflec
eernst
2015/10/15 13:46:11
Now you won't get a mirror on them, so we can revi
|
| + ClassMirror classMirror = instanceMirror.type; |
| + classMirror.declarations.values.forEach((DeclarationMirror declaration) { |
| + expect(declaration is MethodMirror, true); |
| + MethodMirror method = |
| + declaration; // Declaration needed, no type propagation. |
| + expect(method.reflectedReturnType, int); |
| + expect(method.parameters.length, 0); |
| + expect( |
| + <String>["publicMethod", "supposedlyPrivate", "supposedlyPrivateToo"] |
| + .contains(method.simpleName), |
| + true); |
| + if (method.simpleName != "publicMethod") { |
| + expect(instanceMirror.invoke(declaration.simpleName, []), |
| + -object.publicMethod()); |
| + } |
| + }); |
| + }); |
| +} |
| + |
| +main() { |
| + testPrivacyViolation(func1(), "private subclass"); |
| + testPrivacyViolation(func2(), "private subtype"); |
| + testPrivacyViolation(func3(), "public subclass of private subclass"); |
| + testPrivacyViolation(func4(), "public subclass of private subtype"); |
| +} |