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 test.source.package_map_resolver; |
| 6 |
| 7 import 'package:analyzer/file_system/file_system.dart'; |
| 8 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 9 import 'package:analyzer/source/package_map_resolver.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:path/path.dart'; |
| 12 import 'package:unittest/unittest.dart'; |
| 13 |
| 14 import '../reflective_tests.dart'; |
| 15 import '../utils.dart'; |
| 16 |
| 17 main() { |
| 18 initializeTestEnvironment(); |
| 19 runReflectiveTests(_PackageMapUriResolverTest); |
| 20 } |
| 21 |
| 22 @reflectiveTest |
| 23 class _PackageMapUriResolverTest { |
| 24 static const Map EMPTY_MAP = const <String, List<Folder>>{}; |
| 25 MemoryResourceProvider provider = new MemoryResourceProvider(); |
| 26 |
| 27 void test_isPackageUri() { |
| 28 Uri uri = Uri.parse('package:test/test.dart'); |
| 29 expect(uri.scheme, 'package'); |
| 30 expect(PackageMapUriResolver.isPackageUri(uri), isTrue); |
| 31 } |
| 32 |
| 33 void test_isPackageUri_null_scheme() { |
| 34 Uri uri = Uri.parse('foo.dart'); |
| 35 expect(uri.scheme, ''); |
| 36 expect(PackageMapUriResolver.isPackageUri(uri), isFalse); |
| 37 } |
| 38 |
| 39 void test_isPackageUri_other_scheme() { |
| 40 Uri uri = Uri.parse('memfs:/foo.dart'); |
| 41 expect(uri.scheme, 'memfs'); |
| 42 expect(PackageMapUriResolver.isPackageUri(uri), isFalse); |
| 43 } |
| 44 |
| 45 void test_new_null_packageMap() { |
| 46 expect(() { |
| 47 new PackageMapUriResolver(provider, null); |
| 48 }, throws); |
| 49 } |
| 50 |
| 51 void test_new_null_resourceProvider() { |
| 52 expect(() { |
| 53 new PackageMapUriResolver(null, <String, List<Folder>>{}); |
| 54 }, throws); |
| 55 } |
| 56 |
| 57 void test_resolve_multiple_folders() { |
| 58 const pkgFileA = '/part1/lib/libA.dart'; |
| 59 const pkgFileB = '/part2/lib/libB.dart'; |
| 60 provider.newFile(pkgFileA, 'library lib_a'); |
| 61 provider.newFile(pkgFileB, 'library lib_b'); |
| 62 PackageMapUriResolver resolver = |
| 63 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 64 'pkg': [ |
| 65 provider.getResource('/part1/lib/'), |
| 66 provider.getResource('/part2/lib/') |
| 67 ] |
| 68 }); |
| 69 { |
| 70 Uri uri = Uri.parse('package:pkg/libA.dart'); |
| 71 Source result = resolver.resolveAbsolute(uri); |
| 72 expect(result, isNotNull); |
| 73 expect(result.exists(), isTrue); |
| 74 expect(result.uriKind, UriKind.PACKAGE_URI); |
| 75 expect(result.fullName, pkgFileA); |
| 76 } |
| 77 { |
| 78 Uri uri = Uri.parse('package:pkg/libB.dart'); |
| 79 Source result = resolver.resolveAbsolute(uri); |
| 80 expect(result, isNotNull); |
| 81 expect(result.exists(), isTrue); |
| 82 expect(result.uriKind, UriKind.PACKAGE_URI); |
| 83 expect(result.fullName, pkgFileB); |
| 84 } |
| 85 } |
| 86 |
| 87 void test_resolve_nonPackage() { |
| 88 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP); |
| 89 Uri uri = Uri.parse('dart:core'); |
| 90 Source result = resolver.resolveAbsolute(uri); |
| 91 expect(result, isNull); |
| 92 } |
| 93 |
| 94 void test_resolve_OK() { |
| 95 const pkgFileA = '/pkgA/lib/libA.dart'; |
| 96 const pkgFileB = '/pkgB/lib/libB.dart'; |
| 97 provider.newFile(pkgFileA, 'library lib_a;'); |
| 98 provider.newFile(pkgFileB, 'library lib_b;'); |
| 99 PackageMapUriResolver resolver = |
| 100 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 101 'pkgA': [provider.getResource('/pkgA/lib/')], |
| 102 'pkgB': [provider.getResource('/pkgB/lib/')] |
| 103 }); |
| 104 { |
| 105 Uri uri = Uri.parse('package:pkgA/libA.dart'); |
| 106 Source result = resolver.resolveAbsolute(uri); |
| 107 expect(result, isNotNull); |
| 108 expect(result.exists(), isTrue); |
| 109 expect(result.uriKind, UriKind.PACKAGE_URI); |
| 110 expect(result.fullName, pkgFileA); |
| 111 } |
| 112 { |
| 113 Uri uri = Uri.parse('package:pkgB/libB.dart'); |
| 114 Source result = resolver.resolveAbsolute(uri); |
| 115 expect(result, isNotNull); |
| 116 expect(result.exists(), isTrue); |
| 117 expect(result.uriKind, UriKind.PACKAGE_URI); |
| 118 expect(result.fullName, pkgFileB); |
| 119 } |
| 120 } |
| 121 |
| 122 void test_resolve_package_invalid_leadingSlash() { |
| 123 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP); |
| 124 Uri uri = Uri.parse('package:/foo'); |
| 125 Source result = resolver.resolveAbsolute(uri); |
| 126 expect(result, isNull); |
| 127 } |
| 128 |
| 129 void test_resolve_package_invalid_noSlash() { |
| 130 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP); |
| 131 Uri uri = Uri.parse('package:foo'); |
| 132 Source result = resolver.resolveAbsolute(uri); |
| 133 expect(result, isNull); |
| 134 } |
| 135 |
| 136 void test_resolve_package_invalid_onlySlash() { |
| 137 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP); |
| 138 Uri uri = Uri.parse('package:/'); |
| 139 Source result = resolver.resolveAbsolute(uri); |
| 140 expect(result, isNull); |
| 141 } |
| 142 |
| 143 void test_resolve_package_notInMap() { |
| 144 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP); |
| 145 Uri uri = Uri.parse('package:analyzer/analyzer.dart'); |
| 146 Source result = resolver.resolveAbsolute(uri); |
| 147 expect(result, isNotNull); |
| 148 expect(result.exists(), isFalse); |
| 149 expect(result.fullName, 'analyzer.dart'); |
| 150 expect(result.uri.toString(), 'package:analyzer/analyzer.dart'); |
| 151 } |
| 152 |
| 153 void test_restoreAbsolute() { |
| 154 const pkgFileA = '/pkgA/lib/libA.dart'; |
| 155 const pkgFileB = '/pkgB/lib/src/libB.dart'; |
| 156 provider.newFile(pkgFileA, 'library lib_a;'); |
| 157 provider.newFile(pkgFileB, 'library lib_b;'); |
| 158 PackageMapUriResolver resolver = |
| 159 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 160 'pkgA': [provider.getResource('/pkgA/lib/')], |
| 161 'pkgB': [provider.getResource('/pkgB/lib/')] |
| 162 }); |
| 163 { |
| 164 Source source = _createFileSource('/pkgA/lib/libA.dart'); |
| 165 Uri uri = resolver.restoreAbsolute(source); |
| 166 expect(uri, isNotNull); |
| 167 expect(uri.toString(), 'package:pkgA/libA.dart'); |
| 168 } |
| 169 { |
| 170 Source source = _createFileSource('/pkgB/lib/src/libB.dart'); |
| 171 Uri uri = resolver.restoreAbsolute(source); |
| 172 expect(uri, isNotNull); |
| 173 expect(uri.toString(), 'package:pkgB/src/libB.dart'); |
| 174 } |
| 175 { |
| 176 Source source = _createFileSource('/no/such/file'); |
| 177 Uri uri = resolver.restoreAbsolute(source); |
| 178 expect(uri, isNull); |
| 179 } |
| 180 } |
| 181 |
| 182 void test_restoreAbsolute_ambiguous() { |
| 183 const file1 = '/foo1/lib/bar.dart'; |
| 184 const file2 = '/foo2/lib/bar.dart'; |
| 185 provider.newFile(file1, 'library bar'); |
| 186 provider.newFile(file2, 'library bar'); |
| 187 PackageMapUriResolver resolver = |
| 188 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 189 'foo': [ |
| 190 provider.getResource('/foo1/lib'), |
| 191 provider.getResource('/foo2/lib') |
| 192 ] |
| 193 }); |
| 194 // Restoring file1 should yield a package URI, and that package URI should |
| 195 // resolve back to file1. |
| 196 Source source1 = _createFileSource(file1); |
| 197 Uri uri1 = resolver.restoreAbsolute(source1); |
| 198 expect(uri1.toString(), 'package:foo/bar.dart'); |
| 199 expect(resolver.resolveAbsolute(uri1).fullName, file1); |
| 200 // Restoring file2 should not yield a package URI, because there is no URI |
| 201 // that resolves to file2. |
| 202 Source source2 = _createFileSource(file2); |
| 203 expect(resolver.restoreAbsolute(source2), isNull); |
| 204 } |
| 205 |
| 206 void test_restoreAbsolute_longestMatch() { |
| 207 const file1 = '/foo1/bar1/lib.dart'; |
| 208 const file2 = '/foo2/bar2/lib.dart'; |
| 209 provider.newFile(file1, 'library lib'); |
| 210 provider.newFile(file2, 'library lib'); |
| 211 PackageMapUriResolver resolver = |
| 212 new PackageMapUriResolver(provider, <String, List<Folder>>{ |
| 213 'pkg1': [ |
| 214 provider.getResource('/foo1'), |
| 215 provider.getResource('/foo2/bar2') |
| 216 ], |
| 217 'pkg2': [ |
| 218 provider.getResource('/foo1/bar1'), |
| 219 provider.getResource('/foo2') |
| 220 ] |
| 221 }); |
| 222 // 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 Source source1 = _createFileSource(file1); |
| 225 Uri uri1 = resolver.restoreAbsolute(source1); |
| 226 expect(uri1.toString(), 'package:pkg2/lib.dart'); |
| 227 // 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 Source source2 = _createFileSource(file2); |
| 230 Uri uri2 = resolver.restoreAbsolute(source2); |
| 231 expect(uri2.toString(), 'package:pkg1/lib.dart'); |
| 232 } |
| 233 |
| 234 Source _createFileSource(String path) { |
| 235 return new NonExistingSource(path, toUri(path), UriKind.FILE_URI); |
| 236 } |
| 237 } |
OLD | NEW |