| 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.inline_local; |
| 6 |
| 7 import 'package:analysis_server/src/protocol.dart' hide Element; |
| 8 import 'package:analysis_server/src/services/refactoring/inline_local.dart'; |
| 9 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 10 import 'package:analysis_testing/reflective_tests.dart'; |
| 11 import 'package:analyzer/src/generated/ast.dart'; |
| 12 import 'package:analyzer/src/generated/element.dart'; |
| 13 import 'package:unittest/unittest.dart'; |
| 14 |
| 15 import 'abstract_refactoring.dart'; |
| 16 |
| 17 |
| 18 main() { |
| 19 groupSep = ' | '; |
| 20 runReflectiveTests(InlineLocalTest); |
| 21 } |
| 22 |
| 23 |
| 24 @ReflectiveTestCase() |
| 25 class InlineLocalTest extends RefactoringTest { |
| 26 InlineLocalRefactoringImpl refactoring; |
| 27 |
| 28 test_OK_cascade_intoCascade() { |
| 29 indexTestUnit(r''' |
| 30 class A { |
| 31 foo() {} |
| 32 bar() {} |
| 33 } |
| 34 main() { |
| 35 A test = new A()..foo(); |
| 36 test..bar(); |
| 37 } |
| 38 '''); |
| 39 _createRefactoring('test ='); |
| 40 // validate change |
| 41 return assertSuccessfulRefactoring(r''' |
| 42 class A { |
| 43 foo() {} |
| 44 bar() {} |
| 45 } |
| 46 main() { |
| 47 new A()..foo()..bar(); |
| 48 } |
| 49 '''); |
| 50 } |
| 51 |
| 52 test_OK_cascade_intoNotCascade() { |
| 53 indexTestUnit(r''' |
| 54 class A { |
| 55 foo() {} |
| 56 bar() {} |
| 57 } |
| 58 main() { |
| 59 A test = new A()..foo(); |
| 60 test.bar(); |
| 61 } |
| 62 '''); |
| 63 _createRefactoring('test ='); |
| 64 // validate change |
| 65 return assertSuccessfulRefactoring(r''' |
| 66 class A { |
| 67 foo() {} |
| 68 bar() {} |
| 69 } |
| 70 main() { |
| 71 (new A()..foo()).bar(); |
| 72 } |
| 73 '''); |
| 74 } |
| 75 |
| 76 test_OK_intoStringInterpolation() { |
| 77 indexTestUnit(r''' |
| 78 main() { |
| 79 int test = 1 + 2; |
| 80 print('test = $test'); |
| 81 print('test = ${test}'); |
| 82 print('test = ${process(test)}'); |
| 83 } |
| 84 process(x) {} |
| 85 '''); |
| 86 _createRefactoring('test ='); |
| 87 // validate change |
| 88 return assertSuccessfulRefactoring(r''' |
| 89 main() { |
| 90 print('test = ${1 + 2}'); |
| 91 print('test = ${1 + 2}'); |
| 92 print('test = ${process(1 + 2)}'); |
| 93 } |
| 94 process(x) {} |
| 95 '''); |
| 96 } |
| 97 |
| 98 /** |
| 99 * <p> |
| 100 * https://code.google.com/p/dart/issues/detail?id=18587 |
| 101 */ |
| 102 test_OK_keepNextCommentedLine() { |
| 103 indexTestUnit(''' |
| 104 main() { |
| 105 int test = 1 + 2; |
| 106 // foo |
| 107 print(test); |
| 108 // bar |
| 109 } |
| 110 '''); |
| 111 _createRefactoring('test ='); |
| 112 // validate change |
| 113 return assertSuccessfulRefactoring(''' |
| 114 main() { |
| 115 // foo |
| 116 print(1 + 2); |
| 117 // bar |
| 118 } |
| 119 '''); |
| 120 } |
| 121 |
| 122 test_OK_noUsages_1() { |
| 123 indexTestUnit(''' |
| 124 main() { |
| 125 int test = 1 + 2; |
| 126 print(0); |
| 127 } |
| 128 '''); |
| 129 _createRefactoring('test ='); |
| 130 // validate change |
| 131 return assertSuccessfulRefactoring(''' |
| 132 main() { |
| 133 print(0); |
| 134 } |
| 135 '''); |
| 136 } |
| 137 |
| 138 test_OK_noUsages_2() { |
| 139 indexTestUnit(''' |
| 140 main() { |
| 141 int test = 1 + 2; |
| 142 } |
| 143 '''); |
| 144 _createRefactoring('test ='); |
| 145 // validate change |
| 146 return assertSuccessfulRefactoring(''' |
| 147 main() { |
| 148 } |
| 149 '''); |
| 150 } |
| 151 |
| 152 test_OK_oneUsage() { |
| 153 indexTestUnit(''' |
| 154 main() { |
| 155 int test = 1 + 2; |
| 156 print(test); |
| 157 } |
| 158 '''); |
| 159 _createRefactoring('test ='); |
| 160 // validate change |
| 161 return assertSuccessfulRefactoring(''' |
| 162 main() { |
| 163 print(1 + 2); |
| 164 } |
| 165 '''); |
| 166 } |
| 167 |
| 168 test_OK_twoUsages() { |
| 169 indexTestUnit(''' |
| 170 main() { |
| 171 int test = 1 + 2; |
| 172 print(test); |
| 173 print(test); |
| 174 } |
| 175 '''); |
| 176 _createRefactoring('test ='); |
| 177 // validate change |
| 178 return assertSuccessfulRefactoring(''' |
| 179 main() { |
| 180 print(1 + 2); |
| 181 print(1 + 2); |
| 182 } |
| 183 '''); |
| 184 } |
| 185 |
| 186 test_access() { |
| 187 indexTestUnit(''' |
| 188 main() { |
| 189 int test = 1 + 2; |
| 190 print(test); |
| 191 print(test); |
| 192 } |
| 193 '''); |
| 194 _createRefactoring('test ='); |
| 195 expect(refactoring.refactoringName, 'Inline Local Variable'); |
| 196 // check initial conditions and access |
| 197 return refactoring.checkInitialConditions().then((_) { |
| 198 expect(refactoring.referenceCount, 2); |
| 199 }); |
| 200 } |
| 201 |
| 202 test_bad_selectionVariable_hasAssignments_1() { |
| 203 indexTestUnit(r''' |
| 204 main() { |
| 205 int test = 0; |
| 206 test = 1; |
| 207 } |
| 208 '''); |
| 209 _createRefactoring('test = 0'); |
| 210 return refactoring.checkInitialConditions().then((status) { |
| 211 assertRefactoringStatus( |
| 212 status, |
| 213 RefactoringProblemSeverity.FATAL, |
| 214 expectedContextSearch: 'test = 1'); |
| 215 }); |
| 216 } |
| 217 |
| 218 test_bad_selectionVariable_hasAssignments_2() { |
| 219 indexTestUnit(r''' |
| 220 main() { |
| 221 int test = 0; |
| 222 test += 1; |
| 223 } |
| 224 '''); |
| 225 _createRefactoring('test = 0'); |
| 226 return refactoring.checkInitialConditions().then((status) { |
| 227 assertRefactoringStatus( |
| 228 status, |
| 229 RefactoringProblemSeverity.FATAL, |
| 230 expectedContextSearch: 'test += 1'); |
| 231 }); |
| 232 } |
| 233 |
| 234 test_bad_selectionVariable_notInBlock() { |
| 235 indexTestUnit(r''' |
| 236 main() { |
| 237 if (true) |
| 238 int test = 0; |
| 239 } |
| 240 '''); |
| 241 _createRefactoring('test = 0'); |
| 242 return refactoring.checkInitialConditions().then((status) { |
| 243 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL); |
| 244 }); |
| 245 } |
| 246 |
| 247 test_bad_selectionVariable_notInitialized() { |
| 248 indexTestUnit(r''' |
| 249 main() { |
| 250 int test; |
| 251 } |
| 252 '''); |
| 253 _createRefactoring('test;'); |
| 254 return refactoring.checkInitialConditions().then((status) { |
| 255 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL); |
| 256 }); |
| 257 } |
| 258 |
| 259 void _createRefactoring(String search) { |
| 260 SimpleIdentifier identifier = findIdentifier(search); |
| 261 LocalVariableElement element = identifier.bestElement; |
| 262 refactoring = new InlineLocalRefactoring(searchEngine, testUnit, element); |
| 263 } |
| 264 } |
| OLD | NEW |