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