OLD | NEW |
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.completion.invocation; | 5 library test.services.completion.invocation; |
6 | 6 |
7 | 7 |
| 8 import 'dart:async'; |
| 9 |
8 import 'package:analysis_server/src/protocol.dart'; | 10 import 'package:analysis_server/src/protocol.dart'; |
9 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 11 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
10 import 'package:analysis_server/src/services/completion/invocation_computer.dart
'; | 12 import 'package:analysis_server/src/services/completion/invocation_computer.dart
'; |
11 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
12 | 14 |
13 import '../../reflective_tests.dart'; | 15 import '../../reflective_tests.dart'; |
14 import 'completion_test_util.dart'; | 16 import 'completion_test_util.dart'; |
15 | 17 |
16 main() { | 18 main() { |
17 groupSep = ' | '; | 19 groupSep = ' | '; |
18 runReflectiveTests(InvocationComputerTest); | 20 runReflectiveTests(InvocationComputerTest); |
19 } | 21 } |
20 | 22 |
21 @reflectiveTest | 23 @reflectiveTest |
22 class InvocationComputerTest extends AbstractSelectorSuggestionTest { | 24 class InvocationComputerTest extends AbstractSelectorSuggestionTest { |
23 | 25 |
24 @override | 26 @override |
25 CompletionSuggestion assertSuggestInvocationField(String name, String type, | 27 CompletionSuggestion assertSuggestInvocationField(String name, String type, |
26 {int relevance: COMPLETION_RELEVANCE_DEFAULT, bool isDeprecated: false}) { | 28 {int relevance: COMPLETION_RELEVANCE_DEFAULT, bool isDeprecated: false}) { |
27 return assertSuggestField( | 29 return assertSuggestField( |
28 name, | 30 name, |
29 type, | 31 type, |
30 relevance: relevance, | 32 relevance: relevance, |
31 isDeprecated: isDeprecated); | 33 isDeprecated: isDeprecated); |
32 } | 34 } |
33 | 35 |
| 36 /** |
| 37 * Check whether a declaration of the form [shadower] in a derived class |
| 38 * shadows a declaration of the form [shadowee] in a base class, for the |
| 39 * purposes of what is shown during completion. [shouldBeShadowed] indicates |
| 40 * whether shadowing is expected. |
| 41 */ |
| 42 Future check_shadowing(String shadower, String shadowee, |
| 43 bool shouldBeShadowed) { |
| 44 addTestSource(''' |
| 45 class Base { |
| 46 $shadowee |
| 47 } |
| 48 class Derived extends Base { |
| 49 $shadower |
| 50 } |
| 51 void f(Derived d) { |
| 52 d.^ |
| 53 } |
| 54 '''); |
| 55 return computeFull((bool result) { |
| 56 List<CompletionSuggestion> suggestionsForX = request.suggestions.where( |
| 57 (CompletionSuggestion s) => s.completion == 'x').toList(); |
| 58 if (shouldBeShadowed) { |
| 59 expect(suggestionsForX, hasLength(1)); |
| 60 expect(suggestionsForX[0].declaringType, 'Derived'); |
| 61 } else { |
| 62 expect(suggestionsForX, hasLength(2)); |
| 63 } |
| 64 }); |
| 65 } |
| 66 |
34 @override | 67 @override |
35 void setUpComputer() { | 68 void setUpComputer() { |
36 computer = new InvocationComputer(); | 69 computer = new InvocationComputer(); |
37 } | 70 } |
38 | 71 |
| 72 test_generic_field() { |
| 73 addTestSource(''' |
| 74 class C<T> { |
| 75 T t; |
| 76 } |
| 77 void f(C<int> c) { |
| 78 c.^ |
| 79 } |
| 80 '''); |
| 81 return computeFull((bool result) { |
| 82 assertSuggestField('t', 'int'); |
| 83 }); |
| 84 } |
| 85 |
| 86 test_generic_getter() { |
| 87 addTestSource(''' |
| 88 class C<T> { |
| 89 T get t => null; |
| 90 } |
| 91 void f(C<int> c) { |
| 92 c.^ |
| 93 } |
| 94 '''); |
| 95 return computeFull((bool result) { |
| 96 assertSuggestGetter('t', 'int'); |
| 97 }); |
| 98 } |
| 99 |
| 100 test_generic_method() { |
| 101 addTestSource(''' |
| 102 class C<T> { |
| 103 T m(T t) {} |
| 104 } |
| 105 void f(C<int> c) { |
| 106 c.^ |
| 107 } |
| 108 '''); |
| 109 return computeFull((bool result) { |
| 110 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'int'); |
| 111 expect(suggestion.parameterTypes[0], 'int'); |
| 112 expect(suggestion.element.returnType, 'int'); |
| 113 expect(suggestion.element.parameters, '(int t)'); |
| 114 }); |
| 115 } |
| 116 |
| 117 test_generic_setter() { |
| 118 addTestSource(''' |
| 119 class C<T> { |
| 120 set t(T value) {} |
| 121 } |
| 122 void f(C<int> c) { |
| 123 c.^ |
| 124 } |
| 125 '''); |
| 126 return computeFull((bool result) { |
| 127 // TODO(paulberry): modify assertSuggestSetter so that we can pass 'int' |
| 128 // as a parmeter to it, and it will check the appropriate field in |
| 129 // the suggestion object. |
| 130 CompletionSuggestion suggestion = assertSuggestSetter('t'); |
| 131 expect(suggestion.element.parameters, '(int value)'); |
| 132 }); |
| 133 } |
| 134 |
39 test_method_parameters_mixed_required_and_named() { | 135 test_method_parameters_mixed_required_and_named() { |
40 addTestSource(''' | 136 addTestSource(''' |
41 class C { | 137 class C { |
42 void m(x, {int y}) {} | 138 void m(x, {int y}) {} |
43 } | 139 } |
44 void main() {new C().^}'''); | 140 void main() {new C().^}'''); |
45 return computeFull((bool result) { | 141 return computeFull((bool result) { |
46 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void'); | 142 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void'); |
47 expect(suggestion.parameterNames, hasLength(2)); | 143 expect(suggestion.parameterNames, hasLength(2)); |
48 expect(suggestion.parameterNames[0], 'x'); | 144 expect(suggestion.parameterNames[0], 'x'); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 addTestSource(''' | 266 addTestSource(''' |
171 class C { | 267 class C { |
172 set x(int value) {}; | 268 set x(int value) {}; |
173 } | 269 } |
174 void main() {int y = new C().^}'''); | 270 void main() {int y = new C().^}'''); |
175 return computeFull((bool result) { | 271 return computeFull((bool result) { |
176 CompletionSuggestion suggestion = assertSuggestSetter('x'); | 272 CompletionSuggestion suggestion = assertSuggestSetter('x'); |
177 assertHasNoParameterInfo(suggestion); | 273 assertHasNoParameterInfo(suggestion); |
178 }); | 274 }); |
179 } | 275 } |
| 276 |
| 277 test_shadowing_field_over_field() => |
| 278 check_shadowing('int x;', 'int x;', true); |
| 279 |
| 280 test_shadowing_field_over_getter() => |
| 281 check_shadowing('int x;', 'int get x => null;', true); |
| 282 |
| 283 test_shadowing_field_over_method() => |
| 284 check_shadowing('int x;', 'void x() {}', true); |
| 285 |
| 286 test_shadowing_field_over_setter() => |
| 287 check_shadowing('int x;', 'set x(int value) {}', true); |
| 288 |
| 289 test_shadowing_getter_over_field() => |
| 290 check_shadowing('int get x => null;', 'int x;', false); |
| 291 |
| 292 test_shadowing_getter_over_getter() => |
| 293 check_shadowing('int get x => null;', 'int get x => null;', true); |
| 294 |
| 295 test_shadowing_getter_over_method() => |
| 296 check_shadowing('int get x => null;', 'void x() {}', true); |
| 297 |
| 298 test_shadowing_getter_over_setter() => |
| 299 check_shadowing('int get x => null;', 'set x(int value) {}', false); |
| 300 |
| 301 test_shadowing_method_over_field() => |
| 302 check_shadowing('void x() {}', 'int x;', true); |
| 303 |
| 304 test_shadowing_method_over_getter() => |
| 305 check_shadowing('void x() {}', 'int get x => null;', true); |
| 306 |
| 307 test_shadowing_method_over_method() => |
| 308 check_shadowing('void x() {}', 'void x() {}', true); |
| 309 |
| 310 test_shadowing_method_over_setter() => |
| 311 check_shadowing('void x() {}', 'set x(int value) {}', true); |
| 312 |
| 313 test_shadowing_mixin_order() { |
| 314 addTestSource(''' |
| 315 class Base { |
180 } | 316 } |
| 317 class Mixin1 { |
| 318 void f() {} |
| 319 } |
| 320 class Mixin2 { |
| 321 void f() {} |
| 322 } |
| 323 class Derived extends Base with Mixin1, Mixin2 { |
| 324 } |
| 325 void test(Derived d) { |
| 326 d.^ |
| 327 } |
| 328 '''); |
| 329 return computeFull((bool result) { |
| 330 // Note: due to dartbug.com/22069, analyzer currently analyzes mixins in |
| 331 // reverse order. The correct order is that Derived inherits from |
| 332 // "Base with Mixin1, Mixin2", which inherits from "Base with Mixin1", |
| 333 // which inherits from "Base". So the definition of f in Mixin2 should |
| 334 // shadow the definition in Mixin1. |
| 335 assertSuggestMethod('f', 'Mixin2', 'void'); |
| 336 }); |
| 337 } |
| 338 |
| 339 test_shadowing_mixin_over_superclass() { |
| 340 addTestSource(''' |
| 341 class Base { |
| 342 void f() {} |
| 343 } |
| 344 class Mixin { |
| 345 void f() {} |
| 346 } |
| 347 class Derived extends Base with Mixin { |
| 348 } |
| 349 void test(Derived d) { |
| 350 d.^ |
| 351 } |
| 352 '''); |
| 353 return computeFull((bool result) { |
| 354 assertSuggestMethod('f', 'Mixin', 'void'); |
| 355 }); |
| 356 } |
| 357 |
| 358 test_shadowing_setter_over_field() => |
| 359 check_shadowing('set x(int value) {}', 'int x;', false); |
| 360 |
| 361 test_shadowing_setter_over_getter() => |
| 362 check_shadowing('set x(int value) {}', 'int get x => null;', false); |
| 363 |
| 364 test_shadowing_setter_over_method() => |
| 365 check_shadowing('set x(int value) {}', 'void x() {}', true); |
| 366 |
| 367 test_shadowing_setter_over_setter() => |
| 368 check_shadowing('set x(int value) {}', 'set x(int value) {}', true); |
| 369 |
| 370 test_shadowing_superclass_over_interface() { |
| 371 addTestSource(''' |
| 372 class Base { |
| 373 void f() {} |
| 374 } |
| 375 class Interface { |
| 376 void f() {} |
| 377 } |
| 378 class Derived extends Base implements Interface { |
| 379 } |
| 380 void test(Derived d) { |
| 381 d.^ |
| 382 } |
| 383 '''); |
| 384 return computeFull((bool result) { |
| 385 assertSuggestMethod('f', 'Base', 'void'); |
| 386 }); |
| 387 } |
| 388 } |
OLD | NEW |