| 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 // This code was auto-generated, is not intended to be edited, and is subject to | 5 // This code was auto-generated, is not intended to be edited, and is subject to |
| 6 // significant change. Please see the README file for more information. | 6 // significant change. Please see the README file for more information. |
| 7 | 7 |
| 8 library test.services.refactoring.rename_local; | 8 library test.services.refactoring.rename_local; |
| 9 | 9 |
| 10 import 'package:analysis_services/correction/change.dart'; | |
| 11 import 'package:analysis_services/correction/status.dart'; | 10 import 'package:analysis_services/correction/status.dart'; |
| 12 import 'package:analysis_services/index/index.dart'; | |
| 13 import 'package:analysis_services/index/local_memory_index.dart'; | |
| 14 import 'package:analysis_services/refactoring/refactoring.dart'; | |
| 15 import 'package:analysis_services/search/search_engine.dart'; | |
| 16 import 'package:analysis_services/src/correction/source_range.dart'; | |
| 17 import 'package:analysis_services/src/search/search_engine.dart'; | |
| 18 import 'package:analysis_testing/abstract_single_unit.dart'; | |
| 19 import 'package:analysis_testing/reflective_tests.dart'; | 11 import 'package:analysis_testing/reflective_tests.dart'; |
| 20 import 'package:analyzer/src/generated/ast.dart'; | |
| 21 import 'package:analyzer/src/generated/element.dart'; | |
| 22 import 'package:analyzer/src/generated/source.dart'; | |
| 23 import 'package:typed_mock/typed_mock.dart'; | |
| 24 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 25 | 13 |
| 14 import 'abstract_rename.dart'; |
| 15 |
| 26 | 16 |
| 27 | 17 |
| 28 main() { | 18 main() { |
| 29 groupSep = ' | '; | 19 groupSep = ' | '; |
| 30 runReflectiveTests(RenameLocalTest); | 20 runReflectiveTests(RenameLocalTest); |
| 31 } | 21 } |
| 32 | 22 |
| 33 | 23 |
| 34 @ReflectiveTestCase() | 24 @ReflectiveTestCase() |
| 35 class RenameLocalTest extends RenameRefactoringTest { | 25 class RenameLocalTest extends RenameRefactoringTest { |
| 36 void test_createChange_localVariable() { | 26 test_checkFinalConditions_hasLocalFunction_after() { |
| 37 indexTestUnit(''' | 27 indexTestUnit(''' |
| 38 main() { | 28 main() { |
| 39 int test = 0; | 29 int test = 0; |
| 30 newName() => 1; |
| 31 } |
| 32 '''); |
| 33 createRenameRefactoringAtString('test = 0'); |
| 34 // check status |
| 35 refactoring.newName = 'newName'; |
| 36 return refactoring.checkFinalConditions().then((status) { |
| 37 assertRefactoringStatus( |
| 38 status, |
| 39 RefactoringStatusSeverity.ERROR, |
| 40 expectedMessage: "Duplicate function 'newName'.", |
| 41 expectedContextSearch: 'newName() => 1'); |
| 42 }); |
| 43 } |
| 44 |
| 45 test_checkFinalConditions_hasLocalFunction_before() { |
| 46 indexTestUnit(''' |
| 47 main() { |
| 48 newName() => 1; |
| 49 int test = 0; |
| 50 } |
| 51 '''); |
| 52 createRenameRefactoringAtString('test = 0'); |
| 53 // check status |
| 54 refactoring.newName = 'newName'; |
| 55 return refactoring.checkFinalConditions().then((status) { |
| 56 assertRefactoringStatus( |
| 57 status, |
| 58 RefactoringStatusSeverity.ERROR, |
| 59 expectedMessage: "Duplicate function 'newName'."); |
| 60 }); |
| 61 } |
| 62 |
| 63 test_checkFinalConditions_hasLocalVariable_after() { |
| 64 indexTestUnit(''' |
| 65 main() { |
| 66 int test = 0; |
| 67 var newName = 1; |
| 68 } |
| 69 '''); |
| 70 createRenameRefactoringAtString('test = 0'); |
| 71 // check status |
| 72 refactoring.newName = 'newName'; |
| 73 return refactoring.checkFinalConditions().then((status) { |
| 74 assertRefactoringStatus( |
| 75 status, |
| 76 RefactoringStatusSeverity.ERROR, |
| 77 expectedMessage: "Duplicate local variable 'newName'.", |
| 78 expectedContextSearch: 'newName = 1;'); |
| 79 }); |
| 80 } |
| 81 |
| 82 test_checkFinalConditions_hasLocalVariable_before() { |
| 83 indexTestUnit(''' |
| 84 main() { |
| 85 var newName = 1; |
| 86 int test = 0; |
| 87 } |
| 88 '''); |
| 89 createRenameRefactoringAtString('test = 0'); |
| 90 // check status |
| 91 refactoring.newName = 'newName'; |
| 92 return refactoring.checkFinalConditions().then((status) { |
| 93 assertRefactoringStatus( |
| 94 status, |
| 95 RefactoringStatusSeverity.ERROR, |
| 96 expectedMessage: "Duplicate local variable 'newName'.", |
| 97 expectedContextSearch: 'newName = 1;'); |
| 98 }); |
| 99 } |
| 100 |
| 101 test_checkFinalConditions_hasLocalVariable_otherBlock() { |
| 102 indexTestUnit(''' |
| 103 main() { |
| 104 { |
| 105 var newName = 1; |
| 106 } |
| 107 { |
| 108 int test = 0; |
| 109 } |
| 110 } |
| 111 '''); |
| 112 createRenameRefactoringAtString('test = 0'); |
| 113 // check status |
| 114 refactoring.newName = 'newName'; |
| 115 return assertRefactoringStatusOK(); |
| 116 } |
| 117 |
| 118 test_checkFinalConditions_hasLocalVariable_otherFunction() { |
| 119 indexTestUnit(''' |
| 120 main() { |
| 121 int test = 0; |
| 122 } |
| 123 main2() { |
| 124 var newName = 1; |
| 125 } |
| 126 '''); |
| 127 createRenameRefactoringAtString('test = 0'); |
| 128 // check status |
| 129 refactoring.newName = 'newName'; |
| 130 return assertRefactoringStatusOK(); |
| 131 } |
| 132 |
| 133 test_checkFinalConditions_shadows_classMember() { |
| 134 indexTestUnit(''' |
| 135 class A { |
| 136 var newName = 1; |
| 137 main() { |
| 138 var test = 0; |
| 139 print(newName); |
| 140 } |
| 141 } |
| 142 '''); |
| 143 createRenameRefactoringAtString('test = 0'); |
| 144 // check status |
| 145 refactoring.newName = 'newName'; |
| 146 return refactoring.checkFinalConditions().then((status) { |
| 147 assertRefactoringStatus( |
| 148 status, |
| 149 RefactoringStatusSeverity.ERROR, |
| 150 expectedMessage: 'Usage of field "A.newName" declared in "test.dart" ' |
| 151 'will be shadowed by renamed local variable.', |
| 152 expectedContextSearch: 'newName);'); |
| 153 }); |
| 154 } |
| 155 |
| 156 test_checkFinalConditions_shadows_classMemberOK_qualifiedReference() { |
| 157 indexTestUnit(''' |
| 158 class A { |
| 159 var newName = 1; |
| 160 main() { |
| 161 var test = 0; |
| 162 print(this.newName); |
| 163 } |
| 164 } |
| 165 '''); |
| 166 createRenameRefactoringAtString('test = 0'); |
| 167 // check status |
| 168 refactoring.newName = 'newName'; |
| 169 return assertRefactoringStatusOK(); |
| 170 } |
| 171 |
| 172 test_checkFinalConditions_shadows_topLevelFunction() { |
| 173 indexTestUnit(''' |
| 174 newName() {} |
| 175 main() { |
| 176 var test = 0; |
| 177 newName(); // ref |
| 178 } |
| 179 '''); |
| 180 createRenameRefactoringAtString('test = 0'); |
| 181 // check status |
| 182 refactoring.newName = 'newName'; |
| 183 return refactoring.checkFinalConditions().then((status) { |
| 184 assertRefactoringStatus( |
| 185 status, |
| 186 RefactoringStatusSeverity.ERROR, |
| 187 expectedContextSearch: 'newName(); // ref'); |
| 188 }); |
| 189 } |
| 190 |
| 191 test_createChange_localFunction() { |
| 192 indexTestUnit(''' |
| 193 main() { |
| 194 int test() => 0; |
| 195 print(test); |
| 196 print(test()); |
| 197 } |
| 198 '''); |
| 199 // configure refactoring |
| 200 createRenameRefactoringAtString('test() => 0'); |
| 201 expect(refactoring.refactoringName, 'Rename Local Function'); |
| 202 refactoring.newName = 'newName'; |
| 203 // validate change |
| 204 return assertSuccessfulRename(''' |
| 205 main() { |
| 206 int newName() => 0; |
| 207 print(newName); |
| 208 print(newName()); |
| 209 } |
| 210 '''); |
| 211 } |
| 212 |
| 213 test_createChange_localFunction_sameNameDifferenceScopes() { |
| 214 indexTestUnit(''' |
| 215 main() { |
| 216 { |
| 217 int test() => 0; |
| 218 print(test); |
| 219 } |
| 220 { |
| 221 int test() => 1; |
| 222 print(test); |
| 223 } |
| 224 { |
| 225 int test() => 2; |
| 226 print(test); |
| 227 } |
| 228 } |
| 229 '''); |
| 230 // configure refactoring |
| 231 createRenameRefactoringAtString('test() => 1'); |
| 232 expect(refactoring.refactoringName, 'Rename Local Function'); |
| 233 refactoring.newName = 'newName'; |
| 234 // validate change |
| 235 return assertSuccessfulRename(''' |
| 236 main() { |
| 237 { |
| 238 int test() => 0; |
| 239 print(test); |
| 240 } |
| 241 { |
| 242 int newName() => 1; |
| 243 print(newName); |
| 244 } |
| 245 { |
| 246 int test() => 2; |
| 247 print(test); |
| 248 } |
| 249 } |
| 250 '''); |
| 251 } |
| 252 |
| 253 test_createChange_localVariable() { |
| 254 indexTestUnit(''' |
| 255 main() { |
| 256 int test = 0; |
| 40 test = 1; | 257 test = 1; |
| 41 test += 2; | 258 test += 2; |
| 42 print(test); | 259 print(test); |
| 43 } | 260 } |
| 44 '''); | 261 '''); |
| 45 // configure refactoring | 262 // configure refactoring |
| 46 createRenameRefactoringAtString('test = 0'); | 263 createRenameRefactoringAtString('test = 0'); |
| 47 print(refactoring.refactoringName); | |
| 48 expect(refactoring.refactoringName, 'Rename Local Variable'); | 264 expect(refactoring.refactoringName, 'Rename Local Variable'); |
| 49 refactoring.newName = 'newName'; | 265 refactoring.newName = 'newName'; |
| 50 // validate change | 266 // validate change |
| 51 // TODO(scheglov) | 267 return assertSuccessfulRename(''' |
| 52 // assertSuccessfulRename( | 268 main() { |
| 53 // "// filler filler filler filler filler filler filler filler filler fil
ler", | 269 int newName = 0; |
| 54 // "main() {", | 270 newName = 1; |
| 55 // " int newName = 0;", | 271 newName += 2; |
| 56 // " newName = 1;", | 272 print(newName); |
| 57 // " newName += 2;", | 273 } |
| 58 // " print(newName);", | 274 '''); |
| 59 // "}"); | 275 } |
| 276 |
| 277 test_createChange_localVariable_sameNameDifferenceScopes() { |
| 278 indexTestUnit(''' |
| 279 main() { |
| 280 { |
| 281 int test = 0; |
| 282 print(test); |
| 283 } |
| 284 { |
| 285 int test = 1; |
| 286 print(test); |
| 287 } |
| 288 { |
| 289 int test = 2; |
| 290 print(test); |
| 60 } | 291 } |
| 61 } | 292 } |
| 62 | 293 '''); |
| 63 | 294 // configure refactoring |
| 64 /** | 295 createRenameRefactoringAtString('test = 1'); |
| 65 * The base class for all [RenameRefactoring] tests. | 296 expect(refactoring.refactoringName, 'Rename Local Variable'); |
| 66 * | 297 refactoring.newName = 'newName'; |
| 67 * TODO(scheglov) extract | 298 // validate change |
| 68 */ | 299 return assertSuccessfulRename(''' |
| 69 class RenameRefactoringTest extends AbstractSingleUnitTest { | 300 main() { |
| 70 Index index; | 301 { |
| 71 SearchEngineImpl searchEngine; | 302 int test = 0; |
| 72 | 303 print(test); |
| 73 RenameRefactoring refactoring; | 304 } |
| 74 Change refactoringChange; | 305 { |
| 75 | 306 int newName = 1; |
| 76 /** | 307 print(newName); |
| 77 * Creates a new [RenameRefactoring] in [refactoringC] for the [Element] of | 308 } |
| 78 * the [SimpleIdentifier] at the given [search] pattern. | 309 { |
| 79 */ | 310 int test = 2; |
| 80 void createRenameRefactoringAtString(String search) { | 311 print(test); |
| 81 SimpleIdentifier identifier = findIdentifier(search); | 312 } |
| 82 Element element = identifier.bestElement; | 313 } |
| 83 // TODO(scheglov) uncomment later | 314 '''); |
| 84 // if (element instanceof PrefixElement) { | |
| 85 // element = IndexContributor.getImportElement(identifier); | |
| 86 // } | |
| 87 refactoring = new RenameRefactoring(searchEngine, element); | |
| 88 expect(refactoring, isNotNull); | |
| 89 } | 315 } |
| 90 | 316 |
| 91 void indexTestUnit(String code) { | 317 test_createChange_parameter() { |
| 92 resolveTestUnit(code); | 318 indexTestUnit(''' |
| 93 index.indexUnit(context, testUnit); | 319 myFunction({int test}) { |
| 320 test = 1; |
| 321 test += 2; |
| 322 print(test); |
| 323 } |
| 324 main() { |
| 325 myFunction(test: 2); |
| 326 } |
| 327 '''); |
| 328 // configure refactoring |
| 329 createRenameRefactoringAtString('test}) {'); |
| 330 expect(refactoring.refactoringName, 'Rename Parameter'); |
| 331 refactoring.newName = 'newName'; |
| 332 // validate change |
| 333 return assertSuccessfulRename(''' |
| 334 myFunction({int newName}) { |
| 335 newName = 1; |
| 336 newName += 2; |
| 337 print(newName); |
| 338 } |
| 339 main() { |
| 340 myFunction(newName: 2); |
| 341 } |
| 342 '''); |
| 94 } | 343 } |
| 95 | 344 |
| 96 void setUp() { | 345 test_createChange_parameter_namedInOtherFile() { |
| 97 super.setUp(); | 346 indexTestUnit(''' |
| 98 index = createLocalMemoryIndex(); | 347 class A { |
| 99 searchEngine = new SearchEngineImpl(index); | 348 A({test}); |
| 349 } |
| 350 '''); |
| 351 indexUnit('/test2.dart', ''' |
| 352 import 'test.dart'; |
| 353 main() { |
| 354 new A(test: 2); |
| 355 } |
| 356 '''); |
| 357 // configure refactoring |
| 358 createRenameRefactoringAtString('test});'); |
| 359 expect(refactoring.refactoringName, 'Rename Parameter'); |
| 360 refactoring.newName = 'newName'; |
| 361 // validate change |
| 362 return assertSuccessfulRename(''' |
| 363 class A { |
| 364 A({newName}); |
| 365 } |
| 366 ''').then((_) { |
| 367 assertFileChangeResult('/test2.dart', ''' |
| 368 import 'test.dart'; |
| 369 main() { |
| 370 new A(newName: 2); |
| 371 } |
| 372 '''); |
| 373 }); |
| 374 } |
| 375 |
| 376 test_oldName() { |
| 377 indexTestUnit(''' |
| 378 main() { |
| 379 int test = 0; |
| 380 } |
| 381 '''); |
| 382 // configure refactoring |
| 383 createRenameRefactoringAtString('test = 0'); |
| 384 // old name |
| 385 expect(refactoring.oldName, 'test'); |
| 100 } | 386 } |
| 101 } | 387 } |
| OLD | NEW |