| 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 test.source.package_map_resolver; | 5 library analyzer.test.source.package_map_resolver_test; |
| 6 | 6 |
| 7 import 'package:analyzer/file_system/file_system.dart'; | 7 import 'package:analyzer/file_system/file_system.dart'; |
| 8 import 'package:analyzer/file_system/memory_file_system.dart'; | 8 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 9 import 'package:analyzer/source/package_map_resolver.dart'; | 9 import 'package:analyzer/source/package_map_resolver.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:path/path.dart'; | 11 import 'package:path/path.dart'; |
| 12 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 12 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 13 | 14 |
| 14 import '../reflective_tests.dart'; | |
| 15 import '../utils.dart'; | 15 import '../utils.dart'; |
| 16 | 16 |
| 17 main() { | 17 main() { |
| 18 initializeTestEnvironment(); | 18 initializeTestEnvironment(); |
| 19 runReflectiveTests(_PackageMapUriResolverTest); | 19 defineReflectiveTests(_PackageMapUriResolverTest); |
| 20 } | 20 } |
| 21 | 21 |
| 22 @reflectiveTest | 22 @reflectiveTest |
| 23 class _PackageMapUriResolverTest { | 23 class _PackageMapUriResolverTest { |
| 24 static const Map EMPTY_MAP = const <String, List<Folder>>{}; | 24 static const Map<String, List<Folder>> EMPTY_MAP = |
| 25 const <String, List<Folder>>{}; |
| 25 MemoryResourceProvider provider = new MemoryResourceProvider(); | 26 MemoryResourceProvider provider = new MemoryResourceProvider(); |
| 26 | 27 |
| 27 void test_isPackageUri() { | 28 void test_isPackageUri() { |
| 28 Uri uri = Uri.parse('package:test/test.dart'); | 29 Uri uri = Uri.parse('package:test/test.dart'); |
| 29 expect(uri.scheme, 'package'); | 30 expect(uri.scheme, 'package'); |
| 30 expect(PackageMapUriResolver.isPackageUri(uri), isTrue); | 31 expect(PackageMapUriResolver.isPackageUri(uri), isTrue); |
| 31 } | 32 } |
| 32 | 33 |
| 33 void test_isPackageUri_null_scheme() { | 34 void test_isPackageUri_null_scheme() { |
| 34 Uri uri = Uri.parse('foo.dart'); | 35 Uri uri = Uri.parse('foo.dart'); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 54 }, throws); | 55 }, throws); |
| 55 } | 56 } |
| 56 | 57 |
| 57 void test_resolve_multiple_folders() { | 58 void test_resolve_multiple_folders() { |
| 58 const pkgFileA = '/part1/lib/libA.dart'; | 59 const pkgFileA = '/part1/lib/libA.dart'; |
| 59 const pkgFileB = '/part2/lib/libB.dart'; | 60 const pkgFileB = '/part2/lib/libB.dart'; |
| 60 provider.newFile(pkgFileA, 'library lib_a'); | 61 provider.newFile(pkgFileA, 'library lib_a'); |
| 61 provider.newFile(pkgFileB, 'library lib_b'); | 62 provider.newFile(pkgFileB, 'library lib_b'); |
| 62 PackageMapUriResolver resolver = | 63 PackageMapUriResolver resolver = |
| 63 new PackageMapUriResolver(provider, <String, List<Folder>>{ | 64 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 64 'pkg': [ | 65 'pkg': <Folder>[ |
| 65 provider.getResource('/part1/lib/'), | 66 provider.getResource('/part1/lib/'), |
| 66 provider.getResource('/part2/lib/') | 67 provider.getResource('/part2/lib/') |
| 67 ] | 68 ] |
| 68 }); | 69 }); |
| 69 { | 70 { |
| 70 Uri uri = Uri.parse('package:pkg/libA.dart'); | 71 Uri uri = Uri.parse('package:pkg/libA.dart'); |
| 71 Source result = resolver.resolveAbsolute(uri); | 72 Source result = resolver.resolveAbsolute(uri); |
| 72 expect(result, isNotNull); | 73 expect(result, isNotNull); |
| 73 expect(result.exists(), isTrue); | 74 expect(result.exists(), isTrue); |
| 74 expect(result.uriKind, UriKind.PACKAGE_URI); | 75 expect(result.uriKind, UriKind.PACKAGE_URI); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 91 expect(result, isNull); | 92 expect(result, isNull); |
| 92 } | 93 } |
| 93 | 94 |
| 94 void test_resolve_OK() { | 95 void test_resolve_OK() { |
| 95 const pkgFileA = '/pkgA/lib/libA.dart'; | 96 const pkgFileA = '/pkgA/lib/libA.dart'; |
| 96 const pkgFileB = '/pkgB/lib/libB.dart'; | 97 const pkgFileB = '/pkgB/lib/libB.dart'; |
| 97 provider.newFile(pkgFileA, 'library lib_a;'); | 98 provider.newFile(pkgFileA, 'library lib_a;'); |
| 98 provider.newFile(pkgFileB, 'library lib_b;'); | 99 provider.newFile(pkgFileB, 'library lib_b;'); |
| 99 PackageMapUriResolver resolver = | 100 PackageMapUriResolver resolver = |
| 100 new PackageMapUriResolver(provider, <String, List<Folder>>{ | 101 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 101 'pkgA': [provider.getResource('/pkgA/lib/')], | 102 'pkgA': <Folder>[provider.getResource('/pkgA/lib/')], |
| 102 'pkgB': [provider.getResource('/pkgB/lib/')] | 103 'pkgB': <Folder>[provider.getResource('/pkgB/lib/')] |
| 103 }); | 104 }); |
| 104 { | 105 { |
| 105 Uri uri = Uri.parse('package:pkgA/libA.dart'); | 106 Uri uri = Uri.parse('package:pkgA/libA.dart'); |
| 106 Source result = resolver.resolveAbsolute(uri); | 107 Source result = resolver.resolveAbsolute(uri); |
| 107 expect(result, isNotNull); | 108 expect(result, isNotNull); |
| 108 expect(result.exists(), isTrue); | 109 expect(result.exists(), isTrue); |
| 109 expect(result.uriKind, UriKind.PACKAGE_URI); | 110 expect(result.uriKind, UriKind.PACKAGE_URI); |
| 110 expect(result.fullName, pkgFileA); | 111 expect(result.fullName, pkgFileA); |
| 111 } | 112 } |
| 112 { | 113 { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 expect(result.uri.toString(), 'package:analyzer/analyzer.dart'); | 151 expect(result.uri.toString(), 'package:analyzer/analyzer.dart'); |
| 151 } | 152 } |
| 152 | 153 |
| 153 void test_restoreAbsolute() { | 154 void test_restoreAbsolute() { |
| 154 const pkgFileA = '/pkgA/lib/libA.dart'; | 155 const pkgFileA = '/pkgA/lib/libA.dart'; |
| 155 const pkgFileB = '/pkgB/lib/src/libB.dart'; | 156 const pkgFileB = '/pkgB/lib/src/libB.dart'; |
| 156 provider.newFile(pkgFileA, 'library lib_a;'); | 157 provider.newFile(pkgFileA, 'library lib_a;'); |
| 157 provider.newFile(pkgFileB, 'library lib_b;'); | 158 provider.newFile(pkgFileB, 'library lib_b;'); |
| 158 PackageMapUriResolver resolver = | 159 PackageMapUriResolver resolver = |
| 159 new PackageMapUriResolver(provider, <String, List<Folder>>{ | 160 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 160 'pkgA': [provider.getResource('/pkgA/lib/')], | 161 'pkgA': <Folder>[provider.getResource('/pkgA/lib/')], |
| 161 'pkgB': [provider.getResource('/pkgB/lib/')] | 162 'pkgB': <Folder>[provider.getResource('/pkgB/lib/')] |
| 162 }); | 163 }); |
| 163 { | 164 { |
| 164 Source source = _createFileSource('/pkgA/lib/libA.dart'); | 165 Source source = _createFileSource('/pkgA/lib/libA.dart'); |
| 165 Uri uri = resolver.restoreAbsolute(source); | 166 Uri uri = resolver.restoreAbsolute(source); |
| 166 expect(uri, isNotNull); | 167 expect(uri, isNotNull); |
| 167 expect(uri.toString(), 'package:pkgA/libA.dart'); | 168 expect(uri.toString(), 'package:pkgA/libA.dart'); |
| 168 } | 169 } |
| 169 { | 170 { |
| 170 Source source = _createFileSource('/pkgB/lib/src/libB.dart'); | 171 Source source = _createFileSource('/pkgB/lib/src/libB.dart'); |
| 171 Uri uri = resolver.restoreAbsolute(source); | 172 Uri uri = resolver.restoreAbsolute(source); |
| 172 expect(uri, isNotNull); | 173 expect(uri, isNotNull); |
| 173 expect(uri.toString(), 'package:pkgB/src/libB.dart'); | 174 expect(uri.toString(), 'package:pkgB/src/libB.dart'); |
| 174 } | 175 } |
| 175 { | 176 { |
| 176 Source source = _createFileSource('/no/such/file'); | 177 Source source = _createFileSource('/no/such/file'); |
| 177 Uri uri = resolver.restoreAbsolute(source); | 178 Uri uri = resolver.restoreAbsolute(source); |
| 178 expect(uri, isNull); | 179 expect(uri, isNull); |
| 179 } | 180 } |
| 180 } | 181 } |
| 181 | 182 |
| 182 void test_restoreAbsolute_ambiguous() { | 183 void test_restoreAbsolute_ambiguous() { |
| 183 const file1 = '/foo1/lib/bar.dart'; | 184 const file1 = '/foo1/lib/bar.dart'; |
| 184 const file2 = '/foo2/lib/bar.dart'; | 185 const file2 = '/foo2/lib/bar.dart'; |
| 185 provider.newFile(file1, 'library bar'); | 186 provider.newFile(file1, 'library bar'); |
| 186 provider.newFile(file2, 'library bar'); | 187 provider.newFile(file2, 'library bar'); |
| 187 PackageMapUriResolver resolver = | 188 PackageMapUriResolver resolver = |
| 188 new PackageMapUriResolver(provider, <String, List<Folder>>{ | 189 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 189 'foo': [ | 190 'foo': <Folder>[ |
| 190 provider.getResource('/foo1/lib'), | 191 provider.getResource('/foo1/lib'), |
| 191 provider.getResource('/foo2/lib') | 192 provider.getResource('/foo2/lib') |
| 192 ] | 193 ] |
| 193 }); | 194 }); |
| 194 // Restoring file1 should yield a package URI, and that package URI should | 195 // Restoring file1 should yield a package URI, and that package URI should |
| 195 // resolve back to file1. | 196 // resolve back to file1. |
| 196 Source source1 = _createFileSource(file1); | 197 Source source1 = _createFileSource(file1); |
| 197 Uri uri1 = resolver.restoreAbsolute(source1); | 198 Uri uri1 = resolver.restoreAbsolute(source1); |
| 198 expect(uri1.toString(), 'package:foo/bar.dart'); | 199 expect(uri1.toString(), 'package:foo/bar.dart'); |
| 199 expect(resolver.resolveAbsolute(uri1).fullName, file1); | 200 expect(resolver.resolveAbsolute(uri1).fullName, file1); |
| 200 // Restoring file2 should not yield a package URI, because there is no URI | 201 // Restoring file2 should not yield a package URI, because there is no URI |
| 201 // that resolves to file2. | 202 // that resolves to file2. |
| 202 Source source2 = _createFileSource(file2); | 203 Source source2 = _createFileSource(file2); |
| 203 expect(resolver.restoreAbsolute(source2), isNull); | 204 expect(resolver.restoreAbsolute(source2), isNull); |
| 204 } | 205 } |
| 205 | 206 |
| 206 void test_restoreAbsolute_longestMatch() { | 207 void test_restoreAbsolute_longestMatch() { |
| 207 const file1 = '/foo1/bar1/lib.dart'; | 208 const file1 = '/foo1/bar1/lib.dart'; |
| 208 const file2 = '/foo2/bar2/lib.dart'; | 209 const file2 = '/foo2/bar2/lib.dart'; |
| 209 provider.newFile(file1, 'library lib'); | 210 provider.newFile(file1, 'library lib'); |
| 210 provider.newFile(file2, 'library lib'); | 211 provider.newFile(file2, 'library lib'); |
| 211 PackageMapUriResolver resolver = | 212 PackageMapUriResolver resolver = |
| 212 new PackageMapUriResolver(provider, <String, List<Folder>>{ | 213 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 213 'pkg1': [ | 214 'pkg1': <Folder>[ |
| 214 provider.getResource('/foo1'), | 215 provider.getResource('/foo1'), |
| 215 provider.getResource('/foo2/bar2') | 216 provider.getResource('/foo2/bar2') |
| 216 ], | 217 ], |
| 217 'pkg2': [ | 218 'pkg2': <Folder>[ |
| 218 provider.getResource('/foo1/bar1'), | 219 provider.getResource('/foo1/bar1'), |
| 219 provider.getResource('/foo2') | 220 provider.getResource('/foo2') |
| 220 ] | 221 ] |
| 221 }); | 222 }); |
| 222 // Restoring file1 should yield a package URI for pkg2, since pkg2's match | 223 // Restoring file1 should yield a package URI for pkg2, since pkg2's match |
| 223 // for the file path (/foo1/bar1) is longer than pkg1's match (/foo1). | 224 // for the file path (/foo1/bar1) is longer than pkg1's match (/foo1). |
| 224 Source source1 = _createFileSource(file1); | 225 Source source1 = _createFileSource(file1); |
| 225 Uri uri1 = resolver.restoreAbsolute(source1); | 226 Uri uri1 = resolver.restoreAbsolute(source1); |
| 226 expect(uri1.toString(), 'package:pkg2/lib.dart'); | 227 expect(uri1.toString(), 'package:pkg2/lib.dart'); |
| 227 // Restoring file2 should yield a package URI for pkg1, since pkg1's match | 228 // Restoring file2 should yield a package URI for pkg1, since pkg1's match |
| 228 // for the file path (/foo2/bar2) is longer than pkg2's match (/foo2). | 229 // for the file path (/foo2/bar2) is longer than pkg2's match (/foo2). |
| 229 Source source2 = _createFileSource(file2); | 230 Source source2 = _createFileSource(file2); |
| 230 Uri uri2 = resolver.restoreAbsolute(source2); | 231 Uri uri2 = resolver.restoreAbsolute(source2); |
| 231 expect(uri2.toString(), 'package:pkg1/lib.dart'); | 232 expect(uri2.toString(), 'package:pkg1/lib.dart'); |
| 232 } | 233 } |
| 233 | 234 |
| 234 Source _createFileSource(String path) { | 235 Source _createFileSource(String path) { |
| 235 return new NonExistingSource(path, toUri(path), UriKind.FILE_URI); | 236 return new NonExistingSource(path, toUri(path), UriKind.FILE_URI); |
| 236 } | 237 } |
| 237 } | 238 } |
| OLD | NEW |