| 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.edit.refactoring; | 5 library test.edit.refactoring; | 
| 6 | 6 | 
| 7 import 'dart:async'; | 7 import 'dart:async'; | 
| 8 | 8 | 
| 9 import 'package:analysis_server/src/edit/edit_domain.dart'; | 9 import 'package:analysis_server/src/edit/edit_domain.dart'; | 
| 10 import 'package:analysis_server/src/protocol.dart'; | 10 import 'package:analysis_server/src/protocol.dart'; | 
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 293       return sendStringSuffixRequest('1 + 2', '); // marker', 'res', false); | 293       return sendStringSuffixRequest('1 + 2', '); // marker', 'res', false); | 
| 294     }, ''' | 294     }, ''' | 
| 295 main() { | 295 main() { | 
| 296   print(1 + 2); | 296   print(1 + 2); | 
| 297   var res = 1 + 2; | 297   var res = 1 + 2; | 
| 298   print(res); // marker | 298   print(res); // marker | 
| 299 } | 299 } | 
| 300 '''); | 300 '''); | 
| 301   } | 301   } | 
| 302 | 302 | 
|  | 303   test_names() { | 
|  | 304     addTestFile(''' | 
|  | 305 class TreeItem {} | 
|  | 306 TreeItem getSelectedItem() => null; | 
|  | 307 main() { | 
|  | 308   var a = getSelectedItem(); | 
|  | 309 } | 
|  | 310 '''); | 
|  | 311     return getRefactoringResult(() { | 
|  | 312       return sendStringSuffixRequest('getSelectedItem()', ';', null, true); | 
|  | 313     }).then((result) { | 
|  | 314       ExtractLocalVariableFeedback feedback = result.feedback; | 
|  | 315       expect( | 
|  | 316           feedback.names, | 
|  | 317           unorderedEquals(['treeItem', 'item', 'selectedItem'])); | 
|  | 318       expect(result.change, isNull); | 
|  | 319     }); | 
|  | 320   } | 
|  | 321 | 
| 303   test_nameWarning() { | 322   test_nameWarning() { | 
| 304     addTestFile(''' | 323     addTestFile(''' | 
| 305 main() { | 324 main() { | 
| 306   print(1 + 2); | 325   print(1 + 2); | 
| 307 } | 326 } | 
| 308 '''); | 327 '''); | 
| 309     return getRefactoringResult(() { | 328     return getRefactoringResult(() { | 
| 310       return sendStringRequest('1 + 2', 'Name', true); | 329       return sendStringRequest('1 + 2', 'Name', true); | 
| 311     }).then((result) { | 330     }).then((result) { | 
| 312       assertResultProblemsWarning( | 331       assertResultProblemsWarning( | 
| 313           result.optionsProblems, | 332           result.optionsProblems, | 
| 314           'Variable name should start with a lowercase letter.'); | 333           'Variable name should start with a lowercase letter.'); | 
| 315       // ...but there is still a change | 334       // ...but there is still a change | 
| 316       assertTestRefactoringResult(result, ''' | 335       assertTestRefactoringResult(result, ''' | 
| 317 main() { | 336 main() { | 
| 318   var Name = 1 + 2; | 337   var Name = 1 + 2; | 
| 319   print(Name); | 338   print(Name); | 
| 320 } | 339 } | 
| 321 '''); | 340 '''); | 
| 322     }); | 341     }); | 
| 323   } | 342   } | 
| 324 | 343 | 
| 325   test_names() { |  | 
| 326     addTestFile(''' |  | 
| 327 class TreeItem {} |  | 
| 328 TreeItem getSelectedItem() => null; |  | 
| 329 main() { |  | 
| 330   var a = getSelectedItem(); |  | 
| 331 } |  | 
| 332 '''); |  | 
| 333     return getRefactoringResult(() { |  | 
| 334       return sendStringSuffixRequest('getSelectedItem()', ';', null, true); |  | 
| 335     }).then((result) { |  | 
| 336       ExtractLocalVariableFeedback feedback = result.feedback; |  | 
| 337       expect( |  | 
| 338           feedback.names, |  | 
| 339           unorderedEquals(['treeItem', 'item', 'selectedItem'])); |  | 
| 340       expect(result.change, isNull); |  | 
| 341     }); |  | 
| 342   } |  | 
| 343 |  | 
| 344   test_offsetsLengths() { | 344   test_offsetsLengths() { | 
| 345     addTestFile(''' | 345     addTestFile(''' | 
| 346 main() { | 346 main() { | 
| 347   print(1 + 2); | 347   print(1 + 2); | 
| 348   print(1 +  2); | 348   print(1 +  2); | 
| 349 } | 349 } | 
| 350 '''); | 350 '''); | 
| 351     return getRefactoringResult(() { | 351     return getRefactoringResult(() { | 
| 352       return sendStringRequest('1 + 2', 'res', true); | 352       return sendStringRequest('1 + 2', 'res', true); | 
| 353     }).then((result) { | 353     }).then((result) { | 
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 726       List<RefactoringKind> kinds = | 726       List<RefactoringKind> kinds = | 
| 727           getRefactoringsAtString('// not an element'); | 727           getRefactoringsAtString('// not an element'); | 
| 728       expect(kinds, isNot(contains(RefactoringKind.RENAME))); | 728       expect(kinds, isNot(contains(RefactoringKind.RENAME))); | 
| 729     }); | 729     }); | 
| 730   } | 730   } | 
| 731 } | 731 } | 
| 732 | 732 | 
| 733 | 733 | 
| 734 @ReflectiveTestCase() | 734 @ReflectiveTestCase() | 
| 735 class InlineLocalTest extends _AbstractGetRefactoring_Test { | 735 class InlineLocalTest extends _AbstractGetRefactoring_Test { | 
| 736   test_OK() { |  | 
| 737     addTestFile(''' |  | 
| 738 main() { |  | 
| 739   int test = 42; |  | 
| 740   int a = test + 2; |  | 
| 741   print(test); |  | 
| 742 } |  | 
| 743 '''); |  | 
| 744     return assertSuccessfulRefactoring(() { |  | 
| 745       return _sendInlineRequest('test + 2'); |  | 
| 746     }, ''' |  | 
| 747 main() { |  | 
| 748   int a = 42 + 2; |  | 
| 749   print(42); |  | 
| 750 } |  | 
| 751 '''); |  | 
| 752   } |  | 
| 753 |  | 
| 754   test_feedback() { | 736   test_feedback() { | 
| 755     addTestFile(''' | 737     addTestFile(''' | 
| 756 main() { | 738 main() { | 
| 757   int test = 42; | 739   int test = 42; | 
| 758   print(test); | 740   print(test); | 
| 759   print(test); | 741   print(test); | 
| 760 } | 742 } | 
| 761 '''); | 743 '''); | 
| 762     return getRefactoringResult(() { | 744     return getRefactoringResult(() { | 
| 763       return _sendInlineRequest('test ='); | 745       return _sendInlineRequest('test ='); | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 774       return _sendInlineRequest('main() {}'); | 756       return _sendInlineRequest('main() {}'); | 
| 775     }).then((result) { | 757     }).then((result) { | 
| 776       assertResultProblemsFatal( | 758       assertResultProblemsFatal( | 
| 777           result.initialProblems, | 759           result.initialProblems, | 
| 778           'Local variable declaration or reference must be selected to activate 
      this refactoring.'); | 760           'Local variable declaration or reference must be selected to activate 
      this refactoring.'); | 
| 779       // ...there is no any change | 761       // ...there is no any change | 
| 780       expect(result.change, isNull); | 762       expect(result.change, isNull); | 
| 781     }); | 763     }); | 
| 782   } | 764   } | 
| 783 | 765 | 
|  | 766   test_OK() { | 
|  | 767     addTestFile(''' | 
|  | 768 main() { | 
|  | 769   int test = 42; | 
|  | 770   int a = test + 2; | 
|  | 771   print(test); | 
|  | 772 } | 
|  | 773 '''); | 
|  | 774     return assertSuccessfulRefactoring(() { | 
|  | 775       return _sendInlineRequest('test + 2'); | 
|  | 776     }, ''' | 
|  | 777 main() { | 
|  | 778   int a = 42 + 2; | 
|  | 779   print(42); | 
|  | 780 } | 
|  | 781 '''); | 
|  | 782   } | 
|  | 783 | 
| 784   Future<Response> _sendInlineRequest(String search) { | 784   Future<Response> _sendInlineRequest(String search) { | 
| 785     Request request = new EditGetRefactoringParams( | 785     Request request = new EditGetRefactoringParams( | 
| 786         RefactoringKind.INLINE_LOCAL_VARIABLE, | 786         RefactoringKind.INLINE_LOCAL_VARIABLE, | 
| 787         testFile, | 787         testFile, | 
| 788         findOffset(search), | 788         findOffset(search), | 
| 789         0, | 789         0, | 
| 790         false).toRequest('0'); | 790         false).toRequest('0'); | 
| 791     return serverChannel.sendRequest(request); | 791     return serverChannel.sendRequest(request); | 
| 792   } | 792   } | 
| 793 } | 793 } | 
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 962         testFile, | 962         testFile, | 
| 963         findOffset(search), | 963         findOffset(search), | 
| 964         0, | 964         0, | 
| 965         validateOnly, | 965         validateOnly, | 
| 966         options: options).toRequest('0'); | 966         options: options).toRequest('0'); | 
| 967     return serverChannel.sendRequest(request); | 967     return serverChannel.sendRequest(request); | 
| 968   } | 968   } | 
| 969 | 969 | 
| 970   test_class() { | 970   test_class() { | 
| 971     addTestFile(''' | 971     addTestFile(''' | 
|  | 972 class Test { | 
|  | 973   Test() {} | 
|  | 974   Test.named() {} | 
|  | 975 } | 
|  | 976 main() { | 
|  | 977   Test v; | 
|  | 978   new Test(); | 
|  | 979   new Test.named(); | 
|  | 980 } | 
|  | 981 '''); | 
|  | 982     return assertSuccessfulRefactoring(() { | 
|  | 983       return sendRenameRequest('Test {', 'NewName'); | 
|  | 984     }, ''' | 
|  | 985 class NewName { | 
|  | 986   NewName() {} | 
|  | 987   NewName.named() {} | 
|  | 988 } | 
|  | 989 main() { | 
|  | 990   NewName v; | 
|  | 991   new NewName(); | 
|  | 992   new NewName.named(); | 
|  | 993 } | 
|  | 994 '''); | 
|  | 995   } | 
|  | 996 | 
|  | 997   test_class_options_fatalError() { | 
|  | 998     addTestFile(''' | 
| 972 class Test {} | 999 class Test {} | 
| 973 main() { | 1000 main() { | 
| 974   Test v; | 1001   Test v; | 
| 975 } | 1002 } | 
| 976 '''); | 1003 '''); | 
| 977     return assertSuccessfulRefactoring(() { | 1004     return getRefactoringResult(() { | 
| 978       return sendRenameRequest('Test {}', 'NewName'); | 1005       return sendRenameRequest('Test {}', ''); | 
| 979     }, ''' | 1006     }).then((result) { | 
|  | 1007       assertResultProblemsFatal( | 
|  | 1008           result.optionsProblems, | 
|  | 1009           'Class name must not be empty.'); | 
|  | 1010       // ...there is no any change | 
|  | 1011       expect(result.change, isNull); | 
|  | 1012     }); | 
|  | 1013   } | 
|  | 1014 | 
|  | 1015   test_class_validateOnly() { | 
|  | 1016     addTestFile(''' | 
|  | 1017 class Test {} | 
|  | 1018 main() { | 
|  | 1019   Test v; | 
|  | 1020 } | 
|  | 1021 '''); | 
|  | 1022     return getRefactoringResult(() { | 
|  | 1023       return sendRenameRequest('Test {}', 'NewName', true); | 
|  | 1024     }).then((result) { | 
|  | 1025       RenameFeedback feedback = result.feedback; | 
|  | 1026       assertResultProblemsOK(result); | 
|  | 1027       expect(feedback.elementKindName, 'class'); | 
|  | 1028       expect(feedback.oldName, 'Test'); | 
|  | 1029       expect(result.change, isNull); | 
|  | 1030     }); | 
|  | 1031   } | 
|  | 1032 | 
|  | 1033   test_class_warning() { | 
|  | 1034     addTestFile(''' | 
|  | 1035 class Test {} | 
|  | 1036 main() { | 
|  | 1037   Test v; | 
|  | 1038 } | 
|  | 1039 '''); | 
|  | 1040     return getRefactoringResult(() { | 
|  | 1041       return sendRenameRequest('Test {}', 'newName'); | 
|  | 1042     }).then((result) { | 
|  | 1043       assertResultProblemsWarning( | 
|  | 1044           result.optionsProblems, | 
|  | 1045           'Class name should start with an uppercase letter.'); | 
|  | 1046       // ...but there is still a change | 
|  | 1047       assertTestRefactoringResult(result, ''' | 
|  | 1048 class newName {} | 
|  | 1049 main() { | 
|  | 1050   newName v; | 
|  | 1051 } | 
|  | 1052 '''); | 
|  | 1053     }).then((_) { | 
|  | 1054       // "NewName" is a perfectly valid name | 
|  | 1055       return getRefactoringResult(() { | 
|  | 1056         return sendRenameRequest('Test {}', 'NewName'); | 
|  | 1057       }).then((result) { | 
|  | 1058         assertResultProblemsOK(result); | 
|  | 1059         // ...and there is a new change | 
|  | 1060         assertTestRefactoringResult(result, ''' | 
| 980 class NewName {} | 1061 class NewName {} | 
| 981 main() { | 1062 main() { | 
| 982   NewName v; | 1063   NewName v; | 
| 983 } | 1064 } | 
| 984 '''); | 1065 '''); | 
|  | 1066       }); | 
|  | 1067     }); | 
| 985   } | 1068   } | 
| 986 | 1069 | 
| 987   test_classMember_field() { | 1070   test_classMember_field() { | 
| 988     addTestFile(''' | 1071     addTestFile(''' | 
| 989 class A { | 1072 class A { | 
| 990   var test = 0; | 1073   var test = 0; | 
| 991   main() { | 1074   main() { | 
| 992     print(test); | 1075     print(test); | 
| 993   } | 1076   } | 
| 994 } | 1077 } | 
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1094     }, ''' | 1177     }, ''' | 
| 1095 class A { | 1178 class A { | 
| 1096   set newName(x) {} | 1179   set newName(x) {} | 
| 1097   main() { | 1180   main() { | 
| 1098     newName = 0; | 1181     newName = 0; | 
| 1099   } | 1182   } | 
| 1100 } | 1183 } | 
| 1101 '''); | 1184 '''); | 
| 1102   } | 1185   } | 
| 1103 | 1186 | 
| 1104   test_class_fromInstanceCreation() { |  | 
| 1105     addTestFile(''' |  | 
| 1106 class Test { |  | 
| 1107   Test() {} |  | 
| 1108   Test.named() {} |  | 
| 1109 } |  | 
| 1110 main() { |  | 
| 1111   new Test(); |  | 
| 1112   new Test.named(); |  | 
| 1113 } |  | 
| 1114 '''); |  | 
| 1115     return assertSuccessfulRefactoring(() { |  | 
| 1116       return sendRenameRequest('Test();', 'NewName'); |  | 
| 1117     }, ''' |  | 
| 1118 class NewName { |  | 
| 1119   NewName() {} |  | 
| 1120   NewName.named() {} |  | 
| 1121 } |  | 
| 1122 main() { |  | 
| 1123   new NewName(); |  | 
| 1124   new NewName.named(); |  | 
| 1125 } |  | 
| 1126 '''); |  | 
| 1127   } |  | 
| 1128 |  | 
| 1129   test_class_options_fatalError() { |  | 
| 1130     addTestFile(''' |  | 
| 1131 class Test {} |  | 
| 1132 main() { |  | 
| 1133   Test v; |  | 
| 1134 } |  | 
| 1135 '''); |  | 
| 1136     return getRefactoringResult(() { |  | 
| 1137       return sendRenameRequest('Test {}', ''); |  | 
| 1138     }).then((result) { |  | 
| 1139       assertResultProblemsFatal( |  | 
| 1140           result.optionsProblems, |  | 
| 1141           'Class name must not be empty.'); |  | 
| 1142       // ...there is no any change |  | 
| 1143       expect(result.change, isNull); |  | 
| 1144     }); |  | 
| 1145   } |  | 
| 1146 |  | 
| 1147   test_class_validateOnly() { |  | 
| 1148     addTestFile(''' |  | 
| 1149 class Test {} |  | 
| 1150 main() { |  | 
| 1151   Test v; |  | 
| 1152 } |  | 
| 1153 '''); |  | 
| 1154     return getRefactoringResult(() { |  | 
| 1155       return sendRenameRequest('Test {}', 'NewName', true); |  | 
| 1156     }).then((result) { |  | 
| 1157       RenameFeedback feedback = result.feedback; |  | 
| 1158       assertResultProblemsOK(result); |  | 
| 1159       expect(feedback.elementKindName, 'class'); |  | 
| 1160       expect(feedback.oldName, 'Test'); |  | 
| 1161       expect(result.change, isNull); |  | 
| 1162     }); |  | 
| 1163   } |  | 
| 1164 |  | 
| 1165   test_class_warning() { |  | 
| 1166     addTestFile(''' |  | 
| 1167 class Test {} |  | 
| 1168 main() { |  | 
| 1169   Test v; |  | 
| 1170 } |  | 
| 1171 '''); |  | 
| 1172     return getRefactoringResult(() { |  | 
| 1173       return sendRenameRequest('Test {}', 'newName'); |  | 
| 1174     }).then((result) { |  | 
| 1175       assertResultProblemsWarning( |  | 
| 1176           result.optionsProblems, |  | 
| 1177           'Class name should start with an uppercase letter.'); |  | 
| 1178       // ...but there is still a change |  | 
| 1179       assertTestRefactoringResult(result, ''' |  | 
| 1180 class newName {} |  | 
| 1181 main() { |  | 
| 1182   newName v; |  | 
| 1183 } |  | 
| 1184 '''); |  | 
| 1185     }).then((_) { |  | 
| 1186       // "NewName" is a perfectly valid name |  | 
| 1187       return getRefactoringResult(() { |  | 
| 1188         return sendRenameRequest('Test {}', 'NewName'); |  | 
| 1189       }).then((result) { |  | 
| 1190         assertResultProblemsOK(result); |  | 
| 1191         // ...and there is a new change |  | 
| 1192         assertTestRefactoringResult(result, ''' |  | 
| 1193 class NewName {} |  | 
| 1194 main() { |  | 
| 1195   NewName v; |  | 
| 1196 } |  | 
| 1197 '''); |  | 
| 1198       }); |  | 
| 1199     }); |  | 
| 1200   } |  | 
| 1201 |  | 
| 1202   test_constructor_fromInstanceCreation() { | 1187   test_constructor_fromInstanceCreation() { | 
| 1203     addTestFile(''' | 1188     addTestFile(''' | 
| 1204 class A { | 1189 class A { | 
| 1205   A.test() {} | 1190   A.test() {} | 
| 1206 } | 1191 } | 
| 1207 main() { | 1192 main() { | 
| 1208   new A.test(); | 1193   new A.test(); | 
| 1209 } | 1194 } | 
| 1210 '''); | 1195 '''); | 
| 1211     return assertSuccessfulRefactoring(() { | 1196     return assertSuccessfulRefactoring(() { | 
| 1212       return sendRenameRequest('test();', 'newName'); | 1197       return sendRenameRequest('test();', 'newName'); | 
| 1213     }, ''' | 1198     }, ''' | 
| 1214 class A { | 1199 class A { | 
| 1215   A.newName() {} | 1200   A.newName() {} | 
|  | 1201 } | 
|  | 1202 main() { | 
|  | 1203   new A.newName(); | 
|  | 1204 } | 
|  | 1205 '''); | 
|  | 1206   } | 
|  | 1207 | 
|  | 1208   test_constructor_fromInstanceCreation_default_onClassName() { | 
|  | 1209     addTestFile(''' | 
|  | 1210 class A { | 
|  | 1211   A() {} | 
|  | 1212 } | 
|  | 1213 main() { | 
|  | 1214   new A(); | 
|  | 1215 } | 
|  | 1216 '''); | 
|  | 1217     return assertSuccessfulRefactoring(() { | 
|  | 1218       return sendRenameRequest('A();', 'newName'); | 
|  | 1219     }, ''' | 
|  | 1220 class A { | 
|  | 1221   A.newName() {} | 
|  | 1222 } | 
|  | 1223 main() { | 
|  | 1224   new A.newName(); | 
|  | 1225 } | 
|  | 1226 '''); | 
|  | 1227   } | 
|  | 1228 | 
|  | 1229   test_constructor_fromInstanceCreation_default_onNew() { | 
|  | 1230     addTestFile(''' | 
|  | 1231 class A { | 
|  | 1232   A() {} | 
|  | 1233 } | 
|  | 1234 main() { | 
|  | 1235   new A(); | 
|  | 1236 } | 
|  | 1237 '''); | 
|  | 1238     return assertSuccessfulRefactoring(() { | 
|  | 1239       return sendRenameRequest('new A();', 'newName'); | 
|  | 1240     }, ''' | 
|  | 1241 class A { | 
|  | 1242   A.newName() {} | 
| 1216 } | 1243 } | 
| 1217 main() { | 1244 main() { | 
| 1218   new A.newName(); | 1245   new A.newName(); | 
| 1219 } | 1246 } | 
| 1220 '''); | 1247 '''); | 
| 1221   } | 1248   } | 
| 1222 | 1249 | 
| 1223   test_feedback() { | 1250   test_feedback() { | 
| 1224     addTestFile(''' | 1251     addTestFile(''' | 
| 1225 class Test {} | 1252 class Test {} | 
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1542   } | 1569   } | 
| 1543 | 1570 | 
| 1544   @override | 1571   @override | 
| 1545   void setUp() { | 1572   void setUp() { | 
| 1546     super.setUp(); | 1573     super.setUp(); | 
| 1547     server.handlers = [new EditDomainHandler(server),]; | 1574     server.handlers = [new EditDomainHandler(server),]; | 
| 1548     createProject(); | 1575     createProject(); | 
| 1549     handler = new EditDomainHandler(server); | 1576     handler = new EditDomainHandler(server); | 
| 1550   } | 1577   } | 
| 1551 } | 1578 } | 
| OLD | NEW | 
|---|