| 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_local; | |
| 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(RenameLocalTest); | |
| 17 } | |
| 18 | |
| 19 | |
| 20 @ReflectiveTestCase() | |
| 21 class RenameLocalTest extends RenameRefactoringTest { | |
| 22 test_checkFinalConditions_hasLocalFunction_after() { | |
| 23 indexTestUnit(''' | |
| 24 main() { | |
| 25 int test = 0; | |
| 26 newName() => 1; | |
| 27 } | |
| 28 '''); | |
| 29 createRenameRefactoringAtString('test = 0'); | |
| 30 // check status | |
| 31 refactoring.newName = 'newName'; | |
| 32 return refactoring.checkFinalConditions().then((status) { | |
| 33 assertRefactoringStatus( | |
| 34 status, | |
| 35 RefactoringStatusSeverity.ERROR, | |
| 36 expectedMessage: "Duplicate function 'newName'.", | |
| 37 expectedContextSearch: 'newName() => 1'); | |
| 38 }); | |
| 39 } | |
| 40 | |
| 41 test_checkFinalConditions_hasLocalFunction_before() { | |
| 42 indexTestUnit(''' | |
| 43 main() { | |
| 44 newName() => 1; | |
| 45 int test = 0; | |
| 46 } | |
| 47 '''); | |
| 48 createRenameRefactoringAtString('test = 0'); | |
| 49 // check status | |
| 50 refactoring.newName = 'newName'; | |
| 51 return refactoring.checkFinalConditions().then((status) { | |
| 52 assertRefactoringStatus( | |
| 53 status, | |
| 54 RefactoringStatusSeverity.ERROR, | |
| 55 expectedMessage: "Duplicate function 'newName'."); | |
| 56 }); | |
| 57 } | |
| 58 | |
| 59 test_checkFinalConditions_hasLocalVariable_after() { | |
| 60 indexTestUnit(''' | |
| 61 main() { | |
| 62 int test = 0; | |
| 63 var newName = 1; | |
| 64 } | |
| 65 '''); | |
| 66 createRenameRefactoringAtString('test = 0'); | |
| 67 // check status | |
| 68 refactoring.newName = 'newName'; | |
| 69 return refactoring.checkFinalConditions().then((status) { | |
| 70 assertRefactoringStatus( | |
| 71 status, | |
| 72 RefactoringStatusSeverity.ERROR, | |
| 73 expectedMessage: "Duplicate local variable 'newName'.", | |
| 74 expectedContextSearch: 'newName = 1;'); | |
| 75 }); | |
| 76 } | |
| 77 | |
| 78 test_checkFinalConditions_hasLocalVariable_before() { | |
| 79 indexTestUnit(''' | |
| 80 main() { | |
| 81 var newName = 1; | |
| 82 int test = 0; | |
| 83 } | |
| 84 '''); | |
| 85 createRenameRefactoringAtString('test = 0'); | |
| 86 // check status | |
| 87 refactoring.newName = 'newName'; | |
| 88 return refactoring.checkFinalConditions().then((status) { | |
| 89 assertRefactoringStatus( | |
| 90 status, | |
| 91 RefactoringStatusSeverity.ERROR, | |
| 92 expectedMessage: "Duplicate local variable 'newName'.", | |
| 93 expectedContextSearch: 'newName = 1;'); | |
| 94 }); | |
| 95 } | |
| 96 | |
| 97 test_checkFinalConditions_hasLocalVariable_otherBlock() { | |
| 98 indexTestUnit(''' | |
| 99 main() { | |
| 100 { | |
| 101 var newName = 1; | |
| 102 } | |
| 103 { | |
| 104 int test = 0; | |
| 105 } | |
| 106 } | |
| 107 '''); | |
| 108 createRenameRefactoringAtString('test = 0'); | |
| 109 // check status | |
| 110 refactoring.newName = 'newName'; | |
| 111 return assertRefactoringConditionsOK(); | |
| 112 } | |
| 113 | |
| 114 test_checkFinalConditions_hasLocalVariable_otherFunction() { | |
| 115 indexTestUnit(''' | |
| 116 main() { | |
| 117 int test = 0; | |
| 118 } | |
| 119 main2() { | |
| 120 var newName = 1; | |
| 121 } | |
| 122 '''); | |
| 123 createRenameRefactoringAtString('test = 0'); | |
| 124 // check status | |
| 125 refactoring.newName = 'newName'; | |
| 126 return assertRefactoringConditionsOK(); | |
| 127 } | |
| 128 | |
| 129 test_checkFinalConditions_shadows_classMember() { | |
| 130 indexTestUnit(''' | |
| 131 class A { | |
| 132 var newName = 1; | |
| 133 main() { | |
| 134 var test = 0; | |
| 135 print(newName); | |
| 136 } | |
| 137 } | |
| 138 '''); | |
| 139 createRenameRefactoringAtString('test = 0'); | |
| 140 // check status | |
| 141 refactoring.newName = 'newName'; | |
| 142 return refactoring.checkFinalConditions().then((status) { | |
| 143 assertRefactoringStatus( | |
| 144 status, | |
| 145 RefactoringStatusSeverity.ERROR, | |
| 146 expectedMessage: 'Usage of field "A.newName" declared in "test.dart" ' | |
| 147 'will be shadowed by renamed local variable.', | |
| 148 expectedContextSearch: 'newName);'); | |
| 149 }); | |
| 150 } | |
| 151 | |
| 152 test_checkFinalConditions_shadows_classMemberOK_qualifiedReference() { | |
| 153 indexTestUnit(''' | |
| 154 class A { | |
| 155 var newName = 1; | |
| 156 main() { | |
| 157 var test = 0; | |
| 158 print(this.newName); | |
| 159 } | |
| 160 } | |
| 161 '''); | |
| 162 createRenameRefactoringAtString('test = 0'); | |
| 163 // check status | |
| 164 refactoring.newName = 'newName'; | |
| 165 return assertRefactoringConditionsOK(); | |
| 166 } | |
| 167 | |
| 168 test_checkFinalConditions_shadows_topLevelFunction() { | |
| 169 indexTestUnit(''' | |
| 170 newName() {} | |
| 171 main() { | |
| 172 var test = 0; | |
| 173 newName(); // ref | |
| 174 } | |
| 175 '''); | |
| 176 createRenameRefactoringAtString('test = 0'); | |
| 177 // check status | |
| 178 refactoring.newName = 'newName'; | |
| 179 return refactoring.checkFinalConditions().then((status) { | |
| 180 assertRefactoringStatus( | |
| 181 status, | |
| 182 RefactoringStatusSeverity.ERROR, | |
| 183 expectedContextSearch: 'newName(); // ref'); | |
| 184 }); | |
| 185 } | |
| 186 | |
| 187 test_checkNewName_FunctionElement() { | |
| 188 indexTestUnit(''' | |
| 189 main() { | |
| 190 int test() {} | |
| 191 } | |
| 192 '''); | |
| 193 createRenameRefactoringAtString('test() {}'); | |
| 194 // null | |
| 195 refactoring.newName = null; | |
| 196 assertRefactoringStatus( | |
| 197 refactoring.checkNewName(), | |
| 198 RefactoringStatusSeverity.ERROR, | |
| 199 expectedMessage: "Function name must not be null."); | |
| 200 // OK | |
| 201 refactoring.newName = 'newName'; | |
| 202 assertRefactoringStatusOK(refactoring.checkNewName()); | |
| 203 } | |
| 204 | |
| 205 test_checkNewName_LocalVariableElement() { | |
| 206 indexTestUnit(''' | |
| 207 main() { | |
| 208 int test = 0; | |
| 209 } | |
| 210 '''); | |
| 211 createRenameRefactoringAtString('test = 0;'); | |
| 212 // null | |
| 213 refactoring.newName = null; | |
| 214 assertRefactoringStatus( | |
| 215 refactoring.checkNewName(), | |
| 216 RefactoringStatusSeverity.ERROR, | |
| 217 expectedMessage: "Variable name must not be null."); | |
| 218 // empty | |
| 219 refactoring.newName = ''; | |
| 220 assertRefactoringStatus( | |
| 221 refactoring.checkNewName(), | |
| 222 RefactoringStatusSeverity.ERROR, | |
| 223 expectedMessage: "Variable name must not be empty."); | |
| 224 // OK | |
| 225 refactoring.newName = 'newName'; | |
| 226 assertRefactoringStatusOK(refactoring.checkNewName()); | |
| 227 } | |
| 228 | |
| 229 test_checkNewName_LocalVariableElement_const() { | |
| 230 indexTestUnit(''' | |
| 231 main() { | |
| 232 const int TEST = 0; | |
| 233 } | |
| 234 '''); | |
| 235 createRenameRefactoringAtString('TEST = 0;'); | |
| 236 // null | |
| 237 refactoring.newName = null; | |
| 238 assertRefactoringStatus( | |
| 239 refactoring.checkNewName(), | |
| 240 RefactoringStatusSeverity.ERROR, | |
| 241 expectedMessage: "Constant name must not be null."); | |
| 242 // empty | |
| 243 refactoring.newName = ''; | |
| 244 assertRefactoringStatus( | |
| 245 refactoring.checkNewName(), | |
| 246 RefactoringStatusSeverity.ERROR, | |
| 247 expectedMessage: "Constant name must not be empty."); | |
| 248 // same | |
| 249 refactoring.newName = 'TEST'; | |
| 250 assertRefactoringStatus( | |
| 251 refactoring.checkNewName(), | |
| 252 RefactoringStatusSeverity.FATAL, | |
| 253 expectedMessage: "The new name must be different than the current name."
); | |
| 254 // OK | |
| 255 refactoring.newName = 'NEW_NAME'; | |
| 256 assertRefactoringStatusOK(refactoring.checkNewName()); | |
| 257 } | |
| 258 | |
| 259 test_checkNewName_ParameterElement() { | |
| 260 indexTestUnit(''' | |
| 261 main(test) { | |
| 262 } | |
| 263 '''); | |
| 264 createRenameRefactoringAtString('test) {'); | |
| 265 // null | |
| 266 refactoring.newName = null; | |
| 267 assertRefactoringStatus( | |
| 268 refactoring.checkNewName(), | |
| 269 RefactoringStatusSeverity.ERROR, | |
| 270 expectedMessage: "Parameter name must not be null."); | |
| 271 // OK | |
| 272 refactoring.newName = 'newName'; | |
| 273 assertRefactoringStatusOK(refactoring.checkNewName()); | |
| 274 } | |
| 275 | |
| 276 test_createChange_localFunction() { | |
| 277 indexTestUnit(''' | |
| 278 main() { | |
| 279 int test() => 0; | |
| 280 print(test); | |
| 281 print(test()); | |
| 282 } | |
| 283 '''); | |
| 284 // configure refactoring | |
| 285 createRenameRefactoringAtString('test() => 0'); | |
| 286 expect(refactoring.refactoringName, 'Rename Local Function'); | |
| 287 refactoring.newName = 'newName'; | |
| 288 // validate change | |
| 289 return assertSuccessfulRename(''' | |
| 290 main() { | |
| 291 int newName() => 0; | |
| 292 print(newName); | |
| 293 print(newName()); | |
| 294 } | |
| 295 '''); | |
| 296 } | |
| 297 | |
| 298 test_createChange_localFunction_sameNameDifferenceScopes() { | |
| 299 indexTestUnit(''' | |
| 300 main() { | |
| 301 { | |
| 302 int test() => 0; | |
| 303 print(test); | |
| 304 } | |
| 305 { | |
| 306 int test() => 1; | |
| 307 print(test); | |
| 308 } | |
| 309 { | |
| 310 int test() => 2; | |
| 311 print(test); | |
| 312 } | |
| 313 } | |
| 314 '''); | |
| 315 // configure refactoring | |
| 316 createRenameRefactoringAtString('test() => 1'); | |
| 317 expect(refactoring.refactoringName, 'Rename Local Function'); | |
| 318 refactoring.newName = 'newName'; | |
| 319 // validate change | |
| 320 return assertSuccessfulRename(''' | |
| 321 main() { | |
| 322 { | |
| 323 int test() => 0; | |
| 324 print(test); | |
| 325 } | |
| 326 { | |
| 327 int newName() => 1; | |
| 328 print(newName); | |
| 329 } | |
| 330 { | |
| 331 int test() => 2; | |
| 332 print(test); | |
| 333 } | |
| 334 } | |
| 335 '''); | |
| 336 } | |
| 337 | |
| 338 test_createChange_localVariable() { | |
| 339 indexTestUnit(''' | |
| 340 main() { | |
| 341 int test = 0; | |
| 342 test = 1; | |
| 343 test += 2; | |
| 344 print(test); | |
| 345 } | |
| 346 '''); | |
| 347 // configure refactoring | |
| 348 createRenameRefactoringAtString('test = 0'); | |
| 349 expect(refactoring.refactoringName, 'Rename Local Variable'); | |
| 350 refactoring.newName = 'newName'; | |
| 351 // validate change | |
| 352 return assertSuccessfulRename(''' | |
| 353 main() { | |
| 354 int newName = 0; | |
| 355 newName = 1; | |
| 356 newName += 2; | |
| 357 print(newName); | |
| 358 } | |
| 359 '''); | |
| 360 } | |
| 361 | |
| 362 test_createChange_localVariable_sameNameDifferenceScopes() { | |
| 363 indexTestUnit(''' | |
| 364 main() { | |
| 365 { | |
| 366 int test = 0; | |
| 367 print(test); | |
| 368 } | |
| 369 { | |
| 370 int test = 1; | |
| 371 print(test); | |
| 372 } | |
| 373 { | |
| 374 int test = 2; | |
| 375 print(test); | |
| 376 } | |
| 377 } | |
| 378 '''); | |
| 379 // configure refactoring | |
| 380 createRenameRefactoringAtString('test = 1'); | |
| 381 expect(refactoring.refactoringName, 'Rename Local Variable'); | |
| 382 refactoring.newName = 'newName'; | |
| 383 // validate change | |
| 384 return assertSuccessfulRename(''' | |
| 385 main() { | |
| 386 { | |
| 387 int test = 0; | |
| 388 print(test); | |
| 389 } | |
| 390 { | |
| 391 int newName = 1; | |
| 392 print(newName); | |
| 393 } | |
| 394 { | |
| 395 int test = 2; | |
| 396 print(test); | |
| 397 } | |
| 398 } | |
| 399 '''); | |
| 400 } | |
| 401 | |
| 402 test_createChange_parameter() { | |
| 403 indexTestUnit(''' | |
| 404 myFunction({int test}) { | |
| 405 test = 1; | |
| 406 test += 2; | |
| 407 print(test); | |
| 408 } | |
| 409 main() { | |
| 410 myFunction(test: 2); | |
| 411 } | |
| 412 '''); | |
| 413 // configure refactoring | |
| 414 createRenameRefactoringAtString('test}) {'); | |
| 415 expect(refactoring.refactoringName, 'Rename Parameter'); | |
| 416 refactoring.newName = 'newName'; | |
| 417 // validate change | |
| 418 return assertSuccessfulRename(''' | |
| 419 myFunction({int newName}) { | |
| 420 newName = 1; | |
| 421 newName += 2; | |
| 422 print(newName); | |
| 423 } | |
| 424 main() { | |
| 425 myFunction(newName: 2); | |
| 426 } | |
| 427 '''); | |
| 428 } | |
| 429 | |
| 430 test_createChange_parameter_namedInOtherFile() { | |
| 431 indexTestUnit(''' | |
| 432 class A { | |
| 433 A({test}); | |
| 434 } | |
| 435 '''); | |
| 436 indexUnit('/test2.dart', ''' | |
| 437 import 'test.dart'; | |
| 438 main() { | |
| 439 new A(test: 2); | |
| 440 } | |
| 441 '''); | |
| 442 // configure refactoring | |
| 443 createRenameRefactoringAtString('test});'); | |
| 444 expect(refactoring.refactoringName, 'Rename Parameter'); | |
| 445 refactoring.newName = 'newName'; | |
| 446 // validate change | |
| 447 return assertSuccessfulRename(''' | |
| 448 class A { | |
| 449 A({newName}); | |
| 450 } | |
| 451 ''').then((_) { | |
| 452 assertFileChangeResult('/test2.dart', ''' | |
| 453 import 'test.dart'; | |
| 454 main() { | |
| 455 new A(newName: 2); | |
| 456 } | |
| 457 '''); | |
| 458 }); | |
| 459 } | |
| 460 | |
| 461 test_oldName() { | |
| 462 indexTestUnit(''' | |
| 463 main() { | |
| 464 int test = 0; | |
| 465 } | |
| 466 '''); | |
| 467 // configure refactoring | |
| 468 createRenameRefactoringAtString('test = 0'); | |
| 469 // old name | |
| 470 expect(refactoring.oldName, 'test'); | |
| 471 } | |
| 472 } | |
| OLD | NEW |