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