Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: pkg/analysis_server/test/services/refactoring/extract_local_test.dart

Issue 1528013004: Issue 25253. Improve collecting names that may conflict with the extracted local variable. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analysis_server/lib/src/services/refactoring/extract_local.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import 'dart:convert'; 8 import 'dart:convert';
9 9
10 import 'package:analysis_server/plugin/protocol/protocol.dart'; 10 import 'package:analysis_server/plugin/protocol/protocol.dart';
(...skipping 18 matching lines...) Expand all
29 test_checkFinalConditions_sameVariable_after() async { 29 test_checkFinalConditions_sameVariable_after() async {
30 indexTestUnit(''' 30 indexTestUnit('''
31 main() { 31 main() {
32 int a = 1 + 2; 32 int a = 1 + 2;
33 var res; 33 var res;
34 } 34 }
35 '''); 35 ''');
36 _createRefactoringForString('1 + 2'); 36 _createRefactoringForString('1 + 2');
37 // conflicting name 37 // conflicting name
38 RefactoringStatus status = await refactoring.checkAllConditions(); 38 RefactoringStatus status = await refactoring.checkAllConditions();
39 assertRefactoringStatus(status, RefactoringProblemSeverity.WARNING, 39 assertRefactoringStatus(status, RefactoringProblemSeverity.ERROR,
40 expectedMessage: 40 expectedMessage: "The name 'res' is already used in the scope.");
41 "A variable with name 'res' is already defined in the visible scope. ");
42 } 41 }
43 42
44 test_checkFinalConditions_sameVariable_before() async { 43 test_checkFinalConditions_sameVariable_before() async {
45 indexTestUnit(''' 44 indexTestUnit('''
46 main() { 45 main() {
47 var res; 46 var res;
48 int a = 1 + 2; 47 int a = 1 + 2;
49 } 48 }
50 '''); 49 ''');
51 _createRefactoringForString('1 + 2'); 50 _createRefactoringForString('1 + 2');
52 // conflicting name 51 // conflicting name
53 RefactoringStatus status = await refactoring.checkAllConditions(); 52 RefactoringStatus status = await refactoring.checkAllConditions();
54 assertRefactoringStatus(status, RefactoringProblemSeverity.WARNING, 53 assertRefactoringStatus(status, RefactoringProblemSeverity.ERROR,
55 expectedMessage: 54 expectedMessage: "The name 'res' is already used in the scope.");
56 "A variable with name 'res' is already defined in the visible scope. ");
57 } 55 }
58 56
59 test_checkInitialConditions_assignmentLeftHandSize() async { 57 test_checkInitialConditions_assignmentLeftHandSize() async {
60 indexTestUnit(''' 58 indexTestUnit('''
61 main() { 59 main() {
62 var v = 0; 60 var v = 0;
63 v = 1; 61 v = 1;
64 } 62 }
65 '''); 63 ''');
66 _createRefactoringWithSuffix('v', ' = 1;'); 64 _createRefactoringWithSuffix('v', ' = 1;');
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 print(42); 154 print(42);
157 } 155 }
158 '''); 156 ''');
159 _createRefactoringForString('print'); 157 _createRefactoringForString('print');
160 // check conditions 158 // check conditions
161 RefactoringStatus status = await refactoring.checkInitialConditions(); 159 RefactoringStatus status = await refactoring.checkInitialConditions();
162 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, 160 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL,
163 expectedMessage: 'Cannot extract the void expression.'); 161 expectedMessage: 'Cannot extract the void expression.');
164 } 162 }
165 163
166 test_checkLocalName() { 164 test_checkName() {
167 indexTestUnit(''' 165 indexTestUnit('''
168 main() { 166 main() {
169 int a = 1 + 2; 167 int a = 1 + 2;
170 } 168 }
171 '''); 169 ''');
172 _createRefactoringForString('1 + 2'); 170 _createRefactoringForString('1 + 2');
173 expect(refactoring.refactoringName, 'Extract Local Variable'); 171 expect(refactoring.refactoringName, 'Extract Local Variable');
174 // null 172 // null
175 refactoring.name = null; 173 refactoring.name = null;
176 assertRefactoringStatus( 174 assertRefactoringStatus(
177 refactoring.checkName(), RefactoringProblemSeverity.FATAL, 175 refactoring.checkName(), RefactoringProblemSeverity.FATAL,
178 expectedMessage: "Variable name must not be null."); 176 expectedMessage: "Variable name must not be null.");
179 // empty 177 // empty
180 refactoring.name = ''; 178 refactoring.name = '';
181 assertRefactoringStatus( 179 assertRefactoringStatus(
182 refactoring.checkName(), RefactoringProblemSeverity.FATAL, 180 refactoring.checkName(), RefactoringProblemSeverity.FATAL,
183 expectedMessage: "Variable name must not be empty."); 181 expectedMessage: "Variable name must not be empty.");
184 // OK 182 // OK
185 refactoring.name = 'res'; 183 refactoring.name = 'res';
186 assertRefactoringStatusOK(refactoring.checkName()); 184 assertRefactoringStatusOK(refactoring.checkName());
187 } 185 }
188 186
187 test_checkName_conflict_withInvokedFunction() async {
188 indexTestUnit('''
189 main() {
190 int a = 1 + 2;
191 res();
192 }
193
194 void res() {}
195 ''');
196 _createRefactoringForString('1 + 2');
197 await refactoring.checkInitialConditions();
198 refactoring.name = 'res';
199 assertRefactoringStatus(
200 refactoring.checkName(), RefactoringProblemSeverity.ERROR,
201 expectedMessage: "The name 'res' is already used in the scope.");
202 }
203
204 test_checkName_conflict_withOtherLocal() async {
205 indexTestUnit('''
206 main() {
207 var res;
208 int a = 1 + 2;
209 }
210 ''');
211 _createRefactoringForString('1 + 2');
212 await refactoring.checkInitialConditions();
213 refactoring.name = 'res';
214 assertRefactoringStatus(
215 refactoring.checkName(), RefactoringProblemSeverity.ERROR,
216 expectedMessage: "The name 'res' is already used in the scope.");
217 }
218
219 test_checkName_conflict_withTypeName() async {
220 indexTestUnit('''
221 main() {
222 int a = 1 + 2;
223 Res b = null;
224 }
225
226 class Res {}
227 ''');
228 _createRefactoringForString('1 + 2');
229 await refactoring.checkInitialConditions();
230 refactoring.name = 'Res';
231 assertRefactoringStatus(
232 refactoring.checkName(), RefactoringProblemSeverity.ERROR,
233 expectedMessage: "The name 'Res' is already used in the scope.");
234 }
235
189 test_completeStatementExpression() { 236 test_completeStatementExpression() {
190 indexTestUnit(''' 237 indexTestUnit('''
191 main(p) { 238 main(p) {
192 p.toString(); 239 p.toString();
193 } 240 }
194 '''); 241 ''');
195 _createRefactoringForString('p.toString()'); 242 _createRefactoringForString('p.toString()');
196 // apply refactoring 243 // apply refactoring
197 return _assertSuccessfulRefactoring(''' 244 return _assertSuccessfulRefactoring('''
198 main(p) { 245 main(p) {
(...skipping 965 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 List<String> _getCoveringExpressions() { 1211 List<String> _getCoveringExpressions() {
1165 List<String> subExpressions = <String>[]; 1212 List<String> subExpressions = <String>[];
1166 for (int i = 0; i < refactoring.coveringExpressionOffsets.length; i++) { 1213 for (int i = 0; i < refactoring.coveringExpressionOffsets.length; i++) {
1167 int offset = refactoring.coveringExpressionOffsets[i]; 1214 int offset = refactoring.coveringExpressionOffsets[i];
1168 int length = refactoring.coveringExpressionLengths[i]; 1215 int length = refactoring.coveringExpressionLengths[i];
1169 subExpressions.add(testCode.substring(offset, offset + length)); 1216 subExpressions.add(testCode.substring(offset, offset + length));
1170 } 1217 }
1171 return subExpressions; 1218 return subExpressions;
1172 } 1219 }
1173 } 1220 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/services/refactoring/extract_local.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698