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