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

Side by Side Diff: pkg/analysis_server/test/services/correction/assist_test.dart

Issue 706263003: Issue 20827. Add imports as needed the 'Add type annotation' Quick Assist. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « pkg/analysis_server/test/mock_sdk.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) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 test.services.correction.assist; 5 library test.services.correction.assist;
6 6
7 import 'package:analysis_server/src/protocol.dart'; 7 import 'package:analysis_server/src/protocol.dart';
8 import 'package:analysis_server/src/services/correction/assist.dart'; 8 import 'package:analysis_server/src/services/correction/assist.dart';
9 import 'package:analysis_server/src/services/index/index.dart'; 9 import 'package:analysis_server/src/services/index/index.dart';
10 import 'package:analysis_server/src/services/index/local_memory_index.dart'; 10 import 'package:analysis_server/src/services/index/local_memory_index.dart';
11 import 'package:analysis_server/src/services/search/search_engine_internal.dart' ; 11 import 'package:analysis_server/src/services/search/search_engine_internal.dart' ;
12 import 'package:analyzer/src/generated/source.dart';
12 import 'package:unittest/unittest.dart'; 13 import 'package:unittest/unittest.dart';
13 14
14 import '../../abstract_single_unit.dart'; 15 import '../../abstract_single_unit.dart';
15 import '../../reflective_tests.dart'; 16 import '../../reflective_tests.dart';
16 17
17 18
18 main() { 19 main() {
19 groupSep = ' | '; 20 groupSep = ' | ';
20 runReflectiveTests(AssistProcessorTest); 21 runReflectiveTests(AssistProcessorTest);
21 } 22 }
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 '''); 182 ''');
182 // on "for" 183 // on "for"
183 assertHasAssistAt('for (', AssistKind.ADD_TYPE_ANNOTATION, ''' 184 assertHasAssistAt('for (', AssistKind.ADD_TYPE_ANNOTATION, '''
184 main(List<String> items) { 185 main(List<String> items) {
185 for (String item in items) { 186 for (String item in items) {
186 } 187 }
187 } 188 }
188 '''); 189 ''');
189 } 190 }
190 191
192 void test_addTypeAnnotation_declaredIdentifier_OK_addImport_dartUri() {
193 addSource('/my_lib.dart', r'''
194 import 'dart:async';
195 List<Future<int>> getFutures() => null;
196 ''');
197 _indexTestUnit('''
198 import 'my_lib.dart';
199 main() {
200 for (var future in getFutures()) {
201 }
202 }
203 ''');
204 assertHasAssistAt('future in', AssistKind.ADD_TYPE_ANNOTATION, '''
205 import 'my_lib.dart';
206 import 'dart:async';
207 main() {
208 for (Future<int> future in getFutures()) {
209 }
210 }
211 ''');
212 }
213
191 void test_addTypeAnnotation_declaredIdentifier_OK_final() { 214 void test_addTypeAnnotation_declaredIdentifier_OK_final() {
192 _indexTestUnit(''' 215 _indexTestUnit('''
193 main(List<String> items) { 216 main(List<String> items) {
194 for (final item in items) { 217 for (final item in items) {
195 } 218 }
196 } 219 }
197 '''); 220 ''');
198 assertHasAssistAt('item in', AssistKind.ADD_TYPE_ANNOTATION, ''' 221 assertHasAssistAt('item in', AssistKind.ADD_TYPE_ANNOTATION, '''
199 main(List<String> items) { 222 main(List<String> items) {
200 for (final String item in items) { 223 for (final String item in items) {
201 } 224 }
202 } 225 }
203 '''); 226 ''');
204 } 227 }
205 228
229 void test_addTypeAnnotation_local_OK_addImport_dartUri() {
230 addSource('/my_lib.dart', r'''
231 import 'dart:async';
232 Future<int> getFutureInt() => null;
233 ''');
234 _indexTestUnit('''
235 import 'my_lib.dart';
236 main() {
237 var v = getFutureInt();
238 }
239 ''');
240 assertHasAssistAt('v =', AssistKind.ADD_TYPE_ANNOTATION, '''
241 import 'my_lib.dart';
242 import 'dart:async';
243 main() {
244 Future<int> v = getFutureInt();
245 }
246 ''');
247 }
248
249 void test_addTypeAnnotation_local_OK_addImport_notLibraryUnit() {
250 // prepare library
251 addSource('/my_lib.dart', r'''
252 import 'dart:async';
253 Future<int> getFutureInt() => null;
254 ''');
255 // prepare code
256 String appCode = r'''
257 library my_app;
258 import 'my_lib.dart';
259 part 'test.dart';
260 ''';
261 testCode = r'''
262 part of my_app;
263 main() {
264 var v = getFutureInt();
265 }
266 ''';
267 // add sources
268 Source appSource = addSource('/app.dart', appCode);
269 testSource = addSource('/test.dart', testCode);
270 // resolve
271 context.resolveCompilationUnit2(appSource, appSource);
272 testUnit = context.resolveCompilationUnit2(testSource, appSource);
273 assertNoErrorsInSource(testSource);
274 testUnitElement = testUnit.element;
275 testLibraryElement = testUnitElement.library;
276 // prepare the assist
277 offset = findOffset('v = ');
278 assist = _assertHasAssist(AssistKind.ADD_TYPE_ANNOTATION);
279 change = assist.change;
280 // verify
281 {
282 var testFileEdit = change.getFileEdit('/app.dart');
283 var resultCode = SourceEdit.applySequence(appCode, testFileEdit.edits);
284 expect(resultCode, '''
285 library my_app;
286 import 'my_lib.dart';
287 import 'dart:async';
288 part 'test.dart';
289 ''');
290 }
291 {
292 var testFileEdit = change.getFileEdit('/test.dart');
293 var resultCode = SourceEdit.applySequence(testCode, testFileEdit.edits);
294 expect(resultCode, '''
295 part of my_app;
296 main() {
297 Future<int> v = getFutureInt();
298 }
299 ''');
300 }
301 }
302
303 void test_addTypeAnnotation_local_OK_addImport_relUri() {
304 addSource('/aa/bbb/lib_a.dart', r'''
305 class MyClass {}
306 ''');
307 addSource('/ccc/lib_b.dart', r'''
308 import '../aa/bbb/lib_a.dart';
309 MyClass newMyClass() => null;
310 ''');
311 _indexTestUnit('''
312 import 'ccc/lib_b.dart';
313 main() {
314 var v = newMyClass();
315 }
316 ''');
317 assertHasAssistAt('v =', AssistKind.ADD_TYPE_ANNOTATION, '''
318 import 'ccc/lib_b.dart';
319 import 'aa/bbb/lib_a.dart';
320 main() {
321 MyClass v = newMyClass();
322 }
323 ''');
324 }
325
206 void test_addTypeAnnotation_local_OK_Function() { 326 void test_addTypeAnnotation_local_OK_Function() {
207 _indexTestUnit(''' 327 _indexTestUnit('''
208 main() { 328 main() {
209 var v = () => 1; 329 var v = () => 1;
210 } 330 }
211 '''); 331 ''');
212 assertHasAssistAt('v =', AssistKind.ADD_TYPE_ANNOTATION, ''' 332 assertHasAssistAt('v =', AssistKind.ADD_TYPE_ANNOTATION, '''
213 main() { 333 main() {
214 Function v = () => 1; 334 Function v = () => 1;
215 } 335 }
(...skipping 2230 matching lines...) Expand 10 before | Expand all | Expand 10 after
2446 void _indexTestUnit(String code) { 2566 void _indexTestUnit(String code) {
2447 resolveTestUnit(code); 2567 resolveTestUnit(code);
2448 index.indexUnit(context, testUnit); 2568 index.indexUnit(context, testUnit);
2449 } 2569 }
2450 2570
2451 void _setStartEndSelection() { 2571 void _setStartEndSelection() {
2452 offset = findOffset('// start\n') + '// start\n'.length; 2572 offset = findOffset('// start\n') + '// start\n'.length;
2453 length = findOffset('// end') - offset; 2573 length = findOffset('// end') - offset;
2454 } 2574 }
2455 } 2575 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/test/mock_sdk.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698