Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: pkg/analyzer/test/source/package_map_resolver_test.dart

Issue 2812093002: Simplify PackageMapUriResolver - one folder, return any file, or null. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/source/package_map_resolver.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 analyzer.test.source.package_map_resolver_test; 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';
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 }, throws); 48 }, throws);
49 } 49 }
50 50
51 void test_new_null_resourceProvider() { 51 void test_new_null_resourceProvider() {
52 expect(() { 52 expect(() {
53 new PackageMapUriResolver(null, <String, List<Folder>>{}); 53 new PackageMapUriResolver(null, <String, List<Folder>>{});
54 }, throws); 54 }, throws);
55 } 55 }
56 56
57 void test_resolve_multiple_folders() { 57 void test_resolve_multiple_folders() {
58 String pkgFileA = provider.convertPath('/part1/lib/libA.dart'); 58 var a = provider.newFile(provider.convertPath('/aaa/a.dart'), '');
59 String pkgFileB = provider.convertPath('/part2/lib/libB.dart'); 59 var b = provider.newFile(provider.convertPath('/bbb/b.dart'), '');
60 provider.newFile(pkgFileA, 'library lib_a'); 60 expect(() {
61 provider.newFile(pkgFileB, 'library lib_b'); 61 new PackageMapUriResolver(provider, <String, List<Folder>>{
62 PackageMapUriResolver resolver = 62 'pkg': <Folder>[a.parent, b.parent]
63 new PackageMapUriResolver(provider, <String, List<Folder>>{ 63 });
64 'pkg': <Folder>[ 64 }, throwsArgumentError);
65 provider.getResource(provider.convertPath('/part1/lib/')),
66 provider.getResource(provider.convertPath('/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 } 65 }
86 66
87 void test_resolve_nonPackage() { 67 void test_resolve_nonPackage() {
88 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP); 68 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP);
89 Uri uri = Uri.parse('dart:core'); 69 Uri uri = Uri.parse('dart:core');
90 Source result = resolver.resolveAbsolute(uri); 70 Source result = resolver.resolveAbsolute(uri);
91 expect(result, isNull); 71 expect(result, isNull);
92 } 72 }
93 73
94 void test_resolve_OK() { 74 void test_resolve_OK() {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP); 119 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP);
140 Uri uri = Uri.parse('package:/'); 120 Uri uri = Uri.parse('package:/');
141 Source result = resolver.resolveAbsolute(uri); 121 Source result = resolver.resolveAbsolute(uri);
142 expect(result, isNull); 122 expect(result, isNull);
143 } 123 }
144 124
145 void test_resolve_package_notInMap() { 125 void test_resolve_package_notInMap() {
146 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP); 126 UriResolver resolver = new PackageMapUriResolver(provider, EMPTY_MAP);
147 Uri uri = Uri.parse('package:analyzer/analyzer.dart'); 127 Uri uri = Uri.parse('package:analyzer/analyzer.dart');
148 Source result = resolver.resolveAbsolute(uri); 128 Source result = resolver.resolveAbsolute(uri);
149 expect(result, isNotNull); 129 expect(result, isNull);
150 expect(result.exists(), isFalse);
151 expect(result.fullName, 'analyzer.dart');
152 expect(result.uri.toString(), 'package:analyzer/analyzer.dart');
153 } 130 }
154 131
155 void test_restoreAbsolute() { 132 void test_restoreAbsolute() {
156 String pkgFileA = provider.convertPath('/pkgA/lib/libA.dart'); 133 String pkgFileA = provider.convertPath('/pkgA/lib/libA.dart');
157 String pkgFileB = provider.convertPath('/pkgB/lib/src/libB.dart'); 134 String pkgFileB = provider.convertPath('/pkgB/lib/src/libB.dart');
158 provider.newFile(pkgFileA, 'library lib_a;'); 135 provider.newFile(pkgFileA, 'library lib_a;');
159 provider.newFile(pkgFileB, 'library lib_b;'); 136 provider.newFile(pkgFileB, 'library lib_b;');
160 PackageMapUriResolver resolver = 137 PackageMapUriResolver resolver =
161 new PackageMapUriResolver(provider, <String, List<Folder>>{ 138 new PackageMapUriResolver(provider, <String, List<Folder>>{
162 'pkgA': <Folder>[ 139 'pkgA': <Folder>[
(...skipping 15 matching lines...) Expand all
178 expect(uri, isNotNull); 155 expect(uri, isNotNull);
179 expect(uri.toString(), 'package:pkgB/src/libB.dart'); 156 expect(uri.toString(), 'package:pkgB/src/libB.dart');
180 } 157 }
181 { 158 {
182 Source source = _createFileSource('/no/such/file'); 159 Source source = _createFileSource('/no/such/file');
183 Uri uri = resolver.restoreAbsolute(source); 160 Uri uri = resolver.restoreAbsolute(source);
184 expect(uri, isNull); 161 expect(uri, isNull);
185 } 162 }
186 } 163 }
187 164
188 void test_restoreAbsolute_ambiguous() {
189 String file1 = provider.convertPath('/foo1/lib/bar.dart');
190 String file2 = provider.convertPath('/foo2/lib/bar.dart');
191 provider.newFile(file1, 'library bar');
192 provider.newFile(file2, 'library bar');
193 PackageMapUriResolver resolver =
194 new PackageMapUriResolver(provider, <String, List<Folder>>{
195 'foo': <Folder>[
196 provider.getResource(provider.convertPath('/foo1/lib')),
197 provider.getResource(provider.convertPath('/foo2/lib'))
198 ]
199 });
200 // Restoring file1 should yield a package URI, and that package URI should
201 // resolve back to file1.
202 Source source1 = _createFileSource(file1);
203 Uri uri1 = resolver.restoreAbsolute(source1);
204 expect(uri1.toString(), 'package:foo/bar.dart');
205 expect(resolver.resolveAbsolute(uri1).fullName, file1);
206 // Restoring file2 should not yield a package URI, because there is no URI
207 // that resolves to file2.
208 Source source2 = _createFileSource(file2);
209 expect(resolver.restoreAbsolute(source2), isNull);
210 }
211
212 void test_restoreAbsolute_longestMatch() {
213 String file1 = provider.convertPath('/foo1/bar1/lib.dart');
214 String file2 = provider.convertPath('/foo2/bar2/lib.dart');
215 provider.newFile(file1, 'library lib');
216 provider.newFile(file2, 'library lib');
217 PackageMapUriResolver resolver =
218 new PackageMapUriResolver(provider, <String, List<Folder>>{
219 'pkg1': <Folder>[
220 provider.getResource(provider.convertPath('/foo1')),
221 provider.getResource(provider.convertPath('/foo2/bar2'))
222 ],
223 'pkg2': <Folder>[
224 provider.getResource(provider.convertPath('/foo1/bar1')),
225 provider.getResource(provider.convertPath('/foo2'))
226 ]
227 });
228 // Restoring file1 should yield a package URI for pkg2, since pkg2's match
229 // for the file path (/foo1/bar1) is longer than pkg1's match (/foo1).
230 Source source1 = _createFileSource(file1);
231 Uri uri1 = resolver.restoreAbsolute(source1);
232 expect(uri1.toString(), 'package:pkg2/lib.dart');
233 // Restoring file2 should yield a package URI for pkg1, since pkg1's match
234 // for the file path (/foo2/bar2) is longer than pkg2's match (/foo2).
235 Source source2 = _createFileSource(file2);
236 Uri uri2 = resolver.restoreAbsolute(source2);
237 expect(uri2.toString(), 'package:pkg1/lib.dart');
238 }
239
240 Source _createFileSource(String path) { 165 Source _createFileSource(String path) {
241 return new NonExistingSource(path, toUri(path), UriKind.FILE_URI); 166 return new NonExistingSource(path, toUri(path), UriKind.FILE_URI);
242 } 167 }
243 } 168 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/source/package_map_resolver.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698