OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library code_transformers.test.resolver_test; |
| 6 |
| 7 import 'package:barback/barback.dart'; |
| 8 import 'package:code_transformers/resolver.dart'; |
| 9 import 'package:code_transformers/tests.dart'; |
| 10 import 'package:unittest/compact_vm_config.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 main() { |
| 14 useCompactVMConfiguration(); |
| 15 |
| 16 var entryPoint = new AssetId('a', 'web/main.dart'); |
| 17 var transformer = new ResolverTransformer(dartSdkDirectory, |
| 18 (asset) => asset.id == entryPoint); |
| 19 |
| 20 var phases = [[transformer]]; |
| 21 |
| 22 group('Resolver', () { |
| 23 |
| 24 test('should handle empty files', () { |
| 25 return applyTransformers(phases, |
| 26 inputs: { |
| 27 'a|web/main.dart': '', |
| 28 }).then((_) { |
| 29 var resolver = transformer.getResolver(entryPoint); |
| 30 var source = resolver.sources[entryPoint]; |
| 31 expect(source.modificationStamp, 1); |
| 32 |
| 33 var lib = resolver.entryLibrary; |
| 34 expect(lib, isNotNull); |
| 35 expect(lib.entryPoint, isNull); |
| 36 }); |
| 37 }); |
| 38 |
| 39 test('should update when sources change', () { |
| 40 return applyTransformers(phases, |
| 41 inputs: { |
| 42 'a|web/main.dart': ''' main() {} ''', |
| 43 }).then((_) { |
| 44 var resolver = transformer.getResolver(entryPoint); |
| 45 var source = resolver.sources[entryPoint]; |
| 46 expect(source.modificationStamp, 2); |
| 47 |
| 48 var lib = resolver.entryLibrary; |
| 49 expect(lib, isNotNull); |
| 50 expect(lib.entryPoint, isNotNull); |
| 51 }); |
| 52 }); |
| 53 |
| 54 test('should follow imports', () { |
| 55 return applyTransformers(phases, |
| 56 inputs: { |
| 57 'a|web/main.dart': ''' |
| 58 import 'a.dart'; |
| 59 |
| 60 main() { |
| 61 } ''', |
| 62 'a|web/a.dart': ''' |
| 63 library a; |
| 64 ''', |
| 65 }).then((_) { |
| 66 var resolver = transformer.getResolver(entryPoint); |
| 67 var lib = resolver.entryLibrary; |
| 68 expect(lib.importedLibraries.length, 2); |
| 69 var libA = lib.importedLibraries.where((l) => l.name == 'a').single; |
| 70 expect(libA.getType('Foo'), isNull); |
| 71 }); |
| 72 }); |
| 73 |
| 74 test('should update changed imports', () { |
| 75 return applyTransformers(phases, |
| 76 inputs: { |
| 77 'a|web/main.dart': ''' |
| 78 import 'a.dart'; |
| 79 |
| 80 main() { |
| 81 } ''', |
| 82 'a|web/a.dart': ''' |
| 83 library a; |
| 84 class Foo {} |
| 85 ''', |
| 86 }).then((_) { |
| 87 var lib = transformer.getResolver(entryPoint).entryLibrary; |
| 88 expect(lib.importedLibraries.length, 2); |
| 89 var libA = lib.importedLibraries.where((l) => l.name == 'a').single; |
| 90 expect(libA.getType('Foo'), isNotNull); |
| 91 }); |
| 92 }); |
| 93 |
| 94 test('should follow package imports', () { |
| 95 return applyTransformers(phases, |
| 96 inputs: { |
| 97 'a|web/main.dart': ''' |
| 98 import 'package:b/b.dart'; |
| 99 |
| 100 main() { |
| 101 } ''', |
| 102 'b|lib/b.dart': ''' |
| 103 library b; |
| 104 ''', |
| 105 }).then((_) { |
| 106 var lib = transformer.getResolver(entryPoint).entryLibrary; |
| 107 expect(lib.importedLibraries.length, 2); |
| 108 var libB = lib.importedLibraries.where((l) => l.name == 'b').single; |
| 109 expect(libB.getType('Foo'), isNull); |
| 110 }); |
| 111 }); |
| 112 |
| 113 test('should update on changed package imports', () { |
| 114 return applyTransformers(phases, |
| 115 inputs: { |
| 116 'a|web/main.dart': ''' |
| 117 import 'package:b/b.dart'; |
| 118 |
| 119 main() { |
| 120 } ''', |
| 121 'b|lib/b.dart': ''' |
| 122 library b; |
| 123 class Bar {} |
| 124 ''', |
| 125 }).then((_) { |
| 126 var lib = transformer.getResolver(entryPoint).entryLibrary; |
| 127 expect(lib.importedLibraries.length, 2); |
| 128 var libB = lib.importedLibraries.where((l) => l.name == 'b').single; |
| 129 expect(libB.getType('Bar'), isNotNull); |
| 130 }); |
| 131 }); |
| 132 |
| 133 test('should handle deleted files', () { |
| 134 return applyTransformers(phases, |
| 135 inputs: { |
| 136 'a|web/main.dart': ''' |
| 137 import 'package:b/b.dart'; |
| 138 |
| 139 main() { |
| 140 } ''', |
| 141 }, |
| 142 messages: [ |
| 143 'error: Unable to find asset for "package:b/b.dart"', |
| 144 'error: Unable to find asset for "package:b/b.dart"', |
| 145 ]).then((_) { |
| 146 var lib = transformer.getResolver(entryPoint).entryLibrary; |
| 147 expect(lib.importedLibraries.length, 1); |
| 148 }); |
| 149 }); |
| 150 |
| 151 test('should fail on absolute URIs', () { |
| 152 return applyTransformers(phases, |
| 153 inputs: { |
| 154 'a|web/main.dart': ''' |
| 155 import '/b.dart'; |
| 156 |
| 157 main() { |
| 158 } ''', |
| 159 }, |
| 160 messages: [ |
| 161 // First from the AST walker |
| 162 'error: absolute paths not allowed: "/b.dart" (main.dart 0 14)', |
| 163 // Then two from the resolver. |
| 164 'error: absolute paths not allowed: "/b.dart"', |
| 165 'error: absolute paths not allowed: "/b.dart"', |
| 166 ]).then((_) { |
| 167 var lib = transformer.getResolver(entryPoint).entryLibrary; |
| 168 expect(lib.importedLibraries.length, 1); |
| 169 }); |
| 170 }); |
| 171 |
| 172 test('should list all libraries', () { |
| 173 return applyTransformers(phases, |
| 174 inputs: { |
| 175 'a|web/main.dart': ''' |
| 176 library a.main; |
| 177 import 'package:a/a.dart'; |
| 178 import 'package:a/b.dart'; |
| 179 ''', |
| 180 'a|lib/a.dart': 'library a.a;\n import "package:a/c.dart";', |
| 181 'a|lib/b.dart': 'library a.b;\n import "c.dart";', |
| 182 'a|lib/c.dart': 'library a.c;' |
| 183 }).then((_) { |
| 184 var resolver = transformer.getResolver(entryPoint); |
| 185 var libs = resolver.libraries.where((l) => !l.isInSdk); |
| 186 expect(libs.map((l) => l.name), unorderedEquals([ |
| 187 'a.main', |
| 188 'a.a', |
| 189 'a.b', |
| 190 'a.c', |
| 191 ])); |
| 192 }); |
| 193 }); |
| 194 |
| 195 test('should resolve types and library uris', () { |
| 196 return applyTransformers(phases, |
| 197 inputs: { |
| 198 'a|web/main.dart': ''' |
| 199 import 'dart:core'; |
| 200 import 'package:a/a.dart'; |
| 201 import 'package:a/b.dart'; |
| 202 import 'sub_dir/d.dart'; |
| 203 class Foo {} |
| 204 ''', |
| 205 'a|lib/a.dart': 'library a.a;\n import "package:a/c.dart";', |
| 206 'a|lib/b.dart': 'library a.b;\n import "c.dart";', |
| 207 'a|lib/c.dart': ''' |
| 208 library a.c; |
| 209 class Bar {} |
| 210 ''', |
| 211 'a|web/sub_dir/d.dart': ''' |
| 212 library a.web.sub_dir.d; |
| 213 class Baz{} |
| 214 ''', |
| 215 }).then((_) { |
| 216 var resolver = transformer.getResolver(entryPoint); |
| 217 |
| 218 var a = resolver.getLibraryByName('a.a'); |
| 219 expect(a, isNotNull); |
| 220 expect(resolver.getImportUri(a).toString(), |
| 221 'package:a/a.dart'); |
| 222 expect(resolver.getLibraryByUri(Uri.parse('package:a/a.dart')), a); |
| 223 |
| 224 var main = resolver.getLibraryByName(''); |
| 225 expect(main, isNotNull); |
| 226 expect(resolver.getImportUri(main), isNull); |
| 227 |
| 228 var fooType = resolver.getType('Foo'); |
| 229 expect(fooType, isNotNull); |
| 230 expect(fooType.library, main); |
| 231 |
| 232 var barType = resolver.getType('a.c.Bar'); |
| 233 expect(barType, isNotNull); |
| 234 expect(resolver.getImportUri(barType.library).toString(), |
| 235 'package:a/c.dart'); |
| 236 expect(resolver.getSourceAssetId(barType), |
| 237 new AssetId('a', 'lib/c.dart')); |
| 238 |
| 239 var bazType = resolver.getType('a.web.sub_dir.d.Baz'); |
| 240 expect(bazType, isNotNull); |
| 241 expect(resolver.getImportUri(bazType.library), isNull); |
| 242 expect(resolver |
| 243 .getImportUri(bazType.library, from: entryPoint).toString(), |
| 244 'sub_dir/d.dart'); |
| 245 |
| 246 var hashMap = resolver.getType('dart.collection.HashMap'); |
| 247 expect(resolver.getImportUri(hashMap.library).toString(), |
| 248 'dart:collection'); |
| 249 expect(resolver.getLibraryByUri(Uri.parse('dart:collection')), |
| 250 hashMap.library); |
| 251 |
| 252 }); |
| 253 }); |
| 254 |
| 255 test('deleted files should be removed', () { |
| 256 return applyTransformers(phases, |
| 257 inputs: { |
| 258 'a|web/main.dart': '''import 'package:a/a.dart';''', |
| 259 'a|lib/a.dart': '''import 'package:a/b.dart';''', |
| 260 'a|lib/b.dart': '''class Engine{}''', |
| 261 }).then((_) { |
| 262 var resolver = transformer.getResolver(entryPoint); |
| 263 var engine = resolver.getType('Engine'); |
| 264 var uri = resolver.getImportUri(engine.library); |
| 265 expect(uri.toString(), 'package:a/b.dart'); |
| 266 }).then((_) { |
| 267 return applyTransformers(phases, |
| 268 inputs: { |
| 269 'a|web/main.dart': '''import 'package:a/a.dart';''', |
| 270 'a|lib/a.dart': '''lib a;\n class Engine{}''' |
| 271 }); |
| 272 }).then((_) { |
| 273 var resolver = transformer.getResolver(entryPoint); |
| 274 var engine = resolver.getType('Engine'); |
| 275 var uri = resolver.getImportUri(engine.library); |
| 276 expect(uri.toString(), 'package:a/a.dart'); |
| 277 |
| 278 // Make sure that we haven't leaked any sources. |
| 279 expect(resolver.sources.length, 2); |
| 280 }); |
| 281 }); |
| 282 |
| 283 test('handles circular imports', () { |
| 284 return applyTransformers(phases, |
| 285 inputs: { |
| 286 'a|web/main.dart': ''' |
| 287 library main; |
| 288 import 'package:a/a.dart'; ''', |
| 289 'a|lib/a.dart': ''' |
| 290 library a; |
| 291 import 'package:a/b.dart'; ''', |
| 292 'a|lib/b.dart': ''' |
| 293 library b; |
| 294 import 'package:a/a.dart'; ''', |
| 295 }).then((_) { |
| 296 var resolver = transformer.getResolver(entryPoint); |
| 297 |
| 298 var libs = resolver.libraries.map((lib) => lib.name); |
| 299 expect(libs.contains('a'), isTrue); |
| 300 expect(libs.contains('b'), isTrue); |
| 301 }); |
| 302 }); |
| 303 }); |
| 304 } |
OLD | NEW |