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

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

Issue 1228493003: Revert "Package map source factory support" [TBR]. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 5 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
« no previous file with comments | « pkg/analyzer/pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // 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
12 import 'package:analyzer/file_system/file_system.dart'; 10 import 'package:analyzer/file_system/file_system.dart';
13 import 'package:analyzer/file_system/memory_file_system.dart'; 11 import 'package:analyzer/file_system/memory_file_system.dart';
14 import 'package:analyzer/source/package_map_resolver.dart'; 12 import 'package:analyzer/source/package_map_resolver.dart';
15 import 'package:analyzer/src/generated/java_core.dart'; 13 import 'package:analyzer/src/generated/java_core.dart';
16 import 'package:analyzer/src/generated/java_engine_io.dart'; 14 import 'package:analyzer/src/generated/java_engine_io.dart';
17 import 'package:analyzer/src/generated/java_io.dart'; 15 import 'package:analyzer/src/generated/java_io.dart';
18 import 'package:analyzer/src/generated/source.dart'; 16 import 'package:analyzer/src/generated/source.dart';
19 import 'package:analyzer/src/generated/source_io.dart'; 17 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';
24 import 'package:path/path.dart'; 18 import 'package:path/path.dart';
25 import 'package:unittest/unittest.dart'; 19 import 'package:unittest/unittest.dart';
26 20
27 import '../reflective_tests.dart'; 21 import '../reflective_tests.dart';
28 import 'test_support.dart'; 22 import 'test_support.dart';
29 23
30 main() { 24 main() {
31 groupSep = ' | '; 25 groupSep = ' | ';
32 runReflectiveTests(SourceFactoryTest); 26 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);
174 } 27 }
175 28
176 @reflectiveTest 29 @reflectiveTest
177 class SourceFactoryTest { 30 class SourceFactoryTest {
178 void test_creation() { 31 void test_creation() {
179 expect(new SourceFactory([]), isNotNull); 32 expect(new SourceFactory([]), isNotNull);
180 } 33 }
181 34
182 void test_fromEncoding_invalidUri() { 35 void test_fromEncoding_invalidUri() {
183 SourceFactory factory = new SourceFactory([]); 36 SourceFactory factory = new SourceFactory([]);
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 UriResolver_SourceFactoryTest_test_fromEncoding_valid(this.encoding); 169 UriResolver_SourceFactoryTest_test_fromEncoding_valid(this.encoding);
317 170
318 @override 171 @override
319 Source resolveAbsolute(Uri uri) { 172 Source resolveAbsolute(Uri uri) {
320 if (uri.toString() == encoding) { 173 if (uri.toString() == encoding) {
321 return new TestSource(); 174 return new TestSource();
322 } 175 }
323 return null; 176 return null;
324 } 177 }
325 } 178 }
OLDNEW
« no previous file with comments | « pkg/analyzer/pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698