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

Side by Side Diff: tests/compiler/dart2js/mirrors_lookup_test.dart

Issue 18323004: Add lookupInScope to DeclarationMirror. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add test + fix implementation. Created 7 years, 5 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
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 dart2js.test.memory_source_file_helper;
6
7 import 'package:expect/expect.dart';
8 import 'memory_compiler.dart';
9 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart' ;
10 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util. dart';
11
12 const Map MEMORY_SOURCE_FILES = const {
13 'main.dart': r"""
14
15 library main;
16
17 import 'dart:async' as async show Future;
18
19 var variable;
20
21 method(a, [b]) {}
22
23 class Class<A> {
24 var field;
25 var variable;
26 method(c, {d}) {}
27 }
28
29 class Subclass<B> extends Class<B> {
30 var subfield;
31 }
32 """,
33 };
34
35 void main() {
36 mirrorSystemFor(MEMORY_SOURCE_FILES).then(
37 (MirrorSystem mirrors) => test(mirrors),
38 onError: (e) => Expect.fail('$e'));
39 }
40
41 void test(MirrorSystem mirrors) {
42 LibraryMirror dartCore = mirrors.libraries[Uri.parse('dart:core')];
43 Expect.isNotNull(dartCore);
44
45 LibraryMirror dartAsync = mirrors.libraries[Uri.parse('dart:async')];
46 Expect.isNotNull(dartAsync);
47
48 LibraryMirror library = mirrors.libraries[Uri.parse('memory:main.dart')];
49 Expect.isNotNull(library);
50
51 // Check top-level scope.
52
53 DeclarationMirror String_ = library.lookupInScope('String');
54 Expect.isTrue(String_ is ClassMirror);
55 Expect.equals('String', String_.simpleName);
56 Expect.equals(dartCore, String_.owner);
57
58 Expect.isNull(library.lookupInScope('async'));
59 Expect.isNull(library.lookupInScope('Future'));
60 DeclarationMirror Future_ = library.lookupInScope('async.Future');
61 Expect.isTrue(Future_ is ClassMirror);
62 Expect.equals('Future', Future_.simpleName);
63 Expect.equals(dartAsync, Future_.owner);
64 // Timer is not in scope.
65 Expect.isNull(library.lookupInScope('Timer'));
66 // async.Timer is hidden.
67 Expect.isNull(library.lookupInScope('async.Timer'));
68
69 DeclarationMirror variable = library.lookupInScope('variable');
70 Expect.isTrue(variable is VariableMirror);
71 Expect.equals('variable', variable.simpleName);
72 Expect.equals('main.variable', variable.qualifiedName);
73 Expect.equals(library, variable.owner);
74 // Parameter `a` is not in scope.
75 Expect.isNull(library.lookupInScope('a'));
76 // Parameter `b` is not in scope.
77 Expect.isNull(library.lookupInScope('b'));
78
79 DeclarationMirror method = library.lookupInScope('method');
80 Expect.isTrue(method is MethodMirror);
81 Expect.equals('method', method.simpleName);
82 Expect.equals('main.method', method.qualifiedName);
83 Expect.equals(library, method.owner);
84
85 DeclarationMirror Class = library.lookupInScope('Class');
86 Expect.isTrue(Class is ClassMirror);
87 Expect.equals('Class', Class.simpleName);
88 Expect.equals('main.Class', Class.qualifiedName);
89 Expect.equals(library, Class.owner);
90 // Type variable `A` is not in scope.
91 Expect.isNull(library.lookupInScope('A'));
92
93 DeclarationMirror Subclass = library.lookupInScope('Subclass');
94 Expect.isTrue(Subclass is ClassMirror);
95 Expect.equals('Subclass', Subclass.simpleName);
96 Expect.equals('main.Subclass', Subclass.qualifiedName);
97 Expect.equals(library, Subclass.owner);
98 // Type variable `B` is not in scope.
99 Expect.isNull(library.lookupInScope('B'));
100
101 // Check top-level declaration scope.
102 checkTopScope(DeclarationMirror declaration) {
103 Expect.equals(String_, declaration.lookupInScope('String'));
104 Expect.equals(Future_, declaration.lookupInScope('async.Future'));
105 Expect.isNull(method.lookupInScope('Timer'));
106 Expect.isNull(declaration.lookupInScope('async.Timer'));
107 Expect.equals(variable, declaration.lookupInScope('variable'));
108 Expect.equals(method, declaration.lookupInScope('method'));
109 Expect.equals(Class, declaration.lookupInScope('Class'));
110 // Type variable `A` is not in scope.
111 Expect.isNull(declaration.lookupInScope('A'));
112 // Field `field` is not in scope.
113 Expect.isNull(declaration.lookupInScope('field'));
114 Expect.equals(Subclass, declaration.lookupInScope('Subclass'));
115 // Type variable `B` is not in scope.
116 Expect.isNull(declaration.lookupInScope('B'));
117 // Field `subfield` is not in scope.
118 Expect.isNull(declaration.lookupInScope('subfield'));
119 }
120
121 checkTopScope(variable);
122 // Parameter `a` is not in scope of `variable`.
123 Expect.isNull(variable.lookupInScope('a'));
124 // Parameter `b` is not in scope of `variable`.
125 Expect.isNull(variable.lookupInScope('b'));
126
127 checkTopScope(method);
128 // Parameter `a` is in scope of `method`.
129 Expect.isTrue(method.lookupInScope('a') is ParameterMirror);
130 // Parameter `b` is in scope of `method`.
131 Expect.isTrue(method.lookupInScope('b') is ParameterMirror);
132
133 // Check class scope.
134 DeclarationMirror Class_field = Class.lookupInScope('field');
135 Expect.isTrue(Class_field is VariableMirror);
136 Expect.notEquals(variable, Class_field);
137 Expect.equals(Class, Class_field.owner);
138
139 DeclarationMirror Class_variable = Class.lookupInScope('variable');
140 Expect.isTrue(Class_variable is VariableMirror);
141 Expect.notEquals(variable, Class_variable);
142 Expect.equals(Class, Class_variable.owner);
143
144 DeclarationMirror Class_method = Class.lookupInScope('method');
145 Expect.isTrue(Class_method is MethodMirror);
146 Expect.notEquals(method, Class_method);
147 Expect.equals(Class, Class_method.owner);
148
149 checkClassScope(DeclarationMirror declaration, {bool parametersInScope}) {
150 Expect.equals(String_, declaration.lookupInScope('String'));
151 Expect.equals(Future_, declaration.lookupInScope('async.Future'));
152 Expect.isNull(declaration.lookupInScope('Timer'));
153 Expect.isNull(declaration.lookupInScope('async.Timer'));
154
155 Expect.equals(Class_field, declaration.lookupInScope('field'));
156 Expect.equals(Class_variable, declaration.lookupInScope('variable'));
157 Expect.equals(Class_method, declaration.lookupInScope('method'));
158
159 // Parameter `a` is not in scope.
160 Expect.isNull(declaration.lookupInScope('a'));
161 // Parameter `b` is not in scope.
162 Expect.isNull(declaration.lookupInScope('b'));
163
164 if (parametersInScope) {
165 // Parameter `c` is in scope.
166 Expect.isTrue(declaration.lookupInScope('c') is ParameterMirror);
167 // Parameter `d` is in scope.
168 Expect.isTrue(declaration.lookupInScope('d') is ParameterMirror);
169 } else {
170 // Parameter `c` is not in scope.
171 Expect.isNull(declaration.lookupInScope('c'));
172 // Parameter `d` is not in scope.
173 Expect.isNull(declaration.lookupInScope('d'));
174 }
175
176 Expect.equals(Class, declaration.lookupInScope('Class'));
177 // Type variable `A` is in scope.
178 Expect.isTrue(declaration.lookupInScope('A') is TypeVariableMirror);
179 Expect.equals(Subclass, declaration.lookupInScope('Subclass'));
180 // Type variable `B` is not in scope.
181 Expect.isNull(declaration.lookupInScope('B'));
182 // Field `subfield` is not in scope.
183 Expect.isNull(declaration.lookupInScope('subfield'));
184 }
185 checkClassScope(Class, parametersInScope: false);
186 checkClassScope(Class_field, parametersInScope: false);
187 checkClassScope(Class_variable, parametersInScope: false);
188 checkClassScope(Class_method, parametersInScope: true);
189
190 // Check class scope.
191 DeclarationMirror Subclass_subfield = Subclass.lookupInScope('subfield');
192 Expect.isTrue(Subclass_subfield is VariableMirror);
193 Expect.notEquals(variable, Subclass_subfield);
194 Expect.equals(Subclass, Subclass_subfield.owner);
195
196 checkSubclassScope(DeclarationMirror declaration) {
197 Expect.equals(String_, declaration.lookupInScope('String'));
198 Expect.equals(Future_, declaration.lookupInScope('async.Future'));
199 Expect.isNull(declaration.lookupInScope('Timer'));
200 Expect.isNull(declaration.lookupInScope('async.Timer'));
201
202 // Top level `variable` is in scope.
203 Expect.equals(variable, declaration.lookupInScope('variable'));
204 // Top level `method` is in scope.
205 Expect.equals(method, declaration.lookupInScope('method'));
206
207 // Parameter `a` is not in scope
208 Expect.isNull(declaration.lookupInScope('a'));
209 // Parameter `b` is not in scope
210 Expect.isNull(declaration.lookupInScope('b'));
211
212 // Parameter `c` is not in scope.
213 Expect.isNull(declaration.lookupInScope('c'));
214 // Parameter `d` is not in scope.
215 Expect.isNull(declaration.lookupInScope('d'));
216
217 Expect.equals(Class, declaration.lookupInScope('Class'));
218 // Type variable `A` is not in scope
219 Expect.isNull(declaration.lookupInScope('A'));
220 // Field `field` is in scope
221 Expect.equals(Class_field, declaration.lookupInScope('field'));
222 Expect.equals(Subclass, declaration.lookupInScope('Subclass'));
223 // Type variable `B` is in scope
224 Expect.isTrue(declaration.lookupInScope('B') is TypeVariableMirror);
225 // Field `subfield` is in scope
226 Expect.equals(Subclass_subfield, declaration.lookupInScope('subfield'));
227 }
228 checkSubclassScope(Subclass);
229 checkSubclassScope(Subclass_subfield);
230
231 // `Timer` is in scope of `Future`.
232 Expect.isTrue(Future_.lookupInScope('Timer') is ClassMirror);
233
234 // Check qualified lookup.
235 Expect.equals(variable, lookupQualifiedInScope(library, 'variable'));
236 Expect.equals(method, lookupQualifiedInScope(library, 'method'));
237 Expect.isTrue(lookupQualifiedInScope(library, 'method.a') is ParameterMirror);
238
239 Expect.equals(Class, lookupQualifiedInScope(library, 'Class'));
240 Expect.isTrue(
241 lookupQualifiedInScope(library, 'Class.A') is TypeVariableMirror);
242
243 Expect.isNull(library.lookupInScope('Class.field'));
244 Expect.equals(Class_field, lookupQualifiedInScope(library, 'Class.field'));
245
246 Expect.equals(Class_method, lookupQualifiedInScope(library, 'Class.method'));
247 Expect.isTrue(
248 lookupQualifiedInScope(library, 'Class.method.c') is ParameterMirror);
249
250 // `field` should not be found through the prefix `Subclass`.
251 Expect.isNull(lookupQualifiedInScope(library, 'Subclass.field'));
252 Expect.equals(Subclass_subfield,
253 lookupQualifiedInScope(library, 'Subclass.subfield'));
254
255 Expect.equals(Future_, lookupQualifiedInScope(library, 'async.Future'));
256 Expect.isTrue(
257 lookupQualifiedInScope(library, 'async.Future.then') is MethodMirror);
258 // `Timer` should not be found through the prefix `async.Future`.
259 Expect.isNull(
260 lookupQualifiedInScope(library, 'async.Future.Timer'));
261 }
262
Andrei Mouravski 2013/07/03 17:49:49 Too many newlines at end of file.
263
OLDNEW
« tests/compiler/dart2js/memory_compiler.dart ('K') | « tests/compiler/dart2js/memory_compiler.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698