| OLD | NEW |
| (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/src/protocol.dart'; | |
| 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | |
| 11 import 'package:analysis_server/src/services/completion/invocation_computer.dart
'; | |
| 12 import 'package:unittest/unittest.dart'; | |
| 13 | |
| 14 import '../../reflective_tests.dart'; | |
| 15 import 'completion_test_util.dart'; | |
| 16 | |
| 17 main() { | |
| 18 groupSep = ' | '; | |
| 19 runReflectiveTests(PrefixedElementContributorTest); | |
| 20 } | |
| 21 | |
| 22 @reflectiveTest | |
| 23 class PrefixedElementContributorTest extends AbstractSelectorSuggestionTest { | |
| 24 @override | |
| 25 CompletionSuggestion assertSuggestInvocationField(String name, String type, | |
| 26 {int relevance: DART_RELEVANCE_DEFAULT, bool isDeprecated: false}) { | |
| 27 return assertSuggestField(name, type, | |
| 28 relevance: relevance, isDeprecated: isDeprecated); | |
| 29 } | |
| 30 | |
| 31 /** | |
| 32 * Check whether a declaration of the form [shadower] in a derived class | |
| 33 * shadows a declaration of the form [shadowee] in a base class, for the | |
| 34 * purposes of what is shown during completion. [shouldBeShadowed] indicates | |
| 35 * whether shadowing is expected. | |
| 36 */ | |
| 37 Future check_shadowing( | |
| 38 String shadower, String shadowee, bool shouldBeShadowed) { | |
| 39 addTestSource(''' | |
| 40 class Base { | |
| 41 $shadowee | |
| 42 } | |
| 43 class Derived extends Base { | |
| 44 $shadower | |
| 45 } | |
| 46 void f(Derived d) { | |
| 47 d.^ | |
| 48 } | |
| 49 '''); | |
| 50 return computeFull((bool result) { | |
| 51 List<CompletionSuggestion> suggestionsForX = request.suggestions | |
| 52 .where((CompletionSuggestion s) => s.completion == 'x') | |
| 53 .toList(); | |
| 54 if (shouldBeShadowed) { | |
| 55 expect(suggestionsForX, hasLength(1)); | |
| 56 expect(suggestionsForX[0].declaringType, 'Derived'); | |
| 57 } else { | |
| 58 expect(suggestionsForX, hasLength(2)); | |
| 59 } | |
| 60 }); | |
| 61 } | |
| 62 | |
| 63 fail_test_PrefixedIdentifier_trailingStmt_const_untyped() { | |
| 64 // SimpleIdentifier PrefixedIdentifier ExpressionStatement | |
| 65 addTestSource('const g = "hello"; f() {g.^ int y = 0;}'); | |
| 66 computeFast(); | |
| 67 return computeFull((bool result) { | |
| 68 assertSuggestInvocationGetter('length', 'int'); | |
| 69 }); | |
| 70 } | |
| 71 | |
| 72 @override | |
| 73 void setUpContributor() { | |
| 74 contributor = new PrefixedElementContributor(); | |
| 75 } | |
| 76 | |
| 77 test_generic_field() { | |
| 78 addTestSource(''' | |
| 79 class C<T> { | |
| 80 T t; | |
| 81 } | |
| 82 void f(C<int> c) { | |
| 83 c.^ | |
| 84 } | |
| 85 '''); | |
| 86 return computeFull((bool result) { | |
| 87 assertSuggestField('t', 'int'); | |
| 88 }); | |
| 89 } | |
| 90 | |
| 91 test_generic_getter() { | |
| 92 addTestSource(''' | |
| 93 class C<T> { | |
| 94 T get t => null; | |
| 95 } | |
| 96 void f(C<int> c) { | |
| 97 c.^ | |
| 98 } | |
| 99 '''); | |
| 100 return computeFull((bool result) { | |
| 101 assertSuggestGetter('t', 'int'); | |
| 102 }); | |
| 103 } | |
| 104 | |
| 105 test_generic_method() { | |
| 106 addTestSource(''' | |
| 107 class C<T> { | |
| 108 T m(T t) {} | |
| 109 } | |
| 110 void f(C<int> c) { | |
| 111 c.^ | |
| 112 } | |
| 113 '''); | |
| 114 return computeFull((bool result) { | |
| 115 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'int'); | |
| 116 expect(suggestion.parameterTypes[0], 'int'); | |
| 117 expect(suggestion.element.returnType, 'int'); | |
| 118 expect(suggestion.element.parameters, '(int t)'); | |
| 119 }); | |
| 120 } | |
| 121 | |
| 122 test_generic_setter() { | |
| 123 addTestSource(''' | |
| 124 class C<T> { | |
| 125 set t(T value) {} | |
| 126 } | |
| 127 void f(C<int> c) { | |
| 128 c.^ | |
| 129 } | |
| 130 '''); | |
| 131 return computeFull((bool result) { | |
| 132 // TODO(paulberry): modify assertSuggestSetter so that we can pass 'int' | |
| 133 // as a parmeter to it, and it will check the appropriate field in | |
| 134 // the suggestion object. | |
| 135 CompletionSuggestion suggestion = assertSuggestSetter('t'); | |
| 136 expect(suggestion.element.parameters, '(int value)'); | |
| 137 }); | |
| 138 } | |
| 139 | |
| 140 test_libraryPrefix() { | |
| 141 // SimpleIdentifier PrefixedIdentifier ExpressionStatement | |
| 142 addTestSource('import "dart:async" as bar; foo() {bar.^}'); | |
| 143 return computeFull((bool result) { | |
| 144 assertSuggestClass('Future'); | |
| 145 }); | |
| 146 } | |
| 147 | |
| 148 test_libraryPrefix2() { | |
| 149 // SimpleIdentifier MethodInvocation ExpressionStatement | |
| 150 addTestSource('import "dart:async" as bar; foo() {bar.^ print("f")}'); | |
| 151 return computeFull((bool result) { | |
| 152 assertSuggestClass('Future'); | |
| 153 }); | |
| 154 } | |
| 155 | |
| 156 test_local() { | |
| 157 addTestSource('foo() {String x = "bar"; x.^}'); | |
| 158 return computeFull((bool result) { | |
| 159 assertSuggestGetter('length', 'int'); | |
| 160 }); | |
| 161 } | |
| 162 | |
| 163 test_local_is() { | |
| 164 addTestSource('foo() {var x; if (x is String) x.^}'); | |
| 165 return computeFull((bool result) { | |
| 166 assertSuggestGetter('length', 'int'); | |
| 167 }); | |
| 168 } | |
| 169 | |
| 170 test_local_propogatedType() { | |
| 171 addTestSource('foo() {var x = "bar"; x.^}'); | |
| 172 return computeFull((bool result) { | |
| 173 assertSuggestGetter('length', 'int'); | |
| 174 }); | |
| 175 } | |
| 176 | |
| 177 test_method_parameters_mixed_required_and_named() { | |
| 178 addTestSource(''' | |
| 179 class C { | |
| 180 void m(x, {int y}) {} | |
| 181 } | |
| 182 void main() {new C().^}'''); | |
| 183 return computeFull((bool result) { | |
| 184 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void'); | |
| 185 expect(suggestion.parameterNames, hasLength(2)); | |
| 186 expect(suggestion.parameterNames[0], 'x'); | |
| 187 expect(suggestion.parameterTypes[0], 'dynamic'); | |
| 188 expect(suggestion.parameterNames[1], 'y'); | |
| 189 expect(suggestion.parameterTypes[1], 'int'); | |
| 190 expect(suggestion.requiredParameterCount, 1); | |
| 191 expect(suggestion.hasNamedParameters, true); | |
| 192 }); | |
| 193 } | |
| 194 | |
| 195 test_method_parameters_mixed_required_and_positional() { | |
| 196 addTestSource(''' | |
| 197 class C { | |
| 198 void m(x, [int y]) {} | |
| 199 } | |
| 200 void main() {new C().^}'''); | |
| 201 return computeFull((bool result) { | |
| 202 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void'); | |
| 203 expect(suggestion.parameterNames, hasLength(2)); | |
| 204 expect(suggestion.parameterNames[0], 'x'); | |
| 205 expect(suggestion.parameterTypes[0], 'dynamic'); | |
| 206 expect(suggestion.parameterNames[1], 'y'); | |
| 207 expect(suggestion.parameterTypes[1], 'int'); | |
| 208 expect(suggestion.requiredParameterCount, 1); | |
| 209 expect(suggestion.hasNamedParameters, false); | |
| 210 }); | |
| 211 } | |
| 212 | |
| 213 test_method_parameters_named() { | |
| 214 addTestSource(''' | |
| 215 class C { | |
| 216 void m({x, int y}) {} | |
| 217 } | |
| 218 void main() {new C().^}'''); | |
| 219 return computeFull((bool result) { | |
| 220 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void'); | |
| 221 expect(suggestion.parameterNames, hasLength(2)); | |
| 222 expect(suggestion.parameterNames[0], 'x'); | |
| 223 expect(suggestion.parameterTypes[0], 'dynamic'); | |
| 224 expect(suggestion.parameterNames[1], 'y'); | |
| 225 expect(suggestion.parameterTypes[1], 'int'); | |
| 226 expect(suggestion.requiredParameterCount, 0); | |
| 227 expect(suggestion.hasNamedParameters, true); | |
| 228 }); | |
| 229 } | |
| 230 | |
| 231 test_method_parameters_none() { | |
| 232 addTestSource(''' | |
| 233 class C { | |
| 234 void m() {} | |
| 235 } | |
| 236 void main() {new C().^}'''); | |
| 237 computeFast(); | |
| 238 return computeFull((bool result) { | |
| 239 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void'); | |
| 240 expect(suggestion.parameterNames, isEmpty); | |
| 241 expect(suggestion.parameterTypes, isEmpty); | |
| 242 expect(suggestion.requiredParameterCount, 0); | |
| 243 expect(suggestion.hasNamedParameters, false); | |
| 244 }); | |
| 245 } | |
| 246 | |
| 247 test_method_parameters_positional() { | |
| 248 addTestSource(''' | |
| 249 class C { | |
| 250 void m([x, int y]) {} | |
| 251 } | |
| 252 void main() {new C().^}'''); | |
| 253 return computeFull((bool result) { | |
| 254 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void'); | |
| 255 expect(suggestion.parameterNames, hasLength(2)); | |
| 256 expect(suggestion.parameterNames[0], 'x'); | |
| 257 expect(suggestion.parameterTypes[0], 'dynamic'); | |
| 258 expect(suggestion.parameterNames[1], 'y'); | |
| 259 expect(suggestion.parameterTypes[1], 'int'); | |
| 260 expect(suggestion.requiredParameterCount, 0); | |
| 261 expect(suggestion.hasNamedParameters, false); | |
| 262 }); | |
| 263 } | |
| 264 | |
| 265 test_method_parameters_required() { | |
| 266 addTestSource(''' | |
| 267 class C { | |
| 268 void m(x, int y) {} | |
| 269 } | |
| 270 void main() {new C().^}'''); | |
| 271 return computeFull((bool result) { | |
| 272 CompletionSuggestion suggestion = assertSuggestMethod('m', 'C', 'void'); | |
| 273 expect(suggestion.parameterNames, hasLength(2)); | |
| 274 expect(suggestion.parameterNames[0], 'x'); | |
| 275 expect(suggestion.parameterTypes[0], 'dynamic'); | |
| 276 expect(suggestion.parameterNames[1], 'y'); | |
| 277 expect(suggestion.parameterTypes[1], 'int'); | |
| 278 expect(suggestion.requiredParameterCount, 2); | |
| 279 expect(suggestion.hasNamedParameters, false); | |
| 280 }); | |
| 281 } | |
| 282 | |
| 283 test_no_parameters_field() { | |
| 284 addTestSource(''' | |
| 285 class C { | |
| 286 int x; | |
| 287 } | |
| 288 void main() {new C().^}'''); | |
| 289 return computeFull((bool result) { | |
| 290 CompletionSuggestion suggestion = assertSuggestField('x', 'int'); | |
| 291 assertHasNoParameterInfo(suggestion); | |
| 292 }); | |
| 293 } | |
| 294 | |
| 295 test_no_parameters_getter() { | |
| 296 addTestSource(''' | |
| 297 class C { | |
| 298 int get x => null; | |
| 299 } | |
| 300 void main() {int y = new C().^}'''); | |
| 301 return computeFull((bool result) { | |
| 302 CompletionSuggestion suggestion = assertSuggestGetter('x', 'int'); | |
| 303 assertHasNoParameterInfo(suggestion); | |
| 304 }); | |
| 305 } | |
| 306 | |
| 307 test_no_parameters_setter() { | |
| 308 addTestSource(''' | |
| 309 class C { | |
| 310 set x(int value) {}; | |
| 311 } | |
| 312 void main() {int y = new C().^}'''); | |
| 313 return computeFull((bool result) { | |
| 314 CompletionSuggestion suggestion = assertSuggestSetter('x'); | |
| 315 assertHasNoParameterInfo(suggestion); | |
| 316 }); | |
| 317 } | |
| 318 | |
| 319 test_only_instance() { | |
| 320 // SimpleIdentifier PropertyAccess ExpressionStatement | |
| 321 addTestSource(''' | |
| 322 class C { | |
| 323 int f1; | |
| 324 static int f2; | |
| 325 m1() {} | |
| 326 static m2() {} | |
| 327 } | |
| 328 void main() {new C().^}'''); | |
| 329 return computeFull((bool result) { | |
| 330 assertSuggestInvocationField('f1', 'int'); | |
| 331 assertNotSuggested('f2'); | |
| 332 assertSuggestMethod('m1', 'C', null); | |
| 333 assertNotSuggested('m2'); | |
| 334 }); | |
| 335 } | |
| 336 | |
| 337 test_only_instance2() { | |
| 338 // SimpleIdentifier MethodInvocation ExpressionStatement | |
| 339 addTestSource(''' | |
| 340 class C { | |
| 341 int f1; | |
| 342 static int f2; | |
| 343 m1() {} | |
| 344 static m2() {} | |
| 345 } | |
| 346 void main() {new C().^ print("something");}'''); | |
| 347 return computeFull((bool result) { | |
| 348 assertSuggestInvocationField('f1', 'int'); | |
| 349 assertNotSuggested('f2'); | |
| 350 assertSuggestMethod('m1', 'C', null); | |
| 351 assertNotSuggested('m2'); | |
| 352 }); | |
| 353 } | |
| 354 | |
| 355 test_only_static() { | |
| 356 // SimpleIdentifier PrefixedIdentifier ExpressionStatement | |
| 357 addTestSource(''' | |
| 358 class C { | |
| 359 int f1; | |
| 360 static int f2; | |
| 361 m1() {} | |
| 362 static m2() {} | |
| 363 } | |
| 364 void main() {C.^}'''); | |
| 365 return computeFull((bool result) { | |
| 366 assertNotSuggested('f1'); | |
| 367 assertSuggestInvocationField('f2', 'int'); | |
| 368 assertNotSuggested('m1'); | |
| 369 assertSuggestMethod('m2', 'C', null); | |
| 370 }); | |
| 371 } | |
| 372 | |
| 373 test_only_static2() { | |
| 374 // SimpleIdentifier MethodInvocation ExpressionStatement | |
| 375 addTestSource(''' | |
| 376 class C { | |
| 377 int f1; | |
| 378 static int f2; | |
| 379 m1() {} | |
| 380 static m2() {} | |
| 381 } | |
| 382 void main() {C.^ print("something");}'''); | |
| 383 return computeFull((bool result) { | |
| 384 assertNotSuggested('f1'); | |
| 385 assertSuggestInvocationField('f2', 'int'); | |
| 386 assertNotSuggested('m1'); | |
| 387 assertSuggestMethod('m2', 'C', null); | |
| 388 }); | |
| 389 } | |
| 390 | |
| 391 test_param() { | |
| 392 addTestSource('foo(String x) {x.^}'); | |
| 393 return computeFull((bool result) { | |
| 394 assertSuggestGetter('length', 'int'); | |
| 395 }); | |
| 396 } | |
| 397 | |
| 398 test_param_is() { | |
| 399 addTestSource('foo(x) {if (x is String) x.^}'); | |
| 400 return computeFull((bool result) { | |
| 401 assertSuggestGetter('length', 'int'); | |
| 402 }); | |
| 403 } | |
| 404 | |
| 405 test_shadowing_field_over_field() => | |
| 406 check_shadowing('int x;', 'int x;', true); | |
| 407 | |
| 408 test_shadowing_field_over_getter() => | |
| 409 check_shadowing('int x;', 'int get x => null;', true); | |
| 410 | |
| 411 test_shadowing_field_over_method() => | |
| 412 check_shadowing('int x;', 'void x() {}', true); | |
| 413 | |
| 414 test_shadowing_field_over_setter() => | |
| 415 check_shadowing('int x;', 'set x(int value) {}', true); | |
| 416 | |
| 417 test_shadowing_getter_over_field() => | |
| 418 check_shadowing('int get x => null;', 'int x;', true); | |
| 419 | |
| 420 test_shadowing_getter_over_getter() => | |
| 421 check_shadowing('int get x => null;', 'int get x => null;', true); | |
| 422 | |
| 423 test_shadowing_getter_over_method() => | |
| 424 check_shadowing('int get x => null;', 'void x() {}', true); | |
| 425 | |
| 426 test_shadowing_getter_over_setter() => | |
| 427 check_shadowing('int get x => null;', 'set x(int value) {}', true); | |
| 428 | |
| 429 test_shadowing_method_over_field() => | |
| 430 check_shadowing('void x() {}', 'int x;', true); | |
| 431 | |
| 432 test_shadowing_method_over_getter() => | |
| 433 check_shadowing('void x() {}', 'int get x => null;', true); | |
| 434 | |
| 435 test_shadowing_method_over_method() => | |
| 436 check_shadowing('void x() {}', 'void x() {}', true); | |
| 437 | |
| 438 test_shadowing_method_over_setter() => | |
| 439 check_shadowing('void x() {}', 'set x(int value) {}', true); | |
| 440 | |
| 441 test_shadowing_mixin_order() { | |
| 442 addTestSource(''' | |
| 443 class Base { | |
| 444 } | |
| 445 class Mixin1 { | |
| 446 void f() {} | |
| 447 } | |
| 448 class Mixin2 { | |
| 449 void f() {} | |
| 450 } | |
| 451 class Derived extends Base with Mixin1, Mixin2 { | |
| 452 } | |
| 453 void test(Derived d) { | |
| 454 d.^ | |
| 455 } | |
| 456 '''); | |
| 457 return computeFull((bool result) { | |
| 458 // Note: due to dartbug.com/22069, analyzer currently analyzes mixins in | |
| 459 // reverse order. The correct order is that Derived inherits from | |
| 460 // "Base with Mixin1, Mixin2", which inherits from "Base with Mixin1", | |
| 461 // which inherits from "Base". So the definition of f in Mixin2 should | |
| 462 // shadow the definition in Mixin1. | |
| 463 assertSuggestMethod('f', 'Mixin2', 'void'); | |
| 464 }); | |
| 465 } | |
| 466 | |
| 467 test_shadowing_mixin_over_superclass() { | |
| 468 addTestSource(''' | |
| 469 class Base { | |
| 470 void f() {} | |
| 471 } | |
| 472 class Mixin { | |
| 473 void f() {} | |
| 474 } | |
| 475 class Derived extends Base with Mixin { | |
| 476 } | |
| 477 void test(Derived d) { | |
| 478 d.^ | |
| 479 } | |
| 480 '''); | |
| 481 return computeFull((bool result) { | |
| 482 assertSuggestMethod('f', 'Mixin', 'void'); | |
| 483 }); | |
| 484 } | |
| 485 | |
| 486 test_shadowing_setter_over_field() => | |
| 487 check_shadowing('set x(int value) {}', 'int x;', true); | |
| 488 | |
| 489 test_shadowing_setter_over_getter() => | |
| 490 check_shadowing('set x(int value) {}', 'int get x => null;', true); | |
| 491 | |
| 492 test_shadowing_setter_over_method() => | |
| 493 check_shadowing('set x(int value) {}', 'void x() {}', true); | |
| 494 | |
| 495 test_shadowing_setter_over_setter() => | |
| 496 check_shadowing('set x(int value) {}', 'set x(int value) {}', true); | |
| 497 | |
| 498 test_shadowing_superclass_over_interface() { | |
| 499 addTestSource(''' | |
| 500 class Base { | |
| 501 void f() {} | |
| 502 } | |
| 503 class Interface { | |
| 504 void f() {} | |
| 505 } | |
| 506 class Derived extends Base implements Interface { | |
| 507 } | |
| 508 void test(Derived d) { | |
| 509 d.^ | |
| 510 } | |
| 511 '''); | |
| 512 return computeFull((bool result) { | |
| 513 assertSuggestMethod('f', 'Base', 'void'); | |
| 514 }); | |
| 515 } | |
| 516 } | |
| OLD | NEW |