| 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 // This code was auto-generated, is not intended to be edited, and is subject to | 5 // This code was auto-generated, is not intended to be edited, and is subject to |
| 6 // significant change. Please see the README file for more information. | 6 // significant change. Please see the README file for more information. |
| 7 | 7 |
| 8 library test.services.correction.fix; | 8 library test.services.correction.fix; |
| 9 | 9 |
| 10 import 'package:analysis_services/correction/change.dart'; | 10 import 'package:analysis_services/correction/change.dart'; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 void assertNoFix(FixKind kind) { | 60 void assertNoFix(FixKind kind) { |
| 61 AnalysisError error = _findErrorToFix(); | 61 AnalysisError error = _findErrorToFix(); |
| 62 List<Fix> fixes = computeFixes(searchEngine, testUnit, error); | 62 List<Fix> fixes = computeFixes(searchEngine, testUnit, error); |
| 63 for (Fix fix in fixes) { | 63 for (Fix fix in fixes) { |
| 64 if (fix.kind == kind) { | 64 if (fix.kind == kind) { |
| 65 throw fail('Unexpected fix $kind in\n${fixes.join('\n')}'); | 65 throw fail('Unexpected fix $kind in\n${fixes.join('\n')}'); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 void assert_undefinedFunction_create_returnType_bool(String lineWithTest) { |
| 71 _indexTestUnit(''' |
| 72 main() { |
| 73 bool b = true; |
| 74 $lineWithTest |
| 75 } |
| 76 '''); |
| 77 assertHasFix(FixKind.CREATE_FUNCTION, ''' |
| 78 main() { |
| 79 bool b = true; |
| 80 $lineWithTest |
| 81 } |
| 82 |
| 83 bool test() { |
| 84 } |
| 85 '''); |
| 86 } |
| 87 |
| 70 Position expectedPosition(String search) { | 88 Position expectedPosition(String search) { |
| 71 int offset = resultCode.indexOf(search); | 89 int offset = resultCode.indexOf(search); |
| 72 int length = getLeadingIdentifierLength(search); | 90 int length = getLeadingIdentifierLength(search); |
| 73 return new Position(testFile, offset, length); | 91 return new Position(testFile, offset, length); |
| 74 } | 92 } |
| 75 | 93 |
| 76 List<Position> expectedPositions(List<String> patterns) { | 94 List<Position> expectedPositions(List<String> patterns) { |
| 77 List<Position> positions = <Position>[]; | 95 List<Position> positions = <Position>[]; |
| 78 patterns.forEach((String search) { | 96 patterns.forEach((String search) { |
| 79 positions.add(expectedPosition(search)); | 97 positions.add(expectedPosition(search)); |
| (...skipping 915 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 995 const a = new A(); | 1013 const a = new A(); |
| 996 '''); | 1014 '''); |
| 997 assertHasFix(FixKind.USE_CONST, ''' | 1015 assertHasFix(FixKind.USE_CONST, ''' |
| 998 class A { | 1016 class A { |
| 999 const A(); | 1017 const A(); |
| 1000 } | 1018 } |
| 1001 const a = const A(); | 1019 const a = const A(); |
| 1002 '''); | 1020 '''); |
| 1003 } | 1021 } |
| 1004 | 1022 |
| 1023 void test_undefinedFunction_create_fromFunction() { |
| 1024 _indexTestUnit(''' |
| 1025 main() { |
| 1026 int v = myUndefinedFunction(1, 2.0, '3'); |
| 1027 } |
| 1028 '''); |
| 1029 assertHasFix(FixKind.CREATE_FUNCTION, ''' |
| 1030 main() { |
| 1031 int v = myUndefinedFunction(1, 2.0, '3'); |
| 1032 } |
| 1033 |
| 1034 int myUndefinedFunction(int i, double d, String s) { |
| 1035 } |
| 1036 '''); |
| 1037 } |
| 1038 |
| 1039 void test_undefinedFunction_create_fromMethod() { |
| 1040 _indexTestUnit(''' |
| 1041 class A { |
| 1042 main() { |
| 1043 int v = myUndefinedFunction(1, 2.0, '3'); |
| 1044 } |
| 1045 } |
| 1046 '''); |
| 1047 assertHasFix(FixKind.CREATE_FUNCTION, ''' |
| 1048 class A { |
| 1049 main() { |
| 1050 int v = myUndefinedFunction(1, 2.0, '3'); |
| 1051 } |
| 1052 } |
| 1053 |
| 1054 int myUndefinedFunction(int i, double d, String s) { |
| 1055 } |
| 1056 '''); |
| 1057 } |
| 1058 |
| 1059 void test_undefinedFunction_create_returnType_bool_expressions() { |
| 1060 assert_undefinedFunction_create_returnType_bool("!test();"); |
| 1061 assert_undefinedFunction_create_returnType_bool("b && test();"); |
| 1062 assert_undefinedFunction_create_returnType_bool("test() && b;"); |
| 1063 assert_undefinedFunction_create_returnType_bool("b || test();"); |
| 1064 assert_undefinedFunction_create_returnType_bool("test() || b;"); |
| 1065 } |
| 1066 |
| 1067 void test_undefinedFunction_create_returnType_bool_statements() { |
| 1068 assert_undefinedFunction_create_returnType_bool("assert ( test() );"); |
| 1069 assert_undefinedFunction_create_returnType_bool("if ( test() ) {}"); |
| 1070 assert_undefinedFunction_create_returnType_bool("while ( test() ) {}"); |
| 1071 assert_undefinedFunction_create_returnType_bool("do {} while ( test() );"); |
| 1072 } |
| 1073 |
| 1074 void test_undefinedFunction_create_returnType_fromAssignment_eq() { |
| 1075 _indexTestUnit(''' |
| 1076 main() { |
| 1077 int v; |
| 1078 v = myUndefinedFunction(); |
| 1079 } |
| 1080 '''); |
| 1081 assertHasFix(FixKind.CREATE_FUNCTION, ''' |
| 1082 main() { |
| 1083 int v; |
| 1084 v = myUndefinedFunction(); |
| 1085 } |
| 1086 |
| 1087 int myUndefinedFunction() { |
| 1088 } |
| 1089 '''); |
| 1090 } |
| 1091 |
| 1092 void test_undefinedFunction_create_returnType_fromAssignment_plusEq() { |
| 1093 _indexTestUnit(''' |
| 1094 main() { |
| 1095 int v; |
| 1096 v += myUndefinedFunction(); |
| 1097 } |
| 1098 '''); |
| 1099 assertHasFix(FixKind.CREATE_FUNCTION, ''' |
| 1100 main() { |
| 1101 int v; |
| 1102 v += myUndefinedFunction(); |
| 1103 } |
| 1104 |
| 1105 num myUndefinedFunction() { |
| 1106 } |
| 1107 '''); |
| 1108 } |
| 1109 |
| 1110 void test_undefinedFunction_create_returnType_fromBinary_right() { |
| 1111 _indexTestUnit(''' |
| 1112 main() { |
| 1113 0 + myUndefinedFunction(); |
| 1114 } |
| 1115 '''); |
| 1116 assertHasFix(FixKind.CREATE_FUNCTION, ''' |
| 1117 main() { |
| 1118 0 + myUndefinedFunction(); |
| 1119 } |
| 1120 |
| 1121 num myUndefinedFunction() { |
| 1122 } |
| 1123 '''); |
| 1124 } |
| 1125 |
| 1126 void test_undefinedFunction_create_returnType_fromInitializer() { |
| 1127 _indexTestUnit(''' |
| 1128 main() { |
| 1129 int v = myUndefinedFunction(); |
| 1130 } |
| 1131 '''); |
| 1132 assertHasFix(FixKind.CREATE_FUNCTION, ''' |
| 1133 main() { |
| 1134 int v = myUndefinedFunction(); |
| 1135 } |
| 1136 |
| 1137 int myUndefinedFunction() { |
| 1138 } |
| 1139 '''); |
| 1140 } |
| 1141 |
| 1142 void test_undefinedFunction_create_returnType_fromInvocationArgument() { |
| 1143 _indexTestUnit(''' |
| 1144 foo(int p) {} |
| 1145 main() { |
| 1146 foo( myUndefinedFunction() ); |
| 1147 } |
| 1148 '''); |
| 1149 assertHasFix(FixKind.CREATE_FUNCTION, ''' |
| 1150 foo(int p) {} |
| 1151 main() { |
| 1152 foo( myUndefinedFunction() ); |
| 1153 } |
| 1154 |
| 1155 int myUndefinedFunction() { |
| 1156 } |
| 1157 '''); |
| 1158 } |
| 1159 |
| 1160 void test_undefinedFunction_create_returnType_fromReturn() { |
| 1161 _indexTestUnit(''' |
| 1162 int main() { |
| 1163 return myUndefinedFunction(); |
| 1164 } |
| 1165 '''); |
| 1166 assertHasFix(FixKind.CREATE_FUNCTION, ''' |
| 1167 int main() { |
| 1168 return myUndefinedFunction(); |
| 1169 } |
| 1170 |
| 1171 int myUndefinedFunction() { |
| 1172 } |
| 1173 '''); |
| 1174 } |
| 1175 |
| 1176 void test_undefinedFunction_create_returnType_void() { |
| 1177 _indexTestUnit(''' |
| 1178 main() { |
| 1179 myUndefinedFunction(); |
| 1180 } |
| 1181 '''); |
| 1182 assertHasFix(FixKind.CREATE_FUNCTION, ''' |
| 1183 main() { |
| 1184 myUndefinedFunction(); |
| 1185 } |
| 1186 |
| 1187 void myUndefinedFunction() { |
| 1188 } |
| 1189 '''); |
| 1190 } |
| 1191 |
| 1005 void test_undefinedMethod_createQualified_fromClass() { | 1192 void test_undefinedMethod_createQualified_fromClass() { |
| 1006 _indexTestUnit(''' | 1193 _indexTestUnit(''' |
| 1007 class A { | 1194 class A { |
| 1008 } | 1195 } |
| 1009 main() { | 1196 main() { |
| 1010 A.myUndefinedMethod(); | 1197 A.myUndefinedMethod(); |
| 1011 } | 1198 } |
| 1012 '''); | 1199 '''); |
| 1013 assertHasFix(FixKind.CREATE_METHOD, ''' | 1200 assertHasFix(FixKind.CREATE_METHOD, ''' |
| 1014 class A { | 1201 class A { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1036 | 1223 |
| 1037 static void myUndefinedMethod() { | 1224 static void myUndefinedMethod() { |
| 1038 } | 1225 } |
| 1039 } | 1226 } |
| 1040 main() { | 1227 main() { |
| 1041 A.myUndefinedMethod(); | 1228 A.myUndefinedMethod(); |
| 1042 } | 1229 } |
| 1043 '''); | 1230 '''); |
| 1044 } | 1231 } |
| 1045 | 1232 |
| 1046 void test_undefinedMethod_createQualified_fromClass_unresolved() { | 1233 void test_undefinedMethod_createQualified_fromInstance() { |
| 1234 _indexTestUnit(''' |
| 1235 class A { |
| 1236 } |
| 1237 main(A a) { |
| 1238 a.myUndefinedMethod(); |
| 1239 } |
| 1240 '''); |
| 1241 assertHasFix(FixKind.CREATE_METHOD, ''' |
| 1242 class A { |
| 1243 void myUndefinedMethod() { |
| 1244 } |
| 1245 } |
| 1246 main(A a) { |
| 1247 a.myUndefinedMethod(); |
| 1248 } |
| 1249 '''); |
| 1250 } |
| 1251 |
| 1252 void test_undefinedMethod_createQualified_targetIsFunctionType() { |
| 1253 _indexTestUnit(''' |
| 1254 typedef A(); |
| 1255 main() { |
| 1256 A.myUndefinedMethod(); |
| 1257 } |
| 1258 '''); |
| 1259 assertNoFix(FixKind.CREATE_METHOD); |
| 1260 } |
| 1261 |
| 1262 void test_undefinedMethod_createQualified_targetIsUnresolved() { |
| 1047 _indexTestUnit(''' | 1263 _indexTestUnit(''' |
| 1048 main() { | 1264 main() { |
| 1049 NoSuchClass.myUndefinedMethod(); | 1265 NoSuchClass.myUndefinedMethod(); |
| 1050 } | 1266 } |
| 1051 '''); | 1267 '''); |
| 1052 assertNoFix(FixKind.CREATE_METHOD); | 1268 assertNoFix(FixKind.CREATE_METHOD); |
| 1053 } | 1269 } |
| 1054 | 1270 |
| 1271 void test_undefinedMethod_createUnqualified_parameters() { |
| 1272 _indexTestUnit(''' |
| 1273 class A { |
| 1274 main() { |
| 1275 myUndefinedMethod(0, 1.0, '3'); |
| 1276 } |
| 1277 } |
| 1278 '''); |
| 1279 assertHasFix(FixKind.CREATE_METHOD, ''' |
| 1280 class A { |
| 1281 main() { |
| 1282 myUndefinedMethod(0, 1.0, '3'); |
| 1283 } |
| 1284 |
| 1285 void myUndefinedMethod(int i, double d, String s) { |
| 1286 } |
| 1287 } |
| 1288 '''); |
| 1289 // linked positions |
| 1290 _assertHasLinkedPositions( |
| 1291 'NAME', |
| 1292 ['myUndefinedMethod(0', 'myUndefinedMethod(int']); |
| 1293 _assertHasLinkedPositions('RETURN_TYPE', ['void myUndefinedMethod(']); |
| 1294 _assertHasLinkedPositions('TYPE0', ['int i']); |
| 1295 _assertHasLinkedPositions('TYPE1', ['double d']); |
| 1296 _assertHasLinkedPositions('TYPE2', ['String s']); |
| 1297 _assertHasLinkedPositions('ARG0', ['i,']); |
| 1298 _assertHasLinkedPositions('ARG1', ['d,']); |
| 1299 _assertHasLinkedPositions('ARG2', ['s)']); |
| 1300 // linked proposals |
| 1301 _assertHasLinkedProposals('TYPE0', ['int', 'num', 'Object', 'Comparable']); |
| 1302 _assertHasLinkedProposals( |
| 1303 'TYPE1', |
| 1304 ['double', 'num', 'Object', 'Comparable']); |
| 1305 _assertHasLinkedProposals('TYPE2', ['String', 'Object', 'Comparable']); |
| 1306 } |
| 1307 |
| 1308 void test_undefinedMethod_createUnqualified_returnType() { |
| 1309 _indexTestUnit(''' |
| 1310 class A { |
| 1311 main() { |
| 1312 int v = myUndefinedMethod(); |
| 1313 } |
| 1314 } |
| 1315 '''); |
| 1316 assertHasFix(FixKind.CREATE_METHOD, ''' |
| 1317 class A { |
| 1318 main() { |
| 1319 int v = myUndefinedMethod(); |
| 1320 } |
| 1321 |
| 1322 int myUndefinedMethod() { |
| 1323 } |
| 1324 } |
| 1325 '''); |
| 1326 // linked positions |
| 1327 _assertHasLinkedPositions( |
| 1328 'NAME', |
| 1329 ['myUndefinedMethod();', 'myUndefinedMethod() {']); |
| 1330 _assertHasLinkedPositions('RETURN_TYPE', ['int myUndefinedMethod(']); |
| 1331 } |
| 1332 |
| 1333 void test_undefinedMethod_createUnqualified_staticFromField() { |
| 1334 _indexTestUnit(''' |
| 1335 class A { |
| 1336 static var f = myUndefinedMethod(); |
| 1337 } |
| 1338 '''); |
| 1339 assertHasFix(FixKind.CREATE_METHOD, ''' |
| 1340 class A { |
| 1341 static var f = myUndefinedMethod(); |
| 1342 |
| 1343 static myUndefinedMethod() { |
| 1344 } |
| 1345 } |
| 1346 '''); |
| 1347 } |
| 1348 |
| 1349 void test_undefinedMethod_createUnqualified_staticFromMethod() { |
| 1350 _indexTestUnit(''' |
| 1351 class A { |
| 1352 static main() { |
| 1353 myUndefinedMethod(); |
| 1354 } |
| 1355 } |
| 1356 '''); |
| 1357 assertHasFix(FixKind.CREATE_METHOD, ''' |
| 1358 class A { |
| 1359 static main() { |
| 1360 myUndefinedMethod(); |
| 1361 } |
| 1362 |
| 1363 static void myUndefinedMethod() { |
| 1364 } |
| 1365 } |
| 1366 '''); |
| 1367 } |
| 1368 |
| 1369 void test_undefinedMethod_hint_createQualified_fromInstance() { |
| 1370 _indexTestUnit(''' |
| 1371 class A { |
| 1372 } |
| 1373 main() { |
| 1374 var a = new A(); |
| 1375 a.myUndefinedMethod(); |
| 1376 } |
| 1377 '''); |
| 1378 assertHasFix(FixKind.CREATE_METHOD, ''' |
| 1379 class A { |
| 1380 void myUndefinedMethod() { |
| 1381 } |
| 1382 } |
| 1383 main() { |
| 1384 var a = new A(); |
| 1385 a.myUndefinedMethod(); |
| 1386 } |
| 1387 '''); |
| 1388 } |
| 1389 |
| 1055 void test_useEffectiveIntegerDivision() { | 1390 void test_useEffectiveIntegerDivision() { |
| 1056 _indexTestUnit(''' | 1391 _indexTestUnit(''' |
| 1057 main() { | 1392 main() { |
| 1058 var a = 5; | 1393 var a = 5; |
| 1059 var b = 2; | 1394 var b = 2; |
| 1060 print((a / b).toInt()); | 1395 print((a / b).toInt()); |
| 1061 } | 1396 } |
| 1062 '''); | 1397 '''); |
| 1063 assertHasFix(FixKind.USE_EFFECTIVE_INTEGER_DIVISION, ''' | 1398 assertHasFix(FixKind.USE_EFFECTIVE_INTEGER_DIVISION, ''' |
| 1064 main() { | 1399 main() { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1085 Fix _assertHasFix(FixKind kind, AnalysisError error) { | 1420 Fix _assertHasFix(FixKind kind, AnalysisError error) { |
| 1086 List<Fix> fixes = computeFixes(searchEngine, testUnit, error); | 1421 List<Fix> fixes = computeFixes(searchEngine, testUnit, error); |
| 1087 for (Fix fix in fixes) { | 1422 for (Fix fix in fixes) { |
| 1088 if (fix.kind == kind) { | 1423 if (fix.kind == kind) { |
| 1089 return fix; | 1424 return fix; |
| 1090 } | 1425 } |
| 1091 } | 1426 } |
| 1092 throw fail('Expected to find fix $kind in\n${fixes.join('\n')}'); | 1427 throw fail('Expected to find fix $kind in\n${fixes.join('\n')}'); |
| 1093 } | 1428 } |
| 1094 | 1429 |
| 1430 void _assertHasLinkedPositions(String groupId, List<String> expectedStrings) { |
| 1431 List<Position> expectedPositions = _findResultPositions(expectedStrings); |
| 1432 List<LinkedPositionGroup> groups = change.linkedPositionGroups; |
| 1433 for (LinkedPositionGroup group in groups) { |
| 1434 if (group.id == groupId) { |
| 1435 List<Position> actualPositions = group.positions; |
| 1436 expect(actualPositions, unorderedEquals(expectedPositions)); |
| 1437 return; |
| 1438 } |
| 1439 } |
| 1440 fail('No group with ID=$groupId foind in\n${groups.join('\n')}'); |
| 1441 } |
| 1442 |
| 1443 void _assertHasLinkedProposals(String groupId, List<String> expected) { |
| 1444 List<LinkedPositionGroup> groups = change.linkedPositionGroups; |
| 1445 for (LinkedPositionGroup group in groups) { |
| 1446 if (group.id == groupId) { |
| 1447 expect(group.proposals, expected); |
| 1448 return; |
| 1449 } |
| 1450 } |
| 1451 fail('No group with ID=$groupId foind in\n${groups.join('\n')}'); |
| 1452 } |
| 1453 |
| 1095 AnalysisError _findErrorToFix() { | 1454 AnalysisError _findErrorToFix() { |
| 1096 List<AnalysisError> errors = context.computeErrors(testSource); | 1455 List<AnalysisError> errors = context.computeErrors(testSource); |
| 1097 expect( | 1456 expect( |
| 1098 errors, | 1457 errors, |
| 1099 hasLength(1), | 1458 hasLength(1), |
| 1100 reason: 'Exactly 1 error expected, but ${errors.length} found:\n' + | 1459 reason: 'Exactly 1 error expected, but ${errors.length} found:\n' + |
| 1101 errors.join('\n')); | 1460 errors.join('\n')); |
| 1102 return errors[0]; | 1461 return errors[0]; |
| 1103 } | 1462 } |
| 1104 | 1463 |
| 1464 List<Position> _findResultPositions(List<String> searchStrings) { |
| 1465 List<Position> positions = <Position>[]; |
| 1466 for (String search in searchStrings) { |
| 1467 int offset = resultCode.indexOf(search); |
| 1468 int length = getLeadingIdentifierLength(search); |
| 1469 positions.add(new Position(testFile, offset, length)); |
| 1470 } |
| 1471 return positions; |
| 1472 } |
| 1473 |
| 1105 void _indexTestUnit(String code) { | 1474 void _indexTestUnit(String code) { |
| 1106 resolveTestUnit(code); | 1475 resolveTestUnit(code); |
| 1107 index.indexUnit(context, testUnit); | 1476 index.indexUnit(context, testUnit); |
| 1108 } | 1477 } |
| 1109 } | 1478 } |
| OLD | NEW |