OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library code_transformers.test.resolver_test; | 5 library code_transformers.test.resolver_test; |
6 | 6 |
| 7 import 'dart:async'; |
7 import 'dart:io' show File, Platform; | 8 import 'dart:io' show File, Platform; |
8 | 9 |
9 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
10 import 'package:code_transformers/resolver.dart'; | 11 import 'package:code_transformers/resolver.dart'; |
11 import 'package:code_transformers/tests.dart'; | 12 import 'package:code_transformers/tests.dart'; |
12 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
13 import 'package:unittest/compact_vm_config.dart'; | 14 import 'package:unittest/compact_vm_config.dart'; |
14 import 'package:unittest/unittest.dart'; | 15 import 'package:unittest/unittest.dart'; |
15 | 16 |
16 main() { | 17 main() { |
17 useCompactVMConfiguration(); | 18 useCompactVMConfiguration(); |
18 | 19 |
19 var sdkDir = dartSdkDirectory; | 20 var sdkDir = dartSdkDirectory; |
20 if (sdkDir == null) { | 21 if (sdkDir == null) { |
21 // If we cannot find the SDK dir, then assume this is being run from Dart's | 22 // If we cannot find the SDK dir, then assume this is being run from Dart's |
22 // source directory and this script is the main script. | 23 // source directory and this script is the main script. |
23 sdkDir = path.join( | 24 sdkDir = path.join( |
24 path.dirname(path.fromUri(Platform.script)), '..', '..', '..', 'sdk'); | 25 path.dirname(path.fromUri(Platform.script)), '..', '..', '..', 'sdk'); |
25 } | 26 } |
26 | 27 |
27 var entryPoint = new AssetId('a', 'web/main.dart'); | 28 var entryPoint = new AssetId('a', 'web/main.dart'); |
28 var transformer = new ResolverTransformer(sdkDir, | 29 var resolvers = new Resolvers(sdkDir); |
29 (asset) => asset.id == entryPoint); | |
30 | 30 |
31 var phases = [[transformer]]; | 31 Future validateResolver({Map<String, String> inputs, void validator(Resolver), |
| 32 List<String> messages: const[]}) { |
| 33 return applyTransformers( |
| 34 [[new TestTransformer(resolvers, entryPoint, validator)]], |
| 35 inputs: inputs, |
| 36 messages: messages); |
| 37 } |
32 | 38 |
33 group('Resolver', () { | 39 group('Resolver', () { |
34 | 40 |
35 test('should handle empty files', () { | 41 test('should handle empty files', () { |
36 return applyTransformers(phases, | 42 return validateResolver( |
37 inputs: { | 43 inputs: { |
38 'a|web/main.dart': '', | 44 'a|web/main.dart': '', |
39 }).then((_) { | 45 }, |
40 var resolver = transformer.getResolver(entryPoint); | 46 validator: (resolver) { |
41 var source = resolver.sources[entryPoint]; | 47 var source = resolver.sources[entryPoint]; |
42 expect(source.modificationStamp, 1); | 48 expect(source.modificationStamp, 1); |
43 | 49 |
44 var lib = resolver.entryLibrary; | 50 var lib = resolver.entryLibrary; |
45 expect(lib, isNotNull); | 51 expect(lib, isNotNull); |
46 expect(lib.entryPoint, isNull); | 52 expect(lib.entryPoint, isNull); |
47 }); | 53 }); |
48 }); | 54 }); |
49 | 55 |
50 test('should update when sources change', () { | 56 test('should update when sources change', () { |
51 return applyTransformers(phases, | 57 return validateResolver( |
52 inputs: { | 58 inputs: { |
53 'a|web/main.dart': ''' main() {} ''', | 59 'a|web/main.dart': ''' main() {} ''', |
54 }).then((_) { | 60 }, |
55 var resolver = transformer.getResolver(entryPoint); | 61 validator: (resolver) { |
56 var source = resolver.sources[entryPoint]; | 62 var source = resolver.sources[entryPoint]; |
57 expect(source.modificationStamp, 2); | 63 expect(source.modificationStamp, 2); |
58 | 64 |
59 var lib = resolver.entryLibrary; | 65 var lib = resolver.entryLibrary; |
60 expect(lib, isNotNull); | 66 expect(lib, isNotNull); |
61 expect(lib.entryPoint, isNotNull); | 67 expect(lib.entryPoint, isNotNull); |
62 }); | 68 }); |
63 }); | 69 }); |
64 | 70 |
65 test('should follow imports', () { | 71 test('should follow imports', () { |
66 return applyTransformers(phases, | 72 return validateResolver( |
67 inputs: { | 73 inputs: { |
68 'a|web/main.dart': ''' | 74 'a|web/main.dart': ''' |
69 import 'a.dart'; | 75 import 'a.dart'; |
70 | 76 |
71 main() { | 77 main() { |
72 } ''', | 78 } ''', |
73 'a|web/a.dart': ''' | 79 'a|web/a.dart': ''' |
74 library a; | 80 library a; |
75 ''', | 81 ''', |
76 }).then((_) { | 82 }, |
77 var resolver = transformer.getResolver(entryPoint); | 83 validator: (resolver) { |
78 var lib = resolver.entryLibrary; | 84 var lib = resolver.entryLibrary; |
79 expect(lib.importedLibraries.length, 2); | 85 expect(lib.importedLibraries.length, 2); |
80 var libA = lib.importedLibraries.where((l) => l.name == 'a').single; | 86 var libA = lib.importedLibraries.where((l) => l.name == 'a').single; |
81 expect(libA.getType('Foo'), isNull); | 87 expect(libA.getType('Foo'), isNull); |
82 }); | 88 }); |
83 }); | 89 }); |
84 | 90 |
85 test('should update changed imports', () { | 91 test('should update changed imports', () { |
86 return applyTransformers(phases, | 92 return validateResolver( |
87 inputs: { | 93 inputs: { |
88 'a|web/main.dart': ''' | 94 'a|web/main.dart': ''' |
89 import 'a.dart'; | 95 import 'a.dart'; |
90 | 96 |
91 main() { | 97 main() { |
92 } ''', | 98 } ''', |
93 'a|web/a.dart': ''' | 99 'a|web/a.dart': ''' |
94 library a; | 100 library a; |
95 class Foo {} | 101 class Foo {} |
96 ''', | 102 ''', |
97 }).then((_) { | 103 }, |
98 var lib = transformer.getResolver(entryPoint).entryLibrary; | 104 validator: (resolver) { |
| 105 var lib = resolver.entryLibrary; |
99 expect(lib.importedLibraries.length, 2); | 106 expect(lib.importedLibraries.length, 2); |
100 var libA = lib.importedLibraries.where((l) => l.name == 'a').single; | 107 var libA = lib.importedLibraries.where((l) => l.name == 'a').single; |
101 expect(libA.getType('Foo'), isNotNull); | 108 expect(libA.getType('Foo'), isNotNull); |
102 }); | 109 }); |
103 }); | 110 }); |
104 | 111 |
105 test('should follow package imports', () { | 112 test('should follow package imports', () { |
106 return applyTransformers(phases, | 113 return validateResolver( |
107 inputs: { | 114 inputs: { |
108 'a|web/main.dart': ''' | 115 'a|web/main.dart': ''' |
109 import 'package:b/b.dart'; | 116 import 'package:b/b.dart'; |
110 | 117 |
111 main() { | 118 main() { |
112 } ''', | 119 } ''', |
113 'b|lib/b.dart': ''' | 120 'b|lib/b.dart': ''' |
114 library b; | 121 library b; |
115 ''', | 122 ''', |
116 }).then((_) { | 123 }, |
117 var lib = transformer.getResolver(entryPoint).entryLibrary; | 124 validator: (resolver) { |
| 125 var lib = resolver.entryLibrary; |
118 expect(lib.importedLibraries.length, 2); | 126 expect(lib.importedLibraries.length, 2); |
119 var libB = lib.importedLibraries.where((l) => l.name == 'b').single; | 127 var libB = lib.importedLibraries.where((l) => l.name == 'b').single; |
120 expect(libB.getType('Foo'), isNull); | 128 expect(libB.getType('Foo'), isNull); |
121 }); | 129 }); |
122 }); | 130 }); |
123 | 131 |
124 test('should update on changed package imports', () { | 132 test('should update on changed package imports', () { |
125 return applyTransformers(phases, | 133 return validateResolver( |
126 inputs: { | 134 inputs: { |
127 'a|web/main.dart': ''' | 135 'a|web/main.dart': ''' |
128 import 'package:b/b.dart'; | 136 import 'package:b/b.dart'; |
129 | 137 |
130 main() { | 138 main() { |
131 } ''', | 139 } ''', |
132 'b|lib/b.dart': ''' | 140 'b|lib/b.dart': ''' |
133 library b; | 141 library b; |
134 class Bar {} | 142 class Bar {} |
135 ''', | 143 ''', |
136 }).then((_) { | 144 }, |
137 var lib = transformer.getResolver(entryPoint).entryLibrary; | 145 validator: (resolver) { |
| 146 var lib = resolver.entryLibrary; |
138 expect(lib.importedLibraries.length, 2); | 147 expect(lib.importedLibraries.length, 2); |
139 var libB = lib.importedLibraries.where((l) => l.name == 'b').single; | 148 var libB = lib.importedLibraries.where((l) => l.name == 'b').single; |
140 expect(libB.getType('Bar'), isNotNull); | 149 expect(libB.getType('Bar'), isNotNull); |
141 }); | 150 }); |
142 }); | 151 }); |
143 | 152 |
144 test('should handle deleted files', () { | 153 test('should handle deleted files', () { |
145 return applyTransformers(phases, | 154 return validateResolver( |
146 inputs: { | 155 inputs: { |
147 'a|web/main.dart': ''' | 156 'a|web/main.dart': ''' |
148 import 'package:b/b.dart'; | 157 import 'package:b/b.dart'; |
149 | 158 |
150 main() { | 159 main() { |
151 } ''', | 160 } ''', |
152 }, | 161 }, |
153 messages: [ | 162 messages: [ |
154 'error: Unable to find asset for "package:b/b.dart"', | 163 'error: Unable to find asset for "package:b/b.dart"', |
155 'error: Unable to find asset for "package:b/b.dart"', | 164 'error: Unable to find asset for "package:b/b.dart"', |
156 ]).then((_) { | 165 ], |
157 var lib = transformer.getResolver(entryPoint).entryLibrary; | 166 validator: (resolver) { |
| 167 var lib = resolver.entryLibrary; |
158 expect(lib.importedLibraries.length, 1); | 168 expect(lib.importedLibraries.length, 1); |
159 }); | 169 }); |
160 }); | 170 }); |
161 | 171 |
162 test('should fail on absolute URIs', () { | 172 test('should fail on absolute URIs', () { |
163 return applyTransformers(phases, | 173 return validateResolver( |
164 inputs: { | 174 inputs: { |
165 'a|web/main.dart': ''' | 175 'a|web/main.dart': ''' |
166 import '/b.dart'; | 176 import '/b.dart'; |
167 | 177 |
168 main() { | 178 main() { |
169 } ''', | 179 } ''', |
170 }, | 180 }, |
171 messages: [ | 181 messages: [ |
172 // First from the AST walker | 182 // First from the AST walker |
173 'error: absolute paths not allowed: "/b.dart" (main.dart 0 14)', | 183 'error: absolute paths not allowed: "/b.dart" (main.dart 0 14)', |
174 // Then two from the resolver. | 184 // Then two from the resolver. |
175 'error: absolute paths not allowed: "/b.dart"', | 185 'error: absolute paths not allowed: "/b.dart"', |
176 'error: absolute paths not allowed: "/b.dart"', | 186 'error: absolute paths not allowed: "/b.dart"', |
177 ]).then((_) { | 187 ], |
178 var lib = transformer.getResolver(entryPoint).entryLibrary; | 188 validator: (resolver) { |
| 189 var lib = resolver.entryLibrary; |
179 expect(lib.importedLibraries.length, 1); | 190 expect(lib.importedLibraries.length, 1); |
180 }); | 191 }); |
181 }); | 192 }); |
182 | 193 |
183 test('should list all libraries', () { | 194 test('should list all libraries', () { |
184 return applyTransformers(phases, | 195 return validateResolver( |
185 inputs: { | 196 inputs: { |
186 'a|web/main.dart': ''' | 197 'a|web/main.dart': ''' |
187 library a.main; | 198 library a.main; |
188 import 'package:a/a.dart'; | 199 import 'package:a/a.dart'; |
189 import 'package:a/b.dart'; | 200 import 'package:a/b.dart'; |
190 ''', | 201 ''', |
191 'a|lib/a.dart': 'library a.a;\n import "package:a/c.dart";', | 202 'a|lib/a.dart': 'library a.a;\n import "package:a/c.dart";', |
192 'a|lib/b.dart': 'library a.b;\n import "c.dart";', | 203 'a|lib/b.dart': 'library a.b;\n import "c.dart";', |
193 'a|lib/c.dart': 'library a.c;' | 204 'a|lib/c.dart': 'library a.c;' |
194 }).then((_) { | 205 }, |
195 var resolver = transformer.getResolver(entryPoint); | 206 validator: (resolver) { |
196 var libs = resolver.libraries.where((l) => !l.isInSdk); | 207 var libs = resolver.libraries.where((l) => !l.isInSdk); |
197 expect(libs.map((l) => l.name), unorderedEquals([ | 208 expect(libs.map((l) => l.name), unorderedEquals([ |
198 'a.main', | 209 'a.main', |
199 'a.a', | 210 'a.a', |
200 'a.b', | 211 'a.b', |
201 'a.c', | 212 'a.c', |
202 ])); | 213 ])); |
203 }); | 214 }); |
204 }); | 215 }); |
205 | 216 |
206 test('should resolve types and library uris', () { | 217 test('should resolve types and library uris', () { |
207 return applyTransformers(phases, | 218 return validateResolver( |
208 inputs: { | 219 inputs: { |
209 'a|web/main.dart': ''' | 220 'a|web/main.dart': ''' |
210 import 'dart:core'; | 221 import 'dart:core'; |
211 import 'package:a/a.dart'; | 222 import 'package:a/a.dart'; |
212 import 'package:a/b.dart'; | 223 import 'package:a/b.dart'; |
213 import 'sub_dir/d.dart'; | 224 import 'sub_dir/d.dart'; |
214 class Foo {} | 225 class Foo {} |
215 ''', | 226 ''', |
216 'a|lib/a.dart': 'library a.a;\n import "package:a/c.dart";', | 227 'a|lib/a.dart': 'library a.a;\n import "package:a/c.dart";', |
217 'a|lib/b.dart': 'library a.b;\n import "c.dart";', | 228 'a|lib/b.dart': 'library a.b;\n import "c.dart";', |
218 'a|lib/c.dart': ''' | 229 'a|lib/c.dart': ''' |
219 library a.c; | 230 library a.c; |
220 class Bar {} | 231 class Bar {} |
221 ''', | 232 ''', |
222 'a|web/sub_dir/d.dart': ''' | 233 'a|web/sub_dir/d.dart': ''' |
223 library a.web.sub_dir.d; | 234 library a.web.sub_dir.d; |
224 class Baz{} | 235 class Baz{} |
225 ''', | 236 ''', |
226 }).then((_) { | 237 }, |
227 var resolver = transformer.getResolver(entryPoint); | 238 validator: (resolver) { |
228 | |
229 var a = resolver.getLibraryByName('a.a'); | 239 var a = resolver.getLibraryByName('a.a'); |
230 expect(a, isNotNull); | 240 expect(a, isNotNull); |
231 expect(resolver.getImportUri(a).toString(), | 241 expect(resolver.getImportUri(a).toString(), |
232 'package:a/a.dart'); | 242 'package:a/a.dart'); |
233 expect(resolver.getLibraryByUri(Uri.parse('package:a/a.dart')), a); | 243 expect(resolver.getLibraryByUri(Uri.parse('package:a/a.dart')), a); |
234 | 244 |
235 var main = resolver.getLibraryByName(''); | 245 var main = resolver.getLibraryByName(''); |
236 expect(main, isNotNull); | 246 expect(main, isNotNull); |
237 expect(resolver.getImportUri(main), isNull); | 247 expect(resolver.getImportUri(main), isNull); |
238 | 248 |
(...skipping 18 matching lines...) Expand all Loading... |
257 var hashMap = resolver.getType('dart.collection.HashMap'); | 267 var hashMap = resolver.getType('dart.collection.HashMap'); |
258 expect(resolver.getImportUri(hashMap.library).toString(), | 268 expect(resolver.getImportUri(hashMap.library).toString(), |
259 'dart:collection'); | 269 'dart:collection'); |
260 expect(resolver.getLibraryByUri(Uri.parse('dart:collection')), | 270 expect(resolver.getLibraryByUri(Uri.parse('dart:collection')), |
261 hashMap.library); | 271 hashMap.library); |
262 | 272 |
263 }); | 273 }); |
264 }); | 274 }); |
265 | 275 |
266 test('deleted files should be removed', () { | 276 test('deleted files should be removed', () { |
267 return applyTransformers(phases, | 277 return validateResolver( |
268 inputs: { | 278 inputs: { |
269 'a|web/main.dart': '''import 'package:a/a.dart';''', | 279 'a|web/main.dart': '''import 'package:a/a.dart';''', |
270 'a|lib/a.dart': '''import 'package:a/b.dart';''', | 280 'a|lib/a.dart': '''import 'package:a/b.dart';''', |
271 'a|lib/b.dart': '''class Engine{}''', | 281 'a|lib/b.dart': '''class Engine{}''', |
272 }).then((_) { | 282 }, |
273 var resolver = transformer.getResolver(entryPoint); | 283 validator: (resolver) { |
274 var engine = resolver.getType('Engine'); | 284 var engine = resolver.getType('Engine'); |
275 var uri = resolver.getImportUri(engine.library); | 285 var uri = resolver.getImportUri(engine.library); |
276 expect(uri.toString(), 'package:a/b.dart'); | 286 expect(uri.toString(), 'package:a/b.dart'); |
277 }).then((_) { | 287 }).then((_) { |
278 return applyTransformers(phases, | 288 return validateResolver( |
279 inputs: { | 289 inputs: { |
280 'a|web/main.dart': '''import 'package:a/a.dart';''', | 290 'a|web/main.dart': '''import 'package:a/a.dart';''', |
281 'a|lib/a.dart': '''lib a;\n class Engine{}''' | 291 'a|lib/a.dart': '''lib a;\n class Engine{}''' |
| 292 }, |
| 293 validator: (resolver) { |
| 294 var engine = resolver.getType('Engine'); |
| 295 var uri = resolver.getImportUri(engine.library); |
| 296 expect(uri.toString(), 'package:a/a.dart'); |
| 297 |
| 298 // Make sure that we haven't leaked any sources. |
| 299 expect(resolver.sources.length, 2); |
282 }); | 300 }); |
283 }).then((_) { | |
284 var resolver = transformer.getResolver(entryPoint); | |
285 var engine = resolver.getType('Engine'); | |
286 var uri = resolver.getImportUri(engine.library); | |
287 expect(uri.toString(), 'package:a/a.dart'); | |
288 | |
289 // Make sure that we haven't leaked any sources. | |
290 expect(resolver.sources.length, 2); | |
291 }); | 301 }); |
292 }); | 302 }); |
293 | 303 |
294 test('handles circular imports', () { | 304 test('handles circular imports', () { |
295 return applyTransformers(phases, | 305 return validateResolver( |
296 inputs: { | 306 inputs: { |
297 'a|web/main.dart': ''' | 307 'a|web/main.dart': ''' |
298 library main; | 308 library main; |
299 import 'package:a/a.dart'; ''', | 309 import 'package:a/a.dart'; ''', |
300 'a|lib/a.dart': ''' | 310 'a|lib/a.dart': ''' |
301 library a; | 311 library a; |
302 import 'package:a/b.dart'; ''', | 312 import 'package:a/b.dart'; ''', |
303 'a|lib/b.dart': ''' | 313 'a|lib/b.dart': ''' |
304 library b; | 314 library b; |
305 import 'package:a/a.dart'; ''', | 315 import 'package:a/a.dart'; ''', |
306 }).then((_) { | 316 }, |
307 var resolver = transformer.getResolver(entryPoint); | 317 validator: (resolver) { |
308 | |
309 var libs = resolver.libraries.map((lib) => lib.name); | 318 var libs = resolver.libraries.map((lib) => lib.name); |
310 expect(libs.contains('a'), isTrue); | 319 expect(libs.contains('a'), isTrue); |
311 expect(libs.contains('b'), isTrue); | 320 expect(libs.contains('b'), isTrue); |
312 }); | 321 }); |
313 }); | 322 }); |
| 323 |
| 324 test('handles parallel resolves', () { |
| 325 return Future.wait([ |
| 326 validateResolver( |
| 327 inputs: { |
| 328 'a|web/main.dart': ''' |
| 329 library foo;''' |
| 330 }, |
| 331 validator: (resolver) { |
| 332 expect(resolver.entryLibrary.name, 'foo'); |
| 333 }), |
| 334 validateResolver( |
| 335 inputs: { |
| 336 'a|web/main.dart': ''' |
| 337 library bar;''' |
| 338 }, |
| 339 validator: (resolver) { |
| 340 expect(resolver.entryLibrary.name, 'bar'); |
| 341 }), |
| 342 ]); |
| 343 }); |
314 }); | 344 }); |
315 } | 345 } |
| 346 |
| 347 class TestTransformer extends Transformer with ResolverTransformer { |
| 348 final AssetId primary; |
| 349 final Function validator; |
| 350 |
| 351 TestTransformer(Resolvers resolvers, this.primary, this.validator) { |
| 352 this.resolvers = resolvers; |
| 353 } |
| 354 |
| 355 Future<bool> isPrimary(Asset input) => |
| 356 new Future.value(input.id == primary); |
| 357 |
| 358 applyResolver(Transform transform, Resolver resolver) { |
| 359 return validator(resolver); |
| 360 } |
| 361 } |
OLD | NEW |