| 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.rename_unit_member; | |
| 6 | |
| 7 import 'package:analysis_services/correction/status.dart'; | |
| 8 import 'package:analysis_testing/reflective_tests.dart'; | |
| 9 import 'package:unittest/unittest.dart'; | |
| 10 | |
| 11 import 'abstract_rename.dart'; | |
| 12 | |
| 13 | |
| 14 main() { | |
| 15 groupSep = ' | '; | |
| 16 runReflectiveTests(RenameUnitMemberTest); | |
| 17 } | |
| 18 | |
| 19 | |
| 20 @ReflectiveTestCase() | |
| 21 class RenameUnitMemberTest extends RenameRefactoringTest { | |
| 22 test_checkFinalConditions_OK_qualifiedSuper_MethodElement() { | |
| 23 indexTestUnit(''' | |
| 24 class Test {} | |
| 25 class A { | |
| 26 NewName() {} | |
| 27 } | |
| 28 class B extends A { | |
| 29 main() { | |
| 30 super.NewName(); // super-ref | |
| 31 } | |
| 32 } | |
| 33 '''); | |
| 34 createRenameRefactoringAtString('Test {}'); | |
| 35 // check status | |
| 36 refactoring.newName = 'NewName'; | |
| 37 return refactoring.checkFinalConditions().then((status) { | |
| 38 assertRefactoringStatusOK(status); | |
| 39 }); | |
| 40 } | |
| 41 | |
| 42 test_checkFinalConditions_hasTopLevel_ClassElement() { | |
| 43 indexTestUnit(''' | |
| 44 class Test {} | |
| 45 class NewName {} // existing | |
| 46 '''); | |
| 47 createRenameRefactoringAtString('Test {}'); | |
| 48 // check status | |
| 49 refactoring.newName = 'NewName'; | |
| 50 return refactoring.checkFinalConditions().then((status) { | |
| 51 assertRefactoringStatus( | |
| 52 status, | |
| 53 RefactoringStatusSeverity.ERROR, | |
| 54 expectedMessage: "Library already declares class with name 'NewName'."
, | |
| 55 expectedContextSearch: 'NewName {} // existing'); | |
| 56 }); | |
| 57 } | |
| 58 | |
| 59 test_checkFinalConditions_hasTopLevel_FunctionTypeAliasElement() { | |
| 60 indexTestUnit(''' | |
| 61 class Test {} | |
| 62 typedef NewName(); // existing | |
| 63 '''); | |
| 64 createRenameRefactoringAtString('Test {}'); | |
| 65 // check status | |
| 66 refactoring.newName = 'NewName'; | |
| 67 return refactoring.checkFinalConditions().then((status) { | |
| 68 assertRefactoringStatus( | |
| 69 status, | |
| 70 RefactoringStatusSeverity.ERROR, | |
| 71 expectedMessage: | |
| 72 "Library already declares function type alias with name 'NewName'.
", | |
| 73 expectedContextSearch: 'NewName(); // existing'); | |
| 74 }); | |
| 75 } | |
| 76 | |
| 77 test_checkFinalConditions_shadowedBy_MethodElement() { | |
| 78 indexTestUnit(''' | |
| 79 class Test {} | |
| 80 class A { | |
| 81 void NewName() {} | |
| 82 main() { | |
| 83 new Test(); | |
| 84 } | |
| 85 } | |
| 86 '''); | |
| 87 createRenameRefactoringAtString('Test {}'); | |
| 88 // check status | |
| 89 refactoring.newName = 'NewName'; | |
| 90 return refactoring.checkFinalConditions().then((status) { | |
| 91 assertRefactoringStatus( | |
| 92 status, | |
| 93 RefactoringStatusSeverity.ERROR, | |
| 94 expectedMessage: | |
| 95 "Reference to renamed class will be shadowed by method 'A.NewName'
.", | |
| 96 expectedContextSearch: 'NewName() {}'); | |
| 97 }); | |
| 98 } | |
| 99 | |
| 100 test_checkFinalConditions_shadowsInSubClass_MethodElement() { | |
| 101 indexTestUnit(''' | |
| 102 class Test {} | |
| 103 class A { | |
| 104 NewName() {} | |
| 105 } | |
| 106 class B extends A { | |
| 107 main() { | |
| 108 NewName(); // super-ref | |
| 109 } | |
| 110 } | |
| 111 '''); | |
| 112 createRenameRefactoringAtString('Test {}'); | |
| 113 // check status | |
| 114 refactoring.newName = 'NewName'; | |
| 115 return refactoring.checkFinalConditions().then((status) { | |
| 116 assertRefactoringStatus( | |
| 117 status, | |
| 118 RefactoringStatusSeverity.ERROR, | |
| 119 expectedMessage: "Renamed class will shadow method 'A.NewName'.", | |
| 120 expectedContextSearch: 'NewName(); // super-ref'); | |
| 121 }); | |
| 122 } | |
| 123 | |
| 124 test_checkFinalConditions_shadowsInSubClass_importedLib() { | |
| 125 indexTestUnit(''' | |
| 126 class Test {} | |
| 127 '''); | |
| 128 indexUnit('/lib.dart', ''' | |
| 129 library my.lib; | |
| 130 import 'test.dart'; | |
| 131 class A { | |
| 132 NewName() {} | |
| 133 } | |
| 134 class B extends A { | |
| 135 main() { | |
| 136 NewName(); // super-ref | |
| 137 }", | |
| 138 } | |
| 139 '''); | |
| 140 createRenameRefactoringAtString('Test {}'); | |
| 141 // check status | |
| 142 refactoring.newName = 'NewName'; | |
| 143 return refactoring.checkFinalConditions().then((status) { | |
| 144 assertRefactoringStatus( | |
| 145 status, | |
| 146 RefactoringStatusSeverity.ERROR, | |
| 147 expectedMessage: "Renamed class will shadow method 'A.NewName'."); | |
| 148 }); | |
| 149 } | |
| 150 | |
| 151 test_checkFinalConditions_shadowsInSubClass_importedLib_hideCombinator() { | |
| 152 indexTestUnit(''' | |
| 153 class Test {} | |
| 154 '''); | |
| 155 indexUnit('/lib.dart', ''' | |
| 156 library my.lib; | |
| 157 import 'test.dart' hide Test; | |
| 158 class A { | |
| 159 NewName() {} | |
| 160 } | |
| 161 class B extends A { | |
| 162 main() { | |
| 163 NewName(); // super-ref | |
| 164 }", | |
| 165 } | |
| 166 '''); | |
| 167 createRenameRefactoringAtString('Test {}'); | |
| 168 // check status | |
| 169 refactoring.newName = 'NewName'; | |
| 170 return refactoring.checkFinalConditions().then((status) { | |
| 171 assertRefactoringStatusOK(status); | |
| 172 }); | |
| 173 } | |
| 174 | |
| 175 test_checkFinalConditions_shadowsInSubClass_notImportedLib() { | |
| 176 indexTestUnit(''' | |
| 177 class Test {} | |
| 178 '''); | |
| 179 indexUnit('/lib.dart', ''' | |
| 180 library my.lib; | |
| 181 class A { | |
| 182 NewName() {} | |
| 183 } | |
| 184 class B extends A { | |
| 185 main() { | |
| 186 NewName(); // super-ref | |
| 187 }", | |
| 188 } | |
| 189 '''); | |
| 190 createRenameRefactoringAtString('Test {}'); | |
| 191 // check status | |
| 192 refactoring.newName = 'NewName'; | |
| 193 return refactoring.checkFinalConditions().then((status) { | |
| 194 assertRefactoringStatusOK(status); | |
| 195 }); | |
| 196 } | |
| 197 | |
| 198 test_checkFinalConditions_shadowsInSubClass_notSubClass() { | |
| 199 indexTestUnit(''' | |
| 200 class Test {} | |
| 201 class A { | |
| 202 NewName() {} | |
| 203 } | |
| 204 class B { | |
| 205 main(A a) { | |
| 206 a.NewName(); | |
| 207 } | |
| 208 } | |
| 209 '''); | |
| 210 createRenameRefactoringAtString('Test {}'); | |
| 211 // check status | |
| 212 refactoring.newName = 'NewName'; | |
| 213 return refactoring.checkFinalConditions().then((status) { | |
| 214 assertRefactoringStatusOK(status); | |
| 215 }); | |
| 216 } | |
| 217 | |
| 218 test_checkNewName_ClassElement() { | |
| 219 indexTestUnit(''' | |
| 220 class Test {} | |
| 221 '''); | |
| 222 createRenameRefactoringAtString('Test {}'); | |
| 223 // null | |
| 224 refactoring.newName = null; | |
| 225 assertRefactoringStatus( | |
| 226 refactoring.checkNewName(), | |
| 227 RefactoringStatusSeverity.ERROR, | |
| 228 expectedMessage: "Class name must not be null."); | |
| 229 // empty | |
| 230 refactoring.newName = ''; | |
| 231 assertRefactoringStatus( | |
| 232 refactoring.checkNewName(), | |
| 233 RefactoringStatusSeverity.ERROR, | |
| 234 expectedMessage: "Class name must not be empty."); | |
| 235 // same | |
| 236 refactoring.newName = 'Test'; | |
| 237 assertRefactoringStatus( | |
| 238 refactoring.checkNewName(), | |
| 239 RefactoringStatusSeverity.FATAL, | |
| 240 expectedMessage: "The new name must be different than the current name."
); | |
| 241 // OK | |
| 242 refactoring.newName = 'NewName'; | |
| 243 assertRefactoringStatusOK(refactoring.checkNewName()); | |
| 244 } | |
| 245 | |
| 246 test_checkNewName_FunctionElement() { | |
| 247 indexTestUnit(''' | |
| 248 test() {} | |
| 249 '''); | |
| 250 createRenameRefactoringAtString('test() {}'); | |
| 251 // null | |
| 252 refactoring.newName = null; | |
| 253 assertRefactoringStatus( | |
| 254 refactoring.checkNewName(), | |
| 255 RefactoringStatusSeverity.ERROR, | |
| 256 expectedMessage: "Function name must not be null."); | |
| 257 // empty | |
| 258 refactoring.newName = ''; | |
| 259 assertRefactoringStatus( | |
| 260 refactoring.checkNewName(), | |
| 261 RefactoringStatusSeverity.ERROR, | |
| 262 expectedMessage: "Function name must not be empty."); | |
| 263 // OK | |
| 264 refactoring.newName = 'newName'; | |
| 265 assertRefactoringStatusOK(refactoring.checkNewName()); | |
| 266 } | |
| 267 | |
| 268 test_checkNewName_FunctionTypeAliasElement() { | |
| 269 indexTestUnit(''' | |
| 270 typedef Test(); | |
| 271 '''); | |
| 272 createRenameRefactoringAtString('Test();'); | |
| 273 // null | |
| 274 refactoring.newName = null; | |
| 275 assertRefactoringStatus( | |
| 276 refactoring.checkNewName(), | |
| 277 RefactoringStatusSeverity.ERROR, | |
| 278 expectedMessage: "Function type alias name must not be null."); | |
| 279 // OK | |
| 280 refactoring.newName = 'NewName'; | |
| 281 assertRefactoringStatusOK(refactoring.checkNewName()); | |
| 282 } | |
| 283 | |
| 284 test_checkNewName_TopLevelVariableElement() { | |
| 285 indexTestUnit(''' | |
| 286 var test; | |
| 287 '''); | |
| 288 createRenameRefactoringAtString('test;'); | |
| 289 // null | |
| 290 refactoring.newName = null; | |
| 291 assertRefactoringStatus( | |
| 292 refactoring.checkNewName(), | |
| 293 RefactoringStatusSeverity.ERROR, | |
| 294 expectedMessage: "Variable name must not be null."); | |
| 295 // empty | |
| 296 refactoring.newName = ''; | |
| 297 assertRefactoringStatus( | |
| 298 refactoring.checkNewName(), | |
| 299 RefactoringStatusSeverity.ERROR, | |
| 300 expectedMessage: "Variable name must not be empty."); | |
| 301 // OK | |
| 302 refactoring.newName = 'newName'; | |
| 303 assertRefactoringStatusOK(refactoring.checkNewName()); | |
| 304 } | |
| 305 | |
| 306 test_checkNewName_TopLevelVariableElement_const() { | |
| 307 indexTestUnit(''' | |
| 308 const TEST = 0; | |
| 309 '''); | |
| 310 createRenameRefactoringAtString('TEST ='); | |
| 311 // null | |
| 312 refactoring.newName = null; | |
| 313 assertRefactoringStatus( | |
| 314 refactoring.checkNewName(), | |
| 315 RefactoringStatusSeverity.ERROR, | |
| 316 expectedMessage: "Constant name must not be null."); | |
| 317 // empty | |
| 318 refactoring.newName = ''; | |
| 319 assertRefactoringStatus( | |
| 320 refactoring.checkNewName(), | |
| 321 RefactoringStatusSeverity.ERROR, | |
| 322 expectedMessage: "Constant name must not be empty."); | |
| 323 // OK | |
| 324 refactoring.newName = 'NEW_NAME'; | |
| 325 assertRefactoringStatusOK(refactoring.checkNewName()); | |
| 326 } | |
| 327 | |
| 328 test_createChange_ClassElement() { | |
| 329 indexTestUnit(''' | |
| 330 class Test implements Other { | |
| 331 Test() {} | |
| 332 Test.named() {} | |
| 333 } | |
| 334 class Other { | |
| 335 factory Other.a() = Test; | |
| 336 factory Other.b() = Test.named; | |
| 337 } | |
| 338 main() { | |
| 339 Test t1 = new Test(); | |
| 340 Test t2 = new Test.named(); | |
| 341 } | |
| 342 '''); | |
| 343 // configure refactoring | |
| 344 createRenameRefactoringAtString('Test implements'); | |
| 345 expect(refactoring.refactoringName, 'Rename Class'); | |
| 346 expect(refactoring.oldName, 'Test'); | |
| 347 refactoring.newName = 'NewName'; | |
| 348 // validate change | |
| 349 return assertSuccessfulRename(''' | |
| 350 class NewName implements Other { | |
| 351 NewName() {} | |
| 352 NewName.named() {} | |
| 353 } | |
| 354 class Other { | |
| 355 factory Other.a() = NewName; | |
| 356 factory Other.b() = NewName.named; | |
| 357 } | |
| 358 main() { | |
| 359 NewName t1 = new NewName(); | |
| 360 NewName t2 = new NewName.named(); | |
| 361 } | |
| 362 '''); | |
| 363 } | |
| 364 | |
| 365 test_createChange_ClassElement_parameterTypeNested() { | |
| 366 indexTestUnit(''' | |
| 367 class Test { | |
| 368 } | |
| 369 main(f(Test p)) { | |
| 370 } | |
| 371 '''); | |
| 372 // configure refactoring | |
| 373 createRenameRefactoringAtString('Test {'); | |
| 374 expect(refactoring.refactoringName, 'Rename Class'); | |
| 375 expect(refactoring.oldName, 'Test'); | |
| 376 refactoring.newName = 'NewName'; | |
| 377 // validate change | |
| 378 return assertSuccessfulRename(''' | |
| 379 class NewName { | |
| 380 } | |
| 381 main(f(NewName p)) { | |
| 382 } | |
| 383 '''); | |
| 384 } | |
| 385 | |
| 386 test_createChange_ClassElement_typeAlias() { | |
| 387 indexTestUnit(''' | |
| 388 class A {} | |
| 389 class Test = Object with A; | |
| 390 main(Test t) { | |
| 391 } | |
| 392 '''); | |
| 393 // configure refactoring | |
| 394 createRenameRefactoringAtString('Test ='); | |
| 395 expect(refactoring.refactoringName, 'Rename Class'); | |
| 396 expect(refactoring.oldName, 'Test'); | |
| 397 refactoring.newName = 'NewName'; | |
| 398 // validate change | |
| 399 return assertSuccessfulRename(''' | |
| 400 class A {} | |
| 401 class NewName = Object with A; | |
| 402 main(NewName t) { | |
| 403 } | |
| 404 '''); | |
| 405 } | |
| 406 | |
| 407 test_createChange_FunctionElement() { | |
| 408 indexTestUnit(''' | |
| 409 test() {} | |
| 410 foo() {} | |
| 411 main() { | |
| 412 print(test); | |
| 413 print(test()); | |
| 414 foo(); | |
| 415 } | |
| 416 '''); | |
| 417 // configure refactoring | |
| 418 createRenameRefactoringAtString('test() {}'); | |
| 419 expect(refactoring.refactoringName, 'Rename Top-Level Function'); | |
| 420 expect(refactoring.oldName, 'test'); | |
| 421 refactoring.newName = 'newName'; | |
| 422 // validate change | |
| 423 return assertSuccessfulRename(''' | |
| 424 newName() {} | |
| 425 foo() {} | |
| 426 main() { | |
| 427 print(newName); | |
| 428 print(newName()); | |
| 429 foo(); | |
| 430 } | |
| 431 '''); | |
| 432 } | |
| 433 | |
| 434 test_createChange_PropertyAccessorElement_getter_declaration() { | |
| 435 return _test_createChange_PropertyAccessorElement("test {}"); | |
| 436 } | |
| 437 | |
| 438 test_createChange_PropertyAccessorElement_getter_usage() { | |
| 439 return _test_createChange_PropertyAccessorElement("test);"); | |
| 440 } | |
| 441 | |
| 442 test_createChange_PropertyAccessorElement_mix() { | |
| 443 return _test_createChange_PropertyAccessorElement("test += 2"); | |
| 444 } | |
| 445 | |
| 446 test_createChange_PropertyAccessorElement_setter_declaration() { | |
| 447 return _test_createChange_PropertyAccessorElement("test(x) {}"); | |
| 448 } | |
| 449 | |
| 450 test_createChange_PropertyAccessorElement_setter_usage() { | |
| 451 return _test_createChange_PropertyAccessorElement("test = 1"); | |
| 452 } | |
| 453 | |
| 454 test_createChange_TopLevelVariableElement_field() { | |
| 455 return _test_createChange_TopLevelVariableElement("test = 0"); | |
| 456 } | |
| 457 | |
| 458 test_createChange_TopLevelVariableElement_getter() { | |
| 459 return _test_createChange_TopLevelVariableElement("test);"); | |
| 460 } | |
| 461 | |
| 462 test_createChange_TopLevelVariableElement_mix() { | |
| 463 return _test_createChange_TopLevelVariableElement("test += 2"); | |
| 464 } | |
| 465 | |
| 466 test_createChange_TopLevelVariableElement_setter() { | |
| 467 return _test_createChange_TopLevelVariableElement("test = 1"); | |
| 468 } | |
| 469 | |
| 470 _test_createChange_PropertyAccessorElement(String search) { | |
| 471 indexTestUnit(''' | |
| 472 get test {} | |
| 473 set test(x) {} | |
| 474 main() { | |
| 475 print(test); | |
| 476 test = 1; | |
| 477 test += 2; | |
| 478 } | |
| 479 '''); | |
| 480 // configure refactoring | |
| 481 createRenameRefactoringAtString(search); | |
| 482 expect(refactoring.refactoringName, 'Rename Top-Level Variable'); | |
| 483 expect(refactoring.oldName, 'test'); | |
| 484 refactoring.newName = 'newName'; | |
| 485 // validate change | |
| 486 return assertSuccessfulRename(''' | |
| 487 get newName {} | |
| 488 set newName(x) {} | |
| 489 main() { | |
| 490 print(newName); | |
| 491 newName = 1; | |
| 492 newName += 2; | |
| 493 } | |
| 494 '''); | |
| 495 } | |
| 496 | |
| 497 _test_createChange_TopLevelVariableElement(String search) { | |
| 498 indexTestUnit(''' | |
| 499 int test = 0; | |
| 500 main() { | |
| 501 print(test); | |
| 502 test = 1; | |
| 503 test += 2; | |
| 504 } | |
| 505 '''); | |
| 506 // configure refactoring | |
| 507 createRenameRefactoringAtString(search); | |
| 508 expect(refactoring.refactoringName, 'Rename Top-Level Variable'); | |
| 509 expect(refactoring.oldName, 'test'); | |
| 510 refactoring.newName = 'newName'; | |
| 511 // validate change | |
| 512 return assertSuccessfulRename(''' | |
| 513 int newName = 0; | |
| 514 main() { | |
| 515 print(newName); | |
| 516 newName = 1; | |
| 517 newName += 2; | |
| 518 } | |
| 519 '''); | |
| 520 } | |
| 521 } | |
| OLD | NEW |