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

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

Issue 506753002: Parameters and validation for the 'Extract Method' refactoring. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 months 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 | Annotate | Revision Log
« no previous file with comments | « pkg/analysis_server/test/services/refactoring/abstract_refactoring.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_method; 5 library test.services.refactoring.extract_method;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/protocol.dart'; 9 import 'package:analysis_server/src/protocol.dart';
10 import 'package:analysis_server/src/services/refactoring/extract_method.dart'; 10 import 'package:analysis_server/src/services/refactoring/extract_method.dart';
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 class A { 69 class A {
70 void res() {} 70 void res() {}
71 main() { 71 main() {
72 // start 72 // start
73 print(0); 73 print(0);
74 // end 74 // end
75 } 75 }
76 } 76 }
77 '''); 77 ''');
78 _createRefactoringForStartEndComments(); 78 _createRefactoringForStartEndComments();
79 // TODO(scheglov) implement 79 return _assertConditionsError(
80 // return _assertConditionsError("Class 'A' already declares method with name 'res'."); 80 "Class 'A' already declares method with name 'res'.");
81 } 81 }
82 82
83 test_bad_conflict_method_shadowsSuperDeclaration() { 83 test_bad_conflict_method_shadowsSuperDeclaration() {
84 indexTestUnit(''' 84 indexTestUnit('''
85 class A { 85 class A {
86 void res() {} // marker 86 void res() {} // marker
87 } 87 }
88 class B extends A { 88 class B extends A {
89 main() { 89 main() {
90 res(); 90 res();
91 // start 91 // start
92 print(0); 92 print(0);
93 // end 93 // end
94 } 94 }
95 } 95 }
96 '''); 96 ''');
97 _createRefactoringForStartEndComments(); 97 _createRefactoringForStartEndComments();
98 // TODO(scheglov) implement 98 return _assertConditionsError("Created method will shadow method 'A.res'.");
99 // return _assertConditionsError("Created method will shadow method 'A.res'." );
100 } 99 }
101 100
102 test_bad_conflict_topLevel_alreadyDeclaresFunction() { 101 test_bad_conflict_topLevel_alreadyDeclaresFunction() {
103 indexTestUnit(''' 102 indexTestUnit('''
103 library my.lib;
104
104 void res() {} 105 void res() {}
105 main() { 106 main() {
106 // start 107 // start
107 print(0); 108 print(0);
108 // end 109 // end
109 } 110 }
110 '''); 111 ''');
111 _createRefactoringForStartEndComments(); 112 _createRefactoringForStartEndComments();
112 // TODO(scheglov) implement 113 return _assertConditionsError(
113 // return _assertConditionsError("Library already declares function with name 'res'."); 114 "Library already declares function with name 'res'.");
114 } 115 }
115 116
116 test_bad_conflict_topLevel_willHideInheritedMemberUsage() { 117 test_bad_conflict_topLevel_willHideInheritedMemberUsage() {
117 indexTestUnit(''' 118 indexTestUnit('''
118 class A { 119 class A {
119 void res() {} 120 void res() {}
120 } 121 }
121 class B extends A { 122 class B extends A {
122 foo() { 123 foo() {
123 res(); // marker 124 res(); // marker
124 } 125 }
125 } 126 }
126 main() { 127 main() {
127 // start 128 // start
128 print(0); 129 print(0);
129 // end 130 // end
130 } 131 }
131 '''); 132 ''');
132 _createRefactoringForStartEndComments(); 133 _createRefactoringForStartEndComments();
133 // TODO(scheglov) implement 134 return _assertConditionsError("Created function will shadow method 'A.res'." );
134 // return _assertConditionsError("Created function will shadow method 'A.res' .");
135 } 135 }
136 136
137 test_bad_constructor_initializer() { 137 test_bad_constructor_initializer() {
138 indexTestUnit(''' 138 indexTestUnit('''
139 class A { 139 class A {
140 int f; 140 int f;
141 A() : f = 0 {} 141 A() : f = 0 {}
142 } 142 }
143 '''); 143 ''');
144 _createRefactoringForString('f = 0'); 144 _createRefactoringForString('f = 0');
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 test_bad_newMethodName_notIdentifier() { 333 test_bad_newMethodName_notIdentifier() {
334 indexTestUnit(''' 334 indexTestUnit('''
335 main() { 335 main() {
336 // start 336 // start
337 print(0); 337 print(0);
338 // end 338 // end
339 } 339 }
340 '''); 340 ''');
341 _createRefactoringForStartEndComments(); 341 _createRefactoringForStartEndComments();
342 refactoring.name = 'bad-name'; 342 refactoring.name = 'bad-name';
343 // TODO(scheglov) implement 343 // check conditions
344 // return _assertConditionsFatal("Method name must not contain '-'."); 344 return _assertConditionsError("Method name must not contain '-'.");
345 } 345 }
346 346
347 test_bad_notSameParent() { 347 test_bad_notSameParent() {
348 indexTestUnit(''' 348 indexTestUnit('''
349 main() { 349 main() {
350 while (false) 350 while (false)
351 // start 351 // start
352 { 352 {
353 } 353 }
354 print(0); 354 print(0);
355 // end 355 // end
356 } 356 }
357 '''); 357 ''');
358 _createRefactoringForStartEndComments(); 358 _createRefactoringForStartEndComments();
359 return _assertConditionsFatal( 359 return _assertConditionsFatal(
360 'Not all selected statements are enclosed by the same parent statement.' ); 360 'Not all selected statements are enclosed by the same parent statement.' );
361 } 361 }
362 362
363 test_bad_parameterName_duplicate() { 363 test_bad_parameterName_duplicate() {
364 indexTestUnit(''' 364 indexTestUnit('''
365 main() { 365 main() {
366 int v1 = 1; 366 int v1 = 1;
367 int v2 = 2; 367 int v2 = 2;
368 // start 368 // start
369 int a = v1 + v2; // marker 369 int a = v1 + v2; // marker
370 // end 370 // end
371 } 371 }
372 '''); 372 ''');
373 _createRefactoringForStartEndComments(); 373 _createRefactoringForStartEndComments();
374 // TODO(scheglov) implement
375 // update parameters 374 // update parameters
376 // { 375 return refactoring.checkInitialConditions().then((_) {
377 // Parameter[] parameters = refactoring.getParameters(); 376 {
378 // assertThat(parameters).hasSize(2); 377 var parameters = refactoring.parameters.toList();
379 // parameters[0].setNewName("dup"); 378 expect(parameters, hasLength(2));
380 // parameters[1].setNewName("dup"); 379 parameters[0].name = 'dup';
381 // refactoring.setParameters(parameters); 380 parameters[1].name = 'dup';
382 // } 381 refactoring.parameters = parameters;
383 // // check conditions 382 }
384 // refactoringStatus = refactoring.checkFinalConditions(pm); 383 return _assertFinalConditionsError("Parameter 'dup' already exists");
385 // assertRefactoringStatus( 384 });
386 // refactoringStatus,
387 // RefactoringStatusSeverity.ERROR,
388 // "Parameter 'dup' already exists");
389 } 385 }
390 386
391 test_bad_parameterName_inUse() { 387 test_bad_parameterName_inUse() {
392 indexTestUnit(''' 388 indexTestUnit('''
393 main() { 389 main() {
394 int v1 = 1; 390 int v1 = 1;
395 int v2 = 2; 391 int v2 = 2;
396 // start 392 // start
397 int a = v1 + v2; // marker 393 int a = v1 + v2; // marker
398 // end 394 // end
399 } 395 }
400 '''); 396 ''');
401 _createRefactoringForStartEndComments(); 397 _createRefactoringForStartEndComments();
402 // TODO(scheglov) implement
403 // update parameters 398 // update parameters
404 // { 399 return refactoring.checkInitialConditions().then((_) {
405 // Parameter[] parameters = refactoring.getParameters(); 400 {
406 // assertThat(parameters).hasSize(2); 401 var parameters = refactoring.parameters.toList();
407 // parameters[0].setNewName("a"); 402 expect(parameters, hasLength(2));
408 // refactoring.setParameters(parameters); 403 parameters[0].name = 'a';
409 // } 404 refactoring.parameters = parameters;
410 // // check conditions 405 }
411 // refactoringStatus = refactoring.checkFinalConditions(pm); 406 return _assertFinalConditionsError(
412 // assertRefactoringStatus( 407 "'a' is already used as a name in the selected code");
413 // refactoringStatus, 408 });
414 // RefactoringStatusSeverity.ERROR,
415 // "'a' is already used as a name in the selected code");
416 } 409 }
417 410
418 test_bad_selectionEndsInSomeNode() { 411 test_bad_selectionEndsInSomeNode() {
419 indexTestUnit(''' 412 indexTestUnit('''
420 main() { 413 main() {
421 // start 414 // start
422 print(0); 415 print(0);
423 print(1); 416 print(1);
424 // end 417 // end
425 } 418 }
426 '''); 419 ''');
427 _createRefactoringForStartEndString('print(0', 'int(1)'); 420 _createRefactoringForStartEndString('print(0', 'rint(1)');
428 return _assertConditionsFatal( 421 return _assertConditionsFatal(
429 "The selection does not cover a set of statements or an expression. " 422 "The selection does not cover a set of statements or an expression. "
430 "Extend selection to a valid range."); 423 "Extend selection to a valid range.");
431 } 424 }
432 425
433 test_bad_statements_return_andAssignsVariable() { 426 test_bad_statements_return_andAssignsVariable() {
434 indexTestUnit(''' 427 indexTestUnit('''
435 main() { 428 main() {
436 // start 429 // start
437 var v = 0; 430 var v = 0;
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 "parts of try, catch, or finally block."); 568 "parts of try, catch, or finally block.");
576 } 569 }
577 570
578 test_bad_typeReference() { 571 test_bad_typeReference() {
579 indexTestUnit(''' 572 indexTestUnit('''
580 main() { 573 main() {
581 int a = 0; 574 int a = 0;
582 } 575 }
583 '''); 576 ''');
584 _createRefactoringForString("int"); 577 _createRefactoringForString("int");
585 return _assertConditionsFatal( 578 return _assertConditionsFatal("Cannot extract a single type reference.");
586 "Cannot extract a single type reference.");
587 } 579 }
588 580
589 test_bad_variableDeclarationFragment() { 581 test_bad_variableDeclarationFragment() {
590 indexTestUnit(''' 582 indexTestUnit('''
591 main() { 583 main() {
592 int 584 int
593 // start 585 // start
594 a = 1 586 a = 1
595 // end 587 // end
596 ,b = 2; 588 ,b = 2;
(...skipping 872 matching lines...) Expand 10 before | Expand all | Expand 10 after
1469 int v1 = 1; 1461 int v1 = 1;
1470 int v2 = 2; 1462 int v2 = 2;
1471 int a = res(v1, v2); 1463 int a = res(v1, v2);
1472 } 1464 }
1473 1465
1474 int res(int v1, int v2) => v1 + v2 + v1; 1466 int res(int v1, int v2) => v1 + v2 + v1;
1475 '''); 1467 ''');
1476 } 1468 }
1477 1469
1478 test_singleExpression_withVariables_doRename() { 1470 test_singleExpression_withVariables_doRename() {
1479 // TODO(scheglov) 1471 indexTestUnit('''
1472 main() {
1473 int v1 = 1;
1474 int v2 = 2;
1475 int v3 = 3;
1476 int a = v1 + v2 + v1; // marker
1477 int b = v2 + v3 + v2;
1478 }
1479 ''');
1480 _createRefactoringForString('v1 + v2 + v1');
1481 // apply refactoring
1482 return refactoring.checkInitialConditions().then((_) {
1483 {
1484 var parameters = refactoring.parameters.toList();
1485 expect(parameters, hasLength(2));
1486 expect(parameters[0].name, 'v1');
1487 expect(parameters[1].name, 'v2');
1488 parameters[0].name = 'par1';
1489 parameters[1].name = 'param2';
1490 refactoring.parameters = parameters;
1491 }
1492 return assertRefactoringFinalConditionsOK().then((_) {
1493 refactoring.createGetter = false;
1494 return _assertRefactoringChange('''
1495 main() {
1496 int v1 = 1;
1497 int v2 = 2;
1498 int v3 = 3;
1499 int a = res(v1, v2); // marker
1500 int b = res(v2, v3);
1501 }
1502
1503 int res(int par1, int param2) => par1 + param2 + par1;
1504 ''');
1505 });
1506 });
1480 } 1507 }
1481 1508
1482 test_singleExpression_withVariables_doReorder() { 1509 test_singleExpression_withVariables_doReorder() {
1483 // TODO(scheglov) 1510 indexTestUnit('''
1511 main() {
1512 int v1 = 1;
1513 int v2 = 2;
1514 int v3 = 3;
1515 int a = v1 + v2; // marker
1516 int b = v2 + v3;
1517 }
1518 ''');
1519 _createRefactoringForString('v1 + v2');
1520 // apply refactoring
1521 return refactoring.checkInitialConditions().then((_) {
1522 {
1523 var parameters = refactoring.parameters.toList();
1524 expect(parameters, hasLength(2));
1525 expect(parameters[0].name, 'v1');
1526 expect(parameters[1].name, 'v2');
1527 var parameter = parameters.removeAt(1);
1528 parameters.insert(0, parameter);
1529 refactoring.parameters = parameters;
1530 }
1531 return assertRefactoringFinalConditionsOK().then((_) {
1532 refactoring.createGetter = false;
1533 return _assertRefactoringChange('''
1534 main() {
1535 int v1 = 1;
1536 int v2 = 2;
1537 int v3 = 3;
1538 int a = res(v2, v1); // marker
1539 int b = res(v3, v2);
1540 }
1541
1542 int res(int v2, int v1) => v1 + v2;
1543 ''');
1544 });
1545 });
1484 } 1546 }
1485 1547
1486 test_singleExpression_withVariables_namedExpression() { 1548 test_singleExpression_withVariables_namedExpression() {
1487 indexTestUnit(''' 1549 indexTestUnit('''
1488 main() { 1550 main() {
1489 int v1 = 1; 1551 int v1 = 1;
1490 int v2 = 2; 1552 int v2 = 2;
1491 int a = process(arg: v1 + v2); 1553 int a = process(arg: v1 + v2);
1492 } 1554 }
1493 process({arg}) {} 1555 process({arg}) {}
1494 '''); 1556 ''');
1495 _createRefactoringForString('process(arg: v1 + v2)'); 1557 _createRefactoringForString('process(arg: v1 + v2)');
1496 // apply refactoring 1558 // apply refactoring
1497 return _assertSuccessfulRefactoring(''' 1559 return _assertSuccessfulRefactoring('''
1498 main() { 1560 main() {
1499 int v1 = 1; 1561 int v1 = 1;
1500 int v2 = 2; 1562 int v2 = 2;
1501 int a = res(v1, v2); 1563 int a = res(v1, v2);
1502 } 1564 }
1503 1565
1504 res(int v1, int v2) => process(arg: v1 + v2); 1566 res(int v1, int v2) => process(arg: v1 + v2);
1505 process({arg}) {} 1567 process({arg}) {}
1506 '''); 1568 ''');
1507 } 1569 }
1508 1570
1509 test_singleExpression_withVariables_newType() { 1571 test_singleExpression_withVariables_newType() {
1510 // TODO(scheglov) 1572 indexTestUnit('''
1573 main() {
1574 int v1 = 1;
1575 int v2 = 2;
1576 int v3 = 3;
1577 int a = v1 + v2 + v3;
1578 }
1579 ''');
1580 _createRefactoringForString('v1 + v2 + v3');
1581 // apply refactoring
1582 return refactoring.checkInitialConditions().then((_) {
1583 {
1584 var parameters = refactoring.parameters.toList();
1585 expect(parameters, hasLength(3));
1586 expect(parameters[0].name, 'v1');
1587 expect(parameters[1].name, 'v2');
1588 expect(parameters[2].name, 'v3');
1589 parameters[0].type = 'num';
1590 parameters[1].type = 'dynamic';
1591 parameters[2].type = '';
1592 refactoring.parameters = parameters;
1593 }
1594 return assertRefactoringFinalConditionsOK().then((_) {
1595 refactoring.createGetter = false;
1596 return _assertRefactoringChange('''
1597 main() {
1598 int v1 = 1;
1599 int v2 = 2;
1600 int v3 = 3;
1601 int a = res(v1, v2, v3);
1602 }
1603
1604 int res(num v1, v2, v3) => v1 + v2 + v3;
1605 ''');
1606 });
1607 });
1511 } 1608 }
1512 1609
1513 test_singleExpression_withVariables_useBestType() { 1610 test_singleExpression_withVariables_useBestType() {
1514 indexTestUnit(''' 1611 indexTestUnit('''
1515 main() { 1612 main() {
1516 var v1 = 1; 1613 var v1 = 1;
1517 var v2 = 2; 1614 var v2 = 2;
1518 var a = v1 + v2 + v1; // marker 1615 var a = v1 + v2 + v1; // marker
1519 } 1616 }
1520 '''); 1617 ''');
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
2056 2153
2057 Future _assertConditionsFatal(String message) { 2154 Future _assertConditionsFatal(String message) {
2058 return refactoring.checkAllConditions().then((status) { 2155 return refactoring.checkAllConditions().then((status) {
2059 assertRefactoringStatus( 2156 assertRefactoringStatus(
2060 status, 2157 status,
2061 RefactoringProblemSeverity.FATAL, 2158 RefactoringProblemSeverity.FATAL,
2062 expectedMessage: message); 2159 expectedMessage: message);
2063 }); 2160 });
2064 } 2161 }
2065 2162
2163 Future _assertFinalConditionsError(String message) {
2164 return refactoring.checkFinalConditions().then((status) {
2165 assertRefactoringStatus(
2166 status,
2167 RefactoringProblemSeverity.ERROR,
2168 expectedMessage: message);
2169 });
2170 }
2171
2172 Future _assertRefactoringChange(String expectedCode) {
2173 return refactoring.createChange().then((SourceChange refactoringChange) {
2174 this.refactoringChange = refactoringChange;
2175 assertTestChangeResult(expectedCode);
2176 });
2177 }
2178
2066 /** 2179 /**
2067 * Checks that all conditions are OK and the result of applying the [Change] 2180 * Checks that all conditions are OK and the result of applying the [Change]
2068 * to [testUnit] is [expectedCode]. 2181 * to [testUnit] is [expectedCode].
2069 */ 2182 */
2070 Future _assertSuccessfulRefactoring(String expectedCode) { 2183 Future _assertSuccessfulRefactoring(String expectedCode) {
2071 return assertRefactoringConditionsOK().then((_) { 2184 return assertRefactoringConditionsOK().then((_) {
2072 refactoring.createGetter = false; 2185 refactoring.createGetter = false;
2073 return refactoring.createChange().then((SourceChange refactoringChange) { 2186 return _assertRefactoringChange(expectedCode);
2074 this.refactoringChange = refactoringChange;
2075 assertTestChangeResult(expectedCode);
2076 });
2077 }); 2187 });
2078 } 2188 }
2079 2189
2080 void _createRefactoring(int offset, int length) { 2190 void _createRefactoring(int offset, int length) {
2081 refactoring = 2191 refactoring =
2082 new ExtractMethodRefactoringImpl(searchEngine, testUnit, offset, length) ; 2192 new ExtractMethodRefactoringImpl(searchEngine, testUnit, offset, length) ;
2083 refactoring.name = 'res'; 2193 refactoring.name = 'res';
2084 } 2194 }
2085 2195
2086
2087 // Future _assertInitialConditions_fatal_selection() {
2088 // return refactoring.checkInitialConditions().then((status) {
2089 // assertRefactoringStatus(
2090 // status,
2091 // RefactoringProblemSeverity.FATAL,
2092 // expectedMessage: 'Expression must be selected to activate this refac toring.');
2093 // });
2094 // }
2095
2096 void _createRefactoringForStartEndComments() { 2196 void _createRefactoringForStartEndComments() {
2097 int offset = findEnd('// start') + '\n'.length; 2197 int offset = findEnd('// start') + '\n'.length;
2098 int end = findOffset('// end'); 2198 int end = findOffset('// end');
2099 _createRefactoring(offset, end - offset); 2199 _createRefactoring(offset, end - offset);
2100 } 2200 }
2101 2201
2102 void _createRefactoringForStartEndString(String startSearch, 2202 void _createRefactoringForStartEndString(String startSearch,
2103 String endSearch) { 2203 String endSearch) {
2104 int offset = findOffset(startSearch); 2204 int offset = findOffset(startSearch);
2105 int end = findOffset(endSearch); 2205 int end = findOffset(endSearch);
2106 _createRefactoring(offset, end - offset); 2206 _createRefactoring(offset, end - offset);
2107 } 2207 }
2108 2208
2109 /** 2209 /**
2110 * Creates a new refactoring in [refactoring] for the selection range of the 2210 * Creates a new refactoring in [refactoring] for the selection range of the
2111 * given [search] pattern. 2211 * given [search] pattern.
2112 */ 2212 */
2113 void _createRefactoringForString(String search) { 2213 void _createRefactoringForString(String search) {
2114 int offset = findOffset(search); 2214 int offset = findOffset(search);
2115 int length = search.length; 2215 int length = search.length;
2116 _createRefactoring(offset, length); 2216 _createRefactoring(offset, length);
2117 } 2217 }
2118 2218
2119 void _createRefactoringWithSuffix(String selectionSearch, String suffix) { 2219 void _createRefactoringWithSuffix(String selectionSearch, String suffix) {
2120 int offset = findOffset(selectionSearch + suffix); 2220 int offset = findOffset(selectionSearch + suffix);
2121 int length = selectionSearch.length; 2221 int length = selectionSearch.length;
2122 _createRefactoring(offset, length); 2222 _createRefactoring(offset, length);
2123 } 2223 }
2124 } 2224 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/test/services/refactoring/abstract_refactoring.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698