| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 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. |
| 7 |
| 8 library test.services.refactoring.rename; |
| 9 |
| 10 import 'dart:async'; |
| 11 |
| 12 import 'package:analysis_services/correction/change.dart'; |
| 13 import 'package:analysis_services/correction/status.dart'; |
| 14 import 'package:analysis_services/index/index.dart'; |
| 15 import 'package:analysis_services/index/local_memory_index.dart'; |
| 16 import 'package:analysis_services/refactoring/refactoring.dart'; |
| 17 import 'package:analysis_services/src/search/search_engine.dart'; |
| 18 import 'package:analysis_testing/abstract_single_unit.dart'; |
| 19 import 'package:analyzer/file_system/file_system.dart'; |
| 20 import 'package:analyzer/src/generated/ast.dart'; |
| 21 import 'package:analyzer/src/generated/element.dart'; |
| 22 import 'package:analyzer/src/generated/source.dart'; |
| 23 import 'package:collection/collection.dart'; |
| 24 import 'package:unittest/unittest.dart'; |
| 25 |
| 26 |
| 27 /** |
| 28 * The base class for all [Refactoring] tests. |
| 29 */ |
| 30 abstract class RefactoringTest extends AbstractSingleUnitTest { |
| 31 Index index; |
| 32 SearchEngineImpl searchEngine; |
| 33 |
| 34 Change refactoringChange; |
| 35 |
| 36 Refactoring get refactoring; |
| 37 |
| 38 /** |
| 39 * Asserts that [status] has expected severity and message. |
| 40 */ |
| 41 void assertRefactoringStatus(RefactoringStatus status, |
| 42 RefactoringStatusSeverity expectedSeverity, {String expectedMessage, |
| 43 SourceRange expectedContextRange, |
| 44 String expectedContextSearch}) { |
| 45 expect(status.severity, expectedSeverity, reason: status.message); |
| 46 if (expectedSeverity != RefactoringStatusSeverity.OK) { |
| 47 RefactoringStatusEntry entry = status.entryWithHighestSeverity; |
| 48 expect(entry.severity, expectedSeverity); |
| 49 if (expectedMessage != null) { |
| 50 expect(entry.message, expectedMessage); |
| 51 } |
| 52 if (expectedContextRange != null) { |
| 53 expect(entry.context.range, expectedContextRange); |
| 54 } |
| 55 if (expectedContextSearch != null) { |
| 56 SourceRange contextRange = entry.context.range; |
| 57 int expectedOffset = findOffset(expectedContextSearch); |
| 58 int expectedLength = findIdentifierLength(expectedContextSearch); |
| 59 expect(contextRange.offset, expectedOffset); |
| 60 expect(contextRange.length, expectedLength); |
| 61 } |
| 62 } |
| 63 } |
| 64 |
| 65 /** |
| 66 * Asserts that [refactoring] status is OK. |
| 67 */ |
| 68 Future assertRefactoringStatusOK() { |
| 69 return refactoring.checkInitialConditions().then((status) { |
| 70 assertRefactoringStatus(status, RefactoringStatusSeverity.OK); |
| 71 return refactoring.checkFinalConditions().then((status) { |
| 72 assertRefactoringStatus(status, RefactoringStatusSeverity.OK); |
| 73 }); |
| 74 }); |
| 75 // assertRefactoringStatus( |
| 76 // refactoring.checkInitialConditions(), |
| 77 // RefactoringStatusSeverity.OK); |
| 78 // assertRefactoringStatus( |
| 79 // refactoring.checkFinalConditions(), |
| 80 // RefactoringStatusSeverity.OK); |
| 81 } |
| 82 |
| 83 void indexTestUnit(String code) { |
| 84 resolveTestUnit(code); |
| 85 index.indexUnit(context, testUnit); |
| 86 } |
| 87 |
| 88 void indexUnit(String file, String code) { |
| 89 Source source = addSource(file, code); |
| 90 CompilationUnit unit = resolveLibraryUnit(source); |
| 91 index.indexUnit(context, unit); |
| 92 } |
| 93 |
| 94 void setUp() { |
| 95 super.setUp(); |
| 96 index = createLocalMemoryIndex(); |
| 97 searchEngine = new SearchEngineImpl(index); |
| 98 } |
| 99 } |
| 100 |
| 101 |
| 102 /** |
| 103 * The base class for all [RenameRefactoring] tests. |
| 104 */ |
| 105 class RenameRefactoringTest extends RefactoringTest { |
| 106 RenameRefactoring refactoring; |
| 107 |
| 108 /** |
| 109 * Asserts that [refactoringChange] contains a [FileEdit] for the file |
| 110 * with the given [path], and it results the [expectedCode]. |
| 111 */ |
| 112 void assertFileChangeResult(String path, String expectedCode) { |
| 113 // prepare FileEdit |
| 114 FileEdit fileEdit = refactoringChange.getFileEdit(path); |
| 115 expect(fileEdit, isNotNull); |
| 116 // validate resulting code |
| 117 File file = provider.getResource(path); |
| 118 Source source = file.createSource(); |
| 119 String ini = context.getContents(source).data; |
| 120 String actualCode = _applyEdits(ini, fileEdit.edits); |
| 121 expect(actualCode, expectedCode); |
| 122 } |
| 123 |
| 124 /** |
| 125 * Checks that all conditions are OK and the result of applying the [Change] |
| 126 * to [testUnit] is [expectedCode]. |
| 127 */ |
| 128 Future assertSuccessfulRename(String expectedCode) { |
| 129 return assertRefactoringStatusOK().then((_) { |
| 130 return refactoring.createChange().then((Change refactoringChange) { |
| 131 this.refactoringChange = refactoringChange; |
| 132 assertTestChangeResult(expectedCode); |
| 133 }); |
| 134 }); |
| 135 } |
| 136 |
| 137 /** |
| 138 * Asserts that [refactoringChange] contains a [FileEdit] for [testFile], and |
| 139 * it results the [expectedCode]. |
| 140 */ |
| 141 void assertTestChangeResult(String expectedCode) { |
| 142 // prepare FileEdit |
| 143 FileEdit fileEdit = refactoringChange.getFileEdit(testFile); |
| 144 expect(fileEdit, isNotNull); |
| 145 // validate resulting code |
| 146 String actualCode = _applyEdits(testCode, fileEdit.edits); |
| 147 expect(actualCode, expectedCode); |
| 148 } |
| 149 |
| 150 /** |
| 151 * Creates a new [RenameRefactoring] in [refactoringC] for the [Element] of |
| 152 * the [SimpleIdentifier] at the given [search] pattern. |
| 153 */ |
| 154 void createRenameRefactoringAtString(String search) { |
| 155 SimpleIdentifier identifier = findIdentifier(search); |
| 156 Element element = identifier.bestElement; |
| 157 // TODO(scheglov) uncomment later |
| 158 // if (element instanceof PrefixElement) { |
| 159 // element = IndexContributor.getImportElement(identifier); |
| 160 // } |
| 161 refactoring = new RenameRefactoring(searchEngine, element); |
| 162 expect(refactoring, isNotNull); |
| 163 } |
| 164 |
| 165 // /** |
| 166 // * Asserts result of applying [change] to [testCode]. |
| 167 // */ |
| 168 // void assertTestChangeResult(Change change, String expected) |
| 169 // { |
| 170 // assertChangeResult(change, testSource, expected); |
| 171 // } |
| 172 // |
| 173 // /** |
| 174 // * Asserts result of applying [change] to [source]. |
| 175 // */ |
| 176 // void assertChangeResult(Change change, Source source, String expected) |
| 177 // { |
| 178 // SourceChange sourceChange = getSourceChange(compositeChange, source); |
| 179 // assertNotNull("No change for: " + source.toString(), sourceChange); |
| 180 // String sourceResult = getChangeResult(context, source, sourceChange); |
| 181 // assertEquals(expected, sourceResult); |
| 182 //// AnalysisContext context = getAnalysisContext(); |
| 183 //// assertChangeResult(context, compositeChange, source, expected); |
| 184 // } |
| 185 |
| 186 String _applyEdits(String code, List<Edit> edits) { |
| 187 // TODO(scheglov) extract and reuse in assists and fixes tests |
| 188 mergeSort(edits, compare: (a, b) => a.offset - b.offset); |
| 189 edits.reversed.forEach((Edit edit) { |
| 190 code = code.substring(0, edit.offset) + |
| 191 edit.replacement + |
| 192 code.substring(edit.end); |
| 193 }); |
| 194 return code; |
| 195 } |
| 196 } |
| 197 |
| 198 int findIdentifierLength(String search) { |
| 199 int length = 0; |
| 200 while (length < search.length) { |
| 201 int c = search.codeUnitAt(length); |
| 202 if (!(c >= 'a'.codeUnitAt(0) && c <= 'z'.codeUnitAt(0) || |
| 203 c >= 'A'.codeUnitAt(0) && c <= 'Z'.codeUnitAt(0) || |
| 204 c >= '0'.codeUnitAt(0) && c <= '9'.codeUnitAt(0))) { |
| 205 break; |
| 206 } |
| 207 length++; |
| 208 } |
| 209 return length; |
| 210 } |
| OLD | NEW |