| 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.refactoring.rename; | 5 library test.services.refactoring.rename; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol2.dart' show SourceEdit; |
| 9 import 'package:analysis_server/src/services/correction/change.dart'; | 10 import 'package:analysis_server/src/services/correction/change.dart'; |
| 10 import 'package:analysis_server/src/services/correction/namespace.dart'; | 11 import 'package:analysis_server/src/services/correction/namespace.dart'; |
| 11 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 12 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 12 import 'package:analyzer/file_system/file_system.dart'; | 13 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/src/generated/ast.dart'; | 14 import 'package:analyzer/src/generated/ast.dart'; |
| 14 import 'package:analyzer/src/generated/element.dart'; | 15 import 'package:analyzer/src/generated/element.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:unittest/unittest.dart'; | 17 import 'package:unittest/unittest.dart'; |
| 17 | 18 |
| 18 import 'abstract_refactoring.dart'; | 19 import 'abstract_refactoring.dart'; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 29 * with the given [path], and it results the [expectedCode]. | 30 * with the given [path], and it results the [expectedCode]. |
| 30 */ | 31 */ |
| 31 void assertFileChangeResult(String path, String expectedCode) { | 32 void assertFileChangeResult(String path, String expectedCode) { |
| 32 // prepare FileEdit | 33 // prepare FileEdit |
| 33 FileEdit fileEdit = refactoringChange.getFileEdit(path); | 34 FileEdit fileEdit = refactoringChange.getFileEdit(path); |
| 34 expect(fileEdit, isNotNull); | 35 expect(fileEdit, isNotNull); |
| 35 // validate resulting code | 36 // validate resulting code |
| 36 File file = provider.getResource(path); | 37 File file = provider.getResource(path); |
| 37 Source source = file.createSource(); | 38 Source source = file.createSource(); |
| 38 String ini = context.getContents(source).data; | 39 String ini = context.getContents(source).data; |
| 39 String actualCode = Edit.applySequence(ini, fileEdit.edits); | 40 String actualCode = applySequence(ini, fileEdit.edits); |
| 40 expect(actualCode, expectedCode); | 41 expect(actualCode, expectedCode); |
| 41 } | 42 } |
| 42 | 43 |
| 43 /** | 44 /** |
| 44 * Asserts that [refactoringChange] does not contain a [FileEdit] for the file | 45 * Asserts that [refactoringChange] does not contain a [FileEdit] for the file |
| 45 * with the given [path]. | 46 * with the given [path]. |
| 46 */ | 47 */ |
| 47 void assertNoFileChange(String path) { | 48 void assertNoFileChange(String path) { |
| 48 FileEdit fileEdit = refactoringChange.getFileEdit(path); | 49 FileEdit fileEdit = refactoringChange.getFileEdit(path); |
| 49 expect(fileEdit, isNull); | 50 expect(fileEdit, isNull); |
| 50 } | 51 } |
| 51 | 52 |
| 52 /** | 53 /** |
| 53 * Asserts that [refactoring] has potential edits in [testFile] at offset | 54 * Asserts that [refactoring] has potential edits in [testFile] at offset |
| 54 * of the given [searches]. | 55 * of the given [searches]. |
| 55 */ | 56 */ |
| 56 void assertPotentialEdits(List<String> searches) { | 57 void assertPotentialEdits(List<String> searches) { |
| 57 Set<int> expectedOffsets = new Set<int>(); | 58 Set<int> expectedOffsets = new Set<int>(); |
| 58 for (String search in searches) { | 59 for (String search in searches) { |
| 59 int offset = findOffset(search); | 60 int offset = findOffset(search); |
| 60 expectedOffsets.add(offset); | 61 expectedOffsets.add(offset); |
| 61 } | 62 } |
| 62 // remove offset marked as potential | 63 // remove offset marked as potential |
| 63 for (String potentialId in refactoring.potentialEditIds) { | 64 for (String potentialId in refactoring.potentialEditIds) { |
| 64 Edit edit = findEditById(potentialId); | 65 SourceEdit edit = findEditById(potentialId); |
| 65 expect(edit, isNotNull); | 66 expect(edit, isNotNull); |
| 66 expectedOffsets.remove(edit.offset); | 67 expectedOffsets.remove(edit.offset); |
| 67 } | 68 } |
| 68 // all potential offsets are marked as such | 69 // all potential offsets are marked as such |
| 69 expect(expectedOffsets, isEmpty); | 70 expect(expectedOffsets, isEmpty); |
| 70 } | 71 } |
| 71 | 72 |
| 72 /** | 73 /** |
| 73 * Checks that all conditions are OK and the result of applying the [Change] | 74 * Checks that all conditions are OK and the result of applying the [Change] |
| 74 * to [testUnit] is [expectedCode]. | 75 * to [testUnit] is [expectedCode]. |
| 75 */ | 76 */ |
| 76 Future assertSuccessfulRename(String expectedCode) { | 77 Future assertSuccessfulRename(String expectedCode) { |
| 77 return assertRefactoringConditionsOK().then((_) { | 78 return assertRefactoringConditionsOK().then((_) { |
| 78 return refactoring.createChange().then((Change refactoringChange) { | 79 return refactoring.createChange().then((Change refactoringChange) { |
| 79 this.refactoringChange = refactoringChange; | 80 this.refactoringChange = refactoringChange; |
| 80 assertTestChangeResult(expectedCode); | 81 assertTestChangeResult(expectedCode); |
| 81 }); | 82 }); |
| 82 }); | 83 }); |
| 83 } | 84 } |
| 84 | 85 |
| 85 /** | 86 /** |
| 86 * Asserts that [refactoringChange] contains a [FileEdit] for [testFile], and | 87 * Asserts that [refactoringChange] contains a [FileEdit] for [testFile], and |
| 87 * it results the [expectedCode]. | 88 * it results the [expectedCode]. |
| 88 */ | 89 */ |
| 89 void assertTestChangeResult(String expectedCode) { | 90 void assertTestChangeResult(String expectedCode) { |
| 90 // prepare FileEdit | 91 // prepare FileEdit |
| 91 FileEdit fileEdit = refactoringChange.getFileEdit(testFile); | 92 FileEdit fileEdit = refactoringChange.getFileEdit(testFile); |
| 92 expect(fileEdit, isNotNull); | 93 expect(fileEdit, isNotNull); |
| 93 // validate resulting code | 94 // validate resulting code |
| 94 String actualCode = Edit.applySequence(testCode, fileEdit.edits); | 95 String actualCode = applySequence(testCode, fileEdit.edits); |
| 95 expect(actualCode, expectedCode); | 96 expect(actualCode, expectedCode); |
| 96 } | 97 } |
| 97 | 98 |
| 98 /** | 99 /** |
| 99 * Creates a new [RenameRefactoring] in [refactoring] for the [Element] of | 100 * Creates a new [RenameRefactoring] in [refactoring] for the [Element] of |
| 100 * the [SimpleIdentifier] at the given [search] pattern. | 101 * the [SimpleIdentifier] at the given [search] pattern. |
| 101 */ | 102 */ |
| 102 void createRenameRefactoringAtString(String search) { | 103 void createRenameRefactoringAtString(String search) { |
| 103 SimpleIdentifier identifier = findIdentifier(search); | 104 SimpleIdentifier identifier = findIdentifier(search); |
| 104 Element element = identifier.bestElement; | 105 Element element = identifier.bestElement; |
| 105 if (element is PrefixElement) { | 106 if (element is PrefixElement) { |
| 106 element = getImportElement(identifier); | 107 element = getImportElement(identifier); |
| 107 } | 108 } |
| 108 createRenameRefactoringForElement(element); | 109 createRenameRefactoringForElement(element); |
| 109 } | 110 } |
| 110 | 111 |
| 111 /** | 112 /** |
| 112 * Creates a new [RenameRefactoring] in [refactoring] for [element]. | 113 * Creates a new [RenameRefactoring] in [refactoring] for [element]. |
| 113 * Fails if no [RenameRefactoring] can be created. | 114 * Fails if no [RenameRefactoring] can be created. |
| 114 */ | 115 */ |
| 115 void createRenameRefactoringForElement(Element element) { | 116 void createRenameRefactoringForElement(Element element) { |
| 116 refactoring = new RenameRefactoring(searchEngine, element); | 117 refactoring = new RenameRefactoring(searchEngine, element); |
| 117 expect(refactoring, isNotNull, reason: "No refactoring for '$element'."); | 118 expect(refactoring, isNotNull, reason: "No refactoring for '$element'."); |
| 118 } | 119 } |
| 119 | 120 |
| 120 /** | 121 /** |
| 121 * Returns the [Edit] with the given [id], maybe `null`. | 122 * Returns the [Edit] with the given [id], maybe `null`. |
| 122 */ | 123 */ |
| 123 Edit findEditById(String id) { | 124 SourceEdit findEditById(String id) { |
| 124 for (FileEdit fileEdit in refactoringChange.fileEdits) { | 125 for (FileEdit fileEdit in refactoringChange.fileEdits) { |
| 125 for (Edit edit in fileEdit.edits) { | 126 for (SourceEdit edit in fileEdit.edits) { |
| 126 if (edit.id == id) { | 127 if (edit.id == id) { |
| 127 return edit; | 128 return edit; |
| 128 } | 129 } |
| 129 } | 130 } |
| 130 } | 131 } |
| 131 return null; | 132 return null; |
| 132 } | 133 } |
| 133 | 134 |
| 134 // /** | 135 // /** |
| 135 // * Asserts result of applying [change] to [testCode]. | 136 // * Asserts result of applying [change] to [testCode]. |
| 136 // */ | 137 // */ |
| 137 // void assertTestChangeResult(Change change, String expected) | 138 // void assertTestChangeResult(Change change, String expected) |
| 138 // { | 139 // { |
| 139 // assertChangeResult(change, testSource, expected); | 140 // assertChangeResult(change, testSource, expected); |
| 140 // } | 141 // } |
| 141 // | 142 // |
| 142 // /** | 143 // /** |
| 143 // * Asserts result of applying [change] to [source]. | 144 // * Asserts result of applying [change] to [source]. |
| 144 // */ | 145 // */ |
| 145 // void assertChangeResult(Change change, Source source, String expected) | 146 // void assertChangeResult(Change change, Source source, String expected) |
| 146 // { | 147 // { |
| 147 // SourceChange sourceChange = getSourceChange(compositeChange, source); | 148 // SourceChange sourceChange = getSourceChange(compositeChange, source); |
| 148 // assertNotNull("No change for: " + source.toString(), sourceChange); | 149 // assertNotNull("No change for: " + source.toString(), sourceChange); |
| 149 // String sourceResult = getChangeResult(context, source, sourceChange); | 150 // String sourceResult = getChangeResult(context, source, sourceChange); |
| 150 // assertEquals(expected, sourceResult); | 151 // assertEquals(expected, sourceResult); |
| 151 //// AnalysisContext context = getAnalysisContext(); | 152 //// AnalysisContext context = getAnalysisContext(); |
| 152 //// assertChangeResult(context, compositeChange, source, expected); | 153 //// assertChangeResult(context, compositeChange, source, expected); |
| 153 // } | 154 // } |
| 154 } | 155 } |
| OLD | NEW |