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

Side by Side Diff: pkg/analyzer_plugin/test/src/utilities/change_builder/change_builder_dart_test.dart

Issue 2955913002: Fixes and tests for adding library imports. (Closed)
Patch Set: Created 3 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_plugin/lib/src/utilities/change_builder/change_builder_dart.dart ('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) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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 import 'dart:async'; 5 import 'dart:async';
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; 8 import 'package:analyzer/dart/ast/standard_resolution_map.dart';
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/dart/element/type.dart'; 10 import 'package:analyzer/dart/element/type.dart';
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 expect(fileEditBuilder, new isInstanceOf<DartFileEditBuilder>()); 68 expect(fileEditBuilder, new isInstanceOf<DartFileEditBuilder>());
69 SourceFileEdit fileEdit = fileEditBuilder.fileEdit; 69 SourceFileEdit fileEdit = fileEditBuilder.fileEdit;
70 expect(fileEdit.file, path); 70 expect(fileEdit.file, path);
71 expect(fileEdit.fileStamp, timeStamp); 71 expect(fileEdit.fileStamp, timeStamp);
72 } 72 }
73 } 73 }
74 74
75 @reflectiveTest 75 @reflectiveTest
76 class DartEditBuilderImplTest extends AbstractContextTest 76 class DartEditBuilderImplTest extends AbstractContextTest
77 with BuilderTestMixin { 77 with BuilderTestMixin {
78 test_importLibraries_DP() async {
79 await _assertImportLibraries(
80 '''
81 import 'dart:aaa';
82 import 'dart:ccc';
83
84 import 'package:aaa/aaa.dart';
85 import 'package:ccc/ccc.dart';
86 ''',
87 ['dart:bbb', 'package:bbb/bbb.dart'],
88 '''
89 import 'dart:aaa';
90 import 'dart:bbb';
91 import 'dart:ccc';
92
93 import 'package:aaa/aaa.dart';
94 import 'package:bbb/bbb.dart';
95 import 'package:ccc/ccc.dart';
96 ''');
97 }
98
99 test_importLibraries_PD() async {
100 await _assertImportLibraries(
101 '''
102 import 'dart:aaa';
103 import 'dart:ccc';
104
105 import 'package:aaa/aaa.dart';
106 import 'package:ccc/ccc.dart';
107 ''',
108 ['package:bbb/bbb.dart', 'dart:bbb'],
109 '''
110 import 'dart:aaa';
111 import 'dart:bbb';
112 import 'dart:ccc';
113
114 import 'package:aaa/aaa.dart';
115 import 'package:bbb/bbb.dart';
116 import 'package:ccc/ccc.dart';
117 ''');
118 }
119
120 test_importLibrary_afterLibraryDirective_dart() async {
121 await _assertImportLibraries(
122 '''
123 library test;
124
125 class A {}
126 ''',
127 ['dart:async'],
128 '''
129 library test;
130
131 import 'dart:async';
132
133
134 class A {}
135 ''');
136 }
137
138 test_importLibrary_dart_beforeDart() async {
139 await _assertImportLibraries(
140 '''
141 import 'dart:aaa';
142 import 'dart:ccc';
143 ''',
144 ['dart:bbb'],
145 '''
146 import 'dart:aaa';
147 import 'dart:bbb';
148 import 'dart:ccc';
149 ''');
150 }
151
152 test_importLibrary_dart_beforeDart_first() async {
153 await _assertImportLibraries(
154 '''
155 import 'dart:bbb';
156 ''',
157 ['dart:aaa'],
158 '''
159 import 'dart:aaa';
160 import 'dart:bbb';
161 ''');
162 }
163
164 test_importLibrary_dart_beforePackage() async {
165 await _assertImportLibraries(
166 '''
167 import 'package:foo/foo.dart';
168 ''',
169 ['dart:async'],
170 '''
171 import 'dart:async';
172
173 import 'package:foo/foo.dart';
174 ''');
175 }
176
177 test_importLibrary_package_afterDart() async {
178 await _assertImportLibraries(
179 '''
180 import 'dart:async';
181 ''',
182 ['package:aaa/aaa.dart'],
183 '''
184 import 'dart:async';
185
186 import 'package:aaa/aaa.dart';
187 ''');
188 }
189
190 test_importLibrary_package_afterPackage() async {
191 await _assertImportLibraries(
192 '''
193 import 'package:aaa/a1.dart';
194
195 import 'foo.dart';
196 ''',
197 ['package:aaa/a2.dart'],
198 '''
199 import 'package:aaa/a1.dart';
200 import 'package:aaa/a2.dart';
201
202 import 'foo.dart';
203 ''');
204 }
205
206 test_importLibrary_package_beforePackage() async {
207 await _assertImportLibraries(
208 '''
209 import 'package:aaa/a1.dart';
210 import 'package:aaa/a3.dart';
211
212 import 'foo.dart';
213 ''',
214 ['package:aaa/a2.dart'],
215 '''
216 import 'package:aaa/a1.dart';
217 import 'package:aaa/a2.dart';
218 import 'package:aaa/a3.dart';
219
220 import 'foo.dart';
221 ''');
222 }
223
224 test_importLibrary_package_beforePackage_first() async {
225 await _assertImportLibraries(
226 '''
227 import 'package:aaa/a2.dart';
228
229 import 'foo.dart';
230 ''',
231 ['package:aaa/a1.dart'],
232 '''
233 import 'package:aaa/a1.dart';
234 import 'package:aaa/a2.dart';
235
236 import 'foo.dart';
237 ''');
238 }
239
240 test_importLibrary_package_beforeRelative() async {
241 await _assertImportLibraries(
242 '''
243 import 'foo.dart';
244 ''',
245 ['package:aaa/aaa.dart'],
246 '''
247 import 'package:aaa/aaa.dart';
248
249 import 'foo.dart';
250 ''');
251 }
252
253 test_importLibrary_relative_afterDart() async {
254 await _assertImportLibraries(
255 '''
256 import 'dart:async';
257 ''',
258 ['aaa.dart'],
259 '''
260 import 'dart:async';
261
262 import 'aaa.dart';
263 ''');
264 }
265
266 test_importLibrary_relative_afterPackage() async {
267 await _assertImportLibraries(
268 '''
269 import 'package:foo/foo.dart';
270 ''',
271 ['aaa.dart'],
272 '''
273 import 'package:foo/foo.dart';
274
275 import 'aaa.dart';
276 ''');
277 }
278
279 test_importLibrary_relative_beforeRelative() async {
280 await _assertImportLibraries(
281 '''
282 import 'dart:async';
283
284 import 'package:foo/foo.dart';
285
286 import 'aaa.dart';
287 import 'ccc.dart';
288 ''',
289 ['bbb.dart'],
290 '''
291 import 'dart:async';
292
293 import 'package:foo/foo.dart';
294
295 import 'aaa.dart';
296 import 'bbb.dart';
297 import 'ccc.dart';
298 ''');
299 }
300
301 test_importLibrary_relative_beforeRelative_first() async {
302 await _assertImportLibraries(
303 '''
304 import 'dart:async';
305
306 import 'package:foo/foo.dart';
307
308 import 'bbb.dart';
309 ''',
310 ['aaa.dart'],
311 '''
312 import 'dart:async';
313
314 import 'package:foo/foo.dart';
315
316 import 'aaa.dart';
317 import 'bbb.dart';
318 ''');
319 }
320
321 test_importLibrary_relative_last() async {
322 await _assertImportLibraries(
323 '''
324 import 'dart:async';
325
326 import 'package:foo/foo.dart';
327 ''',
328 ['aaa.dart'],
329 '''
330 import 'dart:async';
331
332 import 'package:foo/foo.dart';
333
334 import 'aaa.dart';
335 ''');
336 }
337
78 test_writeClassDeclaration_interfaces() async { 338 test_writeClassDeclaration_interfaces() async {
79 String path = provider.convertPath('/test.dart'); 339 String path = provider.convertPath('/test.dart');
80 addSource(path, 'class A {}'); 340 addSource(path, 'class A {}');
81 DartType typeA = await _getType(path, 'A'); 341 DartType typeA = await _getType(path, 'A');
82 342
83 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); 343 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver);
84 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { 344 await builder.addFileEdit(path, 1, (FileEditBuilder builder) {
85 builder.addInsertion(0, (EditBuilder builder) { 345 builder.addInsertion(0, (EditBuilder builder) {
86 (builder as DartEditBuilder) 346 (builder as DartEditBuilder)
87 .writeClassDeclaration('C', interfaces: [typeA]); 347 .writeClassDeclaration('C', interfaces: [typeA]);
(...skipping 1003 matching lines...) Expand 10 before | Expand all | Expand 10 after
1091 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { 1351 await builder.addFileEdit(path, 1, (FileEditBuilder builder) {
1092 builder.addInsertion(content.length - 1, (EditBuilder builder) { 1352 builder.addInsertion(content.length - 1, (EditBuilder builder) {
1093 (builder as DartEditBuilderImpl) 1353 (builder as DartEditBuilderImpl)
1094 .writeTypes([typeA, typeB], prefix: 'implements '); 1354 .writeTypes([typeA, typeB], prefix: 'implements ');
1095 }); 1355 });
1096 }); 1356 });
1097 SourceEdit edit = getEdit(builder); 1357 SourceEdit edit = getEdit(builder);
1098 expect(edit.replacement, equalsIgnoringWhitespace('implements A, B')); 1358 expect(edit.replacement, equalsIgnoringWhitespace('implements A, B'));
1099 } 1359 }
1100 1360
1361 Future<Null> _assertImportLibraries(
1362 String initialCode, List<String> newUris, String expectedCode) async {
1363 String path = provider.convertPath('/test.dart');
1364 addSource(path, initialCode);
1365 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver);
1366 await builder.addFileEdit(path, 1, (DartFileEditBuilder builder) {
1367 Iterable<_MockSource> sources = newUris.map((newUri) {
1368 String path = newUri.contains(':') ? null : '/$newUri';
1369 return new _MockSource(path, Uri.parse(newUri));
1370 });
1371 builder.importLibraries(sources);
1372 });
1373
1374 String resultCode = initialCode;
1375 List<SourceEdit> edits = getEdits(builder);
1376 for (SourceEdit edit in edits) {
1377 resultCode = edit.apply(resultCode);
1378 }
1379 expect(resultCode, expectedCode);
1380 }
1381
1101 Future<ClassElement> _getClassElement(String path, String name) async { 1382 Future<ClassElement> _getClassElement(String path, String name) async {
1102 UnitElementResult result = await driver.getUnitElement(path); 1383 UnitElementResult result = await driver.getUnitElement(path);
1103 return result.element.getType(name); 1384 return result.element.getType(name);
1104 } 1385 }
1105 1386
1106 Future<DartType> _getType(String path, String name) async { 1387 Future<DartType> _getType(String path, String name) async {
1107 ClassElement classElement = await _getClassElement(path, name); 1388 ClassElement classElement = await _getClassElement(path, name);
1108 return classElement.type; 1389 return classElement.type;
1109 } 1390 }
1110 } 1391 }
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
1192 CompilationUnit unit = (await driver.getResult(path))?.unit; 1473 CompilationUnit unit = (await driver.getResult(path))?.unit;
1193 ClassDeclaration classC = unit.declarations[2]; 1474 ClassDeclaration classC = unit.declarations[2];
1194 DartLinkedEditBuilderImpl builder = new DartLinkedEditBuilderImpl(null); 1475 DartLinkedEditBuilderImpl builder = new DartLinkedEditBuilderImpl(null);
1195 builder.addSuperTypesAsSuggestions(classC.element.type); 1476 builder.addSuperTypesAsSuggestions(classC.element.type);
1196 List<LinkedEditSuggestion> suggestions = builder.suggestions; 1477 List<LinkedEditSuggestion> suggestions = builder.suggestions;
1197 expect(suggestions, hasLength(4)); 1478 expect(suggestions, hasLength(4));
1198 expect(suggestions.map((s) => s.value), 1479 expect(suggestions.map((s) => s.value),
1199 unorderedEquals(['Object', 'A', 'B', 'C'])); 1480 unorderedEquals(['Object', 'A', 'B', 'C']));
1200 } 1481 }
1201 } 1482 }
1483
1484 class _MockSource implements Source {
1485 @override
1486 final String fullName;
1487
1488 @override
1489 final Uri uri;
1490
1491 _MockSource(this.fullName, this.uri);
1492
1493 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
1494 }
OLDNEW
« no previous file with comments | « pkg/analyzer_plugin/lib/src/utilities/change_builder/change_builder_dart.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698