Chromium Code Reviews

Side by Side Diff: test_reflectable/test/private_class_test.dart

Issue 1391013008: Adds limited support for private classes. (Closed) Base URL: https://github.com/dart-lang/reflectable.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
(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 void testPrivacyViolation(PublicClass object, String description) {
29 test("Privacy violation, $description", () {
30 Map<Uri, LibraryMirror> libraries = privacyReflectable.libraries;
31 Uri libraryUri = libraries.keys.firstWhere(
32 (Uri uri) => uri.toString().contains("private_class_library"));
33 LibraryMirror library = libraries[libraryUri];
34 expect(
35 library.declarations.keys,
36 <String>[
37 "PublicClass",
38 "_PrivateClass1",
39 "_PrivateClass2",
40 "PublicSubclass1",
41 "PublicSubclass2"
42 ].toSet());
43
44 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
45 ClassMirror classMirror = instanceMirror.type;
46 classMirror.declarations.values.forEach((DeclarationMirror declaration) {
47 expect(declaration is MethodMirror, true);
48 MethodMirror method =
49 declaration; // Declaration needed, no type propagation.
50 expect(method.reflectedReturnType, int);
51 expect(method.parameters.length, 0);
52 expect(
53 <String>["publicMethod", "supposedlyPrivate", "supposedlyPrivateToo"]
54 .contains(method.simpleName),
55 true);
56 if (method.simpleName != "publicMethod") {
57 expect(instanceMirror.invoke(declaration.simpleName, []),
58 -object.publicMethod());
59 }
60 });
61 });
62 }
63
64 main() {
65 testPrivacyViolation(func1(), "private subclass");
66 testPrivacyViolation(func2(), "private subtype");
67 testPrivacyViolation(func3(), "public subclass of private subclass");
68 testPrivacyViolation(func4(), "public subclass of private subtype");
69 }
OLDNEW

Powered by Google App Engine