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

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

Issue 565973006: Issue 19800. Support for inlining getters/setters. (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
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.inline_method; 5 library test.services.refactoring.inline_method;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/protocol.dart' hide Element; 9 import 'package:analysis_server/src/protocol.dart' hide Element;
10 import 'package:analysis_server/src/services/refactoring/inline_method.dart'; 10 import 'package:analysis_server/src/services/refactoring/inline_method.dart';
11 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; 11 import 'package:analysis_server/src/services/refactoring/refactoring.dart';
12 import '../../reflective_tests.dart';
13 import 'package:analyzer/src/generated/source.dart'; 12 import 'package:analyzer/src/generated/source.dart';
14 import 'package:unittest/unittest.dart'; 13 import 'package:unittest/unittest.dart';
15 14
15 import '../../reflective_tests.dart';
16 import 'abstract_refactoring.dart'; 16 import 'abstract_refactoring.dart';
17 17
18 18
19 main() { 19 main() {
20 groupSep = ' | '; 20 groupSep = ' | ';
21 runReflectiveTests(InlineMethodTest); 21 runReflectiveTests(InlineMethodTest);
22 } 22 }
23 23
24 24
25 @ReflectiveTestCase() 25 @ReflectiveTestCase()
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 return refactoring.checkAllConditions().then((status) { 85 return refactoring.checkAllConditions().then((status) {
86 var location = new SourceRange(findOffset('..test()'), '..test()'.length); 86 var location = new SourceRange(findOffset('..test()'), '..test()'.length);
87 assertRefactoringStatus( 87 assertRefactoringStatus(
88 status, 88 status,
89 RefactoringProblemSeverity.ERROR, 89 RefactoringProblemSeverity.ERROR,
90 expectedMessage: 'Cannot inline cascade invocation.', 90 expectedMessage: 'Cannot inline cascade invocation.',
91 expectedContextRange: location); 91 expectedContextRange: location);
92 }); 92 });
93 } 93 }
94 94
95 test_bad_constructor() {
96 indexTestUnit(r'''
97 class A {
98 A.named() {}
99 }
100 ''');
101 _createRefactoring('named() {}');
102 // error
103 return _assertInvalidSelection();
104 }
105
95 test_bad_deleteSource_inlineOne() { 106 test_bad_deleteSource_inlineOne() {
96 indexTestUnit(r''' 107 indexTestUnit(r'''
97 test(a, b) { 108 test(a, b) {
98 return a + b; 109 return a + b;
99 } 110 }
100 main() { 111 main() {
101 var res1 = test(1, 2); 112 var res1 = test(1, 2);
102 var res2 = test(10, 20); 113 var res2 = test(10, 20);
103 } 114 }
104 '''); 115 ''');
(...skipping 12 matching lines...) Expand all
117 }); 128 });
118 } 129 }
119 130
120 test_bad_notExecutableElement() { 131 test_bad_notExecutableElement() {
121 indexTestUnit(r''' 132 indexTestUnit(r'''
122 main() { 133 main() {
123 } 134 }
124 '''); 135 ''');
125 _createRefactoring(') {'); 136 _createRefactoring(') {');
126 // error 137 // error
127 return _assertConditionsFatal( 138 return _assertInvalidSelection();
128 'Method declaration or reference must be selected to activate this refac toring.');
129 } 139 }
130 140
131 test_bad_notSimpleIdentifier() { 141 test_bad_notSimpleIdentifier() {
132 indexTestUnit(r''' 142 indexTestUnit(r'''
133 main() { 143 main() {
134 var test = 42; 144 var test = 42;
135 var res = test; 145 var res = test;
136 } 146 }
137 '''); 147 ''');
138 _createRefactoring('test;'); 148 _createRefactoring('test;');
139 // error 149 // error
140 return _assertConditionsFatal( 150 return _assertInvalidSelection();
141 'Method declaration or reference must be selected to activate this refac toring.');
142 } 151 }
143 152
144 test_bad_operator() { 153 test_bad_operator() {
145 indexTestUnit(r''' 154 indexTestUnit(r'''
146 class A { 155 class A {
147 operator -(other) => this; 156 operator -(other) => this;
148 } 157 }
149 '''); 158 ''');
150 _createRefactoring('-(other)'); 159 _createRefactoring('-(other)');
151 // error 160 // error
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 _createRefactoring('test() {'); 696 _createRefactoring('test() {');
688 // validate change 697 // validate change
689 return _assertSuccessfulRefactoring(r''' 698 return _assertSuccessfulRefactoring(r'''
690 var topLevelField = 0; 699 var topLevelField = 0;
691 main() { 700 main() {
692 print(topLevelField); 701 print(topLevelField);
693 } 702 }
694 '''); 703 ''');
695 } 704 }
696 705
706 test_getter_classMember_instance() {
707 indexTestUnit(r'''
708 class A {
709 int f;
710 int get result => f + 1;
711 }
712 main(A a) {
713 print(a.result);
714 }
715 ''');
716 _createRefactoring('result =>');
717 // validate change
718 return _assertSuccessfulRefactoring(r'''
719 class A {
720 int f;
721 }
722 main(A a) {
723 print(a.f + 1);
724 }
725 ''');
726 }
727
728 test_getter_classMember_static() {
729 indexTestUnit(r'''
730 class A {
731 static int get result => 1 + 2;
732 }
733 main() {
734 print(A.result);
735 }
736 ''');
737 _createRefactoring('result =>');
738 // validate change
739 return _assertSuccessfulRefactoring(r'''
740 class A {
741 }
742 main() {
743 print(1 + 2);
744 }
745 ''');
746 }
747
748 test_getter_topLevel() {
749 indexTestUnit(r'''
750 String get message => 'Hello, World!';
751 main() {
752 print(message);
753 }
754 ''');
755 _createRefactoring('message =>');
756 // validate change
757 return _assertSuccessfulRefactoring(r'''
758 main() {
759 print('Hello, World!');
760 }
761 ''');
762 }
763
697 test_initialMode_all() { 764 test_initialMode_all() {
698 indexTestUnit(r''' 765 indexTestUnit(r'''
699 test(a, b) { 766 test(a, b) {
700 return a + b; 767 return a + b;
701 } 768 }
702 main() { 769 main() {
703 var res = test(1, 2); 770 var res = test(1, 2);
704 } 771 }
705 '''); 772 ''');
706 _createRefactoring('test(a, b)'); 773 _createRefactoring('test(a, b)');
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 _createRefactoring('test({'); 991 _createRefactoring('test({');
925 // validate change 992 // validate change
926 return _assertSuccessfulRefactoring(r''' 993 return _assertSuccessfulRefactoring(r'''
927 main() { 994 main() {
928 print(10 + 20); 995 print(10 + 20);
929 print(10 + 20); 996 print(10 + 20);
930 } 997 }
931 '''); 998 ''');
932 } 999 }
933 1000
1001 test_reference_expressionBody() {
1002 indexTestUnit(r'''
1003 String message() => 'Hello, World!';
1004 main() {
1005 print(message);
1006 }
1007 ''');
1008 _createRefactoring('message()');
1009 // validate change
1010 return _assertSuccessfulRefactoring(r'''
1011 main() {
1012 print(() => 'Hello, World!');
1013 }
1014 ''');
1015 }
1016
934 test_reference_noStatement() { 1017 test_reference_noStatement() {
935 indexTestUnit(r''' 1018 indexTestUnit(r'''
936 test(a, b) { 1019 test(a, b) {
937 return a || b; 1020 return a || b;
938 } 1021 }
939 foo(p1, p2, p3) => p1 && test(p2, p3); 1022 foo(p1, p2, p3) => p1 && test(p2, p3);
940 bar() => { 1023 bar() => {
941 'name' : baz(test) 1024 'name' : baz(test)
942 }; 1025 };
943 baz(x) {} 1026 baz(x) {}
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
992 return _assertSuccessfulRefactoring(r''' 1075 return _assertSuccessfulRefactoring(r'''
993 main() { 1076 main() {
994 print((a, b) { 1077 print((a, b) {
995 print(a); 1078 print(a);
996 print(b); 1079 print(b);
997 }); 1080 });
998 } 1081 }
999 '''); 1082 ''');
1000 } 1083 }
1001 1084
1002 test_reference_expressionBody() { 1085 test_setter_classMember_instance() {
1003 indexTestUnit(r''' 1086 indexTestUnit(r'''
1004 String message() => 'Hello, World!'; 1087 class A {
1005 main() { 1088 int f;
1006 print(message); 1089 void set result(x) {
1090 f = x + 1;
1091 }
1092 }
1093 main(A a) {
1094 a.result = 5;
1007 } 1095 }
1008 '''); 1096 ''');
1009 _createRefactoring('message()'); 1097 _createRefactoring('result(x)');
1098 // validate change
1099 return _assertSuccessfulRefactoring(r'''
1100 class A {
1101 int f;
1102 }
1103 main(A a) {
1104 a.f = 5 + 1;
1105 }
1106 ''');
1107 }
1108
1109 test_setter_topLevel() {
1110 indexTestUnit(r'''
1111 void set result(x) {
1112 print(x + 1);
1113 }
1114 main() {
1115 result = 5;
1116 }
1117 ''');
1118 _createRefactoring('result(x)');
1010 // validate change 1119 // validate change
1011 return _assertSuccessfulRefactoring(r''' 1120 return _assertSuccessfulRefactoring(r'''
1012 main() { 1121 main() {
1013 print(() => 'Hello, World!'); 1122 print(5 + 1);
1014 } 1123 }
1015 '''); 1124 ''');
1016 } 1125 }
1017 1126
1018 test_singleExpression_oneUsage() { 1127 test_singleExpression_oneUsage() {
1019 indexTestUnit(r''' 1128 indexTestUnit(r'''
1020 test(a, b) { 1129 test(a, b) {
1021 return a + b; 1130 return a + b;
1022 } 1131 }
1023 main() { 1132 main() {
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
1167 1276
1168 Future _assertConditionsFatal(String message) { 1277 Future _assertConditionsFatal(String message) {
1169 return refactoring.checkAllConditions().then((status) { 1278 return refactoring.checkAllConditions().then((status) {
1170 assertRefactoringStatus( 1279 assertRefactoringStatus(
1171 status, 1280 status,
1172 RefactoringProblemSeverity.FATAL, 1281 RefactoringProblemSeverity.FATAL,
1173 expectedMessage: message); 1282 expectedMessage: message);
1174 }); 1283 });
1175 } 1284 }
1176 1285
1286 Future _assertInvalidSelection() {
1287 return _assertConditionsFatal(
1288 'Method declaration or reference must be selected to activate this refac toring.');
1289 }
1290
1177 Future _assertSuccessfulRefactoring(String expectedCode) { 1291 Future _assertSuccessfulRefactoring(String expectedCode) {
1178 return refactoring.checkInitialConditions().then((status) { 1292 return refactoring.checkInitialConditions().then((status) {
1179 assertRefactoringStatusOK(status); 1293 assertRefactoringStatusOK(status);
1180 if (deleteSource != null) { 1294 if (deleteSource != null) {
1181 refactoring.deleteSource = deleteSource; 1295 refactoring.deleteSource = deleteSource;
1182 } 1296 }
1183 if (inlineAll != null) { 1297 if (inlineAll != null) {
1184 refactoring.inlineAll = inlineAll; 1298 refactoring.inlineAll = inlineAll;
1185 } 1299 }
1186 return refactoring.checkFinalConditions().then((status) { 1300 return refactoring.checkFinalConditions().then((status) {
1187 assertRefactoringStatusOK(status); 1301 assertRefactoringStatusOK(status);
1188 return refactoring.createChange().then((SourceChange change) { 1302 return refactoring.createChange().then((SourceChange change) {
1189 this.refactoringChange = change; 1303 this.refactoringChange = change;
1190 assertTestChangeResult(expectedCode); 1304 assertTestChangeResult(expectedCode);
1191 }); 1305 });
1192 }); 1306 });
1193 }); 1307 });
1194 } 1308 }
1195 1309
1196 void _createRefactoring(String search) { 1310 void _createRefactoring(String search) {
1197 int offset = findOffset(search); 1311 int offset = findOffset(search);
1198 refactoring = new InlineMethodRefactoring(searchEngine, testUnit, offset); 1312 refactoring = new InlineMethodRefactoring(searchEngine, testUnit, offset);
1199 } 1313 }
1200 } 1314 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698