| 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_constructor; |
| 6 |
| 7 import 'package:analysis_services/correction/status.dart'; |
| 8 import 'package:analysis_testing/reflective_tests.dart'; |
| 9 import 'package:analyzer/src/generated/ast.dart'; |
| 10 import 'package:analyzer/src/generated/element.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 import 'abstract_rename.dart'; |
| 14 |
| 15 |
| 16 main() { |
| 17 groupSep = ' | '; |
| 18 runReflectiveTests(RenameConstructorTest); |
| 19 } |
| 20 |
| 21 |
| 22 @ReflectiveTestCase() |
| 23 class RenameConstructorTest extends RenameRefactoringTest { |
| 24 test_checkFinalConditions_hasMember_constructor() { |
| 25 indexTestUnit(''' |
| 26 class A { |
| 27 A.test() {} |
| 28 A.newName() {} // existing |
| 29 } |
| 30 '''); |
| 31 _createConstructorDeclarationRefactoring('test() {}'); |
| 32 // check status |
| 33 refactoring.newName = 'newName'; |
| 34 return refactoring.checkFinalConditions().then((status) { |
| 35 assertRefactoringStatus( |
| 36 status, |
| 37 RefactoringStatusSeverity.ERROR, |
| 38 expectedMessage: "Class 'A' already declares constructor with name 'ne
wName'.", |
| 39 expectedContextSearch: 'newName() {} // existing'); |
| 40 }); |
| 41 } |
| 42 |
| 43 test_checkFinalConditions_hasMember_method() { |
| 44 indexTestUnit(''' |
| 45 class A { |
| 46 A.test() {} |
| 47 newName() {} // existing |
| 48 } |
| 49 '''); |
| 50 _createConstructorDeclarationRefactoring('test() {}'); |
| 51 // check status |
| 52 refactoring.newName = 'newName'; |
| 53 return refactoring.checkFinalConditions().then((status) { |
| 54 assertRefactoringStatus( |
| 55 status, |
| 56 RefactoringStatusSeverity.ERROR, |
| 57 expectedMessage: "Class 'A' already declares method with name 'newName
'.", |
| 58 expectedContextSearch: 'newName() {} // existing'); |
| 59 }); |
| 60 } |
| 61 |
| 62 test_checkNewName() { |
| 63 indexTestUnit(''' |
| 64 class A { |
| 65 A.test() {} |
| 66 } |
| 67 '''); |
| 68 createRenameRefactoringAtString('test() {}'); |
| 69 expect(refactoring.oldName, 'test'); |
| 70 // null |
| 71 refactoring.newName = null; |
| 72 assertRefactoringStatus( |
| 73 refactoring.checkNewName(), |
| 74 RefactoringStatusSeverity.ERROR, |
| 75 expectedMessage: "Constructor name must not be null."); |
| 76 // same |
| 77 refactoring.newName = 'test'; |
| 78 assertRefactoringStatus( |
| 79 refactoring.checkNewName(), |
| 80 RefactoringStatusSeverity.FATAL, |
| 81 expectedMessage: "The new name must be different than the current name."
); |
| 82 // empty |
| 83 refactoring.newName = ''; |
| 84 assertRefactoringStatusOK(refactoring.checkNewName()); |
| 85 // OK |
| 86 refactoring.newName = 'newName'; |
| 87 assertRefactoringStatusOK(refactoring.checkNewName()); |
| 88 } |
| 89 |
| 90 test_createChange_add() { |
| 91 indexTestUnit(''' |
| 92 class A { |
| 93 A() {} // marker |
| 94 } |
| 95 class B extends A { |
| 96 B() : super() {} |
| 97 factory B._() = A; |
| 98 } |
| 99 main() { |
| 100 new A(); |
| 101 } |
| 102 '''); |
| 103 // configure refactoring |
| 104 _createConstructorDeclarationRefactoring('() {} // marker'); |
| 105 expect(refactoring.refactoringName, 'Rename Constructor'); |
| 106 expect(refactoring.oldName, ''); |
| 107 // validate change |
| 108 refactoring.newName = 'newName'; |
| 109 return assertSuccessfulRename(''' |
| 110 class A { |
| 111 A.newName() {} // marker |
| 112 } |
| 113 class B extends A { |
| 114 B() : super.newName() {} |
| 115 factory B._() = A.newName; |
| 116 } |
| 117 main() { |
| 118 new A.newName(); |
| 119 } |
| 120 '''); |
| 121 } |
| 122 |
| 123 test_createChange_change() { |
| 124 indexTestUnit(''' |
| 125 class A { |
| 126 A.test() {} // marker |
| 127 } |
| 128 class B extends A { |
| 129 B() : super.test() {} |
| 130 factory B._() = A.test; |
| 131 } |
| 132 main() { |
| 133 new A.test(); |
| 134 } |
| 135 '''); |
| 136 // configure refactoring |
| 137 _createConstructorDeclarationRefactoring('test() {} // marker'); |
| 138 expect(refactoring.refactoringName, 'Rename Constructor'); |
| 139 expect(refactoring.oldName, 'test'); |
| 140 // validate change |
| 141 refactoring.newName = 'newName'; |
| 142 return assertSuccessfulRename(''' |
| 143 class A { |
| 144 A.newName() {} // marker |
| 145 } |
| 146 class B extends A { |
| 147 B() : super.newName() {} |
| 148 factory B._() = A.newName; |
| 149 } |
| 150 main() { |
| 151 new A.newName(); |
| 152 } |
| 153 '''); |
| 154 } |
| 155 |
| 156 test_createChange_remove() { |
| 157 indexTestUnit(''' |
| 158 class A { |
| 159 A.test() {} // marker |
| 160 } |
| 161 class B extends A { |
| 162 B() : super.test() {} |
| 163 factory B._() = A.test; |
| 164 } |
| 165 main() { |
| 166 new A.test(); |
| 167 } |
| 168 '''); |
| 169 // configure refactoring |
| 170 _createConstructorDeclarationRefactoring('test() {} // marker'); |
| 171 expect(refactoring.refactoringName, 'Rename Constructor'); |
| 172 expect(refactoring.oldName, 'test'); |
| 173 // validate change |
| 174 refactoring.newName = ''; |
| 175 return assertSuccessfulRename(''' |
| 176 class A { |
| 177 A() {} // marker |
| 178 } |
| 179 class B extends A { |
| 180 B() : super() {} |
| 181 factory B._() = A; |
| 182 } |
| 183 main() { |
| 184 new A(); |
| 185 } |
| 186 '''); |
| 187 } |
| 188 |
| 189 void _createConstructorDeclarationRefactoring(String search) { |
| 190 ConstructorElement element = |
| 191 findNodeElementAtString(search, (node) => node is ConstructorDeclaration
); |
| 192 createRenameRefactoringForElement(element); |
| 193 } |
| 194 } |
| OLD | NEW |