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_services/correction/change.dart'; | 7 import 'package:analysis_services/correction/change.dart'; |
8 import 'package:analysis_services/correction/fix.dart'; | 8 import 'package:analysis_services/correction/fix.dart'; |
9 import 'package:analysis_services/index/index.dart'; | 9 import 'package:analysis_services/index/index.dart'; |
10 import 'package:analysis_services/index/local_memory_index.dart'; | 10 import 'package:analysis_services/index/local_memory_index.dart'; |
(...skipping 23 matching lines...) Expand all Loading... |
34 Change change; | 34 Change change; |
35 String resultCode; | 35 String resultCode; |
36 | 36 |
37 void assertHasFix(FixKind kind, String expected) { | 37 void assertHasFix(FixKind kind, String expected) { |
38 AnalysisError error = _findErrorToFix(); | 38 AnalysisError error = _findErrorToFix(); |
39 fix = _assertHasFix(kind, error); | 39 fix = _assertHasFix(kind, error); |
40 change = fix.change; | 40 change = fix.change; |
41 // apply to "file" | 41 // apply to "file" |
42 List<FileEdit> fileEdits = change.fileEdits; | 42 List<FileEdit> fileEdits = change.fileEdits; |
43 expect(fileEdits, hasLength(1)); | 43 expect(fileEdits, hasLength(1)); |
44 resultCode = _applyEdits(testCode, change.fileEdits[0].edits); | 44 // TODO(paulberry): should the code under test be responsible for sorting |
| 45 // the edits? |
| 46 resultCode = Edit.applySorted(testCode, change.fileEdits[0].edits); |
45 // verify | 47 // verify |
46 expect(resultCode, expected); | 48 expect(resultCode, expected); |
47 } | 49 } |
48 | 50 |
49 void assertHasPositionGroup(String id, List<Position> expectedPositions) { | 51 void assertHasPositionGroup(String id, List<Position> expectedPositions) { |
50 List<LinkedEditGroup> linkedPositionGroups = change.linkedEditGroups; | 52 List<LinkedEditGroup> linkedPositionGroups = change.linkedEditGroups; |
51 for (LinkedEditGroup group in linkedPositionGroups) { | 53 for (LinkedEditGroup group in linkedPositionGroups) { |
52 if (group.id == id) { | 54 if (group.id == id) { |
53 expect(group.positions, unorderedEquals(expectedPositions)); | 55 expect(group.positions, unorderedEquals(expectedPositions)); |
54 return; | 56 return; |
(...skipping 1737 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1792 '''); | 1794 '''); |
1793 assertHasFix(FixKind.USE_EFFECTIVE_INTEGER_DIVISION, ''' | 1795 assertHasFix(FixKind.USE_EFFECTIVE_INTEGER_DIVISION, ''' |
1794 main() { | 1796 main() { |
1795 var a = 5; | 1797 var a = 5; |
1796 var b = 2; | 1798 var b = 2; |
1797 print(a ~/ b); | 1799 print(a ~/ b); |
1798 } | 1800 } |
1799 '''); | 1801 '''); |
1800 } | 1802 } |
1801 | 1803 |
1802 String _applyEdits(String code, List<Edit> edits) { | |
1803 edits.sort((a, b) => b.offset - a.offset); | |
1804 edits.forEach((Edit edit) { | |
1805 code = code.substring(0, edit.offset) + | |
1806 edit.replacement + | |
1807 code.substring(edit.end); | |
1808 }); | |
1809 return code; | |
1810 } | |
1811 | |
1812 /** | 1804 /** |
1813 * Computes fixes and verifies that there is a fix of the given kind. | 1805 * Computes fixes and verifies that there is a fix of the given kind. |
1814 */ | 1806 */ |
1815 Fix _assertHasFix(FixKind kind, AnalysisError error) { | 1807 Fix _assertHasFix(FixKind kind, AnalysisError error) { |
1816 List<Fix> fixes = computeFixes(searchEngine, testUnit, error); | 1808 List<Fix> fixes = computeFixes(searchEngine, testUnit, error); |
1817 for (Fix fix in fixes) { | 1809 for (Fix fix in fixes) { |
1818 if (fix.kind == kind) { | 1810 if (fix.kind == kind) { |
1819 return fix; | 1811 return fix; |
1820 } | 1812 } |
1821 } | 1813 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1881 positions.add(new Position(testFile, offset)); | 1873 positions.add(new Position(testFile, offset)); |
1882 } | 1874 } |
1883 return positions; | 1875 return positions; |
1884 } | 1876 } |
1885 | 1877 |
1886 void _indexTestUnit(String code) { | 1878 void _indexTestUnit(String code) { |
1887 resolveTestUnit(code); | 1879 resolveTestUnit(code); |
1888 index.indexUnit(context, testUnit); | 1880 index.indexUnit(context, testUnit); |
1889 } | 1881 } |
1890 } | 1882 } |
OLD | NEW |