Chromium Code Reviews| Index: pkg/analyzer/test/generated/source_factory_test.dart |
| diff --git a/pkg/analyzer/test/generated/source_factory_test.dart b/pkg/analyzer/test/generated/source_factory_test.dart |
| index 26b3942c1c0fb47e08e9f7cfeb446ee0ee530d43..afb5d5406dfd09f2bd0c2b6a1bf32e95676e8827 100644 |
| --- a/pkg/analyzer/test/generated/source_factory_test.dart |
| +++ b/pkg/analyzer/test/generated/source_factory_test.dart |
| @@ -7,6 +7,8 @@ |
| library analyzer.test.generated.source_factory; |
| +import 'dart:convert'; |
| + |
| import 'package:analyzer/file_system/file_system.dart'; |
| import 'package:analyzer/file_system/memory_file_system.dart'; |
| import 'package:analyzer/source/package_map_resolver.dart'; |
| @@ -15,6 +17,9 @@ import 'package:analyzer/src/generated/java_engine_io.dart'; |
| import 'package:analyzer/src/generated/java_io.dart'; |
| import 'package:analyzer/src/generated/source.dart'; |
| import 'package:analyzer/src/generated/source_io.dart'; |
| +import 'package:package_config/packages.dart'; |
| +import 'package:package_config/packages_file.dart' as pkgfile show parse; |
| +import 'package:package_config/src/packages_impl.dart'; |
| import 'package:path/path.dart'; |
| import 'package:unittest/unittest.dart'; |
| @@ -24,6 +29,119 @@ import 'test_support.dart'; |
| main() { |
| groupSep = ' | '; |
| runReflectiveTests(SourceFactoryTest); |
| + runPackageMapTests(); |
| +} |
| + |
| +class CustomUriResolver extends UriResolver { |
| + static final Source sharedSource = |
| + new NonExistingSource(null, sharedUri, null); |
| + static final Uri sharedUri = new Uri(); |
| + |
| + @override |
| + Source resolveAbsolute(Uri uri) => sharedSource; |
| +} |
| + |
| +void runPackageMapTests() { |
| + final Uri baseUri = new Uri.file('test/base'); |
| + final List<UriResolver> testResolvers = [new FileUriResolver()]; |
| + |
| + Packages createPackageMap(Uri base, String configFileContents) { |
| + List<int> bytes = UTF8.encode(configFileContents); |
| + Map<String, Uri> map = pkgfile.parse(bytes, base); |
| + return new MapPackages(map); |
| + } |
| + |
| + Map<String, List<Folder>> getPackageMap(String config) { |
| + Packages packages = createPackageMap(baseUri, config); |
| + SourceFactory factory = new SourceFactory(testResolvers, packages); |
| + return factory.packageMap; |
| + } |
| + |
| + Uri resolvePackageUri( |
| + {String uri, String config, UriResolver customResolver}) { |
| + Packages packages = createPackageMap(baseUri, config); |
| + List<UriResolver> resolvers = testResolvers.toList(); |
| + if (customResolver != null) { |
| + resolvers.add(customResolver); |
| + } |
| + SourceFactory factory = new SourceFactory(resolvers, packages); |
|
Brian Wilkerson
2015/07/01 00:43:23
I was expecting to see the SourceFactories get cre
|
| + Source source = factory.resolveUri(null, uri); |
| + return source == null ? null : source.uri; |
| + } |
| + |
| + Uri restorePackageUri( |
| + {Source source, String config, UriResolver customResolver}) { |
| + Packages packages = createPackageMap(baseUri, config); |
| + List<UriResolver> resolvers = testResolvers.toList(); |
| + if (customResolver != null) { |
| + resolvers.add(customResolver); |
| + } |
| + SourceFactory factory = new SourceFactory(resolvers, packages); |
| + return factory.restoreUri(source); |
| + } |
| + |
| + group('SourceFactoryTest', () { |
| + group('package mapping', () { |
| + group('resolveUri', () { |
| + test('URI in mapping', () { |
| + Uri uri = resolvePackageUri(config: ''' |
| +unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/ |
| +async:/home/somebody/.pub/cache/async-1.1.0/lib/ |
| +quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib |
| +''', uri: 'package:unittest/unittest.dart'); |
| + expect(uri.toString(), equals( |
| + 'file:///home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart')); |
| + }); |
| + test('URI not in mapping', () { |
| + Uri uri = resolvePackageUri( |
| + config: 'unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/', |
| + uri: 'package:foo/foo.dart'); |
| + expect(uri, isNull); |
| + }); |
| + test('Non-package URI', () { |
| + Uri uri = resolvePackageUri( |
| + config: 'unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/', |
| + uri: 'custom:custom.dart', |
| + customResolver: new CustomUriResolver()); |
| + expect(uri, CustomUriResolver.sharedUri); |
| + }); |
| + test('Invalid URI', () { |
| + // TODO(pquitslund): fix clients to handle errors appropriately |
| + // CLI: print message 'invalid package file format' |
| + // SERVER: best case tell user somehow and recover... |
| + expect(() => resolvePackageUri( |
| + config: 'foo:<:&%>', uri: 'package:foo/bar.dart'), |
| + throwsA(new isInstanceOf('FormatException'))); |
| + }); |
| + test('Valid URI that cannot be further resolved', () { |
| + Uri uri = resolvePackageUri( |
| + config: 'foo:http://www.google.com', uri: 'package:foo/bar.dart'); |
| + expect(uri, isNull); |
| + }); |
| + }); |
| + group('restoreUri', () { |
| + test('URI in mapping', () { |
| + Uri uri = restorePackageUri(config: ''' |
| +unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/ |
| +async:/home/somebody/.pub/cache/async-1.1.0/lib/ |
| +quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib |
| +''', |
| + source: new FileBasedSource(new JavaFile( |
| + '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart'))); |
| + expect(uri.toString(), equals('package:unittest/unittest.dart')); |
| + }); |
| + }); |
| + group('packageMap', () { |
| + test('non-file URIs filtered', () { |
| + Map<String, List<Folder>> map = getPackageMap(''' |
| +quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib |
| +foo:http://www.google.com |
| +'''); |
| + expect(map.keys, unorderedEquals(['quiver'])); |
| + }); |
| + }); |
| + }); |
| + }); |
| } |
| @reflectiveTest |