| 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.extract_local; | 5 library test.services.refactoring.extract_local; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol2.dart'; |
| 9 import 'package:analysis_server/src/services/correction/change.dart'; | 10 import 'package:analysis_server/src/services/correction/change.dart'; |
| 10 import 'package:analysis_server/src/services/correction/status.dart'; | |
| 11 import 'package:analysis_server/src/services/refactoring/extract_local.dart'; | 11 import 'package:analysis_server/src/services/refactoring/extract_local.dart'; |
| 12 import 'package:analysis_testing/reflective_tests.dart'; | 12 import 'package:analysis_testing/reflective_tests.dart'; |
| 13 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 14 | 14 |
| 15 import 'abstract_refactoring.dart'; | 15 import 'abstract_refactoring.dart'; |
| 16 | 16 |
| 17 | 17 |
| 18 main() { | 18 main() { |
| 19 groupSep = ' | '; | 19 groupSep = ' | '; |
| 20 runReflectiveTests(ExtractLocalTest); | 20 runReflectiveTests(ExtractLocalTest); |
| 21 } | 21 } |
| 22 | 22 |
| 23 | 23 |
| 24 @ReflectiveTestCase() | 24 @ReflectiveTestCase() |
| 25 class ExtractLocalTest extends RefactoringTest { | 25 class ExtractLocalTest extends RefactoringTest { |
| 26 ExtractLocalRefactoringImpl refactoring; | 26 ExtractLocalRefactoringImpl refactoring; |
| 27 | 27 |
| 28 test_checkFinalConditions_sameVariable_after() { | 28 test_checkFinalConditions_sameVariable_after() { |
| 29 indexTestUnit(''' | 29 indexTestUnit(''' |
| 30 main() { | 30 main() { |
| 31 int a = 1 + 2; | 31 int a = 1 + 2; |
| 32 var res; | 32 var res; |
| 33 } | 33 } |
| 34 '''); | 34 '''); |
| 35 _createRefactoringForString('1 + 2'); | 35 _createRefactoringForString('1 + 2'); |
| 36 // conflicting name | 36 // conflicting name |
| 37 return refactoring.checkAllConditions().then((status) { | 37 return refactoring.checkAllConditions().then((status) { |
| 38 assertRefactoringStatus( | 38 assertRefactoringStatus( |
| 39 status, | 39 status, |
| 40 RefactoringStatusSeverity.WARNING, | 40 RefactoringProblemSeverity.WARNING, |
| 41 expectedMessage: | 41 expectedMessage: |
| 42 "A variable with name 'res' is already defined in the visible scop
e."); | 42 "A variable with name 'res' is already defined in the visible scop
e."); |
| 43 }); | 43 }); |
| 44 } | 44 } |
| 45 | 45 |
| 46 test_checkFinalConditions_sameVariable_before() { | 46 test_checkFinalConditions_sameVariable_before() { |
| 47 indexTestUnit(''' | 47 indexTestUnit(''' |
| 48 main() { | 48 main() { |
| 49 var res; | 49 var res; |
| 50 int a = 1 + 2; | 50 int a = 1 + 2; |
| 51 } | 51 } |
| 52 '''); | 52 '''); |
| 53 _createRefactoringForString('1 + 2'); | 53 _createRefactoringForString('1 + 2'); |
| 54 // conflicting name | 54 // conflicting name |
| 55 return refactoring.checkAllConditions().then((status) { | 55 return refactoring.checkAllConditions().then((status) { |
| 56 assertRefactoringStatus( | 56 assertRefactoringStatus( |
| 57 status, | 57 status, |
| 58 RefactoringStatusSeverity.WARNING, | 58 RefactoringProblemSeverity.WARNING, |
| 59 expectedMessage: | 59 expectedMessage: |
| 60 "A variable with name 'res' is already defined in the visible scop
e."); | 60 "A variable with name 'res' is already defined in the visible scop
e."); |
| 61 }); | 61 }); |
| 62 } | 62 } |
| 63 | 63 |
| 64 test_checkInitialConditions_assignmentLeftHandSize() { | 64 test_checkInitialConditions_assignmentLeftHandSize() { |
| 65 indexTestUnit(''' | 65 indexTestUnit(''' |
| 66 main() { | 66 main() { |
| 67 var v = 0; | 67 var v = 0; |
| 68 v = 1; | 68 v = 1; |
| 69 } | 69 } |
| 70 '''); | 70 '''); |
| 71 _createRefactoringWithSuffix('v', ' = 1;'); | 71 _createRefactoringWithSuffix('v', ' = 1;'); |
| 72 // check conditions | 72 // check conditions |
| 73 return refactoring.checkInitialConditions().then((status) { | 73 return refactoring.checkInitialConditions().then((status) { |
| 74 assertRefactoringStatus( | 74 assertRefactoringStatus( |
| 75 status, | 75 status, |
| 76 RefactoringStatusSeverity.FATAL, | 76 RefactoringProblemSeverity.FATAL, |
| 77 expectedMessage: 'Cannot extract the left-hand side of an assignment.'
); | 77 expectedMessage: 'Cannot extract the left-hand side of an assignment.'
); |
| 78 }); | 78 }); |
| 79 } | 79 } |
| 80 | 80 |
| 81 test_checkInitialConditions_methodName_reference() { | 81 test_checkInitialConditions_methodName_reference() { |
| 82 indexTestUnit(''' | 82 indexTestUnit(''' |
| 83 main() { | 83 main() { |
| 84 main(); | 84 main(); |
| 85 } | 85 } |
| 86 '''); | 86 '''); |
| 87 _createRefactoringWithSuffix('main', '();'); | 87 _createRefactoringWithSuffix('main', '();'); |
| 88 // check conditions | 88 // check conditions |
| 89 return refactoring.checkInitialConditions().then((status) { | 89 return refactoring.checkInitialConditions().then((status) { |
| 90 assertRefactoringStatus( | 90 assertRefactoringStatus( |
| 91 status, | 91 status, |
| 92 RefactoringStatusSeverity.FATAL, | 92 RefactoringProblemSeverity.FATAL, |
| 93 expectedMessage: 'Cannot extract a single method name.'); | 93 expectedMessage: 'Cannot extract a single method name.'); |
| 94 }); | 94 }); |
| 95 } | 95 } |
| 96 | 96 |
| 97 test_checkInitialConditions_nameOfProperty_prefixedIdentifier() { | 97 test_checkInitialConditions_nameOfProperty_prefixedIdentifier() { |
| 98 indexTestUnit(''' | 98 indexTestUnit(''' |
| 99 main(p) { | 99 main(p) { |
| 100 p.value; // marker | 100 p.value; // marker |
| 101 } | 101 } |
| 102 '''); | 102 '''); |
| 103 _createRefactoringWithSuffix('value', '; // marker'); | 103 _createRefactoringWithSuffix('value', '; // marker'); |
| 104 // check conditions | 104 // check conditions |
| 105 return refactoring.checkInitialConditions().then((status) { | 105 return refactoring.checkInitialConditions().then((status) { |
| 106 assertRefactoringStatus( | 106 assertRefactoringStatus( |
| 107 status, | 107 status, |
| 108 RefactoringStatusSeverity.FATAL, | 108 RefactoringProblemSeverity.FATAL, |
| 109 expectedMessage: 'Cannot extract name part of a property access.'); | 109 expectedMessage: 'Cannot extract name part of a property access.'); |
| 110 }); | 110 }); |
| 111 } | 111 } |
| 112 | 112 |
| 113 test_checkInitialConditions_nameOfProperty_propertyAccess() { | 113 test_checkInitialConditions_nameOfProperty_propertyAccess() { |
| 114 indexTestUnit(''' | 114 indexTestUnit(''' |
| 115 main() { | 115 main() { |
| 116 foo().length; // marker | 116 foo().length; // marker |
| 117 } | 117 } |
| 118 String foo() => ''; | 118 String foo() => ''; |
| 119 '''); | 119 '''); |
| 120 _createRefactoringWithSuffix('length', '; // marker'); | 120 _createRefactoringWithSuffix('length', '; // marker'); |
| 121 // check conditions | 121 // check conditions |
| 122 return refactoring.checkInitialConditions().then((status) { | 122 return refactoring.checkInitialConditions().then((status) { |
| 123 assertRefactoringStatus( | 123 assertRefactoringStatus( |
| 124 status, | 124 status, |
| 125 RefactoringStatusSeverity.FATAL, | 125 RefactoringProblemSeverity.FATAL, |
| 126 expectedMessage: 'Cannot extract name part of a property access.'); | 126 expectedMessage: 'Cannot extract name part of a property access.'); |
| 127 }); | 127 }); |
| 128 } | 128 } |
| 129 | 129 |
| 130 test_checkInitialConditions_namePartOfDeclaration_variable() { | 130 test_checkInitialConditions_namePartOfDeclaration_variable() { |
| 131 indexTestUnit(''' | 131 indexTestUnit(''' |
| 132 main() { | 132 main() { |
| 133 int vvv = 0; | 133 int vvv = 0; |
| 134 } | 134 } |
| 135 '''); | 135 '''); |
| 136 _createRefactoringWithSuffix('vvv', ' = 0;'); | 136 _createRefactoringWithSuffix('vvv', ' = 0;'); |
| 137 // check conditions | 137 // check conditions |
| 138 return refactoring.checkInitialConditions().then((status) { | 138 return refactoring.checkInitialConditions().then((status) { |
| 139 assertRefactoringStatus( | 139 assertRefactoringStatus( |
| 140 status, | 140 status, |
| 141 RefactoringStatusSeverity.FATAL, | 141 RefactoringProblemSeverity.FATAL, |
| 142 expectedMessage: 'Cannot extract the name part of a declaration.'); | 142 expectedMessage: 'Cannot extract the name part of a declaration.'); |
| 143 }); | 143 }); |
| 144 } | 144 } |
| 145 | 145 |
| 146 test_checkInitialConditions_notPartOfFunction() { | 146 test_checkInitialConditions_notPartOfFunction() { |
| 147 indexTestUnit(''' | 147 indexTestUnit(''' |
| 148 int a = 1 + 2; | 148 int a = 1 + 2; |
| 149 '''); | 149 '''); |
| 150 _createRefactoringForString('1 + 2'); | 150 _createRefactoringForString('1 + 2'); |
| 151 // check conditions | 151 // check conditions |
| 152 return refactoring.checkInitialConditions().then((status) { | 152 return refactoring.checkInitialConditions().then((status) { |
| 153 assertRefactoringStatus( | 153 assertRefactoringStatus( |
| 154 status, | 154 status, |
| 155 RefactoringStatusSeverity.FATAL, | 155 RefactoringProblemSeverity.FATAL, |
| 156 expectedMessage: | 156 expectedMessage: |
| 157 'Expression inside of function must be selected to activate this r
efactoring.'); | 157 'Expression inside of function must be selected to activate this r
efactoring.'); |
| 158 }); | 158 }); |
| 159 } | 159 } |
| 160 | 160 |
| 161 test_checkInitialConditions_stringSelection_leadingQuote() { | 161 test_checkInitialConditions_stringSelection_leadingQuote() { |
| 162 indexTestUnit(''' | 162 indexTestUnit(''' |
| 163 main() { | 163 main() { |
| 164 var vvv = 'abc'; | 164 var vvv = 'abc'; |
| 165 } | 165 } |
| 166 '''); | 166 '''); |
| 167 _createRefactoringForString("'a"); | 167 _createRefactoringForString("'a"); |
| 168 // check conditions | 168 // check conditions |
| 169 return refactoring.checkInitialConditions().then((status) { | 169 return refactoring.checkInitialConditions().then((status) { |
| 170 assertRefactoringStatus( | 170 assertRefactoringStatus( |
| 171 status, | 171 status, |
| 172 RefactoringStatusSeverity.FATAL, | 172 RefactoringProblemSeverity.FATAL, |
| 173 expectedMessage: | 173 expectedMessage: |
| 174 'Cannot extract only leading or trailing quote of string literal.'
); | 174 'Cannot extract only leading or trailing quote of string literal.'
); |
| 175 }); | 175 }); |
| 176 } | 176 } |
| 177 | 177 |
| 178 test_checkInitialConditions_stringSelection_trailingQuote() { | 178 test_checkInitialConditions_stringSelection_trailingQuote() { |
| 179 indexTestUnit(''' | 179 indexTestUnit(''' |
| 180 main() { | 180 main() { |
| 181 var vvv = 'abc'; | 181 var vvv = 'abc'; |
| 182 } | 182 } |
| 183 '''); | 183 '''); |
| 184 _createRefactoringForString("c'"); | 184 _createRefactoringForString("c'"); |
| 185 // check conditions | 185 // check conditions |
| 186 return refactoring.checkInitialConditions().then((status) { | 186 return refactoring.checkInitialConditions().then((status) { |
| 187 assertRefactoringStatus( | 187 assertRefactoringStatus( |
| 188 status, | 188 status, |
| 189 RefactoringStatusSeverity.FATAL, | 189 RefactoringProblemSeverity.FATAL, |
| 190 expectedMessage: | 190 expectedMessage: |
| 191 'Cannot extract only leading or trailing quote of string literal.'
); | 191 'Cannot extract only leading or trailing quote of string literal.'
); |
| 192 }); | 192 }); |
| 193 } | 193 } |
| 194 | 194 |
| 195 test_checkLocalName() { | 195 test_checkLocalName() { |
| 196 indexTestUnit(''' | 196 indexTestUnit(''' |
| 197 main() { | 197 main() { |
| 198 int a = 1 + 2; | 198 int a = 1 + 2; |
| 199 } | 199 } |
| 200 '''); | 200 '''); |
| 201 _createRefactoringForString('1 + 2'); | 201 _createRefactoringForString('1 + 2'); |
| 202 expect(refactoring.refactoringName, 'Extract Local Variable'); | 202 expect(refactoring.refactoringName, 'Extract Local Variable'); |
| 203 // null | 203 // null |
| 204 refactoring.name = null; | 204 refactoring.name = null; |
| 205 assertRefactoringStatus( | 205 assertRefactoringStatus( |
| 206 refactoring.checkName(), | 206 refactoring.checkName(), |
| 207 RefactoringStatusSeverity.ERROR, | 207 RefactoringProblemSeverity.ERROR, |
| 208 expectedMessage: "Variable name must not be null."); | 208 expectedMessage: "Variable name must not be null."); |
| 209 // empty | 209 // empty |
| 210 refactoring.name = ''; | 210 refactoring.name = ''; |
| 211 assertRefactoringStatus( | 211 assertRefactoringStatus( |
| 212 refactoring.checkName(), | 212 refactoring.checkName(), |
| 213 RefactoringStatusSeverity.ERROR, | 213 RefactoringProblemSeverity.ERROR, |
| 214 expectedMessage: "Variable name must not be empty."); | 214 expectedMessage: "Variable name must not be empty."); |
| 215 // OK | 215 // OK |
| 216 refactoring.name = 'res'; | 216 refactoring.name = 'res'; |
| 217 assertRefactoringStatusOK(refactoring.checkName()); | 217 assertRefactoringStatusOK(refactoring.checkName()); |
| 218 } | 218 } |
| 219 | 219 |
| 220 test_completeStatementExpression() { | 220 test_completeStatementExpression() { |
| 221 indexTestUnit(''' | 221 indexTestUnit(''' |
| 222 main(p) { | 222 main(p) { |
| 223 p.toString(); | 223 p.toString(); |
| (...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 858 var res = 'abc'; | 858 var res = 'abc'; |
| 859 print(res); | 859 print(res); |
| 860 } | 860 } |
| 861 '''); | 861 '''); |
| 862 } | 862 } |
| 863 | 863 |
| 864 Future _assertInitialConditions_fatal_selection() { | 864 Future _assertInitialConditions_fatal_selection() { |
| 865 return refactoring.checkInitialConditions().then((status) { | 865 return refactoring.checkInitialConditions().then((status) { |
| 866 assertRefactoringStatus( | 866 assertRefactoringStatus( |
| 867 status, | 867 status, |
| 868 RefactoringStatusSeverity.FATAL, | 868 RefactoringProblemSeverity.FATAL, |
| 869 expectedMessage: 'Expression must be selected to activate this refacto
ring.'); | 869 expectedMessage: 'Expression must be selected to activate this refacto
ring.'); |
| 870 }); | 870 }); |
| 871 } | 871 } |
| 872 | 872 |
| 873 /** | 873 /** |
| 874 * Checks that all conditions are OK and the result of applying the [Change] | 874 * Checks that all conditions are OK and the result of applying the [Change] |
| 875 * to [testUnit] is [expectedCode]. | 875 * to [testUnit] is [expectedCode]. |
| 876 */ | 876 */ |
| 877 Future _assertSuccessfulRefactoring(String expectedCode) { | 877 Future _assertSuccessfulRefactoring(String expectedCode) { |
| 878 return assertRefactoringConditionsOK().then((_) { | 878 return assertRefactoringConditionsOK().then((_) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 897 int length = search.length; | 897 int length = search.length; |
| 898 _createRefactoring(offset, length); | 898 _createRefactoring(offset, length); |
| 899 } | 899 } |
| 900 | 900 |
| 901 void _createRefactoringWithSuffix(String selectionSearch, String suffix) { | 901 void _createRefactoringWithSuffix(String selectionSearch, String suffix) { |
| 902 int offset = findOffset(selectionSearch + suffix); | 902 int offset = findOffset(selectionSearch + suffix); |
| 903 int length = selectionSearch.length; | 903 int length = selectionSearch.length; |
| 904 _createRefactoring(offset, length); | 904 _createRefactoring(offset, length); |
| 905 } | 905 } |
| 906 } | 906 } |
| OLD | NEW |