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

Side by Side Diff: pkg/analyzer/test/generated/source_factory_test.dart

Issue 2269683002: Remove more references to FileBasedSource (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 3 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
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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.generated.test.generated.source_factory; 5 library analyzer.test.generated.test.generated.source_factory;
6 6
7 import 'dart:convert'; 7 import 'dart:convert';
8 8
9 import 'package:analyzer/file_system/file_system.dart'; 9 import 'package:analyzer/file_system/file_system.dart';
10 import 'package:analyzer/file_system/physical_file_system.dart';
11 import 'package:analyzer/file_system/memory_file_system.dart'; 10 import 'package:analyzer/file_system/memory_file_system.dart';
12 import 'package:analyzer/source/package_map_resolver.dart'; 11 import 'package:analyzer/source/package_map_resolver.dart';
13 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine, Logger; 12 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine, Logger;
14 import 'package:analyzer/src/generated/java_core.dart'; 13 import 'package:analyzer/src/generated/java_core.dart';
15 import 'package:analyzer/src/generated/java_engine_io.dart'; 14 import 'package:analyzer/src/generated/java_engine_io.dart';
16 import 'package:analyzer/src/generated/java_io.dart';
17 import 'package:analyzer/src/generated/source.dart'; 15 import 'package:analyzer/src/generated/source.dart';
18 import 'package:analyzer/src/generated/source_io.dart'; 16 import 'package:analyzer/src/generated/source_io.dart';
19 import 'package:analyzer/src/generated/utilities_dart.dart' as utils; 17 import 'package:analyzer/src/generated/utilities_dart.dart' as utils;
18 import 'package:analyzer/src/source/source_resource.dart';
20 import 'package:package_config/packages.dart'; 19 import 'package:package_config/packages.dart';
21 import 'package:package_config/packages_file.dart' as pkgfile show parse; 20 import 'package:package_config/packages_file.dart' as pkgfile show parse;
22 import 'package:package_config/src/packages_impl.dart'; 21 import 'package:package_config/src/packages_impl.dart';
23 import 'package:path/path.dart'; 22 import 'package:path/path.dart';
24 import 'package:unittest/unittest.dart'; 23 import 'package:unittest/unittest.dart';
25 24
26 import '../reflective_tests.dart'; 25 import '../reflective_tests.dart';
27 import '../utils.dart'; 26 import '../utils.dart';
28 import 'test_support.dart'; 27 import 'test_support.dart';
29 28
30 main() { 29 main() {
31 initializeTestEnvironment(); 30 initializeTestEnvironment();
32 runReflectiveTests(SourceFactoryTest); 31 runReflectiveTests(SourceFactoryTest);
33 runPackageMapTests(); 32 runPackageMapTests();
34 } 33 }
35 34
36 Source createSource({String path, String uri}) => 35 Source createSource({String path, String uri}) =>
37 //TODO(pquitslund): find some way to pass an actual URI into source creation 36 //TODO(pquitslund): find some way to pass an actual URI into source creation
38 new MemoryResourceProvider() 37 new MemoryResourceProvider()
39 .getFile(path) 38 .getFile(path)
40 .createSource(uri != null ? Uri.parse(uri) : null); 39 .createSource(uri != null ? Uri.parse(uri) : null);
41 40
42 void runPackageMapTests() { 41 void runPackageMapTests() {
42 MemoryResourceProvider resourceProvider = new MemoryResourceProvider();
43 final Uri baseUri = new Uri.file('test/base'); 43 final Uri baseUri = new Uri.file('test/base');
44 final List<UriResolver> testResolvers = [ 44 final List<UriResolver> testResolvers = [
45 new ResourceUriResolver(PhysicalResourceProvider.INSTANCE) 45 new ResourceUriResolver(resourceProvider)
46 ]; 46 ];
47 47
48 Packages createPackageMap(Uri base, String configFileContents) { 48 Packages createPackageMap(Uri base, String configFileContents) {
49 List<int> bytes = UTF8.encode(configFileContents); 49 List<int> bytes = UTF8.encode(configFileContents);
50 Map<String, Uri> map = pkgfile.parse(bytes, base); 50 Map<String, Uri> map = pkgfile.parse(bytes, base);
51 return new MapPackages(map); 51 return new MapPackages(map);
52 } 52 }
53 53
54 Map<String, List<Folder>> getPackageMap(String config) { 54 Map<String, List<Folder>> getPackageMap(String config) {
55 Packages packages = createPackageMap(baseUri, config); 55 Packages packages = createPackageMap(baseUri, config);
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 }); 165 });
166 }); 166 });
167 group('restoreUri', () { 167 group('restoreUri', () {
168 test('URI in mapping', () { 168 test('URI in mapping', () {
169 Uri uri = restorePackageUri( 169 Uri uri = restorePackageUri(
170 config: ''' 170 config: '''
171 unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/ 171 unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/
172 async:/home/somebody/.pub/cache/async-1.1.0/lib/ 172 async:/home/somebody/.pub/cache/async-1.1.0/lib/
173 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib 173 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib
174 ''', 174 ''',
175 source: new FileBasedSource(FileUtilities2.createFile( 175 source: new FileSource(resourceProvider.getFile(
176 '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart')) ); 176 '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart')) );
177 expect(uri, isNotNull); 177 expect(uri, isNotNull);
178 expect(uri.toString(), equals('package:unittest/unittest.dart')); 178 expect(uri.toString(), equals('package:unittest/unittest.dart'));
179 }); 179 });
180 }); 180 });
181 group('packageMap', () { 181 group('packageMap', () {
182 test('non-file URIs filtered', () { 182 test('non-file URIs filtered', () {
183 Map<String, List<Folder>> map = getPackageMap(''' 183 Map<String, List<Folder>> map = getPackageMap('''
184 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib 184 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib
185 foo:http://www.google.com 185 foo:http://www.google.com
(...skipping 14 matching lines...) Expand all
200 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('/foo/b')), 200 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('/foo/b')),
201 isFalse); 201 isFalse);
202 // Handle odd URIs (https://github.com/dart-lang/sdk/issues/24126) 202 // Handle odd URIs (https://github.com/dart-lang/sdk/issues/24126)
203 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('')), isFalse); 203 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('')), isFalse);
204 expect(utils.startsWith(Uri.parse(''), Uri.parse('/foo/bar')), isFalse); 204 expect(utils.startsWith(Uri.parse(''), Uri.parse('/foo/bar')), isFalse);
205 }); 205 });
206 }); 206 });
207 }); 207 });
208 } 208 }
209 209
210 class AbsoluteUriResolver extends UriResolver {
211 final MemoryResourceProvider resourceProvider;
212
213 AbsoluteUriResolver(this.resourceProvider);
214
215 @override
216 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
217 return new FileSource(
218 resourceProvider.getFile(resourceProvider.pathContext.fromUri(uri)),
219 actualUri);
220 }
221 }
222
210 class CustomUriResolver extends UriResolver { 223 class CustomUriResolver extends UriResolver {
211 String uriPath; 224 String uriPath;
212 CustomUriResolver({this.uriPath}); 225 CustomUriResolver({this.uriPath});
213 226
214 @override 227 @override
215 Source resolveAbsolute(Uri uri, [Uri actualUri]) => 228 Source resolveAbsolute(Uri uri, [Uri actualUri]) =>
216 createSource(path: uriPath); 229 createSource(path: uriPath);
217 } 230 }
218 231
219 @reflectiveTest 232 @reflectiveTest
220 class SourceFactoryTest { 233 class SourceFactoryTest {
234 MemoryResourceProvider resourceProvider = new MemoryResourceProvider();
235
221 void test_creation() { 236 void test_creation() {
222 expect(new SourceFactory([]), isNotNull); 237 expect(new SourceFactory([]), isNotNull);
223 } 238 }
224 239
225 void test_fromEncoding_invalidUri() { 240 void test_fromEncoding_invalidUri() {
226 SourceFactory factory = new SourceFactory([]); 241 SourceFactory factory = new SourceFactory([]);
227 expect(() => factory.fromEncoding("<:&%>"), 242 expect(() => factory.fromEncoding("<:&%>"),
228 throwsA(new isInstanceOf<IllegalArgumentException>())); 243 throwsA(new isInstanceOf<IllegalArgumentException>()));
229 } 244 }
230 245
(...skipping 12 matching lines...) Expand all
243 258
244 void test_resolveUri_absolute() { 259 void test_resolveUri_absolute() {
245 UriResolver_absolute resolver = new UriResolver_absolute(); 260 UriResolver_absolute resolver = new UriResolver_absolute();
246 SourceFactory factory = new SourceFactory([resolver]); 261 SourceFactory factory = new SourceFactory([resolver]);
247 factory.resolveUri(null, "dart:core"); 262 factory.resolveUri(null, "dart:core");
248 expect(resolver.invoked, isTrue); 263 expect(resolver.invoked, isTrue);
249 } 264 }
250 265
251 void test_resolveUri_nonAbsolute_absolute() { 266 void test_resolveUri_nonAbsolute_absolute() {
252 SourceFactory factory = 267 SourceFactory factory =
253 new SourceFactory([new UriResolver_nonAbsolute_absolute()]); 268 new SourceFactory([new AbsoluteUriResolver(resourceProvider)]);
254 String absolutePath = "/does/not/matter.dart"; 269 String absolutePath = "/does/not/matter.dart";
255 Source containingSource = 270 Source containingSource =
256 new FileBasedSource(FileUtilities2.createFile("/does/not/exist.dart")); 271 new FileSource(resourceProvider.getFile("/does/not/exist.dart"));
257 Source result = factory.resolveUri(containingSource, absolutePath); 272 Source result = factory.resolveUri(containingSource, absolutePath);
258 expect(result.fullName, 273 expect(result.fullName,
259 FileUtilities2.createFile(absolutePath).getAbsolutePath()); 274 FileUtilities2.createFile(absolutePath).getAbsolutePath());
260 } 275 }
261 276
262 void test_resolveUri_nonAbsolute_relative() { 277 void test_resolveUri_nonAbsolute_relative() {
263 SourceFactory factory = 278 SourceFactory factory =
264 new SourceFactory([new UriResolver_nonAbsolute_relative()]); 279 new SourceFactory([new AbsoluteUriResolver(resourceProvider)]);
265 Source containingSource = 280 Source containingSource =
266 new FileBasedSource(FileUtilities2.createFile("/does/not/have.dart")); 281 new FileSource(resourceProvider.getFile("/does/not/have.dart"));
267 Source result = factory.resolveUri(containingSource, "exist.dart"); 282 Source result = factory.resolveUri(containingSource, "exist.dart");
268 expect(result.fullName, 283 expect(result.fullName,
269 FileUtilities2.createFile("/does/not/exist.dart").getAbsolutePath()); 284 FileUtilities2.createFile("/does/not/exist.dart").getAbsolutePath());
270 } 285 }
271 286
272 void test_resolveUri_nonAbsolute_relative_package() { 287 void test_resolveUri_nonAbsolute_relative_package() {
273 MemoryResourceProvider provider = new MemoryResourceProvider(); 288 MemoryResourceProvider provider = new MemoryResourceProvider();
274 Context context = provider.pathContext; 289 Context context = provider.pathContext;
275 String packagePath = 290 String packagePath =
276 context.joinAll([context.separator, 'path', 'to', 'package']); 291 context.joinAll([context.separator, 'path', 'to', 'package']);
(...skipping 15 matching lines...) Expand all
292 Source librarySource = 307 Source librarySource =
293 firstFile.createSource(Uri.parse('package:package/dir/first.dart')); 308 firstFile.createSource(Uri.parse('package:package/dir/first.dart'));
294 309
295 Source result = factory.resolveUri(librarySource, 'second.dart'); 310 Source result = factory.resolveUri(librarySource, 'second.dart');
296 expect(result, isNotNull); 311 expect(result, isNotNull);
297 expect(result.fullName, secondPath); 312 expect(result.fullName, secondPath);
298 expect(result.uri.toString(), 'package:package/dir/second.dart'); 313 expect(result.uri.toString(), 'package:package/dir/second.dart');
299 } 314 }
300 315
301 void test_restoreUri() { 316 void test_restoreUri() {
302 JavaFile file1 = FileUtilities2.createFile("/some/file1.dart"); 317 File file1 = resourceProvider.getFile("/some/file1.dart");
303 JavaFile file2 = FileUtilities2.createFile("/some/file2.dart"); 318 File file2 = resourceProvider.getFile("/some/file2.dart");
304 Source source1 = new FileBasedSource(file1); 319 Source source1 = new FileSource(file1);
305 Source source2 = new FileBasedSource(file2); 320 Source source2 = new FileSource(file2);
306 Uri expected1 = parseUriWithException("file:///my_file.dart"); 321 Uri expected1 = parseUriWithException("file:///my_file.dart");
307 SourceFactory factory = 322 SourceFactory factory =
308 new SourceFactory([new UriResolver_restoreUri(source1, expected1)]); 323 new SourceFactory([new UriResolver_restoreUri(source1, expected1)]);
309 expect(factory.restoreUri(source1), same(expected1)); 324 expect(factory.restoreUri(source1), same(expected1));
310 expect(factory.restoreUri(source2), same(null)); 325 expect(factory.restoreUri(source2), same(null));
311 } 326 }
312 } 327 }
313 328
314 class UriResolver_absolute extends UriResolver { 329 class UriResolver_absolute extends UriResolver {
315 bool invoked = false; 330 bool invoked = false;
316 331
317 UriResolver_absolute(); 332 UriResolver_absolute();
318 333
319 @override 334 @override
320 Source resolveAbsolute(Uri uri, [Uri actualUri]) { 335 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
321 invoked = true; 336 invoked = true;
322 return null; 337 return null;
323 } 338 }
324 } 339 }
325 340
326 class UriResolver_nonAbsolute_absolute extends UriResolver {
327 @override
328 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
329 return new FileBasedSource(new JavaFile.fromUri(uri), actualUri);
330 }
331 }
332
333 class UriResolver_nonAbsolute_relative extends UriResolver {
334 @override
335 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
336 return new FileBasedSource(new JavaFile.fromUri(uri), actualUri);
337 }
338 }
339
340 class UriResolver_restoreUri extends UriResolver { 341 class UriResolver_restoreUri extends UriResolver {
341 Source source1; 342 Source source1;
342 Uri expected1; 343 Uri expected1;
343 UriResolver_restoreUri(this.source1, this.expected1); 344 UriResolver_restoreUri(this.source1, this.expected1);
344 345
345 @override 346 @override
346 Source resolveAbsolute(Uri uri, [Uri actualUri]) => null; 347 Source resolveAbsolute(Uri uri, [Uri actualUri]) => null;
347 348
348 @override 349 @override
349 Uri restoreAbsolute(Source source) { 350 Uri restoreAbsolute(Source source) {
(...skipping 10 matching lines...) Expand all
360 UriResolver_SourceFactoryTest_test_fromEncoding_valid(this.encoding); 361 UriResolver_SourceFactoryTest_test_fromEncoding_valid(this.encoding);
361 362
362 @override 363 @override
363 Source resolveAbsolute(Uri uri, [Uri actualUri]) { 364 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
364 if (uri.toString() == encoding) { 365 if (uri.toString() == encoding) {
365 return new TestSource(); 366 return new TestSource();
366 } 367 }
367 return null; 368 return null;
368 } 369 }
369 } 370 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/generated/resolver_test.dart ('k') | pkg/analyzer/test/generated/static_type_analyzer_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698