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

Side by Side Diff: pkg/analysis_server/test/src/computer/import_elements_computer_test.dart

Issue 2986073002: Partial support for intelligent paste operation (Closed)
Patch Set: fix errors 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) 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';
6
5 import 'package:analysis_server/protocol/protocol_generated.dart'; 7 import 'package:analysis_server/protocol/protocol_generated.dart';
6 import 'package:analysis_server/src/computer/import_elements_computer.dart'; 8 import 'package:analysis_server/src/computer/import_elements_computer.dart';
7 import 'package:analyzer/dart/analysis/results.dart'; 9 import 'package:analyzer/src/dart/analysis/driver.dart';
8 import 'package:analyzer_plugin/protocol/protocol_common.dart'; 10 import 'package:analyzer_plugin/protocol/protocol_common.dart';
11 import 'package:front_end/src/base/source.dart';
9 import 'package:test/test.dart'; 12 import 'package:test/test.dart';
10 import 'package:test_reflective_loader/test_reflective_loader.dart'; 13 import 'package:test_reflective_loader/test_reflective_loader.dart';
11 14
12 import '../../abstract_context.dart'; 15 import '../../abstract_context.dart';
13 16
14 main() { 17 main() {
15 defineReflectiveSuite(() { 18 defineReflectiveSuite(() {
16 defineReflectiveTests(ImportElementsComputerTest); 19 defineReflectiveTests(ImportElementsComputerTest);
17 }); 20 });
18 } 21 }
19 22
20 /**
21 * Tests that the [ImportElementsComputer] will correctly update imports. The
22 * tests are generally labeled based on the kind of import in the source (from
23 * which the text was copied) and the kind of import in the target (into which
24 * the text was pasted). Kinds are a combination of "prefix", "hide", "show" and
25 * "deferred", or "bare" when there are none of the previous, or "none" when
26 * there is no import.
27 */
28 @reflectiveTest 23 @reflectiveTest
29 class ImportElementsComputerTest extends AbstractContextTest { 24 class ImportElementsComputerTest extends AbstractContextTest {
30 String targetPath; 25 String path;
31 String targetCode; 26 String originalContent;
32 27 ImportElementsComputer computer;
33 ResolveResult result; 28 SourceFileEdit sourceFileEdit;
34 29
35 setUp() async { 30 void assertChanges(String expectedContent) {
31 String resultCode =
32 SourceEdit.applySequence(originalContent, sourceFileEdit.edits);
33 expect(resultCode, expectedContent);
34 }
35
36 void assertNoChanges() {
37 expect(sourceFileEdit.edits, isEmpty);
38 }
39
40 Future<Null> computeChanges(List<ImportedElements> importedElements) async {
41 SourceChange change = await computer.createEdits(importedElements);
42 expect(change, isNotNull);
43 List<SourceFileEdit> edits = change.edits;
44 expect(edits, hasLength(1));
45 sourceFileEdit = edits[0];
46 expect(sourceFileEdit, isNotNull);
47 }
48
49 Future<Null> createBuilder(String content) async {
50 originalContent = content;
51 provider.newFile(path, content);
52 AnalysisResult result = await driver.getResult(path);
53 computer = new ImportElementsComputer(provider, result);
54 }
55
56 void setUp() {
36 super.setUp(); 57 super.setUp();
37 packageMap['p'] = [provider.newFolder(provider.convertPath('/p/lib'))]; 58 path = provider.convertPath('/test.dart');
38 targetPath = provider.convertPath('/p/lib/target.dart'); 59 }
39 targetCode = ''' 60
40 main() {} 61 test_createEdits_addImport_noPrefix() async {
41 '''; 62 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
42 provider.newFile(targetPath, targetCode); 63 await createBuilder('''
43 result = await driver.getResult(targetPath); 64 import 'package:pkg/foo.dart' as foo;
44 } 65 ''');
45 66 await computeChanges(<ImportedElements>[
46 @failingTest 67 new ImportedElements(fooSource.fullName, '', <String>['A'])
47 test_bare_none() { 68 ]);
48 List<ImportedElements> elements = <ImportedElements>[ 69 assertChanges('''
49 new ImportedElements( 70 import 'package:pkg/foo.dart' as foo;
50 provider.convertPath('/p/lib/a.dart'), '', <String>['A']), 71 import 'package:pkg/foo.dart';
51 ]; 72 ''');
52 List<SourceEdit> edits = _computeEditsFor(elements); 73 }
53 expect(edits, hasLength(1)); 74
54 SourceEdit edit = edits[0]; 75 test_createEdits_addImport_prefix() async {
55 expect(edit, isNotNull); 76 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
56 expect(edit.offset, 0); 77 await createBuilder('''
57 expect(edit.length, 0); 78 import 'package:pkg/foo.dart';
58 expect(edit.apply(targetCode), """ 79 ''');
59 import 'source.dart'; 80 await computeChanges(<ImportedElements>[
60 81 new ImportedElements(fooSource.fullName, 'foo', <String>['A'])
61 main() {} 82 ]);
62 """); 83 assertChanges('''
63 } 84 import 'package:pkg/foo.dart';
64 85 import 'package:pkg/foo.dart' as foo;
65 test_none_none() { 86 ''');
66 List<ImportedElements> elements = <ImportedElements>[]; 87 }
67 List<SourceEdit> edits = _computeEditsFor(elements); 88
68 expect(edits, hasLength(0)); 89 test_createEdits_addShow_multipleNames() async {
69 } 90 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
70 91 await createBuilder('''
71 List<SourceEdit> _computeEditsFor(List<ImportedElements> elements) { 92 import 'package:pkg/foo.dart' show B;
72 ImportElementsComputer computer = 93 import 'package:pkg/foo.dart' as foo;
73 new ImportElementsComputer(result, targetPath, elements); 94 ''');
74 return computer.compute(); 95 await computeChanges(<ImportedElements>[
96 new ImportedElements(fooSource.fullName, '', <String>['A', 'C'])
97 ]);
98 assertChanges('''
99 import 'package:pkg/foo.dart' show B, A, C;
100 import 'package:pkg/foo.dart' as foo;
101 ''');
102 }
103
104 test_createEdits_addShow_removeHide() async {
105 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
106 await createBuilder('''
107 import 'package:pkg/foo.dart' show A, B hide C, D;
108 ''');
109 await computeChanges(<ImportedElements>[
110 new ImportedElements(fooSource.fullName, '', <String>['C'])
111 ]);
112 assertChanges('''
113 import 'package:pkg/foo.dart' show A, B, C hide D;
114 ''');
115 }
116
117 test_createEdits_addShow_singleName_noPrefix() async {
118 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
119 await createBuilder('''
120 import 'package:pkg/foo.dart' show B;
121 ''');
122 await computeChanges(<ImportedElements>[
123 new ImportedElements(fooSource.fullName, '', <String>['A'])
124 ]);
125 assertChanges('''
126 import 'package:pkg/foo.dart' show B, A;
127 ''');
128 }
129
130 test_createEdits_addShow_singleName_prefix() async {
131 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
132 await createBuilder('''
133 import 'package:pkg/foo.dart' show C;
134 import 'package:pkg/foo.dart' as foo show B;
135 ''');
136 await computeChanges(<ImportedElements>[
137 new ImportedElements(fooSource.fullName, 'foo', <String>['A'])
138 ]);
139 assertChanges('''
140 import 'package:pkg/foo.dart' show C;
141 import 'package:pkg/foo.dart' as foo show B, A;
142 ''');
143 }
144
145 test_createEdits_alreadyImported_noCombinators() async {
146 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
147 await createBuilder('''
148 import 'package:pkg/foo.dart';
149 ''');
150 await computeChanges(<ImportedElements>[
151 new ImportedElements(fooSource.fullName, '', <String>['A', 'B'])
152 ]);
153 assertNoChanges();
154 }
155
156 test_createEdits_alreadyImported_withPrefix() async {
157 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
158 await createBuilder('''
159 import 'package:pkg/foo.dart' as foo;
160 ''');
161 await computeChanges(<ImportedElements>[
162 new ImportedElements(fooSource.fullName, 'foo', <String>['A', 'B'])
163 ]);
164 assertNoChanges();
165 }
166
167 test_createEdits_alreadyImported_withShow() async {
168 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
169 await createBuilder('''
170 import 'package:pkg/foo.dart' show A;
171 ''');
172 await computeChanges(<ImportedElements>[
173 new ImportedElements(fooSource.fullName, '', <String>['A'])
174 ]);
175 assertNoChanges();
176 }
177
178 test_createEdits_noElements() async {
179 await createBuilder('');
180 await computeChanges(<ImportedElements>[]);
181 assertNoChanges();
182 }
183
184 test_createEdits_removeHide_firstInCombinator() async {
185 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
186 await createBuilder('''
187 import 'package:pkg/foo.dart' hide A, B, C;
188 ''');
189 await computeChanges(<ImportedElements>[
190 new ImportedElements(fooSource.fullName, '', <String>['A'])
191 ]);
192 assertChanges('''
193 import 'package:pkg/foo.dart' hide B, C;
194 ''');
195 }
196
197 test_createEdits_removeHide_lastInCombinator() async {
198 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
199 await createBuilder('''
200 import 'package:pkg/foo.dart' hide A, B, C;
201 ''');
202 await computeChanges(<ImportedElements>[
203 new ImportedElements(fooSource.fullName, '', <String>['C'])
204 ]);
205 assertChanges('''
206 import 'package:pkg/foo.dart' hide A, B;
207 ''');
208 }
209
210 test_createEdits_removeHide_middleInCombinator() async {
211 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
212 await createBuilder('''
213 import 'package:pkg/foo.dart' hide A, B, C;
214 ''');
215 await computeChanges(<ImportedElements>[
216 new ImportedElements(fooSource.fullName, '', <String>['B'])
217 ]);
218 assertChanges('''
219 import 'package:pkg/foo.dart' hide A, C;
220 ''');
221 }
222
223 test_createEdits_removeHide_multipleCombinators() async {
224 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
225 await createBuilder('''
226 import 'package:pkg/foo.dart' hide A, B, C hide A, B, C;
227 ''');
228 await computeChanges(<ImportedElements>[
229 new ImportedElements(fooSource.fullName, '', <String>['B'])
230 ]);
231 assertChanges('''
232 import 'package:pkg/foo.dart' hide A, C hide A, C;
233 ''');
234 }
235
236 test_createEdits_removeHide_multipleNames() async {
237 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
238 await createBuilder('''
239 import 'package:pkg/foo.dart' hide A, B, C hide D, E, F hide G, H, I;
240 ''');
241 await computeChanges(<ImportedElements>[
242 new ImportedElements(fooSource.fullName, '', <String>['A', 'E', 'I'])
243 ]);
244 assertChanges('''
245 import 'package:pkg/foo.dart' hide B, C hide D, F hide G, H;
246 ''');
247 }
248
249 test_createEdits_removeHideCombinator_first() async {
250 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
251 await createBuilder('''
252 import 'package:pkg/foo.dart' hide A hide B hide C;
253 ''');
254 await computeChanges(<ImportedElements>[
255 new ImportedElements(fooSource.fullName, '', <String>['A'])
256 ]);
257 assertChanges('''
258 import 'package:pkg/foo.dart' hide B hide C;
259 ''');
260 }
261
262 test_createEdits_removeHideCombinator_last() async {
263 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
264 await createBuilder('''
265 import 'package:pkg/foo.dart' hide A hide B hide C;
266 ''');
267 await computeChanges(<ImportedElements>[
268 new ImportedElements(fooSource.fullName, '', <String>['C'])
269 ]);
270 assertChanges('''
271 import 'package:pkg/foo.dart' hide A hide B;
272 ''');
273 }
274
275 test_createEdits_removeHideCombinator_middle() async {
276 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
277 await createBuilder('''
278 import 'package:pkg/foo.dart' hide A hide B hide C;
279 ''');
280 await computeChanges(<ImportedElements>[
281 new ImportedElements(fooSource.fullName, '', <String>['B'])
282 ]);
283 assertChanges('''
284 import 'package:pkg/foo.dart' hide A hide C;
285 ''');
286 }
287
288 test_createEdits_removeHideCombinator_only() async {
289 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
290 await createBuilder('''
291 import 'package:pkg/foo.dart' hide A;
292 ''');
293 await computeChanges(<ImportedElements>[
294 new ImportedElements(fooSource.fullName, '', <String>['A'])
295 ]);
296 assertChanges('''
297 import 'package:pkg/foo.dart';
298 ''');
299 }
300
301 test_createEdits_removeHideCombinator_only_multiple() async {
302 Source fooSource = addPackageSource('pkg', 'foo.dart', '');
303 await createBuilder('''
304 import 'package:pkg/foo.dart' hide A, B;
305 ''');
306 await computeChanges(<ImportedElements>[
307 new ImportedElements(fooSource.fullName, '', <String>['A', 'B'])
308 ]);
309 assertChanges('''
310 import 'package:pkg/foo.dart';
311 ''');
75 } 312 }
76 } 313 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698