| 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_import; | 5 library test.services.refactoring.rename_import; |
| 6 | 6 |
| 7 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 7 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 8 import 'package:analyzer/dart/ast/ast.dart'; | 8 import 'package:analyzer/dart/ast/ast.dart'; |
| 9 import 'package:test/test.dart'; | 9 import 'package:test/test.dart'; |
| 10 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 10 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 11 | 11 |
| 12 import 'abstract_rename.dart'; | 12 import 'abstract_rename.dart'; |
| 13 | 13 |
| 14 main() { | 14 main() { |
| 15 defineReflectiveSuite(() { | 15 defineReflectiveSuite(() { |
| 16 defineReflectiveTests(RenameImportTest); | 16 defineReflectiveTests(RenameImportTest); |
| 17 }); | 17 }); |
| 18 } | 18 } |
| 19 | 19 |
| 20 @reflectiveTest | 20 @reflectiveTest |
| 21 class RenameImportTest extends RenameRefactoringTest { | 21 class RenameImportTest extends RenameRefactoringTest { |
| 22 test_checkNewName() { | 22 test_checkNewName() async { |
| 23 indexTestUnit("import 'dart:async' as test;"); | 23 await indexTestUnit("import 'dart:async' as test;"); |
| 24 _createRefactoring("import 'dart:"); | 24 _createRefactoring("import 'dart:"); |
| 25 expect(refactoring.oldName, 'test'); | 25 expect(refactoring.oldName, 'test'); |
| 26 // null | 26 // null |
| 27 refactoring.newName = null; | 27 refactoring.newName = null; |
| 28 assertRefactoringStatus( | 28 assertRefactoringStatus( |
| 29 refactoring.checkNewName(), RefactoringProblemSeverity.FATAL, | 29 refactoring.checkNewName(), RefactoringProblemSeverity.FATAL, |
| 30 expectedMessage: "Import prefix name must not be null."); | 30 expectedMessage: "Import prefix name must not be null."); |
| 31 // same | 31 // same |
| 32 refactoring.newName = 'test'; | 32 refactoring.newName = 'test'; |
| 33 assertRefactoringStatus( | 33 assertRefactoringStatus( |
| 34 refactoring.checkNewName(), RefactoringProblemSeverity.FATAL, | 34 refactoring.checkNewName(), RefactoringProblemSeverity.FATAL, |
| 35 expectedMessage: | 35 expectedMessage: |
| 36 "The new name must be different than the current name."); | 36 "The new name must be different than the current name."); |
| 37 // empty | 37 // empty |
| 38 refactoring.newName = ''; | 38 refactoring.newName = ''; |
| 39 assertRefactoringStatusOK(refactoring.checkNewName()); | 39 assertRefactoringStatusOK(refactoring.checkNewName()); |
| 40 // OK | 40 // OK |
| 41 refactoring.newName = 'newName'; | 41 refactoring.newName = 'newName'; |
| 42 assertRefactoringStatusOK(refactoring.checkNewName()); | 42 assertRefactoringStatusOK(refactoring.checkNewName()); |
| 43 } | 43 } |
| 44 | 44 |
| 45 test_createChange_add() { | 45 test_createChange_add() async { |
| 46 indexTestUnit(''' | 46 await indexTestUnit(''' |
| 47 import 'dart:async'; | 47 import 'dart:async'; |
| 48 import 'dart:math' show Random, min hide max; | 48 import 'dart:math' show Random, min hide max; |
| 49 main() { | 49 main() { |
| 50 Future f; | 50 Future f; |
| 51 Random r; | 51 Random r; |
| 52 min(1, 2); | 52 min(1, 2); |
| 53 } | 53 } |
| 54 '''); | 54 '''); |
| 55 // configure refactoring | 55 // configure refactoring |
| 56 _createRefactoring("import 'dart:math"); | 56 _createRefactoring("import 'dart:math"); |
| 57 expect(refactoring.refactoringName, 'Rename Import Prefix'); | 57 expect(refactoring.refactoringName, 'Rename Import Prefix'); |
| 58 refactoring.newName = 'newName'; | 58 refactoring.newName = 'newName'; |
| 59 // validate change | 59 // validate change |
| 60 return assertSuccessfulRefactoring(''' | 60 return assertSuccessfulRefactoring(''' |
| 61 import 'dart:async'; | 61 import 'dart:async'; |
| 62 import 'dart:math' as newName show Random, min hide max; | 62 import 'dart:math' as newName show Random, min hide max; |
| 63 main() { | 63 main() { |
| 64 Future f; | 64 Future f; |
| 65 newName.Random r; | 65 newName.Random r; |
| 66 newName.min(1, 2); | 66 newName.min(1, 2); |
| 67 } | 67 } |
| 68 '''); | 68 '''); |
| 69 } | 69 } |
| 70 | 70 |
| 71 test_createChange_add_interpolationExpression_hasCurlyBrackets() { | 71 test_createChange_add_interpolationExpression_hasCurlyBrackets() async { |
| 72 indexTestUnit(r''' | 72 await indexTestUnit(r''' |
| 73 import 'dart:async'; | 73 import 'dart:async'; |
| 74 main() { | 74 main() { |
| 75 Future f; | 75 Future f; |
| 76 print('Future type: ${Future}'); | 76 print('Future type: ${Future}'); |
| 77 } | 77 } |
| 78 '''); | 78 '''); |
| 79 // configure refactoring | 79 // configure refactoring |
| 80 _createRefactoring("import 'dart:async"); | 80 _createRefactoring("import 'dart:async"); |
| 81 expect(refactoring.refactoringName, 'Rename Import Prefix'); | 81 expect(refactoring.refactoringName, 'Rename Import Prefix'); |
| 82 refactoring.newName = 'newName'; | 82 refactoring.newName = 'newName'; |
| 83 // validate change | 83 // validate change |
| 84 return assertSuccessfulRefactoring(r''' | 84 return assertSuccessfulRefactoring(r''' |
| 85 import 'dart:async' as newName; | 85 import 'dart:async' as newName; |
| 86 main() { | 86 main() { |
| 87 newName.Future f; | 87 newName.Future f; |
| 88 print('Future type: ${newName.Future}'); | 88 print('Future type: ${newName.Future}'); |
| 89 } | 89 } |
| 90 '''); | 90 '''); |
| 91 } | 91 } |
| 92 | 92 |
| 93 test_createChange_add_interpolationExpression_noCurlyBrackets() { | 93 test_createChange_add_interpolationExpression_noCurlyBrackets() async { |
| 94 indexTestUnit(r''' | 94 await indexTestUnit(r''' |
| 95 import 'dart:async'; | 95 import 'dart:async'; |
| 96 main() { | 96 main() { |
| 97 Future f; | 97 Future f; |
| 98 print('Future type: $Future'); | 98 print('Future type: $Future'); |
| 99 } | 99 } |
| 100 '''); | 100 '''); |
| 101 // configure refactoring | 101 // configure refactoring |
| 102 _createRefactoring("import 'dart:async"); | 102 _createRefactoring("import 'dart:async"); |
| 103 expect(refactoring.refactoringName, 'Rename Import Prefix'); | 103 expect(refactoring.refactoringName, 'Rename Import Prefix'); |
| 104 refactoring.newName = 'newName'; | 104 refactoring.newName = 'newName'; |
| 105 // validate change | 105 // validate change |
| 106 return assertSuccessfulRefactoring(r''' | 106 return assertSuccessfulRefactoring(r''' |
| 107 import 'dart:async' as newName; | 107 import 'dart:async' as newName; |
| 108 main() { | 108 main() { |
| 109 newName.Future f; | 109 newName.Future f; |
| 110 print('Future type: ${newName.Future}'); | 110 print('Future type: ${newName.Future}'); |
| 111 } | 111 } |
| 112 '''); | 112 '''); |
| 113 } | 113 } |
| 114 | 114 |
| 115 test_createChange_change_className() { | 115 test_createChange_change_className() async { |
| 116 indexTestUnit(''' | 116 await indexTestUnit(''' |
| 117 import 'dart:math' as test; | 117 import 'dart:math' as test; |
| 118 import 'dart:async' as test; | 118 import 'dart:async' as test; |
| 119 main() { | 119 main() { |
| 120 test.Future f; | 120 test.Future f; |
| 121 } | 121 } |
| 122 '''); | 122 '''); |
| 123 // configure refactoring | 123 // configure refactoring |
| 124 _createRefactoring("import 'dart:async"); | 124 _createRefactoring("import 'dart:async"); |
| 125 expect(refactoring.refactoringName, 'Rename Import Prefix'); | 125 expect(refactoring.refactoringName, 'Rename Import Prefix'); |
| 126 expect(refactoring.oldName, 'test'); | 126 expect(refactoring.oldName, 'test'); |
| 127 refactoring.newName = 'newName'; | 127 refactoring.newName = 'newName'; |
| 128 // validate change | 128 // validate change |
| 129 return assertSuccessfulRefactoring(''' | 129 return assertSuccessfulRefactoring(''' |
| 130 import 'dart:math' as test; | 130 import 'dart:math' as test; |
| 131 import 'dart:async' as newName; | 131 import 'dart:async' as newName; |
| 132 main() { | 132 main() { |
| 133 newName.Future f; | 133 newName.Future f; |
| 134 } | 134 } |
| 135 '''); | 135 '''); |
| 136 } | 136 } |
| 137 | 137 |
| 138 test_createChange_change_function() { | 138 test_createChange_change_function() async { |
| 139 indexTestUnit(''' | 139 await indexTestUnit(''' |
| 140 import 'dart:math' as test; | 140 import 'dart:math' as test; |
| 141 import 'dart:async' as test; | 141 import 'dart:async' as test; |
| 142 main() { | 142 main() { |
| 143 test.max(1, 2); | 143 test.max(1, 2); |
| 144 test.Future f; | 144 test.Future f; |
| 145 } | 145 } |
| 146 '''); | 146 '''); |
| 147 // configure refactoring | 147 // configure refactoring |
| 148 _createRefactoring("import 'dart:math"); | 148 _createRefactoring("import 'dart:math"); |
| 149 expect(refactoring.refactoringName, 'Rename Import Prefix'); | 149 expect(refactoring.refactoringName, 'Rename Import Prefix'); |
| 150 expect(refactoring.oldName, 'test'); | 150 expect(refactoring.oldName, 'test'); |
| 151 refactoring.newName = 'newName'; | 151 refactoring.newName = 'newName'; |
| 152 // validate change | 152 // validate change |
| 153 return assertSuccessfulRefactoring(''' | 153 return assertSuccessfulRefactoring(''' |
| 154 import 'dart:math' as newName; | 154 import 'dart:math' as newName; |
| 155 import 'dart:async' as test; | 155 import 'dart:async' as test; |
| 156 main() { | 156 main() { |
| 157 newName.max(1, 2); | 157 newName.max(1, 2); |
| 158 test.Future f; | 158 test.Future f; |
| 159 } | 159 } |
| 160 '''); | 160 '''); |
| 161 } | 161 } |
| 162 | 162 |
| 163 test_createChange_change_onPrefixElement() { | 163 test_createChange_change_onPrefixElement() async { |
| 164 indexTestUnit(''' | 164 await indexTestUnit(''' |
| 165 import 'dart:async' as test; | 165 import 'dart:async' as test; |
| 166 import 'dart:math' as test; | 166 import 'dart:math' as test; |
| 167 main() { | 167 main() { |
| 168 test.Future f; | 168 test.Future f; |
| 169 test.PI; | 169 test.PI; |
| 170 test.E; | 170 test.E; |
| 171 } | 171 } |
| 172 '''); | 172 '''); |
| 173 // configure refactoring | 173 // configure refactoring |
| 174 createRenameRefactoringAtString('test.PI'); | 174 createRenameRefactoringAtString('test.PI'); |
| 175 expect(refactoring.refactoringName, 'Rename Import Prefix'); | 175 expect(refactoring.refactoringName, 'Rename Import Prefix'); |
| 176 expect(refactoring.oldName, 'test'); | 176 expect(refactoring.oldName, 'test'); |
| 177 refactoring.newName = 'newName'; | 177 refactoring.newName = 'newName'; |
| 178 // validate change | 178 // validate change |
| 179 return assertSuccessfulRefactoring(''' | 179 return assertSuccessfulRefactoring(''' |
| 180 import 'dart:async' as test; | 180 import 'dart:async' as test; |
| 181 import 'dart:math' as newName; | 181 import 'dart:math' as newName; |
| 182 main() { | 182 main() { |
| 183 test.Future f; | 183 test.Future f; |
| 184 newName.PI; | 184 newName.PI; |
| 185 newName.E; | 185 newName.E; |
| 186 } | 186 } |
| 187 '''); | 187 '''); |
| 188 } | 188 } |
| 189 | 189 |
| 190 test_createChange_remove() { | 190 test_createChange_remove() async { |
| 191 indexTestUnit(''' | 191 await indexTestUnit(''' |
| 192 import 'dart:math' as test; | 192 import 'dart:math' as test; |
| 193 import 'dart:async' as test; | 193 import 'dart:async' as test; |
| 194 main() { | 194 main() { |
| 195 test.Future f; | 195 test.Future f; |
| 196 } | 196 } |
| 197 '''); | 197 '''); |
| 198 // configure refactoring | 198 // configure refactoring |
| 199 _createRefactoring("import 'dart:async"); | 199 _createRefactoring("import 'dart:async"); |
| 200 expect(refactoring.refactoringName, 'Rename Import Prefix'); | 200 expect(refactoring.refactoringName, 'Rename Import Prefix'); |
| 201 expect(refactoring.oldName, 'test'); | 201 expect(refactoring.oldName, 'test'); |
| 202 refactoring.newName = ''; | 202 refactoring.newName = ''; |
| 203 // validate change | 203 // validate change |
| 204 return assertSuccessfulRefactoring(''' | 204 return assertSuccessfulRefactoring(''' |
| 205 import 'dart:math' as test; | 205 import 'dart:math' as test; |
| 206 import 'dart:async'; | 206 import 'dart:async'; |
| 207 main() { | 207 main() { |
| 208 Future f; | 208 Future f; |
| 209 } | 209 } |
| 210 '''); | 210 '''); |
| 211 } | 211 } |
| 212 | 212 |
| 213 test_oldName_empty() { | 213 test_oldName_empty() async { |
| 214 indexTestUnit(''' | 214 await indexTestUnit(''' |
| 215 import 'dart:math'; | 215 import 'dart:math'; |
| 216 import 'dart:async'; | 216 import 'dart:async'; |
| 217 main() { | 217 main() { |
| 218 Future f; | 218 Future f; |
| 219 } | 219 } |
| 220 '''); | 220 '''); |
| 221 // configure refactoring | 221 // configure refactoring |
| 222 _createRefactoring("import 'dart:async"); | 222 _createRefactoring("import 'dart:async"); |
| 223 expect(refactoring.refactoringName, 'Rename Import Prefix'); | 223 expect(refactoring.refactoringName, 'Rename Import Prefix'); |
| 224 expect(refactoring.oldName, ''); | 224 expect(refactoring.oldName, ''); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void _createRefactoring(String search) { | 227 void _createRefactoring(String search) { |
| 228 ImportDirective directive = | 228 ImportDirective directive = |
| 229 findNodeAtString(search, (node) => node is ImportDirective); | 229 findNodeAtString(search, (node) => node is ImportDirective); |
| 230 createRenameRefactoringForElement(directive.element); | 230 createRenameRefactoringForElement(directive.element); |
| 231 } | 231 } |
| 232 } | 232 } |
| OLD | NEW |