| 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.correction.fix; | 5 library test.services.correction.fix; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart' hide AnalysisError; | 7 import 'package:analysis_server/src/protocol.dart' hide AnalysisError; |
| 8 import 'package:analysis_server/src/services/correction/fix.dart'; | 8 import 'package:analysis_server/src/services/correction/fix.dart'; |
| 9 import 'package:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
| 10 import 'package:analyzer/source/package_map_resolver.dart'; | 10 import 'package:analyzer/source/package_map_resolver.dart'; |
| 11 import 'package:analyzer/src/generated/error.dart'; | 11 import 'package:analyzer/src/generated/error.dart'; |
| 12 import 'package:analyzer/src/generated/parser.dart'; | 12 import 'package:analyzer/src/generated/parser.dart'; |
| 13 import 'package:analyzer/src/generated/source.dart'; | 13 import 'package:analyzer/src/generated/source.dart'; |
| 14 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
| 15 | 15 |
| 16 import '../../abstract_context.dart'; | 16 import '../../abstract_context.dart'; |
| 17 import '../../abstract_single_unit.dart'; | 17 import '../../abstract_single_unit.dart'; |
| 18 import '../../reflective_tests.dart'; | 18 import '../../reflective_tests.dart'; |
| 19 | 19 |
| 20 main() { | 20 main() { |
| 21 groupSep = ' | '; | 21 groupSep = ' | '; |
| 22 runReflectiveTests(FixProcessorTest); | 22 runReflectiveTests(FixProcessorTest); |
| 23 } | 23 } |
| 24 | 24 |
| 25 typedef bool AnalysisErrorFilter(AnalysisError error); | 25 typedef bool AnalysisErrorFilter(AnalysisError error); |
| 26 | 26 |
| 27 @reflectiveTest | 27 @reflectiveTest |
| 28 class FixProcessorTest extends AbstractSingleUnitTest { | 28 class FixProcessorTest extends AbstractSingleUnitTest { |
| 29 AnalysisErrorFilter errorFilter = null; | 29 AnalysisErrorFilter errorFilter = (AnalysisError error) { |
| 30 return error.errorCode != HintCode.UNUSED_CATCH_CLAUSE && |
| 31 error.errorCode != HintCode.UNUSED_CATCH_STACK && |
| 32 error.errorCode != HintCode.UNUSED_ELEMENT && |
| 33 error.errorCode != HintCode.UNUSED_FIELD && |
| 34 error.errorCode != HintCode.UNUSED_LOCAL_VARIABLE; |
| 35 }; |
| 30 | 36 |
| 31 Fix fix; | 37 Fix fix; |
| 32 SourceChange change; | 38 SourceChange change; |
| 33 String resultCode; | 39 String resultCode; |
| 34 | 40 |
| 35 void assert_undefinedFunction_create_returnType_bool(String lineWithTest) { | 41 void assert_undefinedFunction_create_returnType_bool(String lineWithTest) { |
| 36 resolveTestUnit(''' | 42 resolveTestUnit(''' |
| 37 main() { | 43 main() { |
| 38 bool b = true; | 44 bool b = true; |
| 39 $lineWithTest | 45 $lineWithTest |
| (...skipping 2392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2432 '''); | 2438 '''); |
| 2433 assertHasFix(FixKind.REMOVE_UNNECASSARY_CAST, ''' | 2439 assertHasFix(FixKind.REMOVE_UNNECASSARY_CAST, ''' |
| 2434 main(Object p) { | 2440 main(Object p) { |
| 2435 if (p is String) { | 2441 if (p is String) { |
| 2436 String v = p; | 2442 String v = p; |
| 2437 } | 2443 } |
| 2438 } | 2444 } |
| 2439 '''); | 2445 '''); |
| 2440 } | 2446 } |
| 2441 | 2447 |
| 2448 void test_removeUnusedCatchClause() { |
| 2449 errorFilter = (AnalysisError error) => true; |
| 2450 resolveTestUnit(''' |
| 2451 main() { |
| 2452 try { |
| 2453 throw 42; |
| 2454 } on int catch (e) { |
| 2455 } |
| 2456 } |
| 2457 '''); |
| 2458 assertHasFix(FixKind.REMOVE_UNUSED_CATCH_CLAUSE, ''' |
| 2459 main() { |
| 2460 try { |
| 2461 throw 42; |
| 2462 } on int { |
| 2463 } |
| 2464 } |
| 2465 '''); |
| 2466 } |
| 2467 |
| 2468 void test_removeUnusedCatchStack() { |
| 2469 errorFilter = (AnalysisError error) => true; |
| 2470 resolveTestUnit(''' |
| 2471 main() { |
| 2472 try { |
| 2473 throw 42; |
| 2474 } catch (e, stack) { |
| 2475 } |
| 2476 } |
| 2477 '''); |
| 2478 assertHasFix(FixKind.REMOVE_UNUSED_CATCH_STACK, ''' |
| 2479 main() { |
| 2480 try { |
| 2481 throw 42; |
| 2482 } catch (e) { |
| 2483 } |
| 2484 } |
| 2485 '''); |
| 2486 } |
| 2487 |
| 2442 void test_removeUnusedImport() { | 2488 void test_removeUnusedImport() { |
| 2443 resolveTestUnit(''' | 2489 resolveTestUnit(''' |
| 2444 import 'dart:math'; | 2490 import 'dart:math'; |
| 2445 main() { | 2491 main() { |
| 2446 } | 2492 } |
| 2447 '''); | 2493 '''); |
| 2448 assertHasFix(FixKind.REMOVE_UNUSED_IMPORT, ''' | 2494 assertHasFix(FixKind.REMOVE_UNUSED_IMPORT, ''' |
| 2449 main() { | 2495 main() { |
| 2450 } | 2496 } |
| 2451 '''); | 2497 '''); |
| (...skipping 981 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3433 UriResolver pkgResolver = | 3479 UriResolver pkgResolver = |
| 3434 new PackageMapUriResolver(provider, {'my_pkg': [myPkgFolder]}); | 3480 new PackageMapUriResolver(provider, {'my_pkg': [myPkgFolder]}); |
| 3435 context.sourceFactory = new SourceFactory( | 3481 context.sourceFactory = new SourceFactory( |
| 3436 [AbstractContextTest.SDK_RESOLVER, resourceResolver, pkgResolver]); | 3482 [AbstractContextTest.SDK_RESOLVER, resourceResolver, pkgResolver]); |
| 3437 // force 'my_pkg' resolution | 3483 // force 'my_pkg' resolution |
| 3438 addSource('/tmp/other.dart', "import 'package:my_pkg/my_lib.dart';"); | 3484 addSource('/tmp/other.dart', "import 'package:my_pkg/my_lib.dart';"); |
| 3439 } | 3485 } |
| 3440 | 3486 |
| 3441 AnalysisError _findErrorToFix() { | 3487 AnalysisError _findErrorToFix() { |
| 3442 List<AnalysisError> errors = context.computeErrors(testSource); | 3488 List<AnalysisError> errors = context.computeErrors(testSource); |
| 3443 errors.removeWhere((error) { | |
| 3444 return error.errorCode == HintCode.UNUSED_CATCH_CLAUSE || | |
| 3445 error.errorCode == HintCode.UNUSED_CATCH_STACK || | |
| 3446 error.errorCode == HintCode.UNUSED_ELEMENT || | |
| 3447 error.errorCode == HintCode.UNUSED_FIELD || | |
| 3448 error.errorCode == HintCode.UNUSED_LOCAL_VARIABLE; | |
| 3449 }); | |
| 3450 if (errorFilter != null) { | 3489 if (errorFilter != null) { |
| 3451 errors = errors.where(errorFilter).toList(); | 3490 errors = errors.where(errorFilter).toList(); |
| 3452 } | 3491 } |
| 3453 expect(errors, hasLength(1)); | 3492 expect(errors, hasLength(1)); |
| 3454 return errors[0]; | 3493 return errors[0]; |
| 3455 } | 3494 } |
| 3456 | 3495 |
| 3457 List<Position> _findResultPositions(List<String> searchStrings) { | 3496 List<Position> _findResultPositions(List<String> searchStrings) { |
| 3458 List<Position> positions = <Position>[]; | 3497 List<Position> positions = <Position>[]; |
| 3459 for (String search in searchStrings) { | 3498 for (String search in searchStrings) { |
| 3460 int offset = resultCode.indexOf(search); | 3499 int offset = resultCode.indexOf(search); |
| 3461 positions.add(new Position(testFile, offset)); | 3500 positions.add(new Position(testFile, offset)); |
| 3462 } | 3501 } |
| 3463 return positions; | 3502 return positions; |
| 3464 } | 3503 } |
| 3465 } | 3504 } |
| OLD | NEW |