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

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

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 4 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.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/memory_file_system.dart'; 10 import 'package:analyzer/file_system/memory_file_system.dart';
11 import 'package:analyzer/source/package_map_resolver.dart'; 11 import 'package:analyzer/source/package_map_resolver.dart';
12 import 'package:analyzer/src/generated/java_core.dart'; 12 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine, Logger;
13 import 'package:analyzer/src/generated/java_engine_io.dart'; 13 import 'package:analyzer/src/generated/java_engine_io.dart';
14 import 'package:analyzer/src/generated/java_io.dart';
15 import 'package:analyzer/src/generated/source.dart'; 14 import 'package:analyzer/src/generated/source.dart';
16 import 'package:analyzer/src/generated/source_io.dart'; 15 import 'package:analyzer/src/generated/source_io.dart';
17 import 'package:analyzer/src/generated/utilities_dart.dart' as utils; 16 import 'package:analyzer/src/generated/utilities_dart.dart' as utils;
17 import 'package:analyzer/src/source/source_resource.dart';
18 import 'package:package_config/packages.dart'; 18 import 'package:package_config/packages.dart';
19 import 'package:package_config/packages_file.dart' as pkgfile show parse; 19 import 'package:package_config/packages_file.dart' as pkgfile show parse;
20 import 'package:package_config/src/packages_impl.dart'; 20 import 'package:package_config/src/packages_impl.dart';
21 import 'package:path/path.dart'; 21 import 'package:path/path.dart' hide equals;
22 import 'package:test_reflective_loader/test_reflective_loader.dart';
22 import 'package:unittest/unittest.dart'; 23 import 'package:unittest/unittest.dart';
23 24
24 import '../reflective_tests.dart';
25 import '../utils.dart'; 25 import '../utils.dart';
26 import 'test_support.dart'; 26 import 'test_support.dart';
27 27
28 main() { 28 main() {
29 initializeTestEnvironment(); 29 initializeTestEnvironment();
30 runReflectiveTests(SourceFactoryTest); 30 defineReflectiveTests(SourceFactoryTest);
31 runPackageMapTests(); 31 runPackageMapTests();
32 } 32 }
33 33
34 Source createSource({String path, String uri}) => 34 Source createSource({String path, String uri}) =>
35 //TODO(pquitslund): find some way to pass an actual URI into source creation 35 //TODO(pquitslund): find some way to pass an actual URI into source creation
36 new MemoryResourceProvider() 36 new MemoryResourceProvider()
37 .getFile(path) 37 .getFile(path)
38 .createSource(uri != null ? Uri.parse(uri) : null); 38 .createSource(uri != null ? Uri.parse(uri) : null);
39 39
40 void runPackageMapTests() { 40 void runPackageMapTests() {
41 MemoryResourceProvider resourceProvider = new MemoryResourceProvider();
41 final Uri baseUri = new Uri.file('test/base'); 42 final Uri baseUri = new Uri.file('test/base');
42 final List<UriResolver> testResolvers = [new FileUriResolver()]; 43 final List<UriResolver> testResolvers = [
44 new ResourceUriResolver(resourceProvider)
45 ];
43 46
44 Packages createPackageMap(Uri base, String configFileContents) { 47 Packages createPackageMap(Uri base, String configFileContents) {
45 List<int> bytes = UTF8.encode(configFileContents); 48 List<int> bytes = UTF8.encode(configFileContents);
46 Map<String, Uri> map = pkgfile.parse(bytes, base); 49 Map<String, Uri> map = pkgfile.parse(bytes, base);
47 return new MapPackages(map); 50 return new MapPackages(map);
48 } 51 }
49 52
50 Map<String, List<Folder>> getPackageMap(String config) { 53 Map<String, List<Folder>> getPackageMap(String config) {
51 Packages packages = createPackageMap(baseUri, config); 54 Packages packages = createPackageMap(baseUri, config);
52 SourceFactory factory = new SourceFactory(testResolvers, packages); 55 SourceFactory factory = new SourceFactory(testResolvers, packages);
53 return factory.packageMap; 56 return factory.packageMap;
54 } 57 }
55 58
56 String resolvePackageUri( 59 String resolvePackageUri(
57 {String uri, 60 {String uri,
58 String config, 61 String config,
59 Source containingSource, 62 Source containingSource,
60 UriResolver customResolver}) { 63 UriResolver customResolver}) {
61 Packages packages = createPackageMap(baseUri, config); 64 Packages packages = createPackageMap(baseUri, config);
62 List<UriResolver> resolvers = testResolvers.toList(); 65 List<UriResolver> resolvers = testResolvers.toList();
63 if (customResolver != null) { 66 if (customResolver != null) {
64 resolvers.add(customResolver); 67 resolvers.add(customResolver);
65 } 68 }
66 SourceFactory factory = new SourceFactory(resolvers, packages); 69 SourceFactory factory = new SourceFactory(resolvers, packages);
67 Source source = factory.resolveUri(containingSource, uri); 70
68 return source != null ? source.fullName : null; 71 expect(AnalysisEngine.instance.logger, Logger.NULL);
72 var logger = new TestLogger();
73 AnalysisEngine.instance.logger = logger;
74 try {
75 Source source = factory.resolveUri(containingSource, uri);
76 expect(logger.log, []);
77 return source != null ? source.fullName : null;
78 } finally {
79 AnalysisEngine.instance.logger = Logger.NULL;
80 }
69 } 81 }
70 82
71 Uri restorePackageUri( 83 Uri restorePackageUri(
72 {Source source, String config, UriResolver customResolver}) { 84 {Source source, String config, UriResolver customResolver}) {
73 Packages packages = createPackageMap(baseUri, config); 85 Packages packages = createPackageMap(baseUri, config);
74 List<UriResolver> resolvers = testResolvers.toList(); 86 List<UriResolver> resolvers = testResolvers.toList();
75 if (customResolver != null) { 87 if (customResolver != null) {
76 resolvers.add(customResolver); 88 resolvers.add(customResolver);
77 } 89 }
78 SourceFactory factory = new SourceFactory(resolvers, packages); 90 SourceFactory factory = new SourceFactory(resolvers, packages);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 expect(uri, isNull); 127 expect(uri, isNull);
116 }); 128 });
117 test('Non-package URI', () { 129 test('Non-package URI', () {
118 var testResolver = new CustomUriResolver(uriPath: 'test_uri'); 130 var testResolver = new CustomUriResolver(uriPath: 'test_uri');
119 String uri = resolvePackageUri( 131 String uri = resolvePackageUri(
120 config: 'unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/', 132 config: 'unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/',
121 uri: 'custom:custom.dart', 133 uri: 'custom:custom.dart',
122 customResolver: testResolver); 134 customResolver: testResolver);
123 expect(uri, testResolver.uriPath); 135 expect(uri, testResolver.uriPath);
124 }); 136 });
137 test('Bad package URI', () {
138 String uri = resolvePackageUri(config: '', uri: 'package:foo');
139 expect(uri, isNull);
140 });
125 test('Invalid URI', () { 141 test('Invalid URI', () {
126 // TODO(pquitslund): fix clients to handle errors appropriately 142 // TODO(pquitslund): fix clients to handle errors appropriately
127 // CLI: print message 'invalid package file format' 143 // CLI: print message 'invalid package file format'
128 // SERVER: best case tell user somehow and recover... 144 // SERVER: best case tell user somehow and recover...
129 expect( 145 expect(
130 () => resolvePackageUri( 146 () => resolvePackageUri(
131 config: 'foo:<:&%>', uri: 'package:foo/bar.dart'), 147 config: 'foo:<:&%>', uri: 'package:foo/bar.dart'),
132 throwsA(new isInstanceOf('FormatException'))); 148 throwsA(new isInstanceOf<FormatException>()));
133 }); 149 });
134 test('Valid URI that cannot be further resolved', () { 150 test('Valid URI that cannot be further resolved', () {
135 String uri = resolvePackageUri( 151 String uri = resolvePackageUri(
136 config: 'foo:http://www.google.com', uri: 'package:foo/bar.dart'); 152 config: 'foo:http://www.google.com', uri: 'package:foo/bar.dart');
137 expect(uri, isNull); 153 expect(uri, isNull);
138 }); 154 });
139 test('Relative URIs', () { 155 test('Relative URIs', () {
140 Source containingSource = createSource( 156 Source containingSource = createSource(
141 path: '/foo/bar/baz/foo.dart', uri: 'package:foo/foo.dart'); 157 path: '/foo/bar/baz/foo.dart', uri: 'package:foo/foo.dart');
142 String uri = resolvePackageUri( 158 String uri = resolvePackageUri(
143 config: 'foo:/foo/bar/baz', 159 config: 'foo:/foo/bar/baz',
144 uri: 'bar.dart', 160 uri: 'bar.dart',
145 containingSource: containingSource); 161 containingSource: containingSource);
146 expect(uri, isNotNull); 162 expect(uri, isNotNull);
147 expect(uri, equals('/foo/bar/baz/bar.dart')); 163 expect(uri, equals('/foo/bar/baz/bar.dart'));
148 }); 164 });
149 }); 165 });
150 group('restoreUri', () { 166 group('restoreUri', () {
151 test('URI in mapping', () { 167 test('URI in mapping', () {
152 Uri uri = restorePackageUri( 168 Uri uri = restorePackageUri(
153 config: ''' 169 config: '''
154 unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/ 170 unittest:/home/somebody/.pub/cache/unittest-0.9.9/lib/
155 async:/home/somebody/.pub/cache/async-1.1.0/lib/ 171 async:/home/somebody/.pub/cache/async-1.1.0/lib/
156 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib 172 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib
157 ''', 173 ''',
158 source: new FileBasedSource(FileUtilities2.createFile( 174 source: new FileSource(resourceProvider.getFile(
159 '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart')) ); 175 '/home/somebody/.pub/cache/unittest-0.9.9/lib/unittest.dart')) );
160 expect(uri, isNotNull); 176 expect(uri, isNotNull);
161 expect(uri.toString(), equals('package:unittest/unittest.dart')); 177 expect(uri.toString(), equals('package:unittest/unittest.dart'));
162 }); 178 });
163 }); 179 });
164 group('packageMap', () { 180 group('packageMap', () {
165 test('non-file URIs filtered', () { 181 test('non-file URIs filtered', () {
166 Map<String, List<Folder>> map = getPackageMap(''' 182 Map<String, List<Folder>> map = getPackageMap('''
167 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib 183 quiver:/home/somebody/.pub/cache/quiver-1.2.1/lib
168 foo:http://www.google.com 184 foo:http://www.google.com
(...skipping 14 matching lines...) Expand all
183 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('/foo/b')), 199 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('/foo/b')),
184 isFalse); 200 isFalse);
185 // Handle odd URIs (https://github.com/dart-lang/sdk/issues/24126) 201 // Handle odd URIs (https://github.com/dart-lang/sdk/issues/24126)
186 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('')), isFalse); 202 expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('')), isFalse);
187 expect(utils.startsWith(Uri.parse(''), Uri.parse('/foo/bar')), isFalse); 203 expect(utils.startsWith(Uri.parse(''), Uri.parse('/foo/bar')), isFalse);
188 }); 204 });
189 }); 205 });
190 }); 206 });
191 } 207 }
192 208
209 class AbsoluteUriResolver extends UriResolver {
210 final MemoryResourceProvider resourceProvider;
211
212 AbsoluteUriResolver(this.resourceProvider);
213
214 @override
215 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
216 return new FileSource(
217 resourceProvider.getFile(resourceProvider.pathContext.fromUri(uri)),
218 actualUri);
219 }
220 }
221
193 class CustomUriResolver extends UriResolver { 222 class CustomUriResolver extends UriResolver {
194 String uriPath; 223 String uriPath;
195 CustomUriResolver({this.uriPath}); 224 CustomUriResolver({this.uriPath});
196 225
197 @override 226 @override
198 Source resolveAbsolute(Uri uri, [Uri actualUri]) => 227 Source resolveAbsolute(Uri uri, [Uri actualUri]) =>
199 createSource(path: uriPath); 228 createSource(path: uriPath);
200 } 229 }
201 230
202 @reflectiveTest 231 @reflectiveTest
203 class SourceFactoryTest { 232 class SourceFactoryTest {
233 MemoryResourceProvider resourceProvider = new MemoryResourceProvider();
234
204 void test_creation() { 235 void test_creation() {
205 expect(new SourceFactory([]), isNotNull); 236 expect(new SourceFactory([]), isNotNull);
206 } 237 }
207 238
208 void test_fromEncoding_invalidUri() { 239 void test_fromEncoding_invalidUri() {
209 SourceFactory factory = new SourceFactory([]); 240 SourceFactory factory = new SourceFactory([]);
210 expect(() => factory.fromEncoding("<:&%>"), 241 expect(() => factory.fromEncoding("<:&%>"), throwsArgumentError);
211 throwsA(new isInstanceOf<IllegalArgumentException>()));
212 } 242 }
213 243
214 void test_fromEncoding_noResolver() { 244 void test_fromEncoding_noResolver() {
215 SourceFactory factory = new SourceFactory([]); 245 SourceFactory factory = new SourceFactory([]);
216 expect(() => factory.fromEncoding("foo:/does/not/exist.dart"), 246 expect(() => factory.fromEncoding("foo:/does/not/exist.dart"),
217 throwsA(new isInstanceOf<IllegalArgumentException>())); 247 throwsArgumentError);
218 } 248 }
219 249
220 void test_fromEncoding_valid() { 250 void test_fromEncoding_valid() {
221 String encoding = "file:///does/not/exist.dart"; 251 String encoding = "file:///does/not/exist.dart";
222 SourceFactory factory = new SourceFactory( 252 SourceFactory factory = new SourceFactory(
223 [new UriResolver_SourceFactoryTest_test_fromEncoding_valid(encoding)]); 253 [new UriResolver_SourceFactoryTest_test_fromEncoding_valid(encoding)]);
224 expect(factory.fromEncoding(encoding), isNotNull); 254 expect(factory.fromEncoding(encoding), isNotNull);
225 } 255 }
226 256
227 void test_resolveUri_absolute() { 257 void test_resolveUri_absolute() {
228 UriResolver_absolute resolver = new UriResolver_absolute(); 258 UriResolver_absolute resolver = new UriResolver_absolute();
229 SourceFactory factory = new SourceFactory([resolver]); 259 SourceFactory factory = new SourceFactory([resolver]);
230 factory.resolveUri(null, "dart:core"); 260 factory.resolveUri(null, "dart:core");
231 expect(resolver.invoked, isTrue); 261 expect(resolver.invoked, isTrue);
232 } 262 }
233 263
234 void test_resolveUri_nonAbsolute_absolute() { 264 void test_resolveUri_nonAbsolute_absolute() {
235 SourceFactory factory = 265 SourceFactory factory =
236 new SourceFactory([new UriResolver_nonAbsolute_absolute()]); 266 new SourceFactory([new AbsoluteUriResolver(resourceProvider)]);
237 String absolutePath = "/does/not/matter.dart"; 267 String absolutePath = "/does/not/matter.dart";
238 Source containingSource = 268 Source containingSource =
239 new FileBasedSource(FileUtilities2.createFile("/does/not/exist.dart")); 269 new FileSource(resourceProvider.getFile("/does/not/exist.dart"));
240 Source result = factory.resolveUri(containingSource, absolutePath); 270 Source result = factory.resolveUri(containingSource, absolutePath);
241 expect(result.fullName, 271 expect(result.fullName,
242 FileUtilities2.createFile(absolutePath).getAbsolutePath()); 272 FileUtilities2.createFile(absolutePath).getAbsolutePath());
243 } 273 }
244 274
245 void test_resolveUri_nonAbsolute_relative() { 275 void test_resolveUri_nonAbsolute_relative() {
246 SourceFactory factory = 276 SourceFactory factory =
247 new SourceFactory([new UriResolver_nonAbsolute_relative()]); 277 new SourceFactory([new AbsoluteUriResolver(resourceProvider)]);
248 Source containingSource = 278 Source containingSource =
249 new FileBasedSource(FileUtilities2.createFile("/does/not/have.dart")); 279 new FileSource(resourceProvider.getFile("/does/not/have.dart"));
250 Source result = factory.resolveUri(containingSource, "exist.dart"); 280 Source result = factory.resolveUri(containingSource, "exist.dart");
251 expect(result.fullName, 281 expect(result.fullName,
252 FileUtilities2.createFile("/does/not/exist.dart").getAbsolutePath()); 282 FileUtilities2.createFile("/does/not/exist.dart").getAbsolutePath());
253 } 283 }
254 284
255 void test_resolveUri_nonAbsolute_relative_package() { 285 void test_resolveUri_nonAbsolute_relative_package() {
256 MemoryResourceProvider provider = new MemoryResourceProvider(); 286 MemoryResourceProvider provider = new MemoryResourceProvider();
257 Context context = provider.pathContext; 287 Context context = provider.pathContext;
258 String packagePath = 288 String packagePath =
259 context.joinAll([context.separator, 'path', 'to', 'package']); 289 context.joinAll([context.separator, 'path', 'to', 'package']);
(...skipping 15 matching lines...) Expand all
275 Source librarySource = 305 Source librarySource =
276 firstFile.createSource(Uri.parse('package:package/dir/first.dart')); 306 firstFile.createSource(Uri.parse('package:package/dir/first.dart'));
277 307
278 Source result = factory.resolveUri(librarySource, 'second.dart'); 308 Source result = factory.resolveUri(librarySource, 'second.dart');
279 expect(result, isNotNull); 309 expect(result, isNotNull);
280 expect(result.fullName, secondPath); 310 expect(result.fullName, secondPath);
281 expect(result.uri.toString(), 'package:package/dir/second.dart'); 311 expect(result.uri.toString(), 'package:package/dir/second.dart');
282 } 312 }
283 313
284 void test_restoreUri() { 314 void test_restoreUri() {
285 JavaFile file1 = FileUtilities2.createFile("/some/file1.dart"); 315 File file1 = resourceProvider.getFile("/some/file1.dart");
286 JavaFile file2 = FileUtilities2.createFile("/some/file2.dart"); 316 File file2 = resourceProvider.getFile("/some/file2.dart");
287 Source source1 = new FileBasedSource(file1); 317 Source source1 = new FileSource(file1);
288 Source source2 = new FileBasedSource(file2); 318 Source source2 = new FileSource(file2);
289 Uri expected1 = parseUriWithException("file:///my_file.dart"); 319 Uri expected1 = Uri.parse("file:///my_file.dart");
290 SourceFactory factory = 320 SourceFactory factory =
291 new SourceFactory([new UriResolver_restoreUri(source1, expected1)]); 321 new SourceFactory([new UriResolver_restoreUri(source1, expected1)]);
292 expect(factory.restoreUri(source1), same(expected1)); 322 expect(factory.restoreUri(source1), same(expected1));
293 expect(factory.restoreUri(source2), same(null)); 323 expect(factory.restoreUri(source2), same(null));
294 } 324 }
295 } 325 }
296 326
297 class UriResolver_absolute extends UriResolver { 327 class UriResolver_absolute extends UriResolver {
298 bool invoked = false; 328 bool invoked = false;
299 329
300 UriResolver_absolute(); 330 UriResolver_absolute();
301 331
302 @override 332 @override
303 Source resolveAbsolute(Uri uri, [Uri actualUri]) { 333 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
304 invoked = true; 334 invoked = true;
305 return null; 335 return null;
306 } 336 }
307 } 337 }
308 338
309 class UriResolver_nonAbsolute_absolute extends UriResolver {
310 @override
311 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
312 return new FileBasedSource(new JavaFile.fromUri(uri), actualUri);
313 }
314 }
315
316 class UriResolver_nonAbsolute_relative extends UriResolver {
317 @override
318 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
319 return new FileBasedSource(new JavaFile.fromUri(uri), actualUri);
320 }
321 }
322
323 class UriResolver_restoreUri extends UriResolver { 339 class UriResolver_restoreUri extends UriResolver {
324 Source source1; 340 Source source1;
325 Uri expected1; 341 Uri expected1;
326 UriResolver_restoreUri(this.source1, this.expected1); 342 UriResolver_restoreUri(this.source1, this.expected1);
327 343
328 @override 344 @override
329 Source resolveAbsolute(Uri uri, [Uri actualUri]) => null; 345 Source resolveAbsolute(Uri uri, [Uri actualUri]) => null;
330 346
331 @override 347 @override
332 Uri restoreAbsolute(Source source) { 348 Uri restoreAbsolute(Source source) {
(...skipping 10 matching lines...) Expand all
343 UriResolver_SourceFactoryTest_test_fromEncoding_valid(this.encoding); 359 UriResolver_SourceFactoryTest_test_fromEncoding_valid(this.encoding);
344 360
345 @override 361 @override
346 Source resolveAbsolute(Uri uri, [Uri actualUri]) { 362 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
347 if (uri.toString() == encoding) { 363 if (uri.toString() == encoding) {
348 return new TestSource(); 364 return new TestSource();
349 } 365 }
350 return null; 366 return null;
351 } 367 }
352 } 368 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698