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 import 'dart:convert'; |
8 | 9 |
9 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 10 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
10 import 'package:analysis_server/src/services/correction/status.dart'; | 11 import 'package:analysis_server/src/services/correction/status.dart'; |
11 import 'package:analysis_server/src/services/refactoring/extract_local.dart'; | 12 import 'package:analysis_server/src/services/refactoring/extract_local.dart'; |
12 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 13 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
13 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 14 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
14 import 'package:unittest/unittest.dart'; | 15 import 'package:unittest/unittest.dart'; |
15 | 16 |
16 import '../../utils.dart'; | 17 import '../../utils.dart'; |
17 import 'abstract_refactoring.dart'; | 18 import 'abstract_refactoring.dart'; |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 main() { | 528 main() { |
528 var s = 'Hello Bob... welcome to Dart!'; | 529 var s = 'Hello Bob... welcome to Dart!'; |
529 } | 530 } |
530 '''); | 531 '''); |
531 _createRefactoringForString('Hello Bob'); | 532 _createRefactoringForString('Hello Bob'); |
532 // check guesses | 533 // check guesses |
533 await refactoring.checkInitialConditions(); | 534 await refactoring.checkInitialConditions(); |
534 expect(refactoring.names, unorderedEquals(['helloBob', 'bob'])); | 535 expect(refactoring.names, unorderedEquals(['helloBob', 'bob'])); |
535 } | 536 } |
536 | 537 |
537 test_occurrences_differentVariable() { | 538 test_occurrences_differentVariable() async { |
538 indexTestUnit(''' | 539 indexTestUnit(''' |
539 main() { | 540 main() { |
540 { | 541 { |
541 int v = 1; | 542 int v = 1; |
542 print(v + 1); // marker | 543 print(v + 1); // marker |
543 print(v + 1); | 544 print(v + 1); |
544 } | 545 } |
545 { | 546 { |
546 int v = 2; | 547 int v = 2; |
547 print(v + 1); | 548 print(v + 1); |
548 } | 549 } |
549 } | 550 } |
550 '''); | 551 '''); |
551 _createRefactoringWithSuffix('v + 1', '); // marker'); | 552 _createRefactoringWithSuffix('v + 1', '); // marker'); |
552 // apply refactoring | 553 // apply refactoring |
553 return _assertSuccessfulRefactoring(''' | 554 await _assertSuccessfulRefactoring(''' |
554 main() { | 555 main() { |
555 { | 556 { |
556 int v = 1; | 557 int v = 1; |
557 var res = v + 1; | 558 var res = v + 1; |
558 print(res); // marker | 559 print(res); // marker |
559 print(res); | 560 print(res); |
560 } | 561 } |
561 { | 562 { |
562 int v = 2; | 563 int v = 2; |
563 print(v + 1); | 564 print(v + 1); |
564 } | 565 } |
565 } | 566 } |
566 '''); | 567 '''); |
| 568 _assertSingleLinkedEditGroup( |
| 569 length: 3, offsets: [36, 59, 85], names: ['object', 'i']); |
567 } | 570 } |
568 | 571 |
569 test_occurrences_disableOccurrences() { | 572 test_occurrences_disableOccurrences() { |
570 indexTestUnit(''' | 573 indexTestUnit(''' |
571 int foo() => 42; | 574 int foo() => 42; |
572 main() { | 575 main() { |
573 int a = 1 + foo(); | 576 int a = 1 + foo(); |
574 int b = 2 + foo(); // marker | 577 int b = 2 + foo(); // marker |
575 } | 578 } |
576 '''); | 579 '''); |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 // apply refactoring | 784 // apply refactoring |
782 return _assertSuccessfulRefactoring(''' | 785 return _assertSuccessfulRefactoring(''' |
783 main(p) { | 786 main(p) { |
784 foo | 787 foo |
785 var res = p.bar; | 788 var res = p.bar; |
786 res.baz; | 789 res.baz; |
787 } | 790 } |
788 '''); | 791 '''); |
789 } | 792 } |
790 | 793 |
791 test_singleExpression_inExpressionBody() { | 794 test_singleExpression_inExpressionBody() async { |
792 indexTestUnit(''' | 795 indexTestUnit(''' |
793 main() { | 796 main() { |
794 print((x) => x.y * x.y + 1); | 797 print((x) => x.y * x.y + 1); |
795 } | 798 } |
796 '''); | 799 '''); |
797 _createRefactoringForString('x.y'); | 800 _createRefactoringForString('x.y'); |
798 // apply refactoring | 801 // apply refactoring |
799 return _assertSuccessfulRefactoring(''' | 802 await _assertSuccessfulRefactoring(''' |
800 main() { | 803 main() { |
801 print((x) { | 804 print((x) { |
802 var res = x.y; | 805 var res = x.y; |
803 return res * res + 1; | 806 return res * res + 1; |
804 }); | 807 }); |
805 } | 808 } |
806 '''); | 809 '''); |
| 810 _assertSingleLinkedEditGroup( |
| 811 length: 3, offsets: [31, 53, 59], names: ['y']); |
807 } | 812 } |
808 | 813 |
809 test_singleExpression_inIfElseIf() { | 814 test_singleExpression_inIfElseIf() { |
810 indexTestUnit(''' | 815 indexTestUnit(''' |
811 main(int p) { | 816 main(int p) { |
812 if (p == 1) { | 817 if (p == 1) { |
813 print(1); | 818 print(1); |
814 } else if (p == 2) { | 819 } else if (p == 2) { |
815 print(2); | 820 print(2); |
816 } | 821 } |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
980 _createRefactoringForString('1 + 2 '); | 985 _createRefactoringForString('1 + 2 '); |
981 // apply refactoring | 986 // apply refactoring |
982 return _assertSuccessfulRefactoring(''' | 987 return _assertSuccessfulRefactoring(''' |
983 main() { | 988 main() { |
984 var res = 1 + 2; | 989 var res = 1 + 2; |
985 int a = res ; | 990 int a = res ; |
986 } | 991 } |
987 '''); | 992 '''); |
988 } | 993 } |
989 | 994 |
990 test_stringLiteral_part() { | 995 test_stringLiteral_part() async { |
991 indexTestUnit(''' | 996 indexTestUnit(''' |
992 main() { | 997 main() { |
993 print('abcdefgh'); | 998 print('abcdefgh'); |
994 } | 999 } |
995 '''); | 1000 '''); |
996 _createRefactoringForString('cde'); | 1001 _createRefactoringForString('cde'); |
997 // apply refactoring | 1002 // apply refactoring |
998 return _assertSuccessfulRefactoring(r''' | 1003 await _assertSuccessfulRefactoring(r''' |
999 main() { | 1004 main() { |
1000 var res = 'cde'; | 1005 var res = 'cde'; |
1001 print('ab${res}fgh'); | 1006 print('ab${res}fgh'); |
1002 } | 1007 } |
1003 '''); | 1008 '''); |
| 1009 _assertSingleLinkedEditGroup(length: 3, offsets: [15, 41], names: ['cde']); |
1004 } | 1010 } |
1005 | 1011 |
1006 test_stringLiteral_whole() { | 1012 test_stringLiteral_whole() async { |
1007 indexTestUnit(''' | 1013 indexTestUnit(''' |
1008 main() { | 1014 main() { |
1009 print('abc'); | 1015 print('abc'); |
1010 } | 1016 } |
1011 '''); | 1017 '''); |
1012 _createRefactoringForString("'abc'"); | 1018 _createRefactoringForString("'abc'"); |
1013 // apply refactoring | 1019 // apply refactoring |
1014 return _assertSuccessfulRefactoring(''' | 1020 await _assertSuccessfulRefactoring(''' |
1015 main() { | 1021 main() { |
1016 var res = 'abc'; | 1022 var res = 'abc'; |
1017 print(res); | 1023 print(res); |
1018 } | 1024 } |
1019 '''); | 1025 '''); |
| 1026 _assertSingleLinkedEditGroup( |
| 1027 length: 3, offsets: [15, 36], names: ['object', 's']); |
1020 } | 1028 } |
1021 | 1029 |
1022 test_stringLiteralPart() { | 1030 test_stringLiteralPart() async { |
1023 indexTestUnit(r''' | 1031 indexTestUnit(r''' |
1024 main() { | 1032 main() { |
1025 int x = 1; | 1033 int x = 1; |
1026 int y = 2; | 1034 int y = 2; |
1027 print('$x+$y=${x+y}'); | 1035 print('$x+$y=${x+y}'); |
1028 } | 1036 } |
1029 '''); | 1037 '''); |
1030 _createRefactoringForString(r'$x+$y'); | 1038 _createRefactoringForString(r'$x+$y'); |
1031 // apply refactoring | 1039 // apply refactoring |
1032 return _assertSuccessfulRefactoring(r''' | 1040 await _assertSuccessfulRefactoring(r''' |
1033 main() { | 1041 main() { |
1034 int x = 1; | 1042 int x = 1; |
1035 int y = 2; | 1043 int y = 2; |
1036 var res = '$x+$y'; | 1044 var res = '$x+$y'; |
1037 print('${res}=${x+y}'); | 1045 print('${res}=${x+y}'); |
1038 } | 1046 } |
1039 '''); | 1047 '''); |
| 1048 _assertSingleLinkedEditGroup(length: 3, offsets: [41, 67], names: ['xy']); |
1040 } | 1049 } |
1041 | 1050 |
1042 Future _assertInitialConditions_fatal_selection() async { | 1051 Future _assertInitialConditions_fatal_selection() async { |
1043 RefactoringStatus status = await refactoring.checkInitialConditions(); | 1052 RefactoringStatus status = await refactoring.checkInitialConditions(); |
1044 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, | 1053 assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL, |
1045 expectedMessage: | 1054 expectedMessage: |
1046 'Expression must be selected to activate this refactoring.'); | 1055 'Expression must be selected to activate this refactoring.'); |
1047 } | 1056 } |
1048 | 1057 |
| 1058 void _assertSingleLinkedEditGroup( |
| 1059 {int length, List<int> offsets, List<String> names}) { |
| 1060 String positionsString = offsets |
| 1061 .map((offset) => '{"file": "$testFile", "offset": $offset}') |
| 1062 .join(','); |
| 1063 String suggestionsString = |
| 1064 names.map((name) => '{"value": "$name", "kind": "VARIABLE"}').join(','); |
| 1065 _assertSingleLinkedEditGroupJson(''' |
| 1066 { |
| 1067 "length": $length, |
| 1068 "positions": [$positionsString], |
| 1069 "suggestions": [$suggestionsString] |
| 1070 }'''); |
| 1071 } |
| 1072 |
| 1073 void _assertSingleLinkedEditGroupJson(String expectedJsonString) { |
| 1074 List<LinkedEditGroup> editGroups = refactoringChange.linkedEditGroups; |
| 1075 expect(editGroups, hasLength(1)); |
| 1076 expect(editGroups.first.toJson(), JSON.decode(expectedJsonString)); |
| 1077 } |
| 1078 |
1049 /** | 1079 /** |
1050 * Checks that all conditions are OK and the result of applying the | 1080 * Checks that all conditions are OK and the result of applying the |
1051 * [SourceChange] to [testUnit] is [expectedCode]. | 1081 * [SourceChange] to [testUnit] is [expectedCode]. |
1052 */ | 1082 */ |
1053 Future _assertSuccessfulRefactoring(String expectedCode) async { | 1083 Future _assertSuccessfulRefactoring(String expectedCode) async { |
1054 await assertRefactoringConditionsOK(); | 1084 await assertRefactoringConditionsOK(); |
1055 SourceChange refactoringChange = await refactoring.createChange(); | 1085 SourceChange refactoringChange = await refactoring.createChange(); |
1056 this.refactoringChange = refactoringChange; | 1086 this.refactoringChange = refactoringChange; |
1057 assertTestChangeResult(expectedCode); | 1087 assertTestChangeResult(expectedCode); |
1058 } | 1088 } |
(...skipping 22 matching lines...) Expand all Loading... |
1081 List<String> _getCoveringExpressions() { | 1111 List<String> _getCoveringExpressions() { |
1082 List<String> subExpressions = <String>[]; | 1112 List<String> subExpressions = <String>[]; |
1083 for (int i = 0; i < refactoring.coveringExpressionOffsets.length; i++) { | 1113 for (int i = 0; i < refactoring.coveringExpressionOffsets.length; i++) { |
1084 int offset = refactoring.coveringExpressionOffsets[i]; | 1114 int offset = refactoring.coveringExpressionOffsets[i]; |
1085 int length = refactoring.coveringExpressionLengths[i]; | 1115 int length = refactoring.coveringExpressionLengths[i]; |
1086 subExpressions.add(testCode.substring(offset, offset + length)); | 1116 subExpressions.add(testCode.substring(offset, offset + length)); |
1087 } | 1117 } |
1088 return subExpressions; | 1118 return subExpressions; |
1089 } | 1119 } |
1090 } | 1120 } |
OLD | NEW |