| 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:analyzer/src/generated/utilities_dart.dart' as utils; |
| 21 import 'package:package_config/packages.dart'; |
| 22 import 'package:package_config/packages_file.dart' as pkgfile show parse; |
| 23 import 'package:package_config/src/packages_impl.dart'; |
| 18 import 'package:path/path.dart'; | 24 import 'package:path/path.dart'; |
| 19 import 'package:unittest/unittest.dart'; | 25 import 'package:unittest/unittest.dart'; |
| 20 | 26 |
| 21 import '../reflective_tests.dart'; | 27 import '../reflective_tests.dart'; |
| 22 import 'test_support.dart'; | 28 import 'test_support.dart'; |
| 23 | 29 |
| 24 main() { | 30 main() { |
| 25 groupSep = ' | '; | 31 groupSep = ' | '; |
| 26 runReflectiveTests(SourceFactoryTest); | 32 runReflectiveTests(SourceFactoryTest); |
| 33 runPackageMapTests(); |
| 34 } |
| 35 |
| 36 void runPackageMapTests() { |
| 37 final Uri baseUri = new Uri.file('test/base'); |
| 38 final List<UriResolver> testResolvers = [new FileUriResolver()]; |
| 39 |
| 40 Packages createPackageMap(Uri base, String configFileContents) { |
| 41 List<int> bytes = UTF8.encode(configFileContents); |
| 42 Map<String, Uri> map = pkgfile.parse(bytes, base); |
| 43 return new MapPackages(map); |
| 44 } |
| 45 |
| 46 Map<String, List<Folder>> getPackageMap(String config) { |
| 47 Packages packages = createPackageMap(baseUri, config); |
| 48 SourceFactory factory = new SourceFactory(testResolvers, packages); |
| 49 return factory.packageMap; |
| 50 } |
| 51 |
| 52 String resolvePackageUri({String uri, String config, Source containingSource, |
| 53 UriResolver customResolver}) { |
| 54 Packages packages = createPackageMap(baseUri, config); |
| 55 List<UriResolver> resolvers = testResolvers.toList(); |
| 56 if (customResolver != null) { |
| 57 resolvers.add(customResolver); |
| 58 } |
| 59 SourceFactory factory = new SourceFactory(resolvers, packages); |
| 60 Source source = factory.resolveUri(containingSource, uri); |
| 61 return source != null ? source.fullName : null; |
| 62 } |
| 63 |
| 64 Uri restorePackageUri( |
| 65 {Source source, String config, UriResolver customResolver}) { |
| 66 Packages packages = createPackageMap(baseUri, config); |
| 67 List<UriResolver> resolvers = testResolvers.toList(); |
| 68 if (customResolver != null) { |
| 69 resolvers.add(customResolver); |
| 70 } |
| 71 SourceFactory factory = new SourceFactory(resolvers, packages); |
| 72 return factory.restoreUri(source); |
| 73 } |
| 74 |
| 75 group('SourceFactoryTest', () { |
| 76 group('package mapping', () { |
| 77 group('resolveUri', () { |
| 78 test('URI in mapping', () { |
| 79 String uri = resolvePackageUri(config: ''' |
| 80 unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/ |
| 81 async:/home/somebody/.pub/cache/async-1.1.0/lib/ |
| 82 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib |
| 83 ''', uri: 'package:unittest/unittest.dart'); |
| 84 expect(uri, equals( |
| 85 '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart')); |
| 86 }); |
| 87 test('URI not in mapping', () { |
| 88 String uri = resolvePackageUri( |
| 89 config: 'unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/', |
| 90 uri: 'package:foo/foo.dart'); |
| 91 expect(uri, isNull); |
| 92 }); |
| 93 test('Non-package URI', () { |
| 94 var testResolver = new CustomUriResolver(uriPath: 'test_uri'); |
| 95 String uri = resolvePackageUri( |
| 96 config: 'unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/', |
| 97 uri: 'custom:custom.dart', |
| 98 customResolver: testResolver); |
| 99 expect(uri, testResolver.uriPath); |
| 100 }); |
| 101 test('Invalid URI', () { |
| 102 // TODO(pquitslund): fix clients to handle errors appropriately |
| 103 // CLI: print message 'invalid package file format' |
| 104 // SERVER: best case tell user somehow and recover... |
| 105 expect(() => resolvePackageUri( |
| 106 config: 'foo:<:&%>', uri: 'package:foo/bar.dart'), |
| 107 throwsA(new isInstanceOf('FormatException'))); |
| 108 }); |
| 109 test('Valid URI that cannot be further resolved', () { |
| 110 String uri = resolvePackageUri( |
| 111 config: 'foo:http://www.google.com', uri: 'package:foo/bar.dart'); |
| 112 expect(uri, isNull); |
| 113 }); |
| 114 test('Relative URIs', () { |
| 115 Source containingSource = createSource( |
| 116 path: '/foo/bar/baz/foo.dart', uri: 'package:foo/foo.dart'); |
| 117 String uri = resolvePackageUri( |
| 118 config: 'foo:/foo/bar/baz', |
| 119 uri: 'bar.dart', |
| 120 containingSource: containingSource); |
| 121 expect(uri, isNotNull); |
| 122 expect(uri, equals('/foo/bar/baz/bar.dart')); |
| 123 }); |
| 124 }); |
| 125 group('restoreUri', () { |
| 126 test('URI in mapping', () { |
| 127 Uri uri = restorePackageUri(config: ''' |
| 128 unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/ |
| 129 async:/home/somebody/.pub/cache/async-1.1.0/lib/ |
| 130 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib |
| 131 ''', |
| 132 source: new FileBasedSource(FileUtilities2.createFile( |
| 133 '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart'))
); |
| 134 expect(uri, isNotNull); |
| 135 expect(uri.toString(), equals('package:unittest/unittest.dart')); |
| 136 }); |
| 137 }); |
| 138 group('packageMap', () { |
| 139 test('non-file URIs filtered', () { |
| 140 Map<String, List<Folder>> map = getPackageMap(''' |
| 141 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib |
| 142 foo:http://www.google.com |
| 143 '''); |
| 144 expect(map.keys, unorderedEquals(['quiver'])); |
| 145 }); |
| 146 }); |
| 147 }); |
| 148 }); |
| 149 |
| 150 group('URI utils', () { |
| 151 group('URI', () { |
| 152 test('startsWith', () { |
| 153 expect(utils.startsWith(Uri.parse('/foo/bar/'), Uri.parse('/foo/')), |
| 154 isTrue); |
| 155 expect(utils.startsWith(Uri.parse('/foo/bar/'), Uri.parse('/foo/bar/')), |
| 156 isTrue); |
| 157 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('/foo/b')), |
| 158 isFalse); |
| 159 }); |
| 160 }); |
| 161 }); |
| 162 } |
| 163 |
| 164 Source createSource({String path, String uri}) => new MemoryResourceProvider() |
| 165 .getFile(path) |
| 166 .createSource(uri != null ? Uri.parse(uri) : null); |
| 167 |
| 168 class CustomUriResolver extends UriResolver { |
| 169 String uriPath; |
| 170 CustomUriResolver({this.uriPath}); |
| 171 |
| 172 @override |
| 173 Source resolveAbsolute(Uri uri) => createSource(path: uriPath); |
| 27 } | 174 } |
| 28 | 175 |
| 29 @reflectiveTest | 176 @reflectiveTest |
| 30 class SourceFactoryTest { | 177 class SourceFactoryTest { |
| 31 void test_creation() { | 178 void test_creation() { |
| 32 expect(new SourceFactory([]), isNotNull); | 179 expect(new SourceFactory([]), isNotNull); |
| 33 } | 180 } |
| 34 | 181 |
| 35 void test_fromEncoding_invalidUri() { | 182 void test_fromEncoding_invalidUri() { |
| 36 SourceFactory factory = new SourceFactory([]); | 183 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); | 316 UriResolver_SourceFactoryTest_test_fromEncoding_valid(this.encoding); |
| 170 | 317 |
| 171 @override | 318 @override |
| 172 Source resolveAbsolute(Uri uri) { | 319 Source resolveAbsolute(Uri uri) { |
| 173 if (uri.toString() == encoding) { | 320 if (uri.toString() == encoding) { |
| 174 return new TestSource(); | 321 return new TestSource(); |
| 175 } | 322 } |
| 176 return null; | 323 return null; |
| 177 } | 324 } |
| 178 } | 325 } |
| OLD | NEW |