Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 // This code was auto-generated, is not intended to be edited, and is subject to | 5 // This code was auto-generated, is not intended to be edited, and is subject to |
| 6 // significant change. Please see the README file for more information. | 6 // significant change. Please see the README file for more information. |
| 7 | 7 |
| 8 library analyzer.test.generated.source_factory; | 8 library analyzer.test.generated.source_factory; |
| 9 | 9 |
| 10 import 'dart:convert'; | |
| 11 | |
| 10 import 'package:analyzer/file_system/file_system.dart'; | 12 import 'package:analyzer/file_system/file_system.dart'; |
| 11 import 'package:analyzer/file_system/memory_file_system.dart'; | 13 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 12 import 'package:analyzer/source/package_map_resolver.dart'; | 14 import 'package:analyzer/source/package_map_resolver.dart'; |
| 13 import 'package:analyzer/src/generated/java_core.dart'; | 15 import 'package:analyzer/src/generated/java_core.dart'; |
| 14 import 'package:analyzer/src/generated/java_engine_io.dart'; | 16 import 'package:analyzer/src/generated/java_engine_io.dart'; |
| 15 import 'package:analyzer/src/generated/java_io.dart'; | 17 import 'package:analyzer/src/generated/java_io.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; | 18 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer/src/generated/source_io.dart'; | 19 import 'package:analyzer/src/generated/source_io.dart'; |
| 20 import 'package:package_config/packages.dart'; | |
| 21 import 'package:package_config/packages_file.dart' as pkgfile show parse; | |
| 22 import 'package:package_config/src/packages_impl.dart'; | |
| 18 import 'package:path/path.dart'; | 23 import 'package:path/path.dart'; |
| 19 import 'package:unittest/unittest.dart'; | 24 import 'package:unittest/unittest.dart'; |
| 20 | 25 |
| 21 import '../reflective_tests.dart'; | 26 import '../reflective_tests.dart'; |
| 22 import 'test_support.dart'; | 27 import 'test_support.dart'; |
| 23 | 28 |
| 24 main() { | 29 main() { |
| 25 groupSep = ' | '; | 30 groupSep = ' | '; |
| 26 runReflectiveTests(SourceFactoryTest); | 31 runReflectiveTests(SourceFactoryTest); |
| 32 runPackageMapTests(); | |
| 33 } | |
| 34 | |
| 35 class CustomUriResolver extends UriResolver { | |
| 36 static final Source sharedSource = | |
| 37 new NonExistingSource(null, sharedUri, null); | |
| 38 static final Uri sharedUri = new Uri(); | |
| 39 | |
| 40 @override | |
| 41 Source resolveAbsolute(Uri uri) => sharedSource; | |
| 42 } | |
| 43 | |
| 44 void runPackageMapTests() { | |
| 45 final Uri baseUri = new Uri.file('test/base'); | |
| 46 final List<UriResolver> testResolvers = [new FileUriResolver()]; | |
| 47 | |
| 48 Packages createPackageMap(Uri base, String configFileContents) { | |
| 49 List<int> bytes = UTF8.encode(configFileContents); | |
| 50 Map<String, Uri> map = pkgfile.parse(bytes, base); | |
| 51 return new MapPackages(map); | |
| 52 } | |
| 53 | |
| 54 Map<String, List<Folder>> getPackageMap(String config) { | |
| 55 Packages packages = createPackageMap(baseUri, config); | |
| 56 SourceFactory factory = new SourceFactory(testResolvers, packages); | |
| 57 return factory.packageMap; | |
| 58 } | |
| 59 | |
| 60 Uri resolvePackageUri( | |
| 61 {String uri, String config, UriResolver customResolver}) { | |
| 62 Packages packages = createPackageMap(baseUri, config); | |
| 63 List<UriResolver> resolvers = testResolvers.toList(); | |
| 64 if (customResolver != null) { | |
| 65 resolvers.add(customResolver); | |
| 66 } | |
| 67 SourceFactory factory = new SourceFactory(resolvers, packages); | |
|
Brian Wilkerson
2015/07/01 00:43:23
I was expecting to see the SourceFactories get cre
| |
| 68 Source source = factory.resolveUri(null, uri); | |
| 69 return source == null ? null : source.uri; | |
| 70 } | |
| 71 | |
| 72 Uri restorePackageUri( | |
| 73 {Source source, String config, UriResolver customResolver}) { | |
| 74 Packages packages = createPackageMap(baseUri, config); | |
| 75 List<UriResolver> resolvers = testResolvers.toList(); | |
| 76 if (customResolver != null) { | |
| 77 resolvers.add(customResolver); | |
| 78 } | |
| 79 SourceFactory factory = new SourceFactory(resolvers, packages); | |
| 80 return factory.restoreUri(source); | |
| 81 } | |
| 82 | |
| 83 group('SourceFactoryTest', () { | |
| 84 group('package mapping', () { | |
| 85 group('resolveUri', () { | |
| 86 test('URI in mapping', () { | |
| 87 Uri uri = resolvePackageUri(config: ''' | |
| 88 unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/ | |
| 89 async:/home/somebody/.pub/cache/async-1.1.0/lib/ | |
| 90 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib | |
| 91 ''', uri: 'package:unittest/unittest.dart'); | |
| 92 expect(uri.toString(), equals( | |
| 93 'file:///home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart ')); | |
| 94 }); | |
| 95 test('URI not in mapping', () { | |
| 96 Uri uri = resolvePackageUri( | |
| 97 config: 'unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/', | |
| 98 uri: 'package:foo/foo.dart'); | |
| 99 expect(uri, isNull); | |
| 100 }); | |
| 101 test('Non-package URI', () { | |
| 102 Uri uri = resolvePackageUri( | |
| 103 config: 'unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/', | |
| 104 uri: 'custom:custom.dart', | |
| 105 customResolver: new CustomUriResolver()); | |
| 106 expect(uri, CustomUriResolver.sharedUri); | |
| 107 }); | |
| 108 test('Invalid URI', () { | |
| 109 // TODO(pquitslund): fix clients to handle errors appropriately | |
| 110 // CLI: print message 'invalid package file format' | |
| 111 // SERVER: best case tell user somehow and recover... | |
| 112 expect(() => resolvePackageUri( | |
| 113 config: 'foo:<:&%>', uri: 'package:foo/bar.dart'), | |
| 114 throwsA(new isInstanceOf('FormatException'))); | |
| 115 }); | |
| 116 test('Valid URI that cannot be further resolved', () { | |
| 117 Uri uri = resolvePackageUri( | |
| 118 config: 'foo:http://www.google.com', uri: 'package:foo/bar.dart'); | |
| 119 expect(uri, isNull); | |
| 120 }); | |
| 121 }); | |
| 122 group('restoreUri', () { | |
| 123 test('URI in mapping', () { | |
| 124 Uri uri = restorePackageUri(config: ''' | |
| 125 unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/ | |
| 126 async:/home/somebody/.pub/cache/async-1.1.0/lib/ | |
| 127 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib | |
| 128 ''', | |
| 129 source: new FileBasedSource(new JavaFile( | |
| 130 '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart')) ); | |
| 131 expect(uri.toString(), equals('package:unittest/unittest.dart')); | |
| 132 }); | |
| 133 }); | |
| 134 group('packageMap', () { | |
| 135 test('non-file URIs filtered', () { | |
| 136 Map<String, List<Folder>> map = getPackageMap(''' | |
| 137 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib | |
| 138 foo:http://www.google.com | |
| 139 '''); | |
| 140 expect(map.keys, unorderedEquals(['quiver'])); | |
| 141 }); | |
| 142 }); | |
| 143 }); | |
| 144 }); | |
| 27 } | 145 } |
| 28 | 146 |
| 29 @reflectiveTest | 147 @reflectiveTest |
| 30 class SourceFactoryTest { | 148 class SourceFactoryTest { |
| 31 void test_creation() { | 149 void test_creation() { |
| 32 expect(new SourceFactory([]), isNotNull); | 150 expect(new SourceFactory([]), isNotNull); |
| 33 } | 151 } |
| 34 | 152 |
| 35 void test_fromEncoding_invalidUri() { | 153 void test_fromEncoding_invalidUri() { |
| 36 SourceFactory factory = new SourceFactory([]); | 154 SourceFactory factory = new SourceFactory([]); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 UriResolver_SourceFactoryTest_test_fromEncoding_valid(this.encoding); | 287 UriResolver_SourceFactoryTest_test_fromEncoding_valid(this.encoding); |
| 170 | 288 |
| 171 @override | 289 @override |
| 172 Source resolveAbsolute(Uri uri) { | 290 Source resolveAbsolute(Uri uri) { |
| 173 if (uri.toString() == encoding) { | 291 if (uri.toString() == encoding) { |
| 174 return new TestSource(); | 292 return new TestSource(); |
| 175 } | 293 } |
| 176 return null; | 294 return null; |
| 177 } | 295 } |
| 178 } | 296 } |
| OLD | NEW |