| 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.rename_class_member; | 5 library test.services.refactoring.rename_class_member; |
| 6 | 6 |
| 7 import 'package:analysis_services/correction/status.dart'; | 7 import 'package:analysis_services/correction/status.dart'; |
| 8 import 'package:analysis_testing/reflective_tests.dart'; | 8 import 'package:analysis_testing/reflective_tests.dart'; |
| 9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 10 | 10 |
| 11 import 'abstract_rename.dart'; | 11 import 'abstract_rename.dart'; |
| 12 import 'package:analysis_services/correction/change.dart'; | |
| 13 | 12 |
| 14 | 13 |
| 15 main() { | 14 main() { |
| 16 groupSep = ' | '; | 15 groupSep = ' | '; |
| 17 runReflectiveTests(RenameClassMemberTest); | 16 runReflectiveTests(RenameClassMemberTest); |
| 18 } | 17 } |
| 19 | 18 |
| 20 | 19 |
| 21 @ReflectiveTestCase() | 20 @ReflectiveTestCase() |
| 22 class RenameClassMemberTest extends RenameRefactoringTest { | 21 class RenameClassMemberTest extends RenameRefactoringTest { |
| 22 test_checkFinalConditions_OK_noShadow() { |
| 23 indexTestUnit(''' |
| 24 class A { |
| 25 int newName; |
| 26 } |
| 27 class B { |
| 28 test() {} |
| 29 } |
| 30 class C extends A { |
| 31 main() { |
| 32 print(newName); |
| 33 } |
| 34 } |
| 35 '''); |
| 36 createRenameRefactoringAtString('test() {}'); |
| 37 // check status |
| 38 refactoring.newName = 'newName'; |
| 39 return refactoring.checkFinalConditions().then((status) { |
| 40 assertRefactoringStatusOK(status); |
| 41 }); |
| 42 } |
| 43 |
| 44 test_checkFinalConditions_hasMember_MethodElement() { |
| 45 indexTestUnit(''' |
| 46 class A { |
| 47 test() {} |
| 48 newName() {} // existing |
| 49 } |
| 50 '''); |
| 51 createRenameRefactoringAtString('test() {}'); |
| 52 // check status |
| 53 refactoring.newName = 'newName'; |
| 54 return refactoring.checkFinalConditions().then((status) { |
| 55 assertRefactoringStatus( |
| 56 status, |
| 57 RefactoringStatusSeverity.ERROR, |
| 58 expectedMessage: "Class 'A' already declares method with name 'newName
'.", |
| 59 expectedContextSearch: 'newName() {} // existing'); |
| 60 }); |
| 61 } |
| 62 |
| 63 test_checkFinalConditions_shadowed_byLocal_OK_qualifiedReference() { |
| 64 indexTestUnit(''' |
| 65 class A { |
| 66 test() {} |
| 67 main() { |
| 68 var newName; |
| 69 this.test(); // marker |
| 70 } |
| 71 } |
| 72 '''); |
| 73 createRenameRefactoringAtString('test() {}'); |
| 74 // check status |
| 75 refactoring.newName = 'newName'; |
| 76 return refactoring.checkFinalConditions().then((status) { |
| 77 assertRefactoringStatusOK(status); |
| 78 }); |
| 79 } |
| 80 |
| 81 test_checkFinalConditions_shadowed_byLocal_OK_renamedNotUsed() { |
| 82 indexTestUnit(''' |
| 83 class A { |
| 84 test() {} |
| 85 main() { |
| 86 var newName; |
| 87 } |
| 88 } |
| 89 '''); |
| 90 createRenameRefactoringAtString('test() {}'); |
| 91 // check status |
| 92 refactoring.newName = 'newName'; |
| 93 return refactoring.checkFinalConditions().then((status) { |
| 94 assertRefactoringStatusOK(status); |
| 95 }); |
| 96 } |
| 97 |
| 98 test_checkFinalConditions_shadowed_byLocal_inSameClass() { |
| 99 indexTestUnit(''' |
| 100 class A { |
| 101 test() {} |
| 102 main() { |
| 103 var newName; |
| 104 test(); // marker |
| 105 } |
| 106 } |
| 107 '''); |
| 108 createRenameRefactoringAtString('test() {}'); |
| 109 // check status |
| 110 refactoring.newName = 'newName'; |
| 111 return refactoring.checkFinalConditions().then((status) { |
| 112 assertRefactoringStatus( |
| 113 status, |
| 114 RefactoringStatusSeverity.ERROR, |
| 115 expectedMessage: |
| 116 "Usage of renamed method will be shadowed by local variable 'newNa
me'.", |
| 117 expectedContextSearch: 'test(); // marker'); |
| 118 }); |
| 119 } |
| 120 |
| 121 test_checkFinalConditions_shadowed_byLocal_inSubClass() { |
| 122 indexTestUnit(''' |
| 123 class A { |
| 124 test() {} |
| 125 } |
| 126 class B extends A { |
| 127 main() { |
| 128 var newName; |
| 129 test(); // marker |
| 130 } |
| 131 } |
| 132 '''); |
| 133 createRenameRefactoringAtString('test() {}'); |
| 134 // check status |
| 135 refactoring.newName = 'newName'; |
| 136 return refactoring.checkFinalConditions().then((status) { |
| 137 assertRefactoringStatus( |
| 138 status, |
| 139 RefactoringStatusSeverity.ERROR, |
| 140 expectedMessage: |
| 141 "Usage of renamed method will be shadowed by local variable 'newNa
me'.", |
| 142 expectedContextSearch: 'test(); // marker'); |
| 143 }); |
| 144 } |
| 145 |
| 146 test_checkFinalConditions_shadowed_byParameter_inSameClass() { |
| 147 indexTestUnit(''' |
| 148 class A { |
| 149 test() {} |
| 150 main(newName) { |
| 151 test(); // marker |
| 152 } |
| 153 } |
| 154 '''); |
| 155 createRenameRefactoringAtString('test() {}'); |
| 156 // check status |
| 157 refactoring.newName = 'newName'; |
| 158 return refactoring.checkFinalConditions().then((status) { |
| 159 assertRefactoringStatus( |
| 160 status, |
| 161 RefactoringStatusSeverity.ERROR, |
| 162 expectedMessage: |
| 163 "Usage of renamed method will be shadowed by parameter 'newName'."
, |
| 164 expectedContextSearch: 'test(); // marker'); |
| 165 }); |
| 166 } |
| 167 |
| 168 test_checkFinalConditions_shadowed_inSubClass() { |
| 169 indexTestUnit(''' |
| 170 class A { |
| 171 newName() {} // marker |
| 172 } |
| 173 class B extends A { |
| 174 test() {} |
| 175 main() { |
| 176 newName(); |
| 177 } |
| 178 } |
| 179 '''); |
| 180 createRenameRefactoringAtString('test() {}'); |
| 181 // check status |
| 182 refactoring.newName = 'newName'; |
| 183 return refactoring.checkFinalConditions().then((status) { |
| 184 assertRefactoringStatus( |
| 185 status, |
| 186 RefactoringStatusSeverity.ERROR, |
| 187 expectedMessage: "Renamed method will shadow method 'A.newName'.", |
| 188 expectedContextSearch: 'newName() {} // marker'); |
| 189 }); |
| 190 } |
| 191 |
| 192 test_checkFinalConditions_shadowsSuper_MethodElement() { |
| 193 indexTestUnit(''' |
| 194 class A { |
| 195 test() {} |
| 196 } |
| 197 class B extends A { |
| 198 newName() {} // marker |
| 199 main() { |
| 200 test(); |
| 201 } |
| 202 } |
| 203 '''); |
| 204 createRenameRefactoringAtString('test() {}'); |
| 205 // check status |
| 206 refactoring.newName = 'newName'; |
| 207 return refactoring.checkFinalConditions().then((status) { |
| 208 assertRefactoringStatus( |
| 209 status, |
| 210 RefactoringStatusSeverity.ERROR, |
| 211 expectedMessage: "Renamed method will be shadowed by method 'B.newName
'.", |
| 212 expectedContextSearch: 'newName() {} // marker'); |
| 213 }); |
| 214 } |
| 215 |
| 216 test_checkFinalConditions_shadowsSuper_inSubClass_FieldElement() { |
| 217 indexTestUnit(''' |
| 218 class A { |
| 219 int newName; // marker |
| 220 } |
| 221 class B extends A { |
| 222 test() {} |
| 223 } |
| 224 class C extends B { |
| 225 main() { |
| 226 print(newName); |
| 227 } |
| 228 } |
| 229 '''); |
| 230 createRenameRefactoringAtString('test() {}'); |
| 231 // check status |
| 232 refactoring.newName = 'newName'; |
| 233 return refactoring.checkFinalConditions().then((status) { |
| 234 assertRefactoringStatus( |
| 235 status, |
| 236 RefactoringStatusSeverity.ERROR, |
| 237 expectedMessage: "Renamed method will shadow field 'A.newName'.", |
| 238 expectedContextSearch: 'newName; // marker'); |
| 239 }); |
| 240 } |
| 241 |
| 23 test_checkInitialConditions_operator() { | 242 test_checkInitialConditions_operator() { |
| 24 indexTestUnit(''' | 243 indexTestUnit(''' |
| 25 class A { | 244 class A { |
| 26 operator -(other) => this; | 245 operator -(other) => this; |
| 27 } | 246 } |
| 28 '''); | 247 '''); |
| 29 createRenameRefactoringAtString('-(other)'); | 248 createRenameRefactoringAtString('-(other)'); |
| 30 // check status | 249 // check status |
| 31 refactoring.newName = 'newName'; | 250 refactoring.newName = 'newName'; |
| 32 return refactoring.checkInitialConditions().then((status) { | 251 return refactoring.checkInitialConditions().then((status) { |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 } | 592 } |
| 374 main(var a) { | 593 main(var a) { |
| 375 a.newName(); | 594 a.newName(); |
| 376 new A().newName(); | 595 new A().newName(); |
| 377 } | 596 } |
| 378 ''').then((_) { | 597 ''').then((_) { |
| 379 assertNoFileChange('/lib.dart'); | 598 assertNoFileChange('/lib.dart'); |
| 380 }); | 599 }); |
| 381 } | 600 } |
| 382 | 601 |
| 383 test_createChange_TypeParameterElement() { | |
| 384 indexTestUnit(''' | |
| 385 class A<Test> { | |
| 386 Test field; | |
| 387 List<Test> items; | |
| 388 Test method(Test p) => null; | |
| 389 } | |
| 390 '''); | |
| 391 // configure refactoring | |
| 392 createRenameRefactoringAtString('Test> {'); | |
| 393 expect(refactoring.refactoringName, 'Rename Type Parameter'); | |
| 394 expect(refactoring.oldName, 'Test'); | |
| 395 refactoring.newName = 'NewName'; | |
| 396 // validate change | |
| 397 return assertSuccessfulRename(''' | |
| 398 class A<NewName> { | |
| 399 NewName field; | |
| 400 List<NewName> items; | |
| 401 NewName method(NewName p) => null; | |
| 402 } | |
| 403 '''); | |
| 404 } | |
| 405 | |
| 406 test_createChange_PropertyAccessorElement_getter() { | 602 test_createChange_PropertyAccessorElement_getter() { |
| 407 indexTestUnit(''' | 603 indexTestUnit(''' |
| 408 class A { | 604 class A { |
| 409 get test {} // marker | 605 get test {} // marker |
| 410 set test(x) {} | 606 set test(x) {} |
| 411 main() { | 607 main() { |
| 412 print(test); | 608 print(test); |
| 413 test = 1; | 609 test = 1; |
| 414 } | 610 } |
| 415 } | 611 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 A a = new A(); | 701 A a = new A(); |
| 506 print(a.newName); | 702 print(a.newName); |
| 507 a.newName = 2; | 703 a.newName = 2; |
| 508 | 704 |
| 509 B b = new B(); | 705 B b = new B(); |
| 510 print(b.newName); | 706 print(b.newName); |
| 511 b.newName = 2; | 707 b.newName = 2; |
| 512 } | 708 } |
| 513 '''); | 709 '''); |
| 514 } | 710 } |
| 711 |
| 712 test_createChange_TypeParameterElement() { |
| 713 indexTestUnit(''' |
| 714 class A<Test> { |
| 715 Test field; |
| 716 List<Test> items; |
| 717 Test method(Test p) => null; |
| 718 } |
| 719 '''); |
| 720 // configure refactoring |
| 721 createRenameRefactoringAtString('Test> {'); |
| 722 expect(refactoring.refactoringName, 'Rename Type Parameter'); |
| 723 expect(refactoring.oldName, 'Test'); |
| 724 refactoring.newName = 'NewName'; |
| 725 // validate change |
| 726 return assertSuccessfulRename(''' |
| 727 class A<NewName> { |
| 728 NewName field; |
| 729 List<NewName> items; |
| 730 NewName method(NewName p) => null; |
| 731 } |
| 732 '''); |
| 733 } |
| 515 } | 734 } |
| OLD | NEW |