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