| 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.refactoring.extract_local; | 5 library test.services.refactoring.extract_local; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 9 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 10 import 'package:analysis_server/src/services/correction/status.dart'; | 10 import 'package:analysis_server/src/services/correction/status.dart'; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 main(); | 75 main(); |
| 76 } | 76 } |
| 77 '''); | 77 '''); |
| 78 _createRefactoringWithSuffix('main', '();'); | 78 _createRefactoringWithSuffix('main', '();'); |
| 79 // check conditions | 79 // check conditions |
| 80 RefactoringStatus status = await refactoring.checkAllConditions(); | 80 RefactoringStatus status = await refactoring.checkAllConditions(); |
| 81 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, | 81 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, |
| 82 expectedMessage: 'Cannot extract a single method name.'); | 82 expectedMessage: 'Cannot extract a single method name.'); |
| 83 } | 83 } |
| 84 | 84 |
| 85 test_checkInitialConditions_nameOfProperty_prefixedIdentifier() async { | |
| 86 indexTestUnit(''' | |
| 87 main(p) { | |
| 88 p.value; // marker | |
| 89 } | |
| 90 '''); | |
| 91 _createRefactoringWithSuffix('value', '; // marker'); | |
| 92 // check conditions | |
| 93 RefactoringStatus status = await refactoring.checkAllConditions(); | |
| 94 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, | |
| 95 expectedMessage: 'Cannot extract name part of a property access.'); | |
| 96 } | |
| 97 | |
| 98 test_checkInitialConditions_nameOfProperty_propertyAccess() async { | |
| 99 indexTestUnit(''' | |
| 100 main() { | |
| 101 foo().length; // marker | |
| 102 } | |
| 103 String foo() => ''; | |
| 104 '''); | |
| 105 _createRefactoringWithSuffix('length', '; // marker'); | |
| 106 // check conditions | |
| 107 RefactoringStatus status = await refactoring.checkAllConditions(); | |
| 108 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, | |
| 109 expectedMessage: 'Cannot extract name part of a property access.'); | |
| 110 } | |
| 111 | |
| 112 test_checkInitialConditions_namePartOfDeclaration_variable() async { | 85 test_checkInitialConditions_namePartOfDeclaration_variable() async { |
| 113 indexTestUnit(''' | 86 indexTestUnit(''' |
| 114 main() { | 87 main() { |
| 115 int vvv = 0; | 88 int vvv = 0; |
| 116 } | 89 } |
| 117 '''); | 90 '''); |
| 118 _createRefactoringWithSuffix('vvv', ' = 0;'); | 91 _createRefactoringWithSuffix('vvv', ' = 0;'); |
| 119 // check conditions | 92 // check conditions |
| 120 RefactoringStatus status = await refactoring.checkAllConditions(); | 93 RefactoringStatus status = await refactoring.checkAllConditions(); |
| 121 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, | 94 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, |
| 122 expectedMessage: 'Cannot extract the name part of a declaration.'); | 95 expectedMessage: 'Cannot extract the name part of a declaration.'); |
| 123 } | 96 } |
| 124 | 97 |
| 98 test_checkInitialConditions_noExpression() async { |
| 99 indexTestUnit(''' |
| 100 main() { |
| 101 // abc |
| 102 } |
| 103 '''); |
| 104 _createRefactoringForString('abc'); |
| 105 // check conditions |
| 106 _assertInitialConditions_fatal_selection(); |
| 107 } |
| 108 |
| 125 test_checkInitialConditions_notPartOfFunction() async { | 109 test_checkInitialConditions_notPartOfFunction() async { |
| 126 indexTestUnit(''' | 110 indexTestUnit(''' |
| 127 int a = 1 + 2; | 111 int a = 1 + 2; |
| 128 '''); | 112 '''); |
| 129 _createRefactoringForString('1 + 2'); | 113 _createRefactoringForString('1 + 2'); |
| 130 // check conditions | 114 // check conditions |
| 131 RefactoringStatus status = await refactoring.checkAllConditions(); | 115 RefactoringStatus status = await refactoring.checkAllConditions(); |
| 132 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, | 116 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, |
| 133 expectedMessage: | 117 expectedMessage: |
| 134 'Expression inside of function must be selected to activate this ref
actoring.'); | 118 'Expression inside of function must be selected to activate this ref
actoring.'); |
| 135 } | 119 } |
| 136 | 120 |
| 137 test_checkInitialConditions_stringSelection_leadingQuote() async { | 121 test_checkInitialConditions_stringSelection_leadingQuote() async { |
| 138 indexTestUnit(''' | 122 indexTestUnit(''' |
| 139 main() { | 123 main() { |
| 140 var vvv = 'abc'; | 124 var vvv = 'abc'; |
| 141 } | 125 } |
| 142 '''); | 126 '''); |
| 143 _createRefactoringForString("'a"); | 127 _createRefactoringForString("'a"); |
| 144 // check conditions | 128 // apply refactoring |
| 145 RefactoringStatus status = await refactoring.checkAllConditions(); | 129 return _assertSuccessfulRefactoring(''' |
| 146 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, | 130 main() { |
| 147 expectedMessage: | 131 var res = 'abc'; |
| 148 'Cannot extract only leading or trailing quote of string literal.'); | 132 var vvv = res; |
| 133 } |
| 134 '''); |
| 149 } | 135 } |
| 150 | 136 |
| 151 test_checkInitialConditions_stringSelection_trailingQuote() async { | 137 test_checkInitialConditions_stringSelection_trailingQuote() async { |
| 152 indexTestUnit(''' | 138 indexTestUnit(''' |
| 153 main() { | 139 main() { |
| 154 var vvv = 'abc'; | 140 var vvv = 'abc'; |
| 155 } | 141 } |
| 156 '''); | 142 '''); |
| 157 _createRefactoringForString("c'"); | 143 _createRefactoringForString("c'"); |
| 158 // check conditions | 144 // apply refactoring |
| 159 RefactoringStatus status = await refactoring.checkAllConditions(); | 145 return _assertSuccessfulRefactoring(''' |
| 160 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, | 146 main() { |
| 161 expectedMessage: | 147 var res = 'abc'; |
| 162 'Cannot extract only leading or trailing quote of string literal.'); | 148 var vvv = res; |
| 149 } |
| 150 '''); |
| 163 } | 151 } |
| 164 | 152 |
| 165 test_checkLocalName() { | 153 test_checkLocalName() { |
| 166 indexTestUnit(''' | 154 indexTestUnit(''' |
| 167 main() { | 155 main() { |
| 168 int a = 1 + 2; | 156 int a = 1 + 2; |
| 169 } | 157 } |
| 170 '''); | 158 '''); |
| 171 _createRefactoringForString('1 + 2'); | 159 _createRefactoringForString('1 + 2'); |
| 172 expect(refactoring.refactoringName, 'Extract Local Variable'); | 160 expect(refactoring.refactoringName, 'Extract Local Variable'); |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 _createRefactoringForString('2'); | 315 _createRefactoringForString('2'); |
| 328 // apply refactoring | 316 // apply refactoring |
| 329 return _assertSuccessfulRefactoring(''' | 317 return _assertSuccessfulRefactoring(''' |
| 330 main() { | 318 main() { |
| 331 const res = 2; | 319 const res = 2; |
| 332 const {1: res}; | 320 const {1: res}; |
| 333 } | 321 } |
| 334 '''); | 322 '''); |
| 335 } | 323 } |
| 336 | 324 |
| 325 test_coveringExpressions() async { |
| 326 indexTestUnit(''' |
| 327 main() { |
| 328 int aaa = 1; |
| 329 int bbb = 2; |
| 330 var c = aaa + bbb * 2 + 3; |
| 331 } |
| 332 '''); |
| 333 _createRefactoring(testCode.indexOf('bb * 2'), 0); |
| 334 // check conditions |
| 335 await refactoring.checkInitialConditions(); |
| 336 List<String> subExpressions = <String>[]; |
| 337 for (int i = 0; i < refactoring.coveringExpressionOffsets.length; i++) { |
| 338 int offset = refactoring.coveringExpressionOffsets[i]; |
| 339 int length = refactoring.coveringExpressionLengths[i]; |
| 340 subExpressions.add(testCode.substring(offset, offset + length)); |
| 341 } |
| 342 expect(subExpressions, |
| 343 ['bbb', 'bbb * 2', 'aaa + bbb * 2', 'aaa + bbb * 2 + 3']); |
| 344 } |
| 345 |
| 337 test_fragmentExpression() { | 346 test_fragmentExpression() { |
| 338 indexTestUnit(''' | 347 indexTestUnit(''' |
| 339 main() { | 348 main() { |
| 340 int a = 1 + 2 + 3 + 4; | 349 int a = 1 + 2 + 3 + 4; |
| 341 } | 350 } |
| 342 '''); | 351 '''); |
| 343 _createRefactoringForString('2 + 3'); | 352 _createRefactoringForString('2 + 3'); |
| 344 // apply refactoring | 353 // apply refactoring |
| 345 return _assertSuccessfulRefactoring(''' | 354 return _assertSuccessfulRefactoring(''' |
| 346 main() { | 355 main() { |
| 347 var res = 2 + 3; | 356 var res = 1 + 2 + 3; |
| 348 int a = 1 + res + 4; | 357 int a = res + 4; |
| 349 } | 358 } |
| 350 '''); | 359 '''); |
| 351 } | 360 } |
| 352 | 361 |
| 353 test_fragmentExpression_leadingNotWhitespace() { | 362 test_fragmentExpression_leadingNotWhitespace() { |
| 354 indexTestUnit(''' | 363 indexTestUnit(''' |
| 355 main() { | 364 main() { |
| 356 int a = 1 + 2 + 3 + 4; | 365 int a = 1 + 2 + 3 + 4; |
| 357 } | 366 } |
| 358 '''); | 367 '''); |
| 359 _createRefactoringForString('+ 2'); | 368 _createRefactoringForString('+ 2'); |
| 360 // check conditions | 369 // apply refactoring |
| 361 return _assertInitialConditions_fatal_selection(); | 370 return _assertSuccessfulRefactoring(''' |
| 371 main() { |
| 372 var res = 1 + 2; |
| 373 int a = res + 3 + 4; |
| 374 } |
| 375 '''); |
| 362 } | 376 } |
| 363 | 377 |
| 364 test_fragmentExpression_leadingPartialSelection() { | 378 test_fragmentExpression_leadingPartialSelection() { |
| 365 indexTestUnit(''' | 379 indexTestUnit(''' |
| 366 main() { | 380 main() { |
| 367 int a = 111 + 2 + 3 + 4; | 381 int a = 111 + 2 + 3 + 4; |
| 368 } | 382 } |
| 369 '''); | 383 '''); |
| 370 _createRefactoringForString('11 + 2'); | 384 _createRefactoringForString('11 + 2'); |
| 371 // check conditions | 385 // apply refactoring |
| 372 return _assertInitialConditions_fatal_selection(); | 386 return _assertSuccessfulRefactoring(''' |
| 387 main() { |
| 388 var res = 111 + 2; |
| 389 int a = res + 3 + 4; |
| 390 } |
| 391 '''); |
| 373 } | 392 } |
| 374 | 393 |
| 375 test_fragmentExpression_leadingWhitespace() { | 394 test_fragmentExpression_leadingWhitespace() { |
| 376 indexTestUnit(''' | 395 indexTestUnit(''' |
| 377 main() { | 396 main() { |
| 378 int a = 1 + 2 + 3 + 4; | 397 int a = 1 + 2 + 3 + 4; |
| 379 } | 398 } |
| 380 '''); | 399 '''); |
| 381 _createRefactoringForString(' 2 + 3'); | 400 _createRefactoringForString(' 2 + 3'); |
| 382 // apply refactoring | 401 // apply refactoring |
| 383 return _assertSuccessfulRefactoring(''' | 402 return _assertSuccessfulRefactoring(''' |
| 384 main() { | 403 main() { |
| 385 var res = 2 + 3; | 404 var res = 1 + 2 + 3; |
| 386 int a = 1 +res + 4; | 405 int a = res + 4; |
| 387 } | 406 } |
| 388 '''); | 407 '''); |
| 389 } | 408 } |
| 390 | 409 |
| 391 test_fragmentExpression_notAssociativeOperator() { | 410 test_fragmentExpression_notAssociativeOperator() { |
| 392 indexTestUnit(''' | 411 indexTestUnit(''' |
| 393 main() { | 412 main() { |
| 394 int a = 1 - 2 - 3 - 4; | 413 int a = 1 - 2 - 3 - 4; |
| 395 } | 414 } |
| 396 '''); | 415 '''); |
| 397 _createRefactoringForString('2 - 3'); | 416 _createRefactoringForString('2 - 3'); |
| 398 // check conditions | 417 // apply refactoring |
| 399 return _assertInitialConditions_fatal_selection(); | 418 return _assertSuccessfulRefactoring(''' |
| 419 main() { |
| 420 var res = 1 - 2 - 3; |
| 421 int a = res - 4; |
| 422 } |
| 423 '''); |
| 400 } | 424 } |
| 401 | 425 |
| 402 test_fragmentExpression_trailingNotWhitespace() { | 426 test_fragmentExpression_trailingNotWhitespace() { |
| 403 indexTestUnit(''' | 427 indexTestUnit(''' |
| 404 main() { | 428 main() { |
| 405 int a = 1 + 2 + 3 + 4; | 429 int a = 1 + 2 + 3 + 4; |
| 406 } | 430 } |
| 407 '''); | 431 '''); |
| 408 _createRefactoringForString('2 + 3 +'); | 432 _createRefactoringForString('1 + 2 +'); |
| 409 // check conditions | 433 // apply refactoring |
| 410 return _assertInitialConditions_fatal_selection(); | 434 return _assertSuccessfulRefactoring(''' |
| 435 main() { |
| 436 var res = 1 + 2 + 3; |
| 437 int a = res + 4; |
| 438 } |
| 439 '''); |
| 411 } | 440 } |
| 412 | 441 |
| 413 test_fragmentExpression_trailingPartialSelection() { | 442 test_fragmentExpression_trailingPartialSelection() { |
| 414 indexTestUnit(''' | 443 indexTestUnit(''' |
| 415 main() { | 444 main() { |
| 416 int a = 1 + 2 + 3 + 444; | 445 int a = 1 + 2 + 333 + 4; |
| 417 } | 446 } |
| 418 '''); | 447 '''); |
| 419 _createRefactoringForString('2 + 3 + 44'); | 448 _createRefactoringForString('2 + 33'); |
| 420 // check conditions | 449 // apply refactoring |
| 421 return _assertInitialConditions_fatal_selection(); | 450 return _assertSuccessfulRefactoring(''' |
| 451 main() { |
| 452 var res = 1 + 2 + 333; |
| 453 int a = res + 4; |
| 454 } |
| 455 '''); |
| 422 } | 456 } |
| 423 | 457 |
| 424 test_fragmentExpression_trailingWhitespace() { | 458 test_fragmentExpression_trailingWhitespace() { |
| 425 indexTestUnit(''' | 459 indexTestUnit(''' |
| 426 main() { | 460 main() { |
| 427 int a = 1 + 2 + 3 + 4; | 461 int a = 1 + 2 + 3 + 4; |
| 428 } | 462 } |
| 429 '''); | 463 '''); |
| 430 _createRefactoringForString('2 + 3 '); | 464 _createRefactoringForString('2 + 3 '); |
| 431 // apply refactoring | 465 // apply refactoring |
| 432 return _assertSuccessfulRefactoring(''' | 466 return _assertSuccessfulRefactoring(''' |
| 433 main() { | 467 main() { |
| 434 var res = 2 + 3 ; | 468 var res = 1 + 2 + 3; |
| 435 int a = 1 + res+ 4; | 469 int a = res + 4; |
| 436 } | 470 } |
| 437 '''); | 471 '''); |
| 438 } | 472 } |
| 439 | 473 |
| 440 test_guessNames_fragmentExpression() async { | 474 test_guessNames_fragmentExpression() async { |
| 441 indexTestUnit(''' | 475 indexTestUnit(''' |
| 442 main() { | 476 main() { |
| 443 var a = 111 + 222 + 333 + 444; | 477 var a = 111 + 222 + 333 + 444; |
| 444 } | 478 } |
| 445 '''); | 479 '''); |
| 446 _createRefactoringForString('222 + 333'); | 480 _createRefactoringForString('222 + 333'); |
| 447 // check guesses | 481 // check guesses |
| 448 await refactoring.checkInitialConditions(); | 482 await refactoring.checkInitialConditions(); |
| 449 expect(refactoring.names, isEmpty); | 483 expect(refactoring.names, unorderedEquals(['i'])); |
| 450 } | 484 } |
| 451 | 485 |
| 452 test_guessNames_singleExpression() async { | 486 test_guessNames_singleExpression() async { |
| 453 indexTestUnit(''' | 487 indexTestUnit(''' |
| 454 class TreeItem {} | 488 class TreeItem {} |
| 455 TreeItem getSelectedItem() => null; | 489 TreeItem getSelectedItem() => null; |
| 456 process(my) {} | 490 process(my) {} |
| 457 main() { | 491 main() { |
| 458 process(getSelectedItem()); // marker | 492 process(getSelectedItem()); // marker |
| 459 } | 493 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 470 main() { | 504 main() { |
| 471 var s = 'Hello Bob... welcome to Dart!'; | 505 var s = 'Hello Bob... welcome to Dart!'; |
| 472 } | 506 } |
| 473 '''); | 507 '''); |
| 474 _createRefactoringForString('Hello Bob'); | 508 _createRefactoringForString('Hello Bob'); |
| 475 // check guesses | 509 // check guesses |
| 476 await refactoring.checkInitialConditions(); | 510 await refactoring.checkInitialConditions(); |
| 477 expect(refactoring.names, unorderedEquals(['helloBob', 'bob'])); | 511 expect(refactoring.names, unorderedEquals(['helloBob', 'bob'])); |
| 478 } | 512 } |
| 479 | 513 |
| 480 test_occurences_differentVariable() { | 514 test_occurrences_differentVariable() { |
| 481 indexTestUnit(''' | 515 indexTestUnit(''' |
| 482 main() { | 516 main() { |
| 483 { | 517 { |
| 484 int v = 1; | 518 int v = 1; |
| 485 print(v + 1); // marker | 519 print(v + 1); // marker |
| 486 print(v + 1); | 520 print(v + 1); |
| 487 } | 521 } |
| 488 { | 522 { |
| 489 int v = 2; | 523 int v = 2; |
| 490 print(v + 1); | 524 print(v + 1); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 502 print(res); | 536 print(res); |
| 503 } | 537 } |
| 504 { | 538 { |
| 505 int v = 2; | 539 int v = 2; |
| 506 print(v + 1); | 540 print(v + 1); |
| 507 } | 541 } |
| 508 } | 542 } |
| 509 '''); | 543 '''); |
| 510 } | 544 } |
| 511 | 545 |
| 512 test_occurences_disableOccurences() { | 546 test_occurrences_disableOccurrences() { |
| 513 indexTestUnit(''' | 547 indexTestUnit(''' |
| 514 int foo() => 42; | 548 int foo() => 42; |
| 515 main() { | 549 main() { |
| 516 int a = 1 + foo(); | 550 int a = 1 + foo(); |
| 517 int b = 2 + foo(); // marker | 551 int b = 2 + foo(); // marker |
| 518 } | 552 } |
| 519 '''); | 553 '''); |
| 520 _createRefactoringWithSuffix('foo()', '; // marker'); | 554 _createRefactoringWithSuffix('foo()', '; // marker'); |
| 521 refactoring.extractAll = false; | 555 refactoring.extractAll = false; |
| 522 // apply refactoring | 556 // apply refactoring |
| 523 return _assertSuccessfulRefactoring(''' | 557 return _assertSuccessfulRefactoring(''' |
| 524 int foo() => 42; | 558 int foo() => 42; |
| 525 main() { | 559 main() { |
| 526 int a = 1 + foo(); | 560 int a = 1 + foo(); |
| 527 var res = foo(); | 561 var res = foo(); |
| 528 int b = 2 + res; // marker | 562 int b = 2 + res; // marker |
| 529 } | 563 } |
| 530 '''); | 564 '''); |
| 531 } | 565 } |
| 532 | 566 |
| 533 test_occurences_ignore_assignmentLeftHandSize() { | 567 test_occurrences_ignore_assignmentLeftHandSize() { |
| 534 indexTestUnit(''' | 568 indexTestUnit(''' |
| 535 main() { | 569 main() { |
| 536 int v = 1; | 570 int v = 1; |
| 537 v = 2; | 571 v = 2; |
| 538 print(() {v = 2;}); | 572 print(() {v = 2;}); |
| 539 print(1 + (() {v = 2; return 3;})()); | 573 print(1 + (() {v = 2; return 3;})()); |
| 540 print(v); // marker | 574 print(v); // marker |
| 541 } | 575 } |
| 542 '''); | 576 '''); |
| 543 _createRefactoringWithSuffix('v', '); // marker'); | 577 _createRefactoringWithSuffix('v', '); // marker'); |
| 544 // apply refactoring | 578 // apply refactoring |
| 545 return _assertSuccessfulRefactoring(''' | 579 return _assertSuccessfulRefactoring(''' |
| 546 main() { | 580 main() { |
| 547 int v = 1; | 581 int v = 1; |
| 548 v = 2; | 582 v = 2; |
| 549 print(() {v = 2;}); | 583 print(() {v = 2;}); |
| 550 print(1 + (() {v = 2; return 3;})()); | 584 print(1 + (() {v = 2; return 3;})()); |
| 551 var res = v; | 585 var res = v; |
| 552 print(res); // marker | 586 print(res); // marker |
| 553 } | 587 } |
| 554 '''); | 588 '''); |
| 555 } | 589 } |
| 556 | 590 |
| 557 test_occurences_ignore_nameOfVariableDeclariton() { | 591 test_occurrences_ignore_nameOfVariableDeclaration() { |
| 558 indexTestUnit(''' | 592 indexTestUnit(''' |
| 559 main() { | 593 main() { |
| 560 int v = 1; | 594 int v = 1; |
| 561 print(v); // marker | 595 print(v); // marker |
| 562 } | 596 } |
| 563 '''); | 597 '''); |
| 564 _createRefactoringWithSuffix('v', '); // marker'); | 598 _createRefactoringWithSuffix('v', '); // marker'); |
| 565 // apply refactoring | 599 // apply refactoring |
| 566 return _assertSuccessfulRefactoring(''' | 600 return _assertSuccessfulRefactoring(''' |
| 567 main() { | 601 main() { |
| 568 int v = 1; | 602 int v = 1; |
| 569 var res = v; | 603 var res = v; |
| 570 print(res); // marker | 604 print(res); // marker |
| 571 } | 605 } |
| 572 '''); | 606 '''); |
| 573 } | 607 } |
| 574 | 608 |
| 575 test_occurences_singleExpression() { | 609 test_occurrences_singleExpression() { |
| 576 indexTestUnit(''' | 610 indexTestUnit(''' |
| 577 int foo() => 42; | 611 int foo() => 42; |
| 578 main() { | 612 main() { |
| 579 int a = 1 + foo(); | 613 int a = 1 + foo(); |
| 580 int b = 2 + foo(); // marker | 614 int b = 2 + foo(); // marker |
| 581 } | 615 } |
| 582 '''); | 616 '''); |
| 583 _createRefactoringWithSuffix('foo()', '; // marker'); | 617 _createRefactoringWithSuffix('foo()', '; // marker'); |
| 584 // apply refactoring | 618 // apply refactoring |
| 585 return _assertSuccessfulRefactoring(''' | 619 return _assertSuccessfulRefactoring(''' |
| 586 int foo() => 42; | 620 int foo() => 42; |
| 587 main() { | 621 main() { |
| 588 var res = foo(); | 622 var res = foo(); |
| 589 int a = 1 + res; | 623 int a = 1 + res; |
| 590 int b = 2 + res; // marker | 624 int b = 2 + res; // marker |
| 591 } | 625 } |
| 592 '''); | 626 '''); |
| 593 } | 627 } |
| 594 | 628 |
| 595 test_occurences_useDominator() { | 629 test_occurrences_useDominator() { |
| 596 indexTestUnit(''' | 630 indexTestUnit(''' |
| 597 main() { | 631 main() { |
| 598 if (true) { | 632 if (true) { |
| 599 print(42); | 633 print(42); |
| 600 } else { | 634 } else { |
| 601 print(42); | 635 print(42); |
| 602 } | 636 } |
| 603 } | 637 } |
| 604 '''); | 638 '''); |
| 605 _createRefactoringForString('42'); | 639 _createRefactoringForString('42'); |
| 606 // apply refactoring | 640 // apply refactoring |
| 607 return _assertSuccessfulRefactoring(''' | 641 return _assertSuccessfulRefactoring(''' |
| 608 main() { | 642 main() { |
| 609 var res = 42; | 643 var res = 42; |
| 610 if (true) { | 644 if (true) { |
| 611 print(res); | 645 print(res); |
| 612 } else { | 646 } else { |
| 613 print(res); | 647 print(res); |
| 614 } | 648 } |
| 615 } | 649 } |
| 616 '''); | 650 '''); |
| 617 } | 651 } |
| 618 | 652 |
| 619 test_occurences_whenComment() { | 653 test_occurrences_whenComment() { |
| 620 indexTestUnit(''' | 654 indexTestUnit(''' |
| 621 int foo() => 42; | 655 int foo() => 42; |
| 622 main() { | 656 main() { |
| 623 /*int a = 1 + foo();*/ | 657 /*int a = 1 + foo();*/ |
| 624 int b = 2 + foo(); // marker | 658 int b = 2 + foo(); // marker |
| 625 } | 659 } |
| 626 '''); | 660 '''); |
| 627 _createRefactoringWithSuffix('foo()', '; // marker'); | 661 _createRefactoringWithSuffix('foo()', '; // marker'); |
| 628 // apply refactoring | 662 // apply refactoring |
| 629 return _assertSuccessfulRefactoring(''' | 663 return _assertSuccessfulRefactoring(''' |
| 630 int foo() => 42; | 664 int foo() => 42; |
| 631 main() { | 665 main() { |
| 632 /*int a = 1 + foo();*/ | 666 /*int a = 1 + foo();*/ |
| 633 var res = foo(); | 667 var res = foo(); |
| 634 int b = 2 + res; // marker | 668 int b = 2 + res; // marker |
| 635 } | 669 } |
| 636 '''); | 670 '''); |
| 637 } | 671 } |
| 638 | 672 |
| 639 test_occurences_withSpace() { | 673 test_occurrences_withSpace() { |
| 640 indexTestUnit(''' | 674 indexTestUnit(''' |
| 641 int foo(String s) => 42; | 675 int foo(String s) => 42; |
| 642 main() { | 676 main() { |
| 643 int a = 1 + foo('has space'); | 677 int a = 1 + foo('has space'); |
| 644 int b = 2 + foo('has space'); // marker | 678 int b = 2 + foo('has space'); // marker |
| 645 } | 679 } |
| 646 '''); | 680 '''); |
| 647 _createRefactoringWithSuffix("foo('has space')", '; // marker'); | 681 _createRefactoringWithSuffix("foo('has space')", '; // marker'); |
| 648 // apply refactoring | 682 // apply refactoring |
| 649 return _assertSuccessfulRefactoring(''' | 683 return _assertSuccessfulRefactoring(''' |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 793 '''); | 827 '''); |
| 794 } | 828 } |
| 795 | 829 |
| 796 test_singleExpression_leadingNotWhitespace() { | 830 test_singleExpression_leadingNotWhitespace() { |
| 797 indexTestUnit(''' | 831 indexTestUnit(''' |
| 798 main() { | 832 main() { |
| 799 int a = 12 + 345; | 833 int a = 12 + 345; |
| 800 } | 834 } |
| 801 '''); | 835 '''); |
| 802 _createRefactoringForString('+ 345'); | 836 _createRefactoringForString('+ 345'); |
| 803 // check conditions | 837 // apply refactoring |
| 804 return _assertInitialConditions_fatal_selection(); | 838 return _assertSuccessfulRefactoring(''' |
| 839 main() { |
| 840 var res = 12 + 345; |
| 841 int a = res; |
| 842 } |
| 843 '''); |
| 805 } | 844 } |
| 806 | 845 |
| 807 test_singleExpression_leadingWhitespace() { | 846 test_singleExpression_leadingWhitespace() { |
| 808 indexTestUnit(''' | 847 indexTestUnit(''' |
| 809 main() { | 848 main() { |
| 810 int a = 12 /*abc*/ + 345; | 849 int a = 1 /*abc*/ + 2 + 345; |
| 811 } | 850 } |
| 812 '''); | 851 '''); |
| 813 _createRefactoringForString('12 /*abc*/'); | 852 _createRefactoringForString('1 /*abc*/'); |
| 814 // apply refactoring | 853 // apply refactoring |
| 815 return _assertSuccessfulRefactoring(''' | 854 return _assertSuccessfulRefactoring(''' |
| 816 main() { | 855 main() { |
| 817 var res = 12 /*abc*/; | 856 var res = 1 /*abc*/ + 2; |
| 818 int a = res + 345; | 857 int a = res + 345; |
| 819 } | 858 } |
| 820 '''); | 859 '''); |
| 821 } | 860 } |
| 822 | 861 |
| 862 test_singleExpression_nameOfProperty_prefixedIdentifier() async { |
| 863 indexTestUnit(''' |
| 864 main(p) { |
| 865 var v = p.value; // marker |
| 866 } |
| 867 '''); |
| 868 _createRefactoringWithSuffix('value', '; // marker'); |
| 869 // apply refactoring |
| 870 return _assertSuccessfulRefactoring(''' |
| 871 main(p) { |
| 872 var res = p.value; |
| 873 var v = res; // marker |
| 874 } |
| 875 '''); |
| 876 } |
| 877 |
| 878 test_singleExpression_nameOfProperty_propertyAccess() async { |
| 879 indexTestUnit(''' |
| 880 main() { |
| 881 var v = foo().length; // marker |
| 882 } |
| 883 String foo() => ''; |
| 884 '''); |
| 885 _createRefactoringWithSuffix('length', '; // marker'); |
| 886 // apply refactoring |
| 887 return _assertSuccessfulRefactoring(''' |
| 888 main() { |
| 889 var res = foo().length; |
| 890 var v = res; // marker |
| 891 } |
| 892 String foo() => ''; |
| 893 '''); |
| 894 } |
| 895 |
| 823 /** | 896 /** |
| 824 * Here we use knowledge how exactly `1 + 2 + 3 + 41 is parsed. We know that | 897 * Here we use knowledge how exactly `1 + 2 + 3 + 41 is parsed. We know that |
| 825 * `1 + 2` will be a separate and complete binary expression, so it can be | 898 * `1 + 2` will be a separate and complete binary expression, so it can be |
| 826 * handled as a single expression. | 899 * handled as a single expression. |
| 827 */ | 900 */ |
| 828 test_singleExpression_partOfBinaryExpression() { | 901 test_singleExpression_partOfBinaryExpression() { |
| 829 indexTestUnit(''' | 902 indexTestUnit(''' |
| 830 main() { | 903 main() { |
| 831 int a = 1 + 2 + 3 + 4; | 904 int a = 1 + 2 + 3 + 4; |
| 832 } | 905 } |
| 833 '''); | 906 '''); |
| 834 _createRefactoringForString('1 + 2'); | 907 _createRefactoringForString('1 + 2'); |
| 835 // apply refactoring | 908 // apply refactoring |
| 836 return _assertSuccessfulRefactoring(''' | 909 return _assertSuccessfulRefactoring(''' |
| 837 main() { | 910 main() { |
| 838 var res = 1 + 2; | 911 var res = 1 + 2; |
| 839 int a = res + 3 + 4; | 912 int a = res + 3 + 4; |
| 840 } | 913 } |
| 841 '''); | 914 '''); |
| 842 } | 915 } |
| 843 | 916 |
| 844 test_singleExpression_trailingComment() { | |
| 845 indexTestUnit(''' | |
| 846 main() { | |
| 847 int a = 1 + 2; | |
| 848 } | |
| 849 '''); | |
| 850 _createRefactoringForString(' 1 + 2'); | |
| 851 // apply refactoring | |
| 852 return _assertSuccessfulRefactoring(''' | |
| 853 main() { | |
| 854 var res = 1 + 2; | |
| 855 int a = res; | |
| 856 } | |
| 857 '''); | |
| 858 } | |
| 859 | |
| 860 test_singleExpression_trailingNotWhitespace() { | 917 test_singleExpression_trailingNotWhitespace() { |
| 861 indexTestUnit(''' | 918 indexTestUnit(''' |
| 862 main() { | 919 main() { |
| 863 int a = 12 + 345; | 920 int a = 12 + 345; |
| 864 } | 921 } |
| 865 '''); | 922 '''); |
| 866 _createRefactoringForString('12 +'); | 923 _createRefactoringForString('12 +'); |
| 867 // check conditions | 924 // apply refactoring |
| 868 return _assertInitialConditions_fatal_selection(); | 925 return _assertSuccessfulRefactoring(''' |
| 926 main() { |
| 927 var res = 12 + 345; |
| 928 int a = res; |
| 929 } |
| 930 '''); |
| 869 } | 931 } |
| 870 | 932 |
| 871 test_singleExpression_trailingWhitespace() { | 933 test_singleExpression_trailingWhitespace() { |
| 872 indexTestUnit(''' | 934 indexTestUnit(''' |
| 873 main() { | 935 main() { |
| 874 int a = 1 + 2 ; | 936 int a = 1 + 2 ; |
| 875 } | 937 } |
| 876 '''); | 938 '''); |
| 877 _createRefactoringForString('1 + 2 '); | 939 _createRefactoringForString('1 + 2 '); |
| 878 // apply refactoring | 940 // apply refactoring |
| 879 return _assertSuccessfulRefactoring(''' | 941 return _assertSuccessfulRefactoring(''' |
| 880 main() { | 942 main() { |
| 881 var res = 1 + 2 ; | 943 var res = 1 + 2; |
| 882 int a = res; | 944 int a = res ; |
| 883 } | 945 } |
| 884 '''); | 946 '''); |
| 885 } | 947 } |
| 886 | 948 |
| 887 test_stringLiteral_part() { | 949 test_stringLiteral_part() { |
| 888 indexTestUnit(''' | 950 indexTestUnit(''' |
| 889 main() { | 951 main() { |
| 890 print('abcdefgh'); | 952 print('abcdefgh'); |
| 891 } | 953 } |
| 892 '''); | 954 '''); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 937 } | 999 } |
| 938 | 1000 |
| 939 Future _assertInitialConditions_fatal_selection() async { | 1001 Future _assertInitialConditions_fatal_selection() async { |
| 940 RefactoringStatus status = await refactoring.checkInitialConditions(); | 1002 RefactoringStatus status = await refactoring.checkInitialConditions(); |
| 941 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, | 1003 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, |
| 942 expectedMessage: | 1004 expectedMessage: |
| 943 'Expression must be selected to activate this refactoring.'); | 1005 'Expression must be selected to activate this refactoring.'); |
| 944 } | 1006 } |
| 945 | 1007 |
| 946 /** | 1008 /** |
| 947 * Checks that all conditions are OK and the result of applying the [Change] | 1009 * Checks that all conditions are OK and the result of applying the |
| 948 * to [testUnit] is [expectedCode]. | 1010 * [SourceChange] to [testUnit] is [expectedCode]. |
| 949 */ | 1011 */ |
| 950 Future _assertSuccessfulRefactoring(String expectedCode) async { | 1012 Future _assertSuccessfulRefactoring(String expectedCode) async { |
| 951 await assertRefactoringConditionsOK(); | 1013 await assertRefactoringConditionsOK(); |
| 952 SourceChange refactoringChange = await refactoring.createChange(); | 1014 SourceChange refactoringChange = await refactoring.createChange(); |
| 953 this.refactoringChange = refactoringChange; | 1015 this.refactoringChange = refactoringChange; |
| 954 assertTestChangeResult(expectedCode); | 1016 assertTestChangeResult(expectedCode); |
| 955 } | 1017 } |
| 956 | 1018 |
| 957 void _createRefactoring(int offset, int length) { | 1019 void _createRefactoring(int offset, int length) { |
| 958 refactoring = new ExtractLocalRefactoring(testUnit, offset, length); | 1020 refactoring = new ExtractLocalRefactoring(testUnit, offset, length); |
| 959 refactoring.name = 'res'; | 1021 refactoring.name = 'res'; |
| 960 } | 1022 } |
| 961 | 1023 |
| 962 /** | 1024 /** |
| 963 * Creates a new refactoring in [refactoring] for the selection range of the | 1025 * Creates a new refactoring in [refactoring] for the selection range of the |
| 964 * given [search] pattern. | 1026 * given [search] pattern. |
| 965 */ | 1027 */ |
| 966 void _createRefactoringForString(String search) { | 1028 void _createRefactoringForString(String search) { |
| 967 int offset = findOffset(search); | 1029 int offset = findOffset(search); |
| 968 int length = search.length; | 1030 int length = search.length; |
| 969 _createRefactoring(offset, length); | 1031 _createRefactoring(offset, length); |
| 970 } | 1032 } |
| 971 | 1033 |
| 972 void _createRefactoringWithSuffix(String selectionSearch, String suffix) { | 1034 void _createRefactoringWithSuffix(String selectionSearch, String suffix) { |
| 973 int offset = findOffset(selectionSearch + suffix); | 1035 int offset = findOffset(selectionSearch + suffix); |
| 974 int length = selectionSearch.length; | 1036 int length = selectionSearch.length; |
| 975 _createRefactoring(offset, length); | 1037 _createRefactoring(offset, length); |
| 976 } | 1038 } |
| 977 } | 1039 } |
| OLD | NEW |