| 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.refactoring.extract_method; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analysis_server/src/protocol2.dart'; |
| 10 import 'package:analysis_server/src/services/refactoring/extract_method.dart'; |
| 11 import 'package:analysis_testing/reflective_tests.dart'; |
| 12 import 'package:unittest/unittest.dart'; |
| 13 |
| 14 import 'abstract_refactoring.dart'; |
| 15 |
| 16 |
| 17 main() { |
| 18 groupSep = ' | '; |
| 19 runReflectiveTests(ExtractMethodTest); |
| 20 } |
| 21 |
| 22 |
| 23 @ReflectiveTestCase() |
| 24 class ExtractMethodTest extends RefactoringTest { |
| 25 ExtractMethodRefactoringImpl refactoring; |
| 26 |
| 27 test_bad_assignmentLeftHandSide() { |
| 28 indexTestUnit(''' |
| 29 main() { |
| 30 int aaa; |
| 31 aaa = 0; |
| 32 } |
| 33 '''); |
| 34 _createRefactoringForString('aaa '); |
| 35 return _assertConditionsFatal( |
| 36 'Cannot extract the left-hand side of an assignment.'); |
| 37 } |
| 38 |
| 39 test_bad_comment_selectionEndsInside() { |
| 40 indexTestUnit(''' |
| 41 main() { |
| 42 // start |
| 43 print(0); |
| 44 /* |
| 45 // end |
| 46 */ |
| 47 } |
| 48 '''); |
| 49 _createRefactoringForStartEndComments(); |
| 50 return _assertConditionsFatal('Selection ends inside a comment.'); |
| 51 } |
| 52 |
| 53 test_bad_comment_selectionStartsInside() { |
| 54 indexTestUnit(''' |
| 55 main() { |
| 56 /* |
| 57 // start |
| 58 */ |
| 59 print(0); |
| 60 // end |
| 61 } |
| 62 '''); |
| 63 _createRefactoringForStartEndComments(); |
| 64 return _assertConditionsFatal('Selection begins inside a comment.'); |
| 65 } |
| 66 |
| 67 test_bad_conflict_method_alreadyDeclaresMethod() { |
| 68 indexTestUnit(''' |
| 69 class A { |
| 70 void res() {} |
| 71 main() { |
| 72 // start |
| 73 print(0); |
| 74 // end |
| 75 } |
| 76 } |
| 77 '''); |
| 78 _createRefactoringForStartEndComments(); |
| 79 // TODO(scheglov) implement |
| 80 // return _assertConditionsError("Class 'A' already declares method with name
'res'."); |
| 81 } |
| 82 |
| 83 test_bad_conflict_method_shadowsSuperDeclaration() { |
| 84 indexTestUnit(''' |
| 85 class A { |
| 86 void res() {} // marker |
| 87 } |
| 88 class B extends A { |
| 89 main() { |
| 90 res(); |
| 91 // start |
| 92 print(0); |
| 93 // end |
| 94 } |
| 95 } |
| 96 '''); |
| 97 _createRefactoringForStartEndComments(); |
| 98 // TODO(scheglov) implement |
| 99 // return _assertConditionsError("Created method will shadow method 'A.res'."
); |
| 100 } |
| 101 |
| 102 test_bad_conflict_topLevel_alreadyDeclaresFunction() { |
| 103 indexTestUnit(''' |
| 104 void res() {} |
| 105 main() { |
| 106 // start |
| 107 print(0); |
| 108 // end |
| 109 } |
| 110 '''); |
| 111 _createRefactoringForStartEndComments(); |
| 112 // TODO(scheglov) implement |
| 113 // return _assertConditionsError("Library already declares function with name
'res'."); |
| 114 } |
| 115 |
| 116 test_bad_conflict_topLevel_willHideInheritedMemberUsage() { |
| 117 indexTestUnit(''' |
| 118 class A { |
| 119 void res() {} |
| 120 } |
| 121 class B extends A { |
| 122 foo() { |
| 123 res(); // marker |
| 124 } |
| 125 } |
| 126 main() { |
| 127 // start |
| 128 print(0); |
| 129 // end |
| 130 } |
| 131 '''); |
| 132 _createRefactoringForStartEndComments(); |
| 133 // TODO(scheglov) implement |
| 134 // return _assertConditionsError("Created function will shadow method 'A.res'
."); |
| 135 } |
| 136 |
| 137 test_bad_constructor_initializer() { |
| 138 indexTestUnit(''' |
| 139 class A { |
| 140 int f; |
| 141 A() : f = 0 {} |
| 142 } |
| 143 '''); |
| 144 _createRefactoringForString('f = 0'); |
| 145 return _assertConditionsFatal( |
| 146 'Cannot extract a constructor initializer. Select expression part of ini
tializer.'); |
| 147 } |
| 148 |
| 149 test_bad_constructor_redirectingConstructor() { |
| 150 indexTestUnit(''' |
| 151 class A { |
| 152 A() : this.named(); |
| 153 A.named() {} |
| 154 } |
| 155 '''); |
| 156 _createRefactoringForString('this.named()'); |
| 157 return _assertConditionsFatal( |
| 158 'Cannot extract a constructor initializer. Select expression part of ini
tializer.'); |
| 159 } |
| 160 |
| 161 test_bad_constructor_superConstructor() { |
| 162 indexTestUnit(''' |
| 163 class A {} |
| 164 class B extends A { |
| 165 B() : super(); |
| 166 } |
| 167 '''); |
| 168 _createRefactoringForString('super()'); |
| 169 return _assertConditionsFatal( |
| 170 'Cannot extract a constructor initializer. Select expression part of ini
tializer.'); |
| 171 } |
| 172 |
| 173 test_bad_doWhile_body() { |
| 174 indexTestUnit(''' |
| 175 main() { |
| 176 do |
| 177 // start |
| 178 { |
| 179 } |
| 180 // end |
| 181 while (true); |
| 182 } |
| 183 '''); |
| 184 _createRefactoringForStartEndComments(); |
| 185 return _assertConditionsFatal( |
| 186 "Operation not applicable to a 'do' statement's body and expression."); |
| 187 } |
| 188 |
| 189 test_bad_emptySelection() { |
| 190 indexTestUnit(''' |
| 191 main() { |
| 192 // start |
| 193 // end |
| 194 print(0); |
| 195 } |
| 196 '''); |
| 197 _createRefactoringForStartEndComments(); |
| 198 return _assertConditionsFatal( |
| 199 "Can only extract a single expression or a set of statements."); |
| 200 } |
| 201 |
| 202 test_bad_forLoop_conditionAndUpdaters() { |
| 203 indexTestUnit(''' |
| 204 main() { |
| 205 for ( |
| 206 int i = 0; |
| 207 // start |
| 208 i < 10; |
| 209 i++ |
| 210 // end |
| 211 ) {} |
| 212 } |
| 213 '''); |
| 214 _createRefactoringForStartEndComments(); |
| 215 return _assertConditionsFatal( |
| 216 "Operation not applicable to a 'for' statement's condition and updaters.
"); |
| 217 } |
| 218 |
| 219 test_bad_forLoop_init() { |
| 220 indexTestUnit(''' |
| 221 main() { |
| 222 for ( |
| 223 // start |
| 224 int i = 0 |
| 225 // end |
| 226 ; i < 10; |
| 227 i++ |
| 228 ) {} |
| 229 } |
| 230 '''); |
| 231 _createRefactoringForStartEndComments(); |
| 232 return _assertConditionsFatal( |
| 233 "Cannot extract initialization part of a 'for' statement."); |
| 234 } |
| 235 |
| 236 test_bad_forLoop_initAndCondition() { |
| 237 indexTestUnit(''' |
| 238 main() { |
| 239 for ( |
| 240 // start |
| 241 int i = 0; |
| 242 i < 10; |
| 243 // end |
| 244 i++ |
| 245 ) {} |
| 246 } |
| 247 '''); |
| 248 _createRefactoringForStartEndComments(); |
| 249 return _assertConditionsFatal( |
| 250 "Operation not applicable to a 'for' statement's initializer and conditi
on."); |
| 251 } |
| 252 |
| 253 test_bad_forLoop_updaters() { |
| 254 indexTestUnit(''' |
| 255 main() { |
| 256 for ( |
| 257 int i = 0; |
| 258 i < 10; |
| 259 // start |
| 260 i++ |
| 261 // end |
| 262 ) {} |
| 263 } |
| 264 '''); |
| 265 _createRefactoringForStartEndComments(); |
| 266 return _assertConditionsFatal( |
| 267 "Cannot extract increment part of a 'for' statement."); |
| 268 } |
| 269 |
| 270 test_bad_forLoop_updatersAndBody() { |
| 271 indexTestUnit(''' |
| 272 main() { |
| 273 for ( |
| 274 int i = 0; |
| 275 i < 10; |
| 276 // start |
| 277 i++ |
| 278 ) {} |
| 279 // end |
| 280 } |
| 281 '''); |
| 282 _createRefactoringForStartEndComments(); |
| 283 return _assertConditionsFatal( |
| 284 "Operation not applicable to a 'for' statement's updaters and body."); |
| 285 } |
| 286 |
| 287 test_bad_methodName_reference() { |
| 288 indexTestUnit(''' |
| 289 main() { |
| 290 main(); |
| 291 } |
| 292 '''); |
| 293 _createRefactoringWithSuffix('main', '();'); |
| 294 return _assertConditionsFatal("Cannot extract a single method name."); |
| 295 } |
| 296 |
| 297 test_bad_namePartOfDeclaration_function() { |
| 298 indexTestUnit(''' |
| 299 main() { |
| 300 } |
| 301 '''); |
| 302 _createRefactoringForString('main'); |
| 303 return _assertConditionsFatal( |
| 304 "Cannot extract the name part of a declaration."); |
| 305 } |
| 306 |
| 307 test_bad_namePartOfDeclaration_variable() { |
| 308 indexTestUnit(''' |
| 309 main() { |
| 310 int vvv = 0; |
| 311 } |
| 312 '''); |
| 313 _createRefactoringForString('vvv'); |
| 314 return _assertConditionsFatal( |
| 315 "Cannot extract the name part of a declaration."); |
| 316 } |
| 317 |
| 318 test_bad_namePartOfQualified() { |
| 319 indexTestUnit(''' |
| 320 class A { |
| 321 var fff; |
| 322 } |
| 323 main() { |
| 324 A a; |
| 325 a.fff = 1; |
| 326 } |
| 327 '''); |
| 328 _createRefactoringWithSuffix('fff', ' = 1'); |
| 329 return _assertConditionsFatal( |
| 330 "Can not extract name part of a property access."); |
| 331 } |
| 332 |
| 333 test_bad_newMethodName_notIdentifier() { |
| 334 indexTestUnit(''' |
| 335 main() { |
| 336 // start |
| 337 print(0); |
| 338 // end |
| 339 } |
| 340 '''); |
| 341 _createRefactoringForStartEndComments(); |
| 342 refactoring.name = 'bad-name'; |
| 343 // TODO(scheglov) implement |
| 344 // return _assertConditionsFatal("Method name must not contain '-'."); |
| 345 } |
| 346 |
| 347 test_bad_notSameParent() { |
| 348 indexTestUnit(''' |
| 349 main() { |
| 350 while (false) |
| 351 // start |
| 352 { |
| 353 } |
| 354 print(0); |
| 355 // end |
| 356 } |
| 357 '''); |
| 358 _createRefactoringForStartEndComments(); |
| 359 return _assertConditionsFatal( |
| 360 'Not all selected statements are enclosed by the same parent statement.'
); |
| 361 } |
| 362 |
| 363 test_bad_parameterName_duplicate() { |
| 364 indexTestUnit(''' |
| 365 main() { |
| 366 int v1 = 1; |
| 367 int v2 = 2; |
| 368 // start |
| 369 int a = v1 + v2; // marker |
| 370 // end |
| 371 } |
| 372 '''); |
| 373 _createRefactoringForStartEndComments(); |
| 374 // TODO(scheglov) implement |
| 375 // update parameters |
| 376 // { |
| 377 // Parameter[] parameters = refactoring.getParameters(); |
| 378 // assertThat(parameters).hasSize(2); |
| 379 // parameters[0].setNewName("dup"); |
| 380 // parameters[1].setNewName("dup"); |
| 381 // refactoring.setParameters(parameters); |
| 382 // } |
| 383 // // check conditions |
| 384 // refactoringStatus = refactoring.checkFinalConditions(pm); |
| 385 // assertRefactoringStatus( |
| 386 // refactoringStatus, |
| 387 // RefactoringStatusSeverity.ERROR, |
| 388 // "Parameter 'dup' already exists"); |
| 389 } |
| 390 |
| 391 test_bad_parameterName_inUse() { |
| 392 indexTestUnit(''' |
| 393 main() { |
| 394 int v1 = 1; |
| 395 int v2 = 2; |
| 396 // start |
| 397 int a = v1 + v2; // marker |
| 398 // end |
| 399 } |
| 400 '''); |
| 401 _createRefactoringForStartEndComments(); |
| 402 // TODO(scheglov) implement |
| 403 // update parameters |
| 404 // { |
| 405 // Parameter[] parameters = refactoring.getParameters(); |
| 406 // assertThat(parameters).hasSize(2); |
| 407 // parameters[0].setNewName("a"); |
| 408 // refactoring.setParameters(parameters); |
| 409 // } |
| 410 // // check conditions |
| 411 // refactoringStatus = refactoring.checkFinalConditions(pm); |
| 412 // assertRefactoringStatus( |
| 413 // refactoringStatus, |
| 414 // RefactoringStatusSeverity.ERROR, |
| 415 // "'a' is already used as a name in the selected code"); |
| 416 } |
| 417 |
| 418 test_bad_selectionEndsInSomeNode() { |
| 419 indexTestUnit(''' |
| 420 main() { |
| 421 // start |
| 422 print(0); |
| 423 print(1); |
| 424 // end |
| 425 } |
| 426 '''); |
| 427 _createRefactoringForStartEndString('print(0', 'int(1)'); |
| 428 return _assertConditionsFatal( |
| 429 "The selection does not cover a set of statements or an expression. " |
| 430 "Extend selection to a valid range."); |
| 431 } |
| 432 |
| 433 test_bad_statements_return_andAssignsVariable() { |
| 434 indexTestUnit(''' |
| 435 main() { |
| 436 // start |
| 437 var v = 0; |
| 438 return 42; |
| 439 // end |
| 440 print(v); |
| 441 } |
| 442 '''); |
| 443 _createRefactoringForStartEndComments(); |
| 444 return _assertConditionsFatal( |
| 445 "Ambiguous return value: Selected block contains assignment(s) to " |
| 446 "local variables and return statement."); |
| 447 } |
| 448 |
| 449 test_bad_switchCase() { |
| 450 indexTestUnit(''' |
| 451 main() { |
| 452 switch (1) { |
| 453 // start |
| 454 case 0: break; |
| 455 // end |
| 456 } |
| 457 } |
| 458 '''); |
| 459 _createRefactoringForStartEndComments(); |
| 460 return _assertConditionsFatal( |
| 461 "Selection must either cover whole switch statement " |
| 462 "or parts of a single case block."); |
| 463 } |
| 464 |
| 465 test_bad_tokensBetweenLastNodeAndSelectionEnd() { |
| 466 indexTestUnit(''' |
| 467 main() { |
| 468 // start |
| 469 print(0); |
| 470 print(1); |
| 471 } |
| 472 // end |
| 473 '''); |
| 474 _createRefactoringForStartEndComments(); |
| 475 return _assertConditionsFatal( |
| 476 "The end of the selection contains characters that do not belong to a st
atement."); |
| 477 } |
| 478 |
| 479 test_bad_tokensBetweenSelectionStartAndFirstNode() { |
| 480 indexTestUnit(''' |
| 481 main() { |
| 482 // start |
| 483 print(0); // marker |
| 484 print(1); |
| 485 // end |
| 486 } |
| 487 '''); |
| 488 _createRefactoringForStartEndString('); // marker', '// end'); |
| 489 return _assertConditionsFatal( |
| 490 "The beginning of the selection contains characters that do not belong t
o a statement."); |
| 491 } |
| 492 |
| 493 test_bad_try_catchBlock_block() { |
| 494 indexTestUnit(''' |
| 495 main() { |
| 496 try |
| 497 {} |
| 498 catch (e) |
| 499 // start |
| 500 {} |
| 501 // end |
| 502 } |
| 503 '''); |
| 504 _createRefactoringForStartEndComments(); |
| 505 return _assertConditionsFatal( |
| 506 "Selection must either cover whole try statement or " |
| 507 "parts of try, catch, or finally block."); |
| 508 } |
| 509 |
| 510 test_bad_try_catchBlock_complete() { |
| 511 indexTestUnit(''' |
| 512 main() { |
| 513 try |
| 514 {} |
| 515 // start |
| 516 catch (e) |
| 517 {} |
| 518 // end |
| 519 } |
| 520 '''); |
| 521 _createRefactoringForStartEndComments(); |
| 522 return _assertConditionsFatal( |
| 523 "Selection must either cover whole try statement or " |
| 524 "parts of try, catch, or finally block."); |
| 525 } |
| 526 |
| 527 test_bad_try_catchBlock_exception() { |
| 528 indexTestUnit(''' |
| 529 main() { |
| 530 try { |
| 531 } catch ( |
| 532 // start |
| 533 e |
| 534 // end |
| 535 ) { |
| 536 } |
| 537 } |
| 538 '''); |
| 539 _createRefactoringForStartEndComments(); |
| 540 return _assertConditionsFatal( |
| 541 'Cannot extract the name part of a declaration.'); |
| 542 } |
| 543 |
| 544 test_bad_try_finallyBlock() { |
| 545 indexTestUnit(''' |
| 546 main() { |
| 547 try |
| 548 {} |
| 549 finally |
| 550 // start |
| 551 {} |
| 552 // end |
| 553 } |
| 554 '''); |
| 555 _createRefactoringForStartEndComments(); |
| 556 return _assertConditionsFatal( |
| 557 "Selection must either cover whole try statement or " |
| 558 "parts of try, catch, or finally block."); |
| 559 } |
| 560 |
| 561 test_bad_try_tryBlock() { |
| 562 indexTestUnit(''' |
| 563 main() { |
| 564 try |
| 565 // start |
| 566 {} |
| 567 // end |
| 568 finally |
| 569 {} |
| 570 } |
| 571 '''); |
| 572 _createRefactoringForStartEndComments(); |
| 573 return _assertConditionsFatal( |
| 574 "Selection must either cover whole try statement or " |
| 575 "parts of try, catch, or finally block."); |
| 576 } |
| 577 |
| 578 test_bad_typeReference() { |
| 579 indexTestUnit(''' |
| 580 main() { |
| 581 int a = 0; |
| 582 } |
| 583 '''); |
| 584 _createRefactoringForString("int"); |
| 585 return _assertConditionsFatal( |
| 586 "Cannot extract a single type reference."); |
| 587 } |
| 588 |
| 589 test_bad_variableDeclarationFragment() { |
| 590 indexTestUnit(''' |
| 591 main() { |
| 592 int |
| 593 // start |
| 594 a = 1 |
| 595 // end |
| 596 ,b = 2; |
| 597 } |
| 598 '''); |
| 599 _createRefactoringForStartEndComments(); |
| 600 return _assertConditionsFatal( |
| 601 "Cannot extract a variable declaration fragment. Select whole declaratio
n statement."); |
| 602 } |
| 603 |
| 604 test_bad_while_conditionAndBody() { |
| 605 indexTestUnit(''' |
| 606 main() { |
| 607 while |
| 608 // start |
| 609 (false) |
| 610 { |
| 611 } |
| 612 // end |
| 613 } |
| 614 '''); |
| 615 _createRefactoringForStartEndComments(); |
| 616 return _assertConditionsFatal( |
| 617 "Operation not applicable to a while statement's expression and body."); |
| 618 } |
| 619 |
| 620 test_canExtractGetter_false_fieldAssignment() { |
| 621 indexTestUnit(''' |
| 622 class A { |
| 623 var f; |
| 624 main() { |
| 625 // start |
| 626 f = 1; |
| 627 // end |
| 628 } |
| 629 } |
| 630 '''); |
| 631 _createRefactoringForStartEndComments(); |
| 632 // apply refactoring |
| 633 return assertRefactoringConditionsOK().then((_) { |
| 634 expect(refactoring.canCreateGetter, false); |
| 635 expect(refactoring.createGetter, false); |
| 636 }); |
| 637 } |
| 638 |
| 639 test_canExtractGetter_false_hasParameters() { |
| 640 indexTestUnit(''' |
| 641 main(int p) { |
| 642 int a = p + 1; |
| 643 } |
| 644 '''); |
| 645 _createRefactoringForString('p + 1'); |
| 646 // apply refactoring |
| 647 return assertRefactoringConditionsOK().then((_) { |
| 648 expect(refactoring.canCreateGetter, false); |
| 649 expect(refactoring.createGetter, false); |
| 650 }); |
| 651 } |
| 652 |
| 653 test_canExtractGetter_false_returnNotUsed_assignment() { |
| 654 indexTestUnit(''' |
| 655 var topVar = 0; |
| 656 f(int p) { |
| 657 topVar = 5; |
| 658 } |
| 659 '''); |
| 660 _createRefactoringForString('topVar = 5'); |
| 661 // apply refactoring |
| 662 return assertRefactoringConditionsOK().then((_) { |
| 663 expect(refactoring.canCreateGetter, false); |
| 664 expect(refactoring.createGetter, false); |
| 665 }); |
| 666 } |
| 667 |
| 668 test_canExtractGetter_false_returnNotUsed_noReturn() { |
| 669 indexTestUnit(''' |
| 670 var topVar = 0; |
| 671 main() { |
| 672 // start |
| 673 int a = 1; |
| 674 int b = 2; |
| 675 topVar = a + b; |
| 676 // end |
| 677 } |
| 678 '''); |
| 679 _createRefactoringForStartEndComments(); |
| 680 // apply refactoring |
| 681 return assertRefactoringConditionsOK().then((_) { |
| 682 expect(refactoring.canCreateGetter, false); |
| 683 expect(refactoring.createGetter, false); |
| 684 }); |
| 685 } |
| 686 |
| 687 test_canExtractGetter_true() { |
| 688 indexTestUnit(''' |
| 689 main() { |
| 690 int a = 1 + 2; |
| 691 } |
| 692 '''); |
| 693 _createRefactoringForString('1 + 2'); |
| 694 // apply refactoring |
| 695 return assertRefactoringConditionsOK().then((_) { |
| 696 expect(refactoring.canCreateGetter, true); |
| 697 expect(refactoring.createGetter, true); |
| 698 }); |
| 699 } |
| 700 |
| 701 test_checkName() { |
| 702 indexTestUnit(''' |
| 703 main() { |
| 704 int a = 1 + 2; |
| 705 } |
| 706 '''); |
| 707 _createRefactoringForString('1 + 2'); |
| 708 // null |
| 709 refactoring.name = null; |
| 710 assertRefactoringStatus( |
| 711 refactoring.checkName(), |
| 712 RefactoringProblemSeverity.FATAL, |
| 713 expectedMessage: "Method name must not be null."); |
| 714 // empty |
| 715 refactoring.name = ''; |
| 716 assertRefactoringStatus( |
| 717 refactoring.checkName(), |
| 718 RefactoringProblemSeverity.FATAL, |
| 719 expectedMessage: "Method name must not be empty."); |
| 720 // OK |
| 721 refactoring.name = 'res'; |
| 722 assertRefactoringStatusOK(refactoring.checkName()); |
| 723 } |
| 724 |
| 725 test_closure_asFunction_singleExpression() { |
| 726 indexTestUnit(''' |
| 727 process(f(x)) {} |
| 728 main() { |
| 729 process((x) => x * 2); |
| 730 } |
| 731 '''); |
| 732 _createRefactoringForString('(x) => x * 2'); |
| 733 // apply refactoring |
| 734 return _assertSuccessfulRefactoring(''' |
| 735 process(f(x)) {} |
| 736 main() { |
| 737 process(res); |
| 738 } |
| 739 |
| 740 res(x) => x * 2; |
| 741 '''); |
| 742 } |
| 743 |
| 744 test_closure_asFunction_statements() { |
| 745 indexTestUnit(''' |
| 746 process(f(x)) {} |
| 747 main() { |
| 748 process((x) { |
| 749 print(x); |
| 750 return x * 2; |
| 751 }); // marker |
| 752 } |
| 753 '''); |
| 754 _createRefactoringForStartEndString('(x) {', '); // marker'); |
| 755 // apply refactoring |
| 756 return _assertSuccessfulRefactoring(''' |
| 757 process(f(x)) {} |
| 758 main() { |
| 759 process(res); // marker |
| 760 } |
| 761 |
| 762 res(x) { |
| 763 print(x); |
| 764 return x * 2; |
| 765 } |
| 766 '''); |
| 767 } |
| 768 |
| 769 test_closure_asMethod_statements() { |
| 770 indexTestUnit(''' |
| 771 process(f(x)) {} |
| 772 class A { |
| 773 int k = 2; |
| 774 main() { |
| 775 process((x) { |
| 776 print(x); |
| 777 return x * k; |
| 778 }); // marker |
| 779 } |
| 780 } |
| 781 '''); |
| 782 _createRefactoringForStartEndString('(x) {', '); // marker'); |
| 783 // apply refactoring |
| 784 return _assertSuccessfulRefactoring(''' |
| 785 process(f(x)) {} |
| 786 class A { |
| 787 int k = 2; |
| 788 main() { |
| 789 process(res); // marker |
| 790 } |
| 791 |
| 792 res(x) { |
| 793 print(x); |
| 794 return x * k; |
| 795 } |
| 796 } |
| 797 '''); |
| 798 } |
| 799 |
| 800 test_closure_bad_referencesLocalVariable() { |
| 801 indexTestUnit(''' |
| 802 process(f(x)) {} |
| 803 main() { |
| 804 int k = 2; |
| 805 process((x) => x * k); |
| 806 } |
| 807 '''); |
| 808 _createRefactoringForString('(x) => x * k'); |
| 809 // check |
| 810 return refactoring.checkInitialConditions().then((status) { |
| 811 assertRefactoringStatus( |
| 812 status, |
| 813 RefactoringProblemSeverity.FATAL, |
| 814 expectedMessage: |
| 815 'Cannot extract closure as method, it references 1 external variab
le(s).'); |
| 816 }); |
| 817 } |
| 818 |
| 819 test_closure_bad_referencesParameter() { |
| 820 indexTestUnit(''' |
| 821 process(f(x)) {} |
| 822 main(int k) { |
| 823 process((x) => x * k); |
| 824 } |
| 825 '''); |
| 826 _createRefactoringForString('(x) => x * k'); |
| 827 // check |
| 828 return refactoring.checkInitialConditions().then((status) { |
| 829 assertRefactoringStatus( |
| 830 status, |
| 831 RefactoringProblemSeverity.FATAL, |
| 832 expectedMessage: |
| 833 'Cannot extract closure as method, it references 1 external variab
le(s).'); |
| 834 }); |
| 835 } |
| 836 |
| 837 test_fromTopLevelVariableInitializerClosure() { |
| 838 indexTestUnit(''' |
| 839 var X = 1; |
| 840 |
| 841 var Y = () { |
| 842 return 1 + X; |
| 843 }; |
| 844 '''); |
| 845 _createRefactoringForString('1 + X'); |
| 846 // apply refactoring |
| 847 return _assertSuccessfulRefactoring(''' |
| 848 var X = 1; |
| 849 |
| 850 var Y = () { |
| 851 return res(); |
| 852 }; |
| 853 |
| 854 num res() => 1 + X; |
| 855 '''); |
| 856 } |
| 857 |
| 858 test_getExtractGetter_false_do() { |
| 859 indexTestUnit(''' |
| 860 main() { |
| 861 // start |
| 862 int v = 0; |
| 863 do { |
| 864 v++; |
| 865 } while (v < 10); |
| 866 // end |
| 867 print(v); |
| 868 } |
| 869 '''); |
| 870 _createRefactoringForStartEndComments(); |
| 871 // apply refactoring |
| 872 return assertRefactoringConditionsOK().then((_) { |
| 873 expect(refactoring.createGetter, false); |
| 874 }); |
| 875 } |
| 876 |
| 877 test_getExtractGetter_false_for() { |
| 878 indexTestUnit(''' |
| 879 main() { |
| 880 // start |
| 881 int v = 0; |
| 882 for (int i = 0; i < 10; i++) { |
| 883 v += i; |
| 884 } |
| 885 // end |
| 886 print(v); |
| 887 } |
| 888 '''); |
| 889 _createRefactoringForStartEndComments(); |
| 890 // apply refactoring |
| 891 return assertRefactoringConditionsOK().then((_) { |
| 892 expect(refactoring.createGetter, false); |
| 893 }); |
| 894 } |
| 895 |
| 896 test_getExtractGetter_false_forEach() { |
| 897 indexTestUnit(''' |
| 898 main() { |
| 899 // start |
| 900 int v = 0; |
| 901 for (int i in [1, 2, 3]) { |
| 902 v += i; |
| 903 } |
| 904 // end |
| 905 print(v); |
| 906 } |
| 907 '''); |
| 908 _createRefactoringForStartEndComments(); |
| 909 // apply refactoring |
| 910 return assertRefactoringConditionsOK().then((_) { |
| 911 expect(refactoring.createGetter, false); |
| 912 }); |
| 913 } |
| 914 |
| 915 test_getExtractGetter_false_methodInvocation_expression() { |
| 916 indexTestUnit(''' |
| 917 main() { |
| 918 int v = calculateSomething() + 5; |
| 919 } |
| 920 int calculateSomething() => 42; |
| 921 '''); |
| 922 _createRefactoringForString('calculateSomething() + 5'); |
| 923 // apply refactoring |
| 924 return assertRefactoringConditionsOK().then((_) { |
| 925 expect(refactoring.createGetter, false); |
| 926 }); |
| 927 } |
| 928 |
| 929 test_getExtractGetter_false_methodInvocation_statements() { |
| 930 indexTestUnit(''' |
| 931 main() { |
| 932 // start |
| 933 int v = calculateSomething(); |
| 934 // end |
| 935 print(v); |
| 936 } |
| 937 int calculateSomething() => 42; |
| 938 '''); |
| 939 _createRefactoringForStartEndComments(); |
| 940 // apply refactoring |
| 941 return assertRefactoringConditionsOK().then((_) { |
| 942 expect(refactoring.createGetter, false); |
| 943 }); |
| 944 } |
| 945 |
| 946 test_getExtractGetter_false_while() { |
| 947 indexTestUnit(''' |
| 948 main() { |
| 949 // start |
| 950 int v = 0; |
| 951 while (v < 10) { |
| 952 v++; |
| 953 } |
| 954 // end |
| 955 print(v); |
| 956 } |
| 957 '''); |
| 958 _createRefactoringForStartEndComments(); |
| 959 // apply refactoring |
| 960 return assertRefactoringConditionsOK().then((_) { |
| 961 expect(refactoring.createGetter, false); |
| 962 }); |
| 963 } |
| 964 |
| 965 test_getExtractGetter_true_simpleBlock() { |
| 966 indexTestUnit(''' |
| 967 main() { |
| 968 // start |
| 969 int v = 1 + 2; |
| 970 // end |
| 971 print(v); |
| 972 } |
| 973 '''); |
| 974 _createRefactoringForStartEndComments(); |
| 975 // apply refactoring |
| 976 return assertRefactoringConditionsOK().then((_) { |
| 977 expect(refactoring.createGetter, true); |
| 978 }); |
| 979 } |
| 980 |
| 981 test_getExtractGetter_true_singleExpression() { |
| 982 indexTestUnit(''' |
| 983 main() { |
| 984 // start |
| 985 int v = 1 + 2; |
| 986 // end |
| 987 print(v); |
| 988 } |
| 989 '''); |
| 990 _createRefactoringForString('1 + 2'); |
| 991 // apply refactoring |
| 992 return assertRefactoringConditionsOK().then((_) { |
| 993 expect(refactoring.createGetter, true); |
| 994 }); |
| 995 } |
| 996 |
| 997 test_getRefactoringName_function() { |
| 998 indexTestUnit(''' |
| 999 main() { |
| 1000 print(1 + 2); |
| 1001 } |
| 1002 '''); |
| 1003 _createRefactoringForString('1 + 2'); |
| 1004 expect(refactoring.refactoringName, 'Extract Function'); |
| 1005 } |
| 1006 |
| 1007 test_getRefactoringName_method() { |
| 1008 indexTestUnit(''' |
| 1009 class A { |
| 1010 main() { |
| 1011 print(1 + 2); |
| 1012 } |
| 1013 } |
| 1014 '''); |
| 1015 _createRefactoringForString('1 + 2'); |
| 1016 expect(refactoring.refactoringName, 'Extract Method'); |
| 1017 } |
| 1018 |
| 1019 test_setExtractGetter() { |
| 1020 indexTestUnit(''' |
| 1021 main() { |
| 1022 int a = 1 + 2; |
| 1023 } |
| 1024 '''); |
| 1025 _createRefactoringForString('1 + 2'); |
| 1026 // apply refactoring |
| 1027 return assertRefactoringConditionsOK().then((_) { |
| 1028 expect(refactoring.canCreateGetter, true); |
| 1029 expect(refactoring.createGetter, true); |
| 1030 return refactoring.createChange().then((SourceChange refactoringChange) { |
| 1031 this.refactoringChange = refactoringChange; |
| 1032 assertTestChangeResult(''' |
| 1033 main() { |
| 1034 int a = res; |
| 1035 } |
| 1036 |
| 1037 int get res => 1 + 2; |
| 1038 '''); |
| 1039 }); |
| 1040 }); |
| 1041 } |
| 1042 |
| 1043 test_singleExpression() { |
| 1044 indexTestUnit(''' |
| 1045 main() { |
| 1046 int a = 1 + 2; |
| 1047 } |
| 1048 '''); |
| 1049 _createRefactoringForString('1 + 2'); |
| 1050 // apply refactoring |
| 1051 return _assertSuccessfulRefactoring(''' |
| 1052 main() { |
| 1053 int a = res(); |
| 1054 } |
| 1055 |
| 1056 int res() => 1 + 2; |
| 1057 '''); |
| 1058 } |
| 1059 |
| 1060 test_singleExpression_cascade() { |
| 1061 indexTestUnit(''' |
| 1062 main() { |
| 1063 String s = ''; |
| 1064 var v = s..length; |
| 1065 } |
| 1066 '''); |
| 1067 _createRefactoringForString('s..length'); |
| 1068 // apply refactoring |
| 1069 return _assertSuccessfulRefactoring(''' |
| 1070 main() { |
| 1071 String s = ''; |
| 1072 var v = res(s); |
| 1073 } |
| 1074 |
| 1075 String res(String s) => s..length; |
| 1076 '''); |
| 1077 } |
| 1078 |
| 1079 test_singleExpression_dynamic() { |
| 1080 indexTestUnit(''' |
| 1081 dynaFunction() {} |
| 1082 main() { |
| 1083 var v = dynaFunction(); // marker |
| 1084 } |
| 1085 '''); |
| 1086 _createRefactoringWithSuffix('dynaFunction()', '; // marker'); |
| 1087 // apply refactoring |
| 1088 return _assertSuccessfulRefactoring(''' |
| 1089 dynaFunction() {} |
| 1090 main() { |
| 1091 var v = res(); // marker |
| 1092 } |
| 1093 |
| 1094 res() => dynaFunction(); |
| 1095 '''); |
| 1096 } |
| 1097 |
| 1098 test_singleExpression_ignore_assignmentLeftHandSize() { |
| 1099 indexTestUnit(''' |
| 1100 main() { |
| 1101 getButton().text = 'txt'; |
| 1102 print(getButton().text); // marker |
| 1103 } |
| 1104 getButton() {} |
| 1105 '''); |
| 1106 _createRefactoringWithSuffix('getButton().text', '); // marker'); |
| 1107 // apply refactoring |
| 1108 return _assertSuccessfulRefactoring(''' |
| 1109 main() { |
| 1110 getButton().text = 'txt'; |
| 1111 print(res()); // marker |
| 1112 } |
| 1113 |
| 1114 res() => getButton().text; |
| 1115 getButton() {} |
| 1116 '''); |
| 1117 } |
| 1118 |
| 1119 test_singleExpression_occurrences() { |
| 1120 indexTestUnit(''' |
| 1121 main() { |
| 1122 int v1 = 1; |
| 1123 int v2 = 2; |
| 1124 int v3 = 3; |
| 1125 int positiveA = v1 + v2; // marker |
| 1126 int positiveB = v2 + v3; |
| 1127 int positiveC = v1 + v2; |
| 1128 int positiveD = v1/*abc*/ + v2; |
| 1129 int negA = 1 + 2; |
| 1130 int negB = 1 + v2; |
| 1131 int negC = v1 + 2; |
| 1132 int negD = v1 * v2; |
| 1133 } |
| 1134 '''); |
| 1135 _createRefactoringWithSuffix('v1 + v2', '; // marker'); |
| 1136 // apply refactoring |
| 1137 return _assertSuccessfulRefactoring(''' |
| 1138 main() { |
| 1139 int v1 = 1; |
| 1140 int v2 = 2; |
| 1141 int v3 = 3; |
| 1142 int positiveA = res(v1, v2); // marker |
| 1143 int positiveB = res(v2, v3); |
| 1144 int positiveC = res(v1, v2); |
| 1145 int positiveD = res(v1, v2); |
| 1146 int negA = 1 + 2; |
| 1147 int negB = 1 + v2; |
| 1148 int negC = v1 + 2; |
| 1149 int negD = v1 * v2; |
| 1150 } |
| 1151 |
| 1152 int res(int v1, int v2) => v1 + v2; |
| 1153 '''); |
| 1154 } |
| 1155 |
| 1156 test_singleExpression_occurrences_disabled() { |
| 1157 indexTestUnit(''' |
| 1158 main() { |
| 1159 int v1 = 1; |
| 1160 int v2 = 2; |
| 1161 int v3 = 3; |
| 1162 int a = v1 + v2; // marker |
| 1163 int b = v2 + v3; |
| 1164 } |
| 1165 '''); |
| 1166 _createRefactoringWithSuffix('v1 + v2', '; // marker'); |
| 1167 refactoring.extractAll = false; |
| 1168 // apply refactoring |
| 1169 return _assertSuccessfulRefactoring(''' |
| 1170 main() { |
| 1171 int v1 = 1; |
| 1172 int v2 = 2; |
| 1173 int v3 = 3; |
| 1174 int a = res(v1, v2); // marker |
| 1175 int b = v2 + v3; |
| 1176 } |
| 1177 |
| 1178 int res(int v1, int v2) => v1 + v2; |
| 1179 '''); |
| 1180 } |
| 1181 |
| 1182 test_singleExpression_occurrences_inClassOnly() { |
| 1183 indexTestUnit(''' |
| 1184 class A { |
| 1185 myMethod() { |
| 1186 int v1 = 1; |
| 1187 int v2 = 2; |
| 1188 int positiveA = v1 + v2; // marker |
| 1189 } |
| 1190 } |
| 1191 main() { |
| 1192 int v1 = 1; |
| 1193 int v2 = 2; |
| 1194 int negA = v1 + v2; |
| 1195 } |
| 1196 '''); |
| 1197 _createRefactoringWithSuffix('v1 + v2', '; // marker'); |
| 1198 // apply refactoring |
| 1199 return _assertSuccessfulRefactoring(''' |
| 1200 class A { |
| 1201 myMethod() { |
| 1202 int v1 = 1; |
| 1203 int v2 = 2; |
| 1204 int positiveA = res(v1, v2); // marker |
| 1205 } |
| 1206 |
| 1207 int res(int v1, int v2) => v1 + v2; |
| 1208 } |
| 1209 main() { |
| 1210 int v1 = 1; |
| 1211 int v2 = 2; |
| 1212 int negA = v1 + v2; |
| 1213 } |
| 1214 '''); |
| 1215 } |
| 1216 |
| 1217 test_singleExpression_occurrences_inWholeUnit() { |
| 1218 indexTestUnit(''' |
| 1219 main() { |
| 1220 int v1 = 1; |
| 1221 int v2 = 2; |
| 1222 int positiveA = v1 + v2; // marker |
| 1223 } |
| 1224 class A { |
| 1225 myMethod() { |
| 1226 int v1 = 1; |
| 1227 int v2 = 2; |
| 1228 int positiveB = v1 + v2; |
| 1229 } |
| 1230 } |
| 1231 '''); |
| 1232 _createRefactoringWithSuffix('v1 + v2', '; // marker'); |
| 1233 // apply refactoring |
| 1234 return _assertSuccessfulRefactoring(''' |
| 1235 main() { |
| 1236 int v1 = 1; |
| 1237 int v2 = 2; |
| 1238 int positiveA = res(v1, v2); // marker |
| 1239 } |
| 1240 |
| 1241 int res(int v1, int v2) => v1 + v2; |
| 1242 class A { |
| 1243 myMethod() { |
| 1244 int v1 = 1; |
| 1245 int v2 = 2; |
| 1246 int positiveB = res(v1, v2); |
| 1247 } |
| 1248 } |
| 1249 '''); |
| 1250 } |
| 1251 |
| 1252 test_singleExpression_returnTypeGeneric() { |
| 1253 indexTestUnit(''' |
| 1254 main() { |
| 1255 var v = new List<String>(); |
| 1256 } |
| 1257 '''); |
| 1258 _createRefactoringForString('new List<String>()'); |
| 1259 // apply refactoring |
| 1260 return _assertSuccessfulRefactoring(''' |
| 1261 main() { |
| 1262 var v = res(); |
| 1263 } |
| 1264 |
| 1265 List<String> res() => new List<String>(); |
| 1266 '''); |
| 1267 } |
| 1268 |
| 1269 test_singleExpression_returnTypePrefix() { |
| 1270 indexTestUnit(''' |
| 1271 import 'dart:math' as pref; |
| 1272 main() { |
| 1273 var v = new pref.Random(); |
| 1274 } |
| 1275 '''); |
| 1276 _createRefactoringForString('new pref.Random()'); |
| 1277 // apply refactoring |
| 1278 return _assertSuccessfulRefactoring(''' |
| 1279 import 'dart:math' as pref; |
| 1280 main() { |
| 1281 var v = res(); |
| 1282 } |
| 1283 |
| 1284 pref.Random res() => new pref.Random(); |
| 1285 '''); |
| 1286 } |
| 1287 |
| 1288 test_singleExpression_staticContext_extractFromInitializer() { |
| 1289 indexTestUnit(''' |
| 1290 class A { |
| 1291 A(int v) {} |
| 1292 } |
| 1293 class B extends A { |
| 1294 B() : super(1 + 2) {} |
| 1295 } |
| 1296 '''); |
| 1297 _createRefactoringForString('1 + 2'); |
| 1298 // apply refactoring |
| 1299 return _assertSuccessfulRefactoring(''' |
| 1300 class A { |
| 1301 A(int v) {} |
| 1302 } |
| 1303 class B extends A { |
| 1304 B() : super(res()) {} |
| 1305 |
| 1306 static int res() => 1 + 2; |
| 1307 } |
| 1308 '''); |
| 1309 } |
| 1310 |
| 1311 test_singleExpression_staticContext_extractFromInstance() { |
| 1312 indexTestUnit(''' |
| 1313 class A { |
| 1314 instanceMethodA() { |
| 1315 int v1 = 1; |
| 1316 int v2 = 2; |
| 1317 int positiveA = v1 + v2; // marker |
| 1318 } |
| 1319 instanceMethodB() { |
| 1320 int v1 = 1; |
| 1321 int v2 = 2; |
| 1322 int positiveB = v1 + v2; |
| 1323 } |
| 1324 static staticMethodA() { |
| 1325 int v1 = 1; |
| 1326 int v2 = 2; |
| 1327 int positiveA = v1 + v2; |
| 1328 } |
| 1329 } |
| 1330 '''); |
| 1331 _createRefactoringWithSuffix('v1 + v2', '; // marker'); |
| 1332 // apply refactoring |
| 1333 return _assertSuccessfulRefactoring(''' |
| 1334 class A { |
| 1335 instanceMethodA() { |
| 1336 int v1 = 1; |
| 1337 int v2 = 2; |
| 1338 int positiveA = res(v1, v2); // marker |
| 1339 } |
| 1340 |
| 1341 static int res(int v1, int v2) => v1 + v2; |
| 1342 instanceMethodB() { |
| 1343 int v1 = 1; |
| 1344 int v2 = 2; |
| 1345 int positiveB = res(v1, v2); |
| 1346 } |
| 1347 static staticMethodA() { |
| 1348 int v1 = 1; |
| 1349 int v2 = 2; |
| 1350 int positiveA = res(v1, v2); |
| 1351 } |
| 1352 } |
| 1353 '''); |
| 1354 } |
| 1355 |
| 1356 test_singleExpression_staticContext_extractFromStatic() { |
| 1357 indexTestUnit(''' |
| 1358 class A { |
| 1359 static staticMethodA() { |
| 1360 int v1 = 1; |
| 1361 int v2 = 2; |
| 1362 int positiveA = v1 + v2; // marker |
| 1363 } |
| 1364 static staticMethodB() { |
| 1365 int v1 = 1; |
| 1366 int v2 = 2; |
| 1367 int positiveB = v1 + v2; |
| 1368 } |
| 1369 instanceMethodA() { |
| 1370 int v1 = 1; |
| 1371 int v2 = 2; |
| 1372 int positiveA = v1 + v2; |
| 1373 } |
| 1374 } |
| 1375 '''); |
| 1376 _createRefactoringWithSuffix('v1 + v2', '; // marker'); |
| 1377 // apply refactoring |
| 1378 return _assertSuccessfulRefactoring(''' |
| 1379 class A { |
| 1380 static staticMethodA() { |
| 1381 int v1 = 1; |
| 1382 int v2 = 2; |
| 1383 int positiveA = res(v1, v2); // marker |
| 1384 } |
| 1385 |
| 1386 static int res(int v1, int v2) => v1 + v2; |
| 1387 static staticMethodB() { |
| 1388 int v1 = 1; |
| 1389 int v2 = 2; |
| 1390 int positiveB = res(v1, v2); |
| 1391 } |
| 1392 instanceMethodA() { |
| 1393 int v1 = 1; |
| 1394 int v2 = 2; |
| 1395 int positiveA = res(v1, v2); |
| 1396 } |
| 1397 } |
| 1398 '''); |
| 1399 } |
| 1400 |
| 1401 test_singleExpression_staticContext_hasInInitializer() { |
| 1402 indexTestUnit(''' |
| 1403 class A { |
| 1404 A(int v) {} |
| 1405 } |
| 1406 class B extends A { |
| 1407 B() : super(1 + 2) {} |
| 1408 foo() { |
| 1409 print(1 + 2); // marker |
| 1410 } |
| 1411 } |
| 1412 '''); |
| 1413 _createRefactoringWithSuffix('1 + 2', '); // marker'); |
| 1414 // apply refactoring |
| 1415 return _assertSuccessfulRefactoring(''' |
| 1416 class A { |
| 1417 A(int v) {} |
| 1418 } |
| 1419 class B extends A { |
| 1420 B() : super(res()) {} |
| 1421 foo() { |
| 1422 print(res()); // marker |
| 1423 } |
| 1424 |
| 1425 static int res() => 1 + 2; |
| 1426 } |
| 1427 '''); |
| 1428 } |
| 1429 |
| 1430 test_singleExpression_usesParameter() { |
| 1431 indexTestUnit(''' |
| 1432 fooA(int a1) { |
| 1433 int a2 = 2; |
| 1434 int a = a1 + a2; |
| 1435 } |
| 1436 fooB(int b1) { |
| 1437 int b2 = 2; |
| 1438 int b = b1 + b2; |
| 1439 } |
| 1440 '''); |
| 1441 _createRefactoringForString('a1 + a2'); |
| 1442 // apply refactoring |
| 1443 return _assertSuccessfulRefactoring(''' |
| 1444 fooA(int a1) { |
| 1445 int a2 = 2; |
| 1446 int a = res(a1, a2); |
| 1447 } |
| 1448 |
| 1449 int res(int a1, int a2) => a1 + a2; |
| 1450 fooB(int b1) { |
| 1451 int b2 = 2; |
| 1452 int b = res(b1, b2); |
| 1453 } |
| 1454 '''); |
| 1455 } |
| 1456 |
| 1457 test_singleExpression_withVariables() { |
| 1458 indexTestUnit(''' |
| 1459 main() { |
| 1460 int v1 = 1; |
| 1461 int v2 = 2; |
| 1462 int a = v1 + v2 + v1; |
| 1463 } |
| 1464 '''); |
| 1465 _createRefactoringForString('v1 + v2 + v1'); |
| 1466 // apply refactoring |
| 1467 return _assertSuccessfulRefactoring(''' |
| 1468 main() { |
| 1469 int v1 = 1; |
| 1470 int v2 = 2; |
| 1471 int a = res(v1, v2); |
| 1472 } |
| 1473 |
| 1474 int res(int v1, int v2) => v1 + v2 + v1; |
| 1475 '''); |
| 1476 } |
| 1477 |
| 1478 test_singleExpression_withVariables_doRename() { |
| 1479 // TODO(scheglov) |
| 1480 } |
| 1481 |
| 1482 test_singleExpression_withVariables_doReorder() { |
| 1483 // TODO(scheglov) |
| 1484 } |
| 1485 |
| 1486 test_singleExpression_withVariables_namedExpression() { |
| 1487 indexTestUnit(''' |
| 1488 main() { |
| 1489 int v1 = 1; |
| 1490 int v2 = 2; |
| 1491 int a = process(arg: v1 + v2); |
| 1492 } |
| 1493 process({arg}) {} |
| 1494 '''); |
| 1495 _createRefactoringForString('process(arg: v1 + v2)'); |
| 1496 // apply refactoring |
| 1497 return _assertSuccessfulRefactoring(''' |
| 1498 main() { |
| 1499 int v1 = 1; |
| 1500 int v2 = 2; |
| 1501 int a = res(v1, v2); |
| 1502 } |
| 1503 |
| 1504 res(int v1, int v2) => process(arg: v1 + v2); |
| 1505 process({arg}) {} |
| 1506 '''); |
| 1507 } |
| 1508 |
| 1509 test_singleExpression_withVariables_newType() { |
| 1510 // TODO(scheglov) |
| 1511 } |
| 1512 |
| 1513 test_singleExpression_withVariables_useBestType() { |
| 1514 indexTestUnit(''' |
| 1515 main() { |
| 1516 var v1 = 1; |
| 1517 var v2 = 2; |
| 1518 var a = v1 + v2 + v1; // marker |
| 1519 } |
| 1520 '''); |
| 1521 _createRefactoringForString('v1 + v2 + v1'); |
| 1522 // apply refactoring |
| 1523 return _assertSuccessfulRefactoring(''' |
| 1524 main() { |
| 1525 var v1 = 1; |
| 1526 var v2 = 2; |
| 1527 var a = res(v1, v2); // marker |
| 1528 } |
| 1529 |
| 1530 num res(int v1, int v2) => v1 + v2 + v1; |
| 1531 '''); |
| 1532 } |
| 1533 |
| 1534 test_statements_assignment() { |
| 1535 indexTestUnit(''' |
| 1536 main() { |
| 1537 int v; |
| 1538 // start |
| 1539 v = 5; |
| 1540 // end |
| 1541 print(v); |
| 1542 } |
| 1543 '''); |
| 1544 _createRefactoringForStartEndComments(); |
| 1545 // apply refactoring |
| 1546 return _assertSuccessfulRefactoring(''' |
| 1547 main() { |
| 1548 int v; |
| 1549 // start |
| 1550 v = res(v); |
| 1551 // end |
| 1552 print(v); |
| 1553 } |
| 1554 |
| 1555 int res(int v) { |
| 1556 v = 5; |
| 1557 return v; |
| 1558 } |
| 1559 '''); |
| 1560 } |
| 1561 |
| 1562 test_statements_changeIndentation() { |
| 1563 indexTestUnit(''' |
| 1564 main() { |
| 1565 { |
| 1566 // start |
| 1567 if (true) { |
| 1568 print(0); |
| 1569 } |
| 1570 // end |
| 1571 } |
| 1572 } |
| 1573 '''); |
| 1574 _createRefactoringForStartEndComments(); |
| 1575 // apply refactoring |
| 1576 return _assertSuccessfulRefactoring(''' |
| 1577 main() { |
| 1578 { |
| 1579 // start |
| 1580 res(); |
| 1581 // end |
| 1582 } |
| 1583 } |
| 1584 |
| 1585 void res() { |
| 1586 if (true) { |
| 1587 print(0); |
| 1588 } |
| 1589 } |
| 1590 '''); |
| 1591 } |
| 1592 |
| 1593 test_statements_changeIndentation_multilineString() { |
| 1594 indexTestUnit(''' |
| 1595 main() { |
| 1596 { |
| 1597 // start |
| 1598 print(""" |
| 1599 first line |
| 1600 second line |
| 1601 """); |
| 1602 // end |
| 1603 } |
| 1604 } |
| 1605 '''); |
| 1606 _createRefactoringForStartEndComments(); |
| 1607 // apply refactoring |
| 1608 return _assertSuccessfulRefactoring(''' |
| 1609 main() { |
| 1610 { |
| 1611 // start |
| 1612 res(); |
| 1613 // end |
| 1614 } |
| 1615 } |
| 1616 |
| 1617 void res() { |
| 1618 print(""" |
| 1619 first line |
| 1620 second line |
| 1621 """); |
| 1622 } |
| 1623 '''); |
| 1624 } |
| 1625 |
| 1626 test_statements_definesVariable_notUsedOutside() { |
| 1627 indexTestUnit(''' |
| 1628 main() { |
| 1629 int a = 1; |
| 1630 int b = 1; |
| 1631 // start |
| 1632 int v = a + b; |
| 1633 print(v); |
| 1634 // end |
| 1635 } |
| 1636 '''); |
| 1637 _createRefactoringForStartEndComments(); |
| 1638 // apply refactoring |
| 1639 return _assertSuccessfulRefactoring(''' |
| 1640 main() { |
| 1641 int a = 1; |
| 1642 int b = 1; |
| 1643 // start |
| 1644 res(a, b); |
| 1645 // end |
| 1646 } |
| 1647 |
| 1648 void res(int a, int b) { |
| 1649 int v = a + b; |
| 1650 print(v); |
| 1651 } |
| 1652 '''); |
| 1653 } |
| 1654 |
| 1655 test_statements_definesVariable_oneUsedOutside_assignment() { |
| 1656 indexTestUnit(''' |
| 1657 myFunctionA() { |
| 1658 int a = 1; |
| 1659 // start |
| 1660 a += 10; |
| 1661 // end |
| 1662 print(a); |
| 1663 } |
| 1664 myFunctionB() { |
| 1665 int b = 2; |
| 1666 b += 10; |
| 1667 print(b); |
| 1668 } |
| 1669 '''); |
| 1670 _createRefactoringForStartEndComments(); |
| 1671 // apply refactoring |
| 1672 return _assertSuccessfulRefactoring(''' |
| 1673 myFunctionA() { |
| 1674 int a = 1; |
| 1675 // start |
| 1676 a = res(a); |
| 1677 // end |
| 1678 print(a); |
| 1679 } |
| 1680 |
| 1681 int res(int a) { |
| 1682 a += 10; |
| 1683 return a; |
| 1684 } |
| 1685 myFunctionB() { |
| 1686 int b = 2; |
| 1687 b = res(b); |
| 1688 print(b); |
| 1689 } |
| 1690 '''); |
| 1691 } |
| 1692 |
| 1693 test_statements_definesVariable_oneUsedOutside_declaration() { |
| 1694 indexTestUnit(''' |
| 1695 myFunctionA() { |
| 1696 int a = 1; |
| 1697 int b = 2; |
| 1698 // start |
| 1699 int v1 = a + b; |
| 1700 // end |
| 1701 print(v1); |
| 1702 } |
| 1703 myFunctionB() { |
| 1704 int a = 3; |
| 1705 int b = 4; |
| 1706 int v2 = a + b; |
| 1707 print(v2); |
| 1708 } |
| 1709 '''); |
| 1710 _createRefactoringForStartEndComments(); |
| 1711 // apply refactoring |
| 1712 return _assertSuccessfulRefactoring(''' |
| 1713 myFunctionA() { |
| 1714 int a = 1; |
| 1715 int b = 2; |
| 1716 // start |
| 1717 int v1 = res(a, b); |
| 1718 // end |
| 1719 print(v1); |
| 1720 } |
| 1721 |
| 1722 int res(int a, int b) { |
| 1723 int v1 = a + b; |
| 1724 return v1; |
| 1725 } |
| 1726 myFunctionB() { |
| 1727 int a = 3; |
| 1728 int b = 4; |
| 1729 int v2 = res(a, b); |
| 1730 print(v2); |
| 1731 } |
| 1732 '''); |
| 1733 } |
| 1734 |
| 1735 test_statements_definesVariable_twoUsedOutside() { |
| 1736 indexTestUnit(''' |
| 1737 main() { |
| 1738 // start |
| 1739 int varA = 1; |
| 1740 int varB = 2; |
| 1741 // end |
| 1742 int v = varA + varB; |
| 1743 } |
| 1744 '''); |
| 1745 _createRefactoringForStartEndComments(); |
| 1746 // check conditions |
| 1747 return refactoring.checkInitialConditions().then((status) { |
| 1748 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL); |
| 1749 }); |
| 1750 } |
| 1751 |
| 1752 test_statements_duplicate_absolutelySame() { |
| 1753 indexTestUnit(''' |
| 1754 myFunctionA() { |
| 1755 print(0); |
| 1756 print(1); |
| 1757 } |
| 1758 myFunctionB() { |
| 1759 // start |
| 1760 print(0); |
| 1761 print(1); |
| 1762 // end |
| 1763 } |
| 1764 '''); |
| 1765 _createRefactoringForStartEndComments(); |
| 1766 // apply refactoring |
| 1767 return _assertSuccessfulRefactoring(''' |
| 1768 myFunctionA() { |
| 1769 res(); |
| 1770 } |
| 1771 myFunctionB() { |
| 1772 // start |
| 1773 res(); |
| 1774 // end |
| 1775 } |
| 1776 |
| 1777 void res() { |
| 1778 print(0); |
| 1779 print(1); |
| 1780 } |
| 1781 '''); |
| 1782 } |
| 1783 |
| 1784 test_statements_duplicate_declaresDifferentlyNamedVariable() { |
| 1785 indexTestUnit(''' |
| 1786 myFunctionA() { |
| 1787 int varA = 1; |
| 1788 print(varA); |
| 1789 } |
| 1790 myFunctionB() { |
| 1791 // start |
| 1792 int varB = 1; |
| 1793 print(varB); |
| 1794 // end |
| 1795 } |
| 1796 '''); |
| 1797 _createRefactoringForStartEndComments(); |
| 1798 // apply refactoring |
| 1799 return _assertSuccessfulRefactoring(''' |
| 1800 myFunctionA() { |
| 1801 res(); |
| 1802 } |
| 1803 myFunctionB() { |
| 1804 // start |
| 1805 res(); |
| 1806 // end |
| 1807 } |
| 1808 |
| 1809 void res() { |
| 1810 int varB = 1; |
| 1811 print(varB); |
| 1812 } |
| 1813 '''); |
| 1814 } |
| 1815 |
| 1816 test_statements_dynamic() { |
| 1817 indexTestUnit(''' |
| 1818 dynaFunction(p) => 0; |
| 1819 main() { |
| 1820 // start |
| 1821 var a = 1; |
| 1822 var v = dynaFunction(a); |
| 1823 // end |
| 1824 print(v); |
| 1825 } |
| 1826 '''); |
| 1827 _createRefactoringForStartEndComments(); |
| 1828 // apply refactoring |
| 1829 return _assertSuccessfulRefactoring(''' |
| 1830 dynaFunction(p) => 0; |
| 1831 main() { |
| 1832 // start |
| 1833 var v = res(); |
| 1834 // end |
| 1835 print(v); |
| 1836 } |
| 1837 |
| 1838 res() { |
| 1839 var a = 1; |
| 1840 var v = dynaFunction(a); |
| 1841 return v; |
| 1842 } |
| 1843 '''); |
| 1844 } |
| 1845 |
| 1846 /** |
| 1847 * We should always add ";" when invoke method with extracted statements. |
| 1848 */ |
| 1849 test_statements_endsWithBlock() { |
| 1850 indexTestUnit(''' |
| 1851 main() { |
| 1852 // start |
| 1853 if (true) { |
| 1854 print(0); |
| 1855 } |
| 1856 // end |
| 1857 } |
| 1858 '''); |
| 1859 _createRefactoringForStartEndComments(); |
| 1860 // apply refactoring |
| 1861 return _assertSuccessfulRefactoring(''' |
| 1862 main() { |
| 1863 // start |
| 1864 res(); |
| 1865 // end |
| 1866 } |
| 1867 |
| 1868 void res() { |
| 1869 if (true) { |
| 1870 print(0); |
| 1871 } |
| 1872 } |
| 1873 '''); |
| 1874 } |
| 1875 |
| 1876 test_statements_inSwitchMember() { |
| 1877 indexTestUnit(''' |
| 1878 class A { |
| 1879 foo(int p) { |
| 1880 switch (p) { |
| 1881 case 0: |
| 1882 // start |
| 1883 print(0); |
| 1884 // end |
| 1885 break; |
| 1886 default: |
| 1887 break; |
| 1888 } |
| 1889 } |
| 1890 } |
| 1891 '''); |
| 1892 _createRefactoringForStartEndComments(); |
| 1893 // apply refactoring |
| 1894 return _assertSuccessfulRefactoring(''' |
| 1895 class A { |
| 1896 foo(int p) { |
| 1897 switch (p) { |
| 1898 case 0: |
| 1899 // start |
| 1900 res(); |
| 1901 // end |
| 1902 break; |
| 1903 default: |
| 1904 break; |
| 1905 } |
| 1906 } |
| 1907 |
| 1908 void res() { |
| 1909 print(0); |
| 1910 } |
| 1911 } |
| 1912 '''); |
| 1913 } |
| 1914 |
| 1915 test_statements_method() { |
| 1916 indexTestUnit(''' |
| 1917 class A { |
| 1918 foo() { |
| 1919 // start |
| 1920 print(0); |
| 1921 // end |
| 1922 } |
| 1923 } |
| 1924 '''); |
| 1925 _createRefactoringForStartEndComments(); |
| 1926 // apply refactoring |
| 1927 return _assertSuccessfulRefactoring(''' |
| 1928 class A { |
| 1929 foo() { |
| 1930 // start |
| 1931 res(); |
| 1932 // end |
| 1933 } |
| 1934 |
| 1935 void res() { |
| 1936 print(0); |
| 1937 } |
| 1938 } |
| 1939 '''); |
| 1940 } |
| 1941 |
| 1942 test_statements_noDuplicates() { |
| 1943 indexTestUnit(''' |
| 1944 main() { |
| 1945 int a = 1; |
| 1946 int b = 1; |
| 1947 // start |
| 1948 print(a); |
| 1949 // end |
| 1950 } |
| 1951 '''); |
| 1952 _createRefactoringForStartEndComments(); |
| 1953 // apply refactoring |
| 1954 return _assertSuccessfulRefactoring(''' |
| 1955 main() { |
| 1956 int a = 1; |
| 1957 int b = 1; |
| 1958 // start |
| 1959 res(a); |
| 1960 // end |
| 1961 } |
| 1962 |
| 1963 void res(int a) { |
| 1964 print(a); |
| 1965 } |
| 1966 '''); |
| 1967 } |
| 1968 |
| 1969 test_statements_return_last() { |
| 1970 indexTestUnit(''' |
| 1971 main() { |
| 1972 // start |
| 1973 int v = 5; |
| 1974 return v + 1; |
| 1975 // end |
| 1976 } |
| 1977 '''); |
| 1978 _createRefactoringForStartEndComments(); |
| 1979 // apply refactoring |
| 1980 return _assertSuccessfulRefactoring(''' |
| 1981 main() { |
| 1982 // start |
| 1983 return res(); |
| 1984 // end |
| 1985 } |
| 1986 |
| 1987 int res() { |
| 1988 int v = 5; |
| 1989 return v + 1; |
| 1990 } |
| 1991 '''); |
| 1992 } |
| 1993 |
| 1994 test_statements_return_single() { |
| 1995 indexTestUnit(''' |
| 1996 main() { |
| 1997 // start |
| 1998 return 42; |
| 1999 // end |
| 2000 } |
| 2001 '''); |
| 2002 _createRefactoringForStartEndComments(); |
| 2003 // apply refactoring |
| 2004 return _assertSuccessfulRefactoring(''' |
| 2005 main() { |
| 2006 // start |
| 2007 return res(); |
| 2008 // end |
| 2009 } |
| 2010 |
| 2011 int res() { |
| 2012 return 42; |
| 2013 } |
| 2014 '''); |
| 2015 } |
| 2016 |
| 2017 /** |
| 2018 * We have 3 identical statements, but select only 2. |
| 2019 * This should not cause problems. |
| 2020 */ |
| 2021 test_statements_twoOfThree() { |
| 2022 indexTestUnit(''' |
| 2023 main() { |
| 2024 // start |
| 2025 print(0); |
| 2026 print(0); |
| 2027 // end |
| 2028 print(0); |
| 2029 } |
| 2030 '''); |
| 2031 _createRefactoringForStartEndComments(); |
| 2032 // apply refactoring |
| 2033 return _assertSuccessfulRefactoring(''' |
| 2034 main() { |
| 2035 // start |
| 2036 res(); |
| 2037 // end |
| 2038 print(0); |
| 2039 } |
| 2040 |
| 2041 void res() { |
| 2042 print(0); |
| 2043 print(0); |
| 2044 } |
| 2045 '''); |
| 2046 } |
| 2047 |
| 2048 Future _assertConditionsError(String message) { |
| 2049 return refactoring.checkAllConditions().then((status) { |
| 2050 assertRefactoringStatus( |
| 2051 status, |
| 2052 RefactoringProblemSeverity.ERROR, |
| 2053 expectedMessage: message); |
| 2054 }); |
| 2055 } |
| 2056 |
| 2057 Future _assertConditionsFatal(String message) { |
| 2058 return refactoring.checkAllConditions().then((status) { |
| 2059 assertRefactoringStatus( |
| 2060 status, |
| 2061 RefactoringProblemSeverity.FATAL, |
| 2062 expectedMessage: message); |
| 2063 }); |
| 2064 } |
| 2065 |
| 2066 /** |
| 2067 * Checks that all conditions are OK and the result of applying the [Change] |
| 2068 * to [testUnit] is [expectedCode]. |
| 2069 */ |
| 2070 Future _assertSuccessfulRefactoring(String expectedCode) { |
| 2071 return assertRefactoringConditionsOK().then((_) { |
| 2072 refactoring.createGetter = false; |
| 2073 return refactoring.createChange().then((SourceChange refactoringChange) { |
| 2074 this.refactoringChange = refactoringChange; |
| 2075 assertTestChangeResult(expectedCode); |
| 2076 }); |
| 2077 }); |
| 2078 } |
| 2079 |
| 2080 void _createRefactoring(int offset, int length) { |
| 2081 refactoring = |
| 2082 new ExtractMethodRefactoringImpl(searchEngine, testUnit, offset, length)
; |
| 2083 refactoring.name = 'res'; |
| 2084 } |
| 2085 |
| 2086 |
| 2087 // Future _assertInitialConditions_fatal_selection() { |
| 2088 // return refactoring.checkInitialConditions().then((status) { |
| 2089 // assertRefactoringStatus( |
| 2090 // status, |
| 2091 // RefactoringProblemSeverity.FATAL, |
| 2092 // expectedMessage: 'Expression must be selected to activate this refac
toring.'); |
| 2093 // }); |
| 2094 // } |
| 2095 |
| 2096 void _createRefactoringForStartEndComments() { |
| 2097 int offset = findEnd('// start') + '\n'.length; |
| 2098 int end = findOffset('// end'); |
| 2099 _createRefactoring(offset, end - offset); |
| 2100 } |
| 2101 |
| 2102 void _createRefactoringForStartEndString(String startSearch, |
| 2103 String endSearch) { |
| 2104 int offset = findOffset(startSearch); |
| 2105 int end = findOffset(endSearch); |
| 2106 _createRefactoring(offset, end - offset); |
| 2107 } |
| 2108 |
| 2109 /** |
| 2110 * Creates a new refactoring in [refactoring] for the selection range of the |
| 2111 * given [search] pattern. |
| 2112 */ |
| 2113 void _createRefactoringForString(String search) { |
| 2114 int offset = findOffset(search); |
| 2115 int length = search.length; |
| 2116 _createRefactoring(offset, length); |
| 2117 } |
| 2118 |
| 2119 void _createRefactoringWithSuffix(String selectionSearch, String suffix) { |
| 2120 int offset = findOffset(selectionSearch + suffix); |
| 2121 int length = selectionSearch.length; |
| 2122 _createRefactoring(offset, length); |
| 2123 } |
| 2124 } |
| OLD | NEW |