Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: dart/tests/lib/mirrors/reflect_model_test.dart

Issue 16780003: Implement various mirror member maps (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Forgot CSP Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « dart/tests/lib/mirrors/model_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
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.
4
5 library test.reflect_model_test;
6
7 import 'dart:mirrors';
8
9 import 'package:expect/expect.dart';
10
11 import 'model.dart';
12
13 isNoSuchMethodError(e) => e is NoSuchMethodError;
14
15 name(Declaration mirror) {
16 return (mirror == null) ? '<null>' : stringify(mirror.simpleName);
17 }
18
19 stringifyMap(Map map) {
20 var buffer = new StringBuffer('{');
21 bool first = true;
22 for (String key in map.keys.map(MirrorSystem.getName).toList()..sort()) {
23 if (!first) buffer.write(', ');
24 first = false;
25 buffer.write(key);
26 buffer.write(': ');
27 buffer.write(stringify(map[new Symbol(key)]));
28 }
29 return (buffer..write('}')).toString();
30 }
31
32 stringifySymbol(Symbol symbol) => 's(${MirrorSystem.getName(symbol)})';
33
34 writeDeclarationOn(DeclarationMirror mirror, StringBuffer buffer) {
35 buffer.write(stringify(mirror.simpleName));
36 if (mirror.owner != null) {
37 buffer.write(' in ');
38 buffer.write(name(mirror.owner));
39 }
40 if (mirror.isPrivate) buffer.write(', private');
41 if (mirror.isTopLevel) buffer.write(', top-level');
42 }
43
44 stringifyVariable(VariableMirror variable) {
45 var buffer = new StringBuffer('Variable(');
46 writeDeclarationOn(variable, buffer);
47 if (variable.isStatic) buffer.write(', static');
48 if (variable.isFinal) buffer.write(', final');
49 return (buffer..write(')')).toString();
50 }
51
52 stringifyMethod(MethodMirror method) {
53 var buffer = new StringBuffer('Method(');
54 writeDeclarationOn(method, buffer);
55 if (method.isStatic) buffer.write(', static');
56 if (method.isGetter) buffer.write(', getter');
57 if (method.isSetter) buffer.write(', setter');
58 return (buffer..write(')')).toString();
59 }
60
61 stringify(value) {
62 if (value is Map) return stringifyMap(value);
63 if (value is VariableMirror) return stringifyVariable(value);
64 if (value is MethodMirror) return stringifyMethod(value);
65 if (value is Symbol) return stringifySymbol(value);
66 if (value == null) return '<null>';
67 throw 'Unexpected value: $value';
68 }
69
70 expect(expected, actual) => Expect.stringEquals(expected, stringify(actual));
71
72 main() {
73 var unnamed = new Symbol('');
74 var field = new Symbol('field');
75 var instanceMethod = new Symbol('instanceMethod');
76 var accessor = new Symbol('accessor');
77 var aMethod = new Symbol('aMethod');
78 var bMethod = new Symbol('bMethod');
79 var cMethod = new Symbol('cMethod');
80
81 var aClass = reflectClass(A);
82 var bClass = reflectClass(B);
83 var cClass = reflectClass(C);
84 var a = aClass.newInstance(unnamed, []);
85 var b = bClass.newInstance(unnamed, []);
86 var c = cClass.newInstance(unnamed, []);
87
88 expect('{field: Variable(s(field) in s(A))}', aClass.variables);
89 expect('{}', bClass.variables);
90 expect('{}', cClass.variables);
91
92 Expect.isNull(a.getField(field).reflectee);
93 Expect.equals('B:get field', b.getField(field).reflectee);
94 Expect.equals('B:get field', c.getField(field).reflectee);
95
96 Expect.equals(42, a.setField(field, 42).reflectee);
97 Expect.equals(87, b.setField(field, 87).reflectee);
98 Expect.equals(89, c.setField(field, 89).reflectee);
99
100 Expect.equals(42, a.getField(field).reflectee);
101 Expect.equals('B:get field', b.getField(field).reflectee);
102 Expect.equals('B:get field', c.getField(field).reflectee);
103 Expect.equals(89, fieldC);
104
105 expect('{accessor: Method(s(accessor) in s(A), getter)'
106 // TODO(ahe): Include instance field getters?
107 ', field: Method(s(field) in s(A), getter)'
108 '}',
109 aClass.getters);
110 expect('{accessor: Method(s(accessor) in s(B), getter)'
111 ', field: Method(s(field) in s(B), getter)}',
112 bClass.getters);
113 expect('{accessor: Method(s(accessor) in s(C), getter)}',
114 cClass.getters);
115
116 expect('{accessor=: Method(s(accessor=) in s(A), setter)'
117 // TODO(ahe): Include instance field setters?
118 ', field=: Method(s(field=) in s(A), setter)'
119 '}',
120 aClass.setters);
121 expect('{accessor=: Method(s(accessor=) in s(B), setter)}',
122 bClass.setters);
123 expect('{accessor=: Method(s(accessor=) in s(C), setter)'
124 ', field=: Method(s(field=) in s(C), setter)}',
125 cClass.setters);
126
127 Expect.equals('A:instanceMethod(7)', a.invoke(instanceMethod, [7]).reflectee);
128 Expect.equals('B:instanceMethod(9)', b.invoke(instanceMethod, [9]).reflectee);
129 Expect.equals(
130 'C:instanceMethod(13)', c.invoke(instanceMethod, [13]).reflectee);
131
132 expect(
133 '{aMethod: Method(s(aMethod) in s(A))'
134 ', instanceMethod: Method(s(instanceMethod) in s(A))}',
135 aClass.methods);
136
137 expect(
138 '{bMethod: Method(s(bMethod) in s(B))'
139 ', instanceMethod: Method(s(instanceMethod) in s(B))}',
140 bClass.methods);
141 expect(
142 '{cMethod: Method(s(cMethod) in s(C))'
143 ', instanceMethod: Method(s(instanceMethod) in s(C))}',
144 cClass.methods);
145
146 Expect.equals('A:get accessor', a.getField(accessor).reflectee);
147 Expect.equals('B:get accessor', b.getField(accessor).reflectee);
148 Expect.equals('C:get accessor', c.getField(accessor).reflectee);
149
150 Expect.equals('foo', a.setField(accessor, 'foo').reflectee);
151 Expect.equals('bar', b.setField(accessor, 'bar').reflectee);
152 Expect.equals('baz', c.setField(accessor, 'baz').reflectee);
153
154 Expect.equals('foo', accessorA);
155 Expect.equals('bar', accessorB);
156 Expect.equals('baz', accessorC);
157
158 Expect.equals('aMethod', a.invoke(aMethod, []).reflectee);
159 Expect.equals('aMethod', b.invoke(aMethod, []).reflectee);
160 Expect.equals('aMethod', c.invoke(aMethod, []).reflectee);
161
162 Expect.throws(() { a.invoke(bMethod, []); }, isNoSuchMethodError);
163 Expect.equals('bMethod', b.invoke(bMethod, []).reflectee);
164 Expect.equals('bMethod', c.invoke(bMethod, []).reflectee);
165
166 Expect.throws(() { a.invoke(cMethod, []); }, isNoSuchMethodError);
167 Expect.throws(() { b.invoke(cMethod, []); }, isNoSuchMethodError);
168 Expect.equals('cMethod', c.invoke(cMethod, []).reflectee);
169 }
OLDNEW
« no previous file with comments | « dart/tests/lib/mirrors/model_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698