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

Side by Side Diff: pkg/analysis_server/test/services/completion/prefixed_element_contributor_test.dart

Issue 1504223005: extract TypeMemberContributor from prefixed element contributor (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: mege Created 5 years 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
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library test.services.completion.invocation;
6
7 import 'dart:async';
8
9 import 'package:analysis_server/plugin/protocol/protocol.dart';
10 import 'package:analysis_server/src/services/completion/dart_completion_manager. dart';
11 import 'package:analysis_server/src/services/completion/prefixed_element_contrib utor.dart';
12 import 'package:test_reflective_loader/test_reflective_loader.dart';
13 import 'package:unittest/unittest.dart';
14
15 import '../../utils.dart';
16 import 'completion_test_util.dart';
17
18 main() {
19 initializeTestEnvironment();
20 defineReflectiveTests(PrefixedElementContributorTest);
21 }
22
23 @reflectiveTest
24 class PrefixedElementContributorTest extends AbstractSelectorSuggestionTest {
25 @override
26 CompletionSuggestion assertSuggestInvocationField(String name, String type,
27 {int relevance: DART_RELEVANCE_DEFAULT, bool isDeprecated: false}) {
28 return assertSuggestField(name, type,
29 relevance: relevance, isDeprecated: isDeprecated);
30 }
31
32 /**
33 * Check whether a declaration of the form [shadower] in a derived class
34 * shadows a declaration of the form [shadowee] in a base class, for the
35 * purposes of what is shown during completion. [shouldBeShadowed] indicates
36 * whether shadowing is expected.
37 */
38 Future check_shadowing(
39 String shadower, String shadowee, bool shouldBeShadowed) {
40 addTestSource('''
41 class Base {
42 $shadowee
43 }
44 class Derived extends Base {
45 $shadower
46 }
47 void f(Derived d) {
48 d.^
49 }
50 ''');
51 return computeFull((bool result) {
52 List<CompletionSuggestion> suggestionsForX = request.suggestions
53 .where((CompletionSuggestion s) => s.completion == 'x')
54 .toList();
55 if (shouldBeShadowed) {
56 expect(suggestionsForX, hasLength(1));
57 expect(suggestionsForX[0].declaringType, 'Derived');
58 } else {
59 expect(suggestionsForX, hasLength(2));
60 }
61 });
62 }
63
64 fail_test_PrefixedIdentifier_trailingStmt_const_untyped() {
65 // SimpleIdentifier PrefixedIdentifier ExpressionStatement
66 addTestSource('const g = "hello"; f() {g.^ int y = 0;}');
67 computeFast();
68 return computeFull((bool result) {
69 assertSuggestInvocationGetter('length', 'int');
70 });
71 }
72
73 @override
74 void setUpContributor() {
75 contributor = new PrefixedElementContributor();
76 }
77
78 test_enumConst() {
79 addTestSource('enum E { one, two } main() {E.^}');
80 return computeFull((bool result) {
81 assertNotSuggested('E');
82 // Suggested by StaticMemberContributor
83 assertNotSuggested('one');
84 assertNotSuggested('two');
85 assertNotSuggested('index');
86 assertNotSuggested('values');
87 });
88 }
89
90 test_enumConst2() {
91 addTestSource('enum E { one, two } main() {E.o^}');
92 return computeFull((bool result) {
93 assertNotSuggested('E');
94 // Suggested by StaticMemberContributor
95 assertNotSuggested('one');
96 assertNotSuggested('two');
97 assertNotSuggested('index');
98 assertNotSuggested('values');
99 });
100 }
101
102 test_enumConst3() {
103 addTestSource('enum E { one, two } main() {E.^ int g;}');
104 return computeFull((bool result) {
105 assertNotSuggested('E');
106 // Suggested by StaticMemberContributor
107 assertNotSuggested('one');
108 assertNotSuggested('two');
109 assertNotSuggested('index');
110 assertNotSuggested('values');
111 });
112 }
113
114 test_enumConst_index() {
115 addTestSource('enum E { one, two } main() {E.one.^}');
116 return computeFull((bool result) {
117 assertNotSuggested('E');
118 assertNotSuggested('one');
119 assertNotSuggested('two');
120 assertSuggestField('index', 'int');
121 assertNotSuggested('values');
122 });
123 }
124
125 test_enumConst_index2() {
126 addTestSource('enum E { one, two } main() {E.one.i^}');
127 return computeFull((bool result) {
128 assertNotSuggested('E');
129 assertNotSuggested('one');
130 assertNotSuggested('two');
131 assertSuggestField('index', 'int');
132 assertNotSuggested('values');
133 });
134 }
135
136 test_enumConst_index3() {
137 addTestSource('enum E { one, two } main() {E.one.^ int g;}');
138 return computeFull((bool result) {
139 assertNotSuggested('E');
140 assertNotSuggested('one');
141 assertNotSuggested('two');
142 assertSuggestField('index', 'int');
143 assertNotSuggested('values');
144 });
145 }
146
147 test_generic_field() {
148 addTestSource('''
149 class C<T> {
150 T t;
151 }
152 void f(C<int> c) {
153 c.^
154 }
155 ''');
156 return computeFull((bool result) {
157 assertSuggestField('t', 'int');
158 });
159 }
160
161 test_generic_getter() {
162 addTestSource('''
163 class C<T> {
164 T get t => null;
165 }
166 void f(C<int> c) {
167 c.^
168 }
169 ''');
170 return computeFull((bool result) {
171 assertSuggestGetter('t', 'int');
172 });
173 }
174
175 test_generic_method() {
176 addTestSource('''
177 class C<T> {
178 T m(T t) {}
179 }
180 void f(C<int> c) {
181 c.^
182 }
183 ''');
184 return computeFull((bool result) {
185 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'int');
186 expect(suggestion.parameterTypes[0], 'int');
187 expect(suggestion.element.returnType, 'int');
188 expect(suggestion.element.parameters, '(int t)');
189 });
190 }
191
192 test_generic_setter() {
193 addTestSource('''
194 class C<T> {
195 set t(T value) {}
196 }
197 void f(C<int> c) {
198 c.^
199 }
200 ''');
201 return computeFull((bool result) {
202 // TODO(paulberry): modify assertSuggestSetter so that we can pass 'int'
203 // as a parmeter to it, and it will check the appropriate field in
204 // the suggestion object.
205 CompletionSuggestion suggestion = assertSuggestSetter('t');
206 expect(suggestion.element.parameters, '(int value)');
207 });
208 }
209
210 test_keyword() {
211 addTestSource('class C { static C get instance => null; } main() {C.in^}');
212 return computeFull((bool result) {
213 // Suggested by StaticMemberContributor
214 assertNotSuggested('instance');
215 });
216 }
217
218 test_libraryPrefix() {
219 // SimpleIdentifier PrefixedIdentifier ExpressionStatement
220 addTestSource('import "dart:async" as bar; foo() {bar.^}');
221 return computeFull((bool result) {
222 // Suggested by LibraryMemberContributor
223 assertNotSuggested('Future');
224 assertNotSuggested('loadLibrary');
225 });
226 }
227
228 test_libraryPrefix2() {
229 // SimpleIdentifier MethodInvocation ExpressionStatement
230 addTestSource('import "dart:async" as bar; foo() {bar.^ print("f")}');
231 return computeFull((bool result) {
232 // Suggested by LibraryMemberContributor
233 assertNotSuggested('Future');
234 });
235 }
236
237 test_libraryPrefix3() {
238 // SimpleIdentifier MethodInvocation ExpressionStatement
239 addTestSource('import "dart:async" as bar; foo() {new bar.F^ print("f")}');
240 return computeFull((bool result) {
241 // Suggested by LibraryMemberContributor
242 assertNotSuggested('Future');
243 assertNotSuggested('Future.delayed');
244 });
245 }
246
247 test_libraryPrefix_deferred() {
248 // SimpleIdentifier PrefixedIdentifier ExpressionStatement
249 addTestSource('import "dart:async" deferred as bar; foo() {bar.^}');
250 return computeFull((bool result) {
251 // Suggested by LibraryMemberContributor
252 assertNotSuggested('Future');
253 assertNotSuggested('loadLibrary');
254 });
255 }
256
257 test_libraryPrefix_with_exports() {
258 addSource('/libA.dart', 'library libA; class A { }');
259 addSource('/libB.dart', 'library libB; export "/libA.dart"; class B { }');
260 addTestSource('import "/libB.dart" as foo; main() {foo.^} class C { }');
261 computeFast();
262 return computeFull((bool result) {
263 // Suggested by LibraryMemberContributor
264 assertNotSuggested('B');
265 assertNotSuggested('A');
266 });
267 }
268
269 test_local() {
270 addTestSource('foo() {String x = "bar"; x.^}');
271 return computeFull((bool result) {
272 assertSuggestGetter('length', 'int');
273 });
274 }
275
276 test_local_is() {
277 addTestSource('foo() {var x; if (x is String) x.^}');
278 return computeFull((bool result) {
279 assertSuggestGetter('length', 'int');
280 });
281 }
282
283 test_local_propogatedType() {
284 addTestSource('foo() {var x = "bar"; x.^}');
285 return computeFull((bool result) {
286 assertSuggestGetter('length', 'int');
287 });
288 }
289
290 test_method_parameters_mixed_required_and_named() {
291 addTestSource('''
292 class C {
293 void m(x, {int y}) {}
294 }
295 void main() {new C().^}''');
296 return computeFull((bool result) {
297 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void');
298 expect(suggestion.parameterNames, hasLength(2));
299 expect(suggestion.parameterNames[0], 'x');
300 expect(suggestion.parameterTypes[0], 'dynamic');
301 expect(suggestion.parameterNames[1], 'y');
302 expect(suggestion.parameterTypes[1], 'int');
303 expect(suggestion.requiredParameterCount, 1);
304 expect(suggestion.hasNamedParameters, true);
305 });
306 }
307
308 test_method_parameters_mixed_required_and_positional() {
309 addTestSource('''
310 class C {
311 void m(x, [int y]) {}
312 }
313 void main() {new C().^}''');
314 return computeFull((bool result) {
315 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void');
316 expect(suggestion.parameterNames, hasLength(2));
317 expect(suggestion.parameterNames[0], 'x');
318 expect(suggestion.parameterTypes[0], 'dynamic');
319 expect(suggestion.parameterNames[1], 'y');
320 expect(suggestion.parameterTypes[1], 'int');
321 expect(suggestion.requiredParameterCount, 1);
322 expect(suggestion.hasNamedParameters, false);
323 });
324 }
325
326 test_method_parameters_named() {
327 addTestSource('''
328 class C {
329 void m({x, int y}) {}
330 }
331 void main() {new C().^}''');
332 return computeFull((bool result) {
333 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void');
334 expect(suggestion.parameterNames, hasLength(2));
335 expect(suggestion.parameterNames[0], 'x');
336 expect(suggestion.parameterTypes[0], 'dynamic');
337 expect(suggestion.parameterNames[1], 'y');
338 expect(suggestion.parameterTypes[1], 'int');
339 expect(suggestion.requiredParameterCount, 0);
340 expect(suggestion.hasNamedParameters, true);
341 });
342 }
343
344 test_method_parameters_none() {
345 addTestSource('''
346 class C {
347 void m() {}
348 }
349 void main() {new C().^}''');
350 computeFast();
351 return computeFull((bool result) {
352 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void');
353 expect(suggestion.parameterNames, isEmpty);
354 expect(suggestion.parameterTypes, isEmpty);
355 expect(suggestion.requiredParameterCount, 0);
356 expect(suggestion.hasNamedParameters, false);
357 });
358 }
359
360 test_method_parameters_positional() {
361 addTestSource('''
362 class C {
363 void m([x, int y]) {}
364 }
365 void main() {new C().^}''');
366 return computeFull((bool result) {
367 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void');
368 expect(suggestion.parameterNames, hasLength(2));
369 expect(suggestion.parameterNames[0], 'x');
370 expect(suggestion.parameterTypes[0], 'dynamic');
371 expect(suggestion.parameterNames[1], 'y');
372 expect(suggestion.parameterTypes[1], 'int');
373 expect(suggestion.requiredParameterCount, 0);
374 expect(suggestion.hasNamedParameters, false);
375 });
376 }
377
378 test_method_parameters_required() {
379 addTestSource('''
380 class C {
381 void m(x, int y) {}
382 }
383 void main() {new C().^}''');
384 return computeFull((bool result) {
385 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void');
386 expect(suggestion.parameterNames, hasLength(2));
387 expect(suggestion.parameterNames[0], 'x');
388 expect(suggestion.parameterTypes[0], 'dynamic');
389 expect(suggestion.parameterNames[1], 'y');
390 expect(suggestion.parameterTypes[1], 'int');
391 expect(suggestion.requiredParameterCount, 2);
392 expect(suggestion.hasNamedParameters, false);
393 });
394 }
395
396 test_no_parameters_field() {
397 addTestSource('''
398 class C {
399 int x;
400 }
401 void main() {new C().^}''');
402 return computeFull((bool result) {
403 CompletionSuggestion suggestion = assertSuggestField('x', 'int');
404 assertHasNoParameterInfo(suggestion);
405 });
406 }
407
408 test_no_parameters_getter() {
409 addTestSource('''
410 class C {
411 int get x => null;
412 }
413 void main() {int y = new C().^}''');
414 return computeFull((bool result) {
415 CompletionSuggestion suggestion = assertSuggestGetter('x', 'int');
416 assertHasNoParameterInfo(suggestion);
417 });
418 }
419
420 test_no_parameters_setter() {
421 addTestSource('''
422 class C {
423 set x(int value) {};
424 }
425 void main() {int y = new C().^}''');
426 return computeFull((bool result) {
427 CompletionSuggestion suggestion = assertSuggestSetter('x');
428 assertHasNoParameterInfo(suggestion);
429 });
430 }
431
432 test_only_instance() {
433 // SimpleIdentifier PropertyAccess ExpressionStatement
434 addTestSource('''
435 class C {
436 int f1;
437 static int f2;
438 m1() {}
439 static m2() {}
440 }
441 void main() {new C().^}''');
442 return computeFull((bool result) {
443 assertSuggestInvocationField('f1', 'int');
444 assertNotSuggested('f2');
445 assertSuggestMethod('m1', 'C', null);
446 assertNotSuggested('m2');
447 });
448 }
449
450 test_only_instance2() {
451 // SimpleIdentifier MethodInvocation ExpressionStatement
452 addTestSource('''
453 class C {
454 int f1;
455 static int f2;
456 m1() {}
457 static m2() {}
458 }
459 void main() {new C().^ print("something");}''');
460 return computeFull((bool result) {
461 assertSuggestInvocationField('f1', 'int');
462 assertNotSuggested('f2');
463 assertSuggestMethod('m1', 'C', null);
464 assertNotSuggested('m2');
465 });
466 }
467
468 test_only_static() {
469 // SimpleIdentifier PrefixedIdentifier ExpressionStatement
470 addTestSource('''
471 class C {
472 int f1;
473 static int f2;
474 m1() {}
475 static m2() {}
476 }
477 void main() {C.^}''');
478 return computeFull((bool result) {
479 assertNotSuggested('f1');
480 // Suggested by StaticMemberContributor
481 assertNotSuggested('f2');
482 assertNotSuggested('m1');
483 assertNotSuggested('m2');
484 });
485 }
486
487 test_only_static2() {
488 // SimpleIdentifier MethodInvocation ExpressionStatement
489 addTestSource('''
490 class C {
491 int f1;
492 static int f2;
493 m1() {}
494 static m2() {}
495 }
496 void main() {C.^ print("something");}''');
497 return computeFull((bool result) {
498 assertNotSuggested('f1');
499 // Suggested by StaticMemberContributor
500 assertNotSuggested('f2');
501 assertNotSuggested('m1');
502 assertNotSuggested('m2');
503 });
504 }
505
506 test_param() {
507 addTestSource('foo(String x) {x.^}');
508 return computeFull((bool result) {
509 assertSuggestGetter('length', 'int');
510 });
511 }
512
513 test_param_is() {
514 addTestSource('foo(x) {if (x is String) x.^}');
515 return computeFull((bool result) {
516 assertSuggestGetter('length', 'int');
517 });
518 }
519
520 test_shadowing_field_over_field() =>
521 check_shadowing('int x;', 'int x;', true);
522
523 test_shadowing_field_over_getter() =>
524 check_shadowing('int x;', 'int get x => null;', true);
525
526 test_shadowing_field_over_method() =>
527 check_shadowing('int x;', 'void x() {}', true);
528
529 test_shadowing_field_over_setter() =>
530 check_shadowing('int x;', 'set x(int value) {}', true);
531
532 test_shadowing_getter_over_field() =>
533 check_shadowing('int get x => null;', 'int x;', true);
534
535 test_shadowing_getter_over_getter() =>
536 check_shadowing('int get x => null;', 'int get x => null;', true);
537
538 test_shadowing_getter_over_method() =>
539 check_shadowing('int get x => null;', 'void x() {}', true);
540
541 test_shadowing_getter_over_setter() =>
542 check_shadowing('int get x => null;', 'set x(int value) {}', true);
543
544 test_shadowing_method_over_field() =>
545 check_shadowing('void x() {}', 'int x;', true);
546
547 test_shadowing_method_over_getter() =>
548 check_shadowing('void x() {}', 'int get x => null;', true);
549
550 test_shadowing_method_over_method() =>
551 check_shadowing('void x() {}', 'void x() {}', true);
552
553 test_shadowing_method_over_setter() =>
554 check_shadowing('void x() {}', 'set x(int value) {}', true);
555
556 test_shadowing_mixin_order() {
557 addTestSource('''
558 class Base {
559 }
560 class Mixin1 {
561 void f() {}
562 }
563 class Mixin2 {
564 void f() {}
565 }
566 class Derived extends Base with Mixin1, Mixin2 {
567 }
568 void test(Derived d) {
569 d.^
570 }
571 ''');
572 return computeFull((bool result) {
573 // Note: due to dartbug.com/22069, analyzer currently analyzes mixins in
574 // reverse order. The correct order is that Derived inherits from
575 // "Base with Mixin1, Mixin2", which inherits from "Base with Mixin1",
576 // which inherits from "Base". So the definition of f in Mixin2 should
577 // shadow the definition in Mixin1.
578 assertSuggestMethod('f', 'Mixin2', 'void');
579 });
580 }
581
582 test_shadowing_mixin_over_superclass() {
583 addTestSource('''
584 class Base {
585 void f() {}
586 }
587 class Mixin {
588 void f() {}
589 }
590 class Derived extends Base with Mixin {
591 }
592 void test(Derived d) {
593 d.^
594 }
595 ''');
596 return computeFull((bool result) {
597 assertSuggestMethod('f', 'Mixin', 'void');
598 });
599 }
600
601 test_shadowing_setter_over_field() =>
602 check_shadowing('set x(int value) {}', 'int x;', true);
603
604 test_shadowing_setter_over_getter() =>
605 check_shadowing('set x(int value) {}', 'int get x => null;', true);
606
607 test_shadowing_setter_over_method() =>
608 check_shadowing('set x(int value) {}', 'void x() {}', true);
609
610 test_shadowing_setter_over_setter() =>
611 check_shadowing('set x(int value) {}', 'set x(int value) {}', true);
612
613 test_shadowing_superclass_over_interface() {
614 addTestSource('''
615 class Base {
616 void f() {}
617 }
618 class Interface {
619 void f() {}
620 }
621 class Derived extends Base implements Interface {
622 }
623 void test(Derived d) {
624 d.^
625 }
626 ''');
627 return computeFull((bool result) {
628 assertSuggestMethod('f', 'Base', 'void');
629 });
630 }
631
632 test_super() {
633 // SimpleIdentifier MethodInvocation ExpressionStatement
634 addTestSource('''
635 class C3 {
636 int fi3;
637 static int fs3;
638 m() {}
639 mi3() {}
640 static ms3() {}
641 }
642 class C2 {
643 int fi2;
644 static int fs2;
645 m() {}
646 mi2() {}
647 static ms2() {}
648 }
649 class C1 extends C2 implements C3 {
650 int fi1;
651 static int fs1;
652 m() {super.^}
653 mi1() {}
654 static ms1() {}
655 }''');
656 return computeFull((bool result) {
657 assertNotSuggested('fi1');
658 assertNotSuggested('fs1');
659 assertNotSuggested('mi1');
660 assertNotSuggested('ms1');
661 assertSuggestInvocationField('fi2', 'int');
662 assertNotSuggested('fs2');
663 assertSuggestInvocationMethod('mi2', 'C2', null);
664 assertNotSuggested('ms2');
665 assertSuggestInvocationMethod('m', 'C2', null,
666 relevance: DART_RELEVANCE_HIGH);
667 assertNotSuggested('fi3');
668 assertNotSuggested('fs3');
669 assertNotSuggested('mi3');
670 assertNotSuggested('ms3');
671 });
672 }
673 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698