| 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.inline_local; | 5 library test.services.refactoring.inline_local; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart' hide Element; | 7 import 'package:analysis_server/src/protocol.dart' hide Element; |
| 8 import 'package:analysis_server/src/services/correction/status.dart'; | 8 import 'package:analysis_server/src/services/correction/status.dart'; |
| 9 import 'package:analysis_server/src/services/refactoring/inline_local.dart'; | 9 import 'package:analysis_server/src/services/refactoring/inline_local.dart'; |
| 10 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 10 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 11 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
| 12 | 12 |
| 13 import '../../reflective_tests.dart'; | 13 import '../../reflective_tests.dart'; |
| 14 import 'abstract_refactoring.dart'; | 14 import 'abstract_refactoring.dart'; |
| 15 | 15 |
| 16 | 16 |
| 17 main() { | 17 main() { |
| 18 groupSep = ' | '; | 18 groupSep = ' | '; |
| 19 runReflectiveTests(InlineLocalTest); | 19 runReflectiveTests(InlineLocalTest); |
| 20 } | 20 } |
| 21 | 21 |
| 22 | 22 |
| 23 @ReflectiveTestCase() | 23 @ReflectiveTestCase() |
| 24 class InlineLocalTest extends RefactoringTest { | 24 class InlineLocalTest extends RefactoringTest { |
| 25 InlineLocalRefactoringImpl refactoring; | 25 InlineLocalRefactoringImpl refactoring; |
| 26 | 26 |
| 27 test_access() { |
| 28 indexTestUnit(''' |
| 29 main() { |
| 30 int test = 1 + 2; |
| 31 print(test); |
| 32 print(test); |
| 33 } |
| 34 '''); |
| 35 _createRefactoring('test ='); |
| 36 expect(refactoring.refactoringName, 'Inline Local Variable'); |
| 37 // check initial conditions and access |
| 38 return refactoring.checkInitialConditions().then((_) { |
| 39 expect(refactoring.variableName, 'test'); |
| 40 expect(refactoring.referenceCount, 2); |
| 41 }); |
| 42 } |
| 43 |
| 44 test_bad_selectionMethod() { |
| 45 indexTestUnit(r''' |
| 46 main() { |
| 47 } |
| 48 '''); |
| 49 _createRefactoring('main() {'); |
| 50 return refactoring.checkInitialConditions().then((status) { |
| 51 _assert_fatalError_selection(status); |
| 52 }); |
| 53 } |
| 54 |
| 55 test_bad_selectionParameter() { |
| 56 indexTestUnit(r''' |
| 57 main(int test) { |
| 58 } |
| 59 '''); |
| 60 _createRefactoring('test) {'); |
| 61 return refactoring.checkInitialConditions().then((status) { |
| 62 _assert_fatalError_selection(status); |
| 63 }); |
| 64 } |
| 65 |
| 66 test_bad_selectionVariable_hasAssignments_1() { |
| 67 indexTestUnit(r''' |
| 68 main() { |
| 69 int test = 0; |
| 70 test = 1; |
| 71 } |
| 72 '''); |
| 73 _createRefactoring('test = 0'); |
| 74 return refactoring.checkInitialConditions().then((status) { |
| 75 assertRefactoringStatus( |
| 76 status, |
| 77 RefactoringProblemSeverity.FATAL, |
| 78 expectedContextSearch: 'test = 1'); |
| 79 }); |
| 80 } |
| 81 |
| 82 test_bad_selectionVariable_hasAssignments_2() { |
| 83 indexTestUnit(r''' |
| 84 main() { |
| 85 int test = 0; |
| 86 test += 1; |
| 87 } |
| 88 '''); |
| 89 _createRefactoring('test = 0'); |
| 90 return refactoring.checkInitialConditions().then((status) { |
| 91 assertRefactoringStatus( |
| 92 status, |
| 93 RefactoringProblemSeverity.FATAL, |
| 94 expectedContextSearch: 'test += 1'); |
| 95 }); |
| 96 } |
| 97 |
| 98 test_bad_selectionVariable_notInBlock() { |
| 99 indexTestUnit(r''' |
| 100 main() { |
| 101 if (true) |
| 102 int test = 0; |
| 103 } |
| 104 '''); |
| 105 _createRefactoring('test = 0'); |
| 106 return refactoring.checkInitialConditions().then((status) { |
| 107 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL); |
| 108 }); |
| 109 } |
| 110 |
| 111 test_bad_selectionVariable_notInitialized() { |
| 112 indexTestUnit(r''' |
| 113 main() { |
| 114 int test; |
| 115 } |
| 116 '''); |
| 117 _createRefactoring('test;'); |
| 118 return refactoring.checkInitialConditions().then((status) { |
| 119 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL); |
| 120 }); |
| 121 } |
| 122 |
| 27 test_OK_cascade_intoCascade() { | 123 test_OK_cascade_intoCascade() { |
| 28 indexTestUnit(r''' | 124 indexTestUnit(r''' |
| 29 class A { | 125 class A { |
| 30 foo() {} | 126 foo() {} |
| 31 bar() {} | 127 bar() {} |
| 32 } | 128 } |
| 33 main() { | 129 main() { |
| 34 A test = new A()..foo(); | 130 A test = new A()..foo(); |
| 35 test..bar(); | 131 test..bar(); |
| 36 } | 132 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 main() { | 207 main() { |
| 112 int foo = 1 + 2; | 208 int foo = 1 + 2; |
| 113 print('test = $foo'); | 209 print('test = $foo'); |
| 114 print('test = ${foo}'); | 210 print('test = ${foo}'); |
| 115 print('test = ${process(foo)}'); | 211 print('test = ${process(foo)}'); |
| 116 } | 212 } |
| 117 process(x) {} | 213 process(x) {} |
| 118 '''); | 214 '''); |
| 119 } | 215 } |
| 120 | 216 |
| 121 test_OK_intoStringInterpolation_stringInterpolation() { | |
| 122 indexTestUnit(r''' | |
| 123 main() { | |
| 124 String a = 'aaa'; | |
| 125 String b = '$a bbb'; | |
| 126 String c = '$b ccc'; | |
| 127 } | |
| 128 '''); | |
| 129 _createRefactoring('b ='); | |
| 130 // validate change | |
| 131 return assertSuccessfulRefactoring(r''' | |
| 132 main() { | |
| 133 String a = 'aaa'; | |
| 134 String c = '$a bbb ccc'; | |
| 135 } | |
| 136 '''); | |
| 137 } | |
| 138 | |
| 139 test_OK_intoStringInterpolation_string_differentQuotes() { | 217 test_OK_intoStringInterpolation_string_differentQuotes() { |
| 140 indexTestUnit(r''' | 218 indexTestUnit(r''' |
| 141 main() { | 219 main() { |
| 142 String a = "aaa"; | 220 String a = "aaa"; |
| 143 String b = '$a bbb'; | 221 String b = '$a bbb'; |
| 144 } | 222 } |
| 145 '''); | 223 '''); |
| 146 _createRefactoring('a ='); | 224 _createRefactoring('a ='); |
| 147 // validate change | 225 // validate change |
| 148 return assertSuccessfulRefactoring(r''' | 226 return assertSuccessfulRefactoring(r''' |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 '''); | 369 '''); |
| 292 _createRefactoring('a ='); | 370 _createRefactoring('a ='); |
| 293 // validate change | 371 // validate change |
| 294 return assertSuccessfulRefactoring(r''' | 372 return assertSuccessfulRefactoring(r''' |
| 295 main() { | 373 main() { |
| 296 String b = 'aaa bbb'; | 374 String b = 'aaa bbb'; |
| 297 } | 375 } |
| 298 '''); | 376 '''); |
| 299 } | 377 } |
| 300 | 378 |
| 379 test_OK_intoStringInterpolation_stringInterpolation() { |
| 380 indexTestUnit(r''' |
| 381 main() { |
| 382 String a = 'aaa'; |
| 383 String b = '$a bbb'; |
| 384 String c = '$b ccc'; |
| 385 } |
| 386 '''); |
| 387 _createRefactoring('b ='); |
| 388 // validate change |
| 389 return assertSuccessfulRefactoring(r''' |
| 390 main() { |
| 391 String a = 'aaa'; |
| 392 String c = '$a bbb ccc'; |
| 393 } |
| 394 '''); |
| 395 } |
| 396 |
| 301 /** | 397 /** |
| 302 * <p> | 398 * <p> |
| 303 * https://code.google.com/p/dart/issues/detail?id=18587 | 399 * https://code.google.com/p/dart/issues/detail?id=18587 |
| 304 */ | 400 */ |
| 305 test_OK_keepNextCommentedLine() { | 401 test_OK_keepNextCommentedLine() { |
| 306 indexTestUnit(''' | 402 indexTestUnit(''' |
| 307 main() { | 403 main() { |
| 308 int test = 1 + 2; | 404 int test = 1 + 2; |
| 309 // foo | 405 // foo |
| 310 print(test); | 406 print(test); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 '''); | 457 '''); |
| 362 _createRefactoring('test ='); | 458 _createRefactoring('test ='); |
| 363 // validate change | 459 // validate change |
| 364 return assertSuccessfulRefactoring(''' | 460 return assertSuccessfulRefactoring(''' |
| 365 main() { | 461 main() { |
| 366 print(1 + 2); | 462 print(1 + 2); |
| 367 } | 463 } |
| 368 '''); | 464 '''); |
| 369 } | 465 } |
| 370 | 466 |
| 467 test_OK_parenthesis_decrement_intoNegate() { |
| 468 indexTestUnit(''' |
| 469 main() { |
| 470 var a = 1; |
| 471 var test = --a; |
| 472 var b = -test; |
| 473 } |
| 474 '''); |
| 475 _createRefactoring('test ='); |
| 476 // validate change |
| 477 return assertSuccessfulRefactoring(''' |
| 478 main() { |
| 479 var a = 1; |
| 480 var b = -(--a); |
| 481 } |
| 482 '''); |
| 483 } |
| 484 |
| 371 test_OK_parenthesis_instanceCreation_intoList() { | 485 test_OK_parenthesis_instanceCreation_intoList() { |
| 372 indexTestUnit(''' | 486 indexTestUnit(''' |
| 373 class A {} | 487 class A {} |
| 374 main() { | 488 main() { |
| 375 var test = new A(); | 489 var test = new A(); |
| 376 var list = [test]; | 490 var list = [test]; |
| 377 } | 491 } |
| 378 '''); | 492 '''); |
| 379 _createRefactoring('test ='); | 493 _createRefactoring('test ='); |
| 380 // validate change | 494 // validate change |
| 381 return assertSuccessfulRefactoring(''' | 495 return assertSuccessfulRefactoring(''' |
| 382 class A {} | 496 class A {} |
| 383 main() { | 497 main() { |
| 384 var list = [new A()]; | 498 var list = [new A()]; |
| 385 } | 499 } |
| 386 '''); | 500 '''); |
| 387 } | 501 } |
| 388 | 502 |
| 503 test_OK_parenthesis_negate_intoNegate() { |
| 504 indexTestUnit(''' |
| 505 main() { |
| 506 var a = 1; |
| 507 var test = -a; |
| 508 var b = -test; |
| 509 } |
| 510 '''); |
| 511 _createRefactoring('test ='); |
| 512 // validate change |
| 513 return assertSuccessfulRefactoring(''' |
| 514 main() { |
| 515 var a = 1; |
| 516 var b = -(-a); |
| 517 } |
| 518 '''); |
| 519 } |
| 520 |
| 389 test_OK_parenthesis_plus_intoMultiply() { | 521 test_OK_parenthesis_plus_intoMultiply() { |
| 390 indexTestUnit(''' | 522 indexTestUnit(''' |
| 391 main() { | 523 main() { |
| 392 var test = 1 + 2; | 524 var test = 1 + 2; |
| 393 print(test * 3); | 525 print(test * 3); |
| 394 } | 526 } |
| 395 '''); | 527 '''); |
| 396 _createRefactoring('test ='); | 528 _createRefactoring('test ='); |
| 397 // validate change | 529 // validate change |
| 398 return assertSuccessfulRefactoring(''' | 530 return assertSuccessfulRefactoring(''' |
| 399 main() { | 531 main() { |
| 400 print((1 + 2) * 3); | 532 print((1 + 2) * 3); |
| 401 } | 533 } |
| 402 '''); | 534 '''); |
| 403 } | 535 } |
| 404 | 536 |
| 405 test_OK_twoUsages() { | 537 test_OK_twoUsages() { |
| 406 indexTestUnit(''' | 538 indexTestUnit(''' |
| 407 main() { | 539 main() { |
| 408 int test = 1 + 2; | 540 int test = 1 + 2; |
| 409 print(test); | |
| 410 print(test); | |
| 411 } | |
| 412 '''); | |
| 413 _createRefactoring('test ='); | |
| 414 // validate change | |
| 415 return assertSuccessfulRefactoring(''' | |
| 416 main() { | |
| 417 print(1 + 2); | |
| 418 print(1 + 2); | |
| 419 } | |
| 420 '''); | |
| 421 } | |
| 422 | |
| 423 test_access() { | |
| 424 indexTestUnit(''' | |
| 425 main() { | |
| 426 int test = 1 + 2; | |
| 427 print(test); | 541 print(test); |
| 428 print(test); | 542 print(test); |
| 429 } | 543 } |
| 430 '''); | 544 '''); |
| 431 _createRefactoring('test ='); | 545 _createRefactoring('test ='); |
| 432 expect(refactoring.refactoringName, 'Inline Local Variable'); | 546 // validate change |
| 433 // check initial conditions and access | 547 return assertSuccessfulRefactoring(''' |
| 434 return refactoring.checkInitialConditions().then((_) { | |
| 435 expect(refactoring.variableName, 'test'); | |
| 436 expect(refactoring.referenceCount, 2); | |
| 437 }); | |
| 438 } | |
| 439 | |
| 440 test_bad_selectionMethod() { | |
| 441 indexTestUnit(r''' | |
| 442 main() { | 548 main() { |
| 549 print(1 + 2); |
| 550 print(1 + 2); |
| 443 } | 551 } |
| 444 '''); | 552 '''); |
| 445 _createRefactoring('main() {'); | |
| 446 return refactoring.checkInitialConditions().then((status) { | |
| 447 _assert_fatalError_selection(status); | |
| 448 }); | |
| 449 } | |
| 450 | |
| 451 test_bad_selectionParameter() { | |
| 452 indexTestUnit(r''' | |
| 453 main(int test) { | |
| 454 } | |
| 455 '''); | |
| 456 _createRefactoring('test) {'); | |
| 457 return refactoring.checkInitialConditions().then((status) { | |
| 458 _assert_fatalError_selection(status); | |
| 459 }); | |
| 460 } | |
| 461 | |
| 462 test_bad_selectionVariable_hasAssignments_1() { | |
| 463 indexTestUnit(r''' | |
| 464 main() { | |
| 465 int test = 0; | |
| 466 test = 1; | |
| 467 } | |
| 468 '''); | |
| 469 _createRefactoring('test = 0'); | |
| 470 return refactoring.checkInitialConditions().then((status) { | |
| 471 assertRefactoringStatus( | |
| 472 status, | |
| 473 RefactoringProblemSeverity.FATAL, | |
| 474 expectedContextSearch: 'test = 1'); | |
| 475 }); | |
| 476 } | |
| 477 | |
| 478 test_bad_selectionVariable_hasAssignments_2() { | |
| 479 indexTestUnit(r''' | |
| 480 main() { | |
| 481 int test = 0; | |
| 482 test += 1; | |
| 483 } | |
| 484 '''); | |
| 485 _createRefactoring('test = 0'); | |
| 486 return refactoring.checkInitialConditions().then((status) { | |
| 487 assertRefactoringStatus( | |
| 488 status, | |
| 489 RefactoringProblemSeverity.FATAL, | |
| 490 expectedContextSearch: 'test += 1'); | |
| 491 }); | |
| 492 } | |
| 493 | |
| 494 test_bad_selectionVariable_notInBlock() { | |
| 495 indexTestUnit(r''' | |
| 496 main() { | |
| 497 if (true) | |
| 498 int test = 0; | |
| 499 } | |
| 500 '''); | |
| 501 _createRefactoring('test = 0'); | |
| 502 return refactoring.checkInitialConditions().then((status) { | |
| 503 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL); | |
| 504 }); | |
| 505 } | |
| 506 | |
| 507 test_bad_selectionVariable_notInitialized() { | |
| 508 indexTestUnit(r''' | |
| 509 main() { | |
| 510 int test; | |
| 511 } | |
| 512 '''); | |
| 513 _createRefactoring('test;'); | |
| 514 return refactoring.checkInitialConditions().then((status) { | |
| 515 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL); | |
| 516 }); | |
| 517 } | 553 } |
| 518 | 554 |
| 519 void _assert_fatalError_selection(RefactoringStatus status) { | 555 void _assert_fatalError_selection(RefactoringStatus status) { |
| 520 expect(refactoring.variableName, isNull); | 556 expect(refactoring.variableName, isNull); |
| 521 expect(refactoring.referenceCount, 0); | 557 expect(refactoring.referenceCount, 0); |
| 522 assertRefactoringStatus( | 558 assertRefactoringStatus( |
| 523 status, | 559 status, |
| 524 RefactoringProblemSeverity.FATAL, | 560 RefactoringProblemSeverity.FATAL, |
| 525 expectedMessage: 'Local variable declaration or reference must be ' | 561 expectedMessage: 'Local variable declaration or reference must be ' |
| 526 'selected to activate this refactoring.'); | 562 'selected to activate this refactoring.'); |
| 527 } | 563 } |
| 528 | 564 |
| 529 void _createRefactoring(String search) { | 565 void _createRefactoring(String search) { |
| 530 int offset = findOffset(search); | 566 int offset = findOffset(search); |
| 531 refactoring = new InlineLocalRefactoring(searchEngine, testUnit, offset); | 567 refactoring = new InlineLocalRefactoring(searchEngine, testUnit, offset); |
| 532 } | 568 } |
| 533 } | 569 } |
| OLD | NEW |