| 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.assist; | 8 library test.services.correction.assist; |
| 9 | 9 |
| 10 import 'package:analysis_services/correction/assist.dart'; | 10 import 'package:analysis_services/correction/assist.dart'; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 class AssistProcessorTest extends AbstractSingleUnitTest { | 27 class AssistProcessorTest extends AbstractSingleUnitTest { |
| 28 Index index; | 28 Index index; |
| 29 SearchEngineImpl searchEngine; | 29 SearchEngineImpl searchEngine; |
| 30 | 30 |
| 31 int offset; | 31 int offset; |
| 32 int length; | 32 int length; |
| 33 | 33 |
| 34 Assist assist; | 34 Assist assist; |
| 35 Change change; | 35 Change change; |
| 36 String resultCode; | 36 String resultCode; |
| 37 LinkedPositionGroup linkedPositionGroup; | 37 LinkedEditGroup linkedPositionGroup; |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * Asserts that there is an [Assist] of the given [kind] at [offset] which | 40 * Asserts that there is an [Assist] of the given [kind] at [offset] which |
| 41 * produces the [expected] code when applied to [testCode]. | 41 * produces the [expected] code when applied to [testCode]. |
| 42 */ | 42 */ |
| 43 void assertHasAssist(AssistKind kind, String expected) { | 43 void assertHasAssist(AssistKind kind, String expected) { |
| 44 assist = _assertHasAssist(kind); | 44 assist = _assertHasAssist(kind); |
| 45 change = assist.change; | 45 change = assist.change; |
| 46 // apply to "file" | 46 // apply to "file" |
| 47 List<FileEdit> fileEdits = change.edits; | 47 List<FileEdit> fileEdits = change.edits; |
| 48 expect(fileEdits, hasLength(1)); | 48 expect(fileEdits, hasLength(1)); |
| 49 resultCode = _applyEdits(testCode, change.edits[0].edits); | 49 resultCode = _applyEdits(testCode, change.edits[0].edits); |
| 50 // verify | 50 // verify |
| 51 expect(resultCode, expected); | 51 expect(resultCode, expected); |
| 52 } | 52 } |
| 53 | 53 |
| 54 /** | 54 /** |
| 55 * Calls [assertHasAssist] at the offset of [offsetSearch] in [testCode]. | 55 * Calls [assertHasAssist] at the offset of [offsetSearch] in [testCode]. |
| 56 */ | 56 */ |
| 57 void assertHasAssistAt(String offsetSearch, AssistKind kind, | 57 void assertHasAssistAt(String offsetSearch, AssistKind kind, |
| 58 String expected) { | 58 String expected) { |
| 59 offset = findOffset(offsetSearch); | 59 offset = findOffset(offsetSearch); |
| 60 assertHasAssist(kind, expected); | 60 assertHasAssist(kind, expected); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void assertHasPositionGroup(String id, List<Position> expectedPositions) { | 63 void assertHasPositionGroup(String id, int expectedLength, |
| 64 List<LinkedPositionGroup> linkedPositionGroups = | 64 List<Position> expectedPositions) { |
| 65 change.linkedPositionGroups; | 65 List<LinkedEditGroup> linkedPositionGroups = change.linkedEditGroups; |
| 66 for (LinkedPositionGroup group in linkedPositionGroups) { | 66 for (LinkedEditGroup group in linkedPositionGroups) { |
| 67 if (group.id == id) { | 67 if (group.id == id) { |
| 68 expect(group.length, expectedLength); |
| 68 expect(group.positions, unorderedEquals(expectedPositions)); | 69 expect(group.positions, unorderedEquals(expectedPositions)); |
| 69 linkedPositionGroup = group; | 70 linkedPositionGroup = group; |
| 70 return; | 71 return; |
| 71 } | 72 } |
| 72 } | 73 } |
| 73 fail('No PositionGroup with id=$id found in $linkedPositionGroups'); | 74 fail('No PositionGroup with id=$id found in $linkedPositionGroups'); |
| 74 } | 75 } |
| 75 | 76 |
| 76 /** | 77 /** |
| 77 * Asserts that there is no [Assist] of the given [kind] at [offset]. | 78 * Asserts that there is no [Assist] of the given [kind] at [offset]. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 89 /** | 90 /** |
| 90 * Calls [assertNoAssist] at the offset of [offsetSearch] in [testCode]. | 91 * Calls [assertNoAssist] at the offset of [offsetSearch] in [testCode]. |
| 91 */ | 92 */ |
| 92 void assertNoAssistAt(String offsetSearch, AssistKind kind) { | 93 void assertNoAssistAt(String offsetSearch, AssistKind kind) { |
| 93 offset = findOffset(offsetSearch); | 94 offset = findOffset(offsetSearch); |
| 94 assertNoAssist(kind); | 95 assertNoAssist(kind); |
| 95 } | 96 } |
| 96 | 97 |
| 97 Position expectedPosition(String search) { | 98 Position expectedPosition(String search) { |
| 98 int offset = resultCode.indexOf(search); | 99 int offset = resultCode.indexOf(search); |
| 99 int length = getLeadingIdentifierLength(search); | 100 return new Position(testFile, offset); |
| 100 return new Position(testFile, offset, length); | |
| 101 } | 101 } |
| 102 | 102 |
| 103 List<Position> expectedPositions(List<String> patterns) { | 103 List<Position> expectedPositions(List<String> patterns) { |
| 104 List<Position> positions = <Position>[]; | 104 List<Position> positions = <Position>[]; |
| 105 patterns.forEach((String search) { | 105 patterns.forEach((String search) { |
| 106 positions.add(expectedPosition(search)); | 106 positions.add(expectedPosition(search)); |
| 107 }); | 107 }); |
| 108 return positions; | 108 return positions; |
| 109 } | 109 } |
| 110 | 110 |
| 111 List<LinkedEditSuggestion> expectedSuggestions(LinkedEditSuggestionKind kind, |
| 112 List<String> values) { |
| 113 return values.map((value) { |
| 114 return new LinkedEditSuggestion(kind, value); |
| 115 }).toList(); |
| 116 } |
| 117 |
| 111 void setUp() { | 118 void setUp() { |
| 112 super.setUp(); | 119 super.setUp(); |
| 113 index = createLocalMemoryIndex(); | 120 index = createLocalMemoryIndex(); |
| 114 searchEngine = new SearchEngineImpl(index); | 121 searchEngine = new SearchEngineImpl(index); |
| 115 offset = 0; | 122 offset = 0; |
| 116 length = 0; | 123 length = 0; |
| 117 } | 124 } |
| 118 | 125 |
| 119 void test_addTypeAnnotation_classField_OK_final() { | 126 void test_addTypeAnnotation_classField_OK_final() { |
| 120 _indexTestUnit(''' | 127 _indexTestUnit(''' |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 } | 305 } |
| 299 List<int> readBytes() => <int>[]; | 306 List<int> readBytes() => <int>[]; |
| 300 '''); | 307 '''); |
| 301 assertHasAssistAt('readBytes();', AssistKind.ASSIGN_TO_LOCAL_VARIABLE, ''' | 308 assertHasAssistAt('readBytes();', AssistKind.ASSIGN_TO_LOCAL_VARIABLE, ''' |
| 302 main() { | 309 main() { |
| 303 List<int> bytes; | 310 List<int> bytes; |
| 304 var readBytes = readBytes(); | 311 var readBytes = readBytes(); |
| 305 } | 312 } |
| 306 List<int> readBytes() => <int>[]; | 313 List<int> readBytes() => <int>[]; |
| 307 '''); | 314 '''); |
| 308 assertHasPositionGroup('NAME', expectedPositions(['readBytes = '])); | 315 assertHasPositionGroup('NAME', 9, expectedPositions(['readBytes = '])); |
| 309 expect( | 316 expect( |
| 310 linkedPositionGroup.proposals, | 317 linkedPositionGroup.suggestions, |
| 311 unorderedEquals(['list', 'bytes2', 'readBytes'])); | 318 unorderedEquals( |
| 319 expectedSuggestions( |
| 320 LinkedEditSuggestionKind.VARIABLE, |
| 321 ['list', 'bytes2', 'readBytes']))); |
| 312 } | 322 } |
| 313 | 323 |
| 314 void test_assignToLocalVariable_alreadyAssignment() { | 324 void test_assignToLocalVariable_alreadyAssignment() { |
| 315 _indexTestUnit(''' | 325 _indexTestUnit(''' |
| 316 main() { | 326 main() { |
| 317 var vvv; | 327 var vvv; |
| 318 vvv = 42; | 328 vvv = 42; |
| 319 } | 329 } |
| 320 '''); | 330 '''); |
| 321 assertNoAssistAt('vvv =', AssistKind.ASSIGN_TO_LOCAL_VARIABLE); | 331 assertNoAssistAt('vvv =', AssistKind.ASSIGN_TO_LOCAL_VARIABLE); |
| (...skipping 1698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2020 for (Assist assist in assists) { | 2030 for (Assist assist in assists) { |
| 2021 if (assist.kind == kind) { | 2031 if (assist.kind == kind) { |
| 2022 return assist; | 2032 return assist; |
| 2023 } | 2033 } |
| 2024 } | 2034 } |
| 2025 throw fail('Expected to find assist $kind in\n${assists.join('\n')}'); | 2035 throw fail('Expected to find assist $kind in\n${assists.join('\n')}'); |
| 2026 } | 2036 } |
| 2027 | 2037 |
| 2028 void _assertHasLinkedPositions(String groupId, List<String> expectedStrings) { | 2038 void _assertHasLinkedPositions(String groupId, List<String> expectedStrings) { |
| 2029 List<Position> expectedPositions = _findResultPositions(expectedStrings); | 2039 List<Position> expectedPositions = _findResultPositions(expectedStrings); |
| 2030 List<LinkedPositionGroup> groups = change.linkedPositionGroups; | 2040 List<LinkedEditGroup> groups = change.linkedEditGroups; |
| 2031 for (LinkedPositionGroup group in groups) { | 2041 for (LinkedEditGroup group in groups) { |
| 2032 if (group.id == groupId) { | 2042 if (group.id == groupId) { |
| 2033 List<Position> actualPositions = group.positions; | 2043 List<Position> actualPositions = group.positions; |
| 2034 expect(actualPositions, unorderedEquals(expectedPositions)); | 2044 expect(actualPositions, unorderedEquals(expectedPositions)); |
| 2035 return; | 2045 return; |
| 2036 } | 2046 } |
| 2037 } | 2047 } |
| 2038 fail('No group with ID=$groupId foind in\n${groups.join('\n')}'); | 2048 fail('No group with ID=$groupId foind in\n${groups.join('\n')}'); |
| 2039 } | 2049 } |
| 2040 | 2050 |
| 2041 void _assertHasLinkedProposals(String groupId, List<String> expected) { | 2051 void _assertHasLinkedProposals(String groupId, List<String> expected) { |
| 2042 List<LinkedPositionGroup> groups = change.linkedPositionGroups; | 2052 List<LinkedEditGroup> groups = change.linkedEditGroups; |
| 2043 for (LinkedPositionGroup group in groups) { | 2053 for (LinkedEditGroup group in groups) { |
| 2044 if (group.id == groupId) { | 2054 if (group.id == groupId) { |
| 2045 expect(group.proposals, expected); | 2055 expect(group.suggestions, expected); |
| 2046 return; | 2056 return; |
| 2047 } | 2057 } |
| 2048 } | 2058 } |
| 2049 fail('No group with ID=$groupId foind in\n${groups.join('\n')}'); | 2059 fail('No group with ID=$groupId foind in\n${groups.join('\n')}'); |
| 2050 } | 2060 } |
| 2051 | 2061 |
| 2052 List<Position> _findResultPositions(List<String> searchStrings) { | 2062 List<Position> _findResultPositions(List<String> searchStrings) { |
| 2053 List<Position> positions = <Position>[]; | 2063 List<Position> positions = <Position>[]; |
| 2054 for (String search in searchStrings) { | 2064 for (String search in searchStrings) { |
| 2055 int offset = resultCode.indexOf(search); | 2065 int offset = resultCode.indexOf(search); |
| 2056 int length = getLeadingIdentifierLength(search); | 2066 positions.add(new Position(testFile, offset)); |
| 2057 positions.add(new Position(testFile, offset, length)); | |
| 2058 } | 2067 } |
| 2059 return positions; | 2068 return positions; |
| 2060 } | 2069 } |
| 2061 | 2070 |
| 2062 void _indexTestUnit(String code) { | 2071 void _indexTestUnit(String code) { |
| 2063 resolveTestUnit(code); | 2072 resolveTestUnit(code); |
| 2064 index.indexUnit(context, testUnit); | 2073 index.indexUnit(context, testUnit); |
| 2065 } | 2074 } |
| 2066 } | 2075 } |
| OLD | NEW |