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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 7 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
8 import 'package:analyzer/file_system/file_system.dart'; | |
9 import 'package:analyzer/source/package_map_resolver.dart'; | |
10 import 'package:analyzer/src/generated/engine.dart'; | |
11 import 'package:analyzer/src/generated/source.dart'; | |
12 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
13 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 9 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
14 | 10 |
15 import 'abstract_refactoring.dart'; | 11 import 'abstract_refactoring.dart'; |
16 | 12 |
17 main() { | 13 main() { |
18 defineReflectiveSuite(() { | 14 defineReflectiveSuite(() { |
19 defineReflectiveTests(MoveFileTest); | 15 defineReflectiveTests(MoveFileTest); |
20 }); | 16 }); |
21 } | 17 } |
(...skipping 16 matching lines...) Expand all Loading... |
38 addSource(pathC, ''); | 34 addSource(pathC, ''); |
39 addSource(pathD, ''); | 35 addSource(pathD, ''); |
40 addTestSource(''' | 36 addTestSource(''' |
41 library lib; | 37 library lib; |
42 import 'dart:math'; | 38 import 'dart:math'; |
43 import '22/c.dart'; | 39 import '22/c.dart'; |
44 export '333/d.dart'; | 40 export '333/d.dart'; |
45 part 'a.dart'; | 41 part 'a.dart'; |
46 part '/absolute/uri.dart'; | 42 part '/absolute/uri.dart'; |
47 '''); | 43 '''); |
48 _performAnalysis(); | |
49 // perform refactoring | 44 // perform refactoring |
50 _createRefactoring('/project/000/1111/22/new_name.dart'); | 45 _createRefactoring('/project/000/1111/22/new_name.dart'); |
51 await _assertSuccessfulRefactoring(); | 46 await _assertSuccessfulRefactoring(); |
52 assertNoFileChange(pathA); | 47 assertNoFileChange(pathA); |
53 assertFileChangeResult(pathB, "import '22/new_name.dart';"); | 48 assertFileChangeResult(pathB, "import '22/new_name.dart';"); |
54 assertNoFileChange(pathC); | 49 assertNoFileChange(pathC); |
55 assertFileChangeResult( | 50 assertFileChangeResult( |
56 testFile, | 51 testFile, |
57 ''' | 52 ''' |
58 library lib; | 53 library lib; |
59 import 'dart:math'; | 54 import 'dart:math'; |
60 import 'c.dart'; | 55 import 'c.dart'; |
61 export '../333/d.dart'; | 56 export '../333/d.dart'; |
62 part '../a.dart'; | 57 part '../a.dart'; |
63 part '/absolute/uri.dart'; | 58 part '/absolute/uri.dart'; |
64 '''); | 59 '''); |
65 } | 60 } |
66 | 61 |
67 @failingTest | 62 @failingTest |
68 test_file_importedLibrary() async { | 63 test_file_importedLibrary() async { |
69 fail('The move file refactoring is not supported under the new driver'); | 64 fail('The move file refactoring is not supported under the new driver'); |
70 String pathA = '/project/000/1111/a.dart'; | 65 String pathA = '/project/000/1111/a.dart'; |
71 testFile = '/project/000/1111/sub/folder/test.dart'; | 66 testFile = '/project/000/1111/sub/folder/test.dart'; |
72 addSource( | 67 addSource( |
73 pathA, | 68 pathA, |
74 ''' | 69 ''' |
75 import 'sub/folder/test.dart'; | 70 import 'sub/folder/test.dart'; |
76 '''); | 71 '''); |
77 addTestSource(''); | 72 addTestSource(''); |
78 _performAnalysis(); | |
79 // perform refactoring | 73 // perform refactoring |
80 _createRefactoring('/project/000/new/folder/name/new_name.dart'); | 74 _createRefactoring('/project/000/new/folder/name/new_name.dart'); |
81 await _assertSuccessfulRefactoring(); | 75 await _assertSuccessfulRefactoring(); |
82 assertFileChangeResult( | 76 assertFileChangeResult( |
83 pathA, | 77 pathA, |
84 ''' | 78 ''' |
85 import '../new/folder/name/new_name.dart'; | 79 import '../new/folder/name/new_name.dart'; |
86 '''); | 80 '''); |
87 assertNoFileChange(testFile); | 81 assertNoFileChange(testFile); |
88 } | 82 } |
89 | 83 |
90 @failingTest | 84 @failingTest |
91 test_file_importedLibrary_down() async { | 85 test_file_importedLibrary_down() async { |
92 fail('The move file refactoring is not supported under the new driver'); | 86 fail('The move file refactoring is not supported under the new driver'); |
93 String pathA = '/project/000/1111/a.dart'; | 87 String pathA = '/project/000/1111/a.dart'; |
94 testFile = '/project/000/1111/test.dart'; | 88 testFile = '/project/000/1111/test.dart'; |
95 addSource( | 89 addSource( |
96 pathA, | 90 pathA, |
97 ''' | 91 ''' |
98 import 'test.dart'; | 92 import 'test.dart'; |
99 '''); | 93 '''); |
100 addTestSource(''); | 94 addTestSource(''); |
101 _performAnalysis(); | |
102 // perform refactoring | 95 // perform refactoring |
103 _createRefactoring('/project/000/1111/22/new_name.dart'); | 96 _createRefactoring('/project/000/1111/22/new_name.dart'); |
104 await _assertSuccessfulRefactoring(); | 97 await _assertSuccessfulRefactoring(); |
105 assertFileChangeResult( | 98 assertFileChangeResult( |
106 pathA, | 99 pathA, |
107 ''' | 100 ''' |
108 import '22/new_name.dart'; | 101 import '22/new_name.dart'; |
109 '''); | 102 '''); |
110 assertNoFileChange(testFile); | 103 assertNoFileChange(testFile); |
111 } | 104 } |
112 | 105 |
113 @failingTest | 106 @failingTest |
114 test_file_importedLibrary_package() async { | 107 test_file_importedLibrary_package() async { |
115 fail('The move file refactoring is not supported under the new driver'); | 108 fail('The move file refactoring is not supported under the new driver'); |
116 // configure packages | 109 // configure packages |
117 testFile = '/packages/my_pkg/lib/aaa/test.dart'; | 110 testFile = '/packages/my_pkg/lib/aaa/test.dart'; |
118 provider.newFile(testFile, ''); | 111 provider.newFile(testFile, ''); |
119 Map<String, List<Folder>> packageMap = { | 112 // TODO(brianwilkerson) Figure out what this should be replaced with. |
120 'my_pkg': <Folder>[provider.getResource('/packages/my_pkg/lib')] | 113 // Map<String, List<Folder>> packageMap = { |
121 }; | 114 // 'my_pkg': <Folder>[provider.getResource('/packages/my_pkg/lib')] |
122 context.sourceFactory = new SourceFactory([ | 115 // }; |
123 new DartUriResolver(sdk), | 116 // context.sourceFactory = new SourceFactory([ |
124 new PackageMapUriResolver(provider, packageMap), | 117 // new DartUriResolver(sdk), |
125 resourceResolver | 118 // new PackageMapUriResolver(provider, packageMap), |
126 ]); | 119 // resourceResolver |
| 120 // ]); |
127 // do testing | 121 // do testing |
128 String pathA = '/project/bin/a.dart'; | 122 String pathA = '/project/bin/a.dart'; |
129 addSource( | 123 addSource( |
130 pathA, | 124 pathA, |
131 ''' | 125 ''' |
132 import 'package:my_pkg/aaa/test.dart'; | 126 import 'package:my_pkg/aaa/test.dart'; |
133 '''); | 127 '''); |
134 addTestSource('', Uri.parse('package:my_pkg/aaa/test.dart')); | 128 addTestSource('', Uri.parse('package:my_pkg/aaa/test.dart')); |
135 _performAnalysis(); | |
136 // perform refactoring | 129 // perform refactoring |
137 _createRefactoring('/packages/my_pkg/lib/bbb/ccc/new_name.dart'); | 130 _createRefactoring('/packages/my_pkg/lib/bbb/ccc/new_name.dart'); |
138 await _assertSuccessfulRefactoring(); | 131 await _assertSuccessfulRefactoring(); |
139 assertFileChangeResult( | 132 assertFileChangeResult( |
140 pathA, | 133 pathA, |
141 ''' | 134 ''' |
142 import 'package:my_pkg/bbb/ccc/new_name.dart'; | 135 import 'package:my_pkg/bbb/ccc/new_name.dart'; |
143 '''); | 136 '''); |
144 assertNoFileChange(testFile); | 137 assertNoFileChange(testFile); |
145 } | 138 } |
146 | 139 |
147 @failingTest | 140 @failingTest |
148 test_file_importedLibrary_up() async { | 141 test_file_importedLibrary_up() async { |
149 fail('The move file refactoring is not supported under the new driver'); | 142 fail('The move file refactoring is not supported under the new driver'); |
150 String pathA = '/project/000/1111/a.dart'; | 143 String pathA = '/project/000/1111/a.dart'; |
151 testFile = '/project/000/1111/22/test.dart'; | 144 testFile = '/project/000/1111/22/test.dart'; |
152 addSource( | 145 addSource( |
153 pathA, | 146 pathA, |
154 ''' | 147 ''' |
155 import '22/test.dart'; | 148 import '22/test.dart'; |
156 '''); | 149 '''); |
157 addTestSource(''); | 150 addTestSource(''); |
158 _performAnalysis(); | |
159 // perform refactoring | 151 // perform refactoring |
160 _createRefactoring('/project/000/1111/new_name.dart'); | 152 _createRefactoring('/project/000/1111/new_name.dart'); |
161 await _assertSuccessfulRefactoring(); | 153 await _assertSuccessfulRefactoring(); |
162 assertFileChangeResult( | 154 assertFileChangeResult( |
163 pathA, | 155 pathA, |
164 ''' | 156 ''' |
165 import 'new_name.dart'; | 157 import 'new_name.dart'; |
166 '''); | 158 '''); |
167 assertNoFileChange(testFile); | 159 assertNoFileChange(testFile); |
168 } | 160 } |
169 | 161 |
170 @failingTest | 162 @failingTest |
171 test_file_sourcedUnit() async { | 163 test_file_sourcedUnit() async { |
172 fail('The move file refactoring is not supported under the new driver'); | 164 fail('The move file refactoring is not supported under the new driver'); |
173 String pathA = '/project/000/1111/a.dart'; | 165 String pathA = '/project/000/1111/a.dart'; |
174 testFile = '/project/000/1111/22/test.dart'; | 166 testFile = '/project/000/1111/22/test.dart'; |
175 addSource( | 167 addSource( |
176 pathA, | 168 pathA, |
177 ''' | 169 ''' |
178 library lib; | 170 library lib; |
179 part '22/test.dart'; | 171 part '22/test.dart'; |
180 '''); | 172 '''); |
181 addTestSource(''' | 173 addTestSource(''' |
182 part of lib; | 174 part of lib; |
183 '''); | 175 '''); |
184 _performAnalysis(); | |
185 // perform refactoring | 176 // perform refactoring |
186 _createRefactoring('/project/000/1111/22/new_name.dart'); | 177 _createRefactoring('/project/000/1111/22/new_name.dart'); |
187 await _assertSuccessfulRefactoring(); | 178 await _assertSuccessfulRefactoring(); |
188 assertFileChangeResult( | 179 assertFileChangeResult( |
189 pathA, | 180 pathA, |
190 ''' | 181 ''' |
191 library lib; | 182 library lib; |
192 part '22/new_name.dart'; | 183 part '22/new_name.dart'; |
193 '''); | 184 '''); |
194 assertNoFileChange(testFile); | 185 assertNoFileChange(testFile); |
(...skipping 13 matching lines...) Expand all Loading... |
208 '''); | 199 '''); |
209 addSource( | 200 addSource( |
210 pathB, | 201 pathB, |
211 ''' | 202 ''' |
212 library lib; | 203 library lib; |
213 part '1111/22/test.dart'; | 204 part '1111/22/test.dart'; |
214 '''); | 205 '''); |
215 addTestSource(''' | 206 addTestSource(''' |
216 part of lib; | 207 part of lib; |
217 '''); | 208 '''); |
218 _performAnalysis(); | |
219 // perform refactoring | 209 // perform refactoring |
220 _createRefactoring('/project/000/1111/22/new_name.dart'); | 210 _createRefactoring('/project/000/1111/22/new_name.dart'); |
221 await _assertSuccessfulRefactoring(); | 211 await _assertSuccessfulRefactoring(); |
222 assertFileChangeResult( | 212 assertFileChangeResult( |
223 pathA, | 213 pathA, |
224 ''' | 214 ''' |
225 library lib; | 215 library lib; |
226 part '22/new_name.dart'; | 216 part '22/new_name.dart'; |
227 '''); | 217 '''); |
228 assertFileChangeResult( | 218 assertFileChangeResult( |
(...skipping 18 matching lines...) Expand all Loading... |
247 description: My pubspec file. | 237 description: My pubspec file. |
248 '''); | 238 '''); |
249 addSource('/testName/lib/myLib.dart', ''); | 239 addSource('/testName/lib/myLib.dart', ''); |
250 addSource( | 240 addSource( |
251 appPath, | 241 appPath, |
252 ''' | 242 ''' |
253 import 'package:testName/myLib.dart'; | 243 import 'package:testName/myLib.dart'; |
254 export 'package:testName/myLib.dart'; | 244 export 'package:testName/myLib.dart'; |
255 '''); | 245 '''); |
256 // configure Uri resolves | 246 // configure Uri resolves |
257 context.sourceFactory = new SourceFactory([ | 247 // TODO(brianwilkerson) Figure out what this should be replaced with. |
258 new DartUriResolver(sdk), | 248 // context.sourceFactory = new SourceFactory([ |
259 new PackageMapUriResolver(provider, <String, List<Folder>>{ | 249 // new DartUriResolver(sdk), |
260 'testName': <Folder>[provider.getResource('/testName/lib')] | 250 // new PackageMapUriResolver(provider, <String, List<Folder>>{ |
261 }), | 251 // 'testName': <Folder>[provider.getResource('/testName/lib')] |
262 resourceResolver, | 252 // }), |
263 ]); | 253 // resourceResolver, |
264 // analyze | 254 // ]); |
265 _performAnalysis(); | |
266 // perform refactoring | 255 // perform refactoring |
267 refactoring = new MoveFileRefactoring( | 256 refactoring = new MoveFileRefactoring( |
268 provider, searchEngine, context, null, '/testName'); | 257 provider, searchEngine, null, null, '/testName'); |
269 refactoring.newFile = '/newName'; | 258 refactoring.newFile = '/newName'; |
270 await _assertSuccessfulRefactoring(); | 259 await _assertSuccessfulRefactoring(); |
271 assertFileChangeResult( | 260 assertFileChangeResult( |
272 pubspecPath, | 261 pubspecPath, |
273 ''' | 262 ''' |
274 name: newName | 263 name: newName |
275 version: 0.0.1 | 264 version: 0.0.1 |
276 description: My pubspec file. | 265 description: My pubspec file. |
277 '''); | 266 '''); |
278 assertFileChangeResult( | 267 assertFileChangeResult( |
279 appPath, | 268 appPath, |
280 ''' | 269 ''' |
281 import 'package:newName/myLib.dart'; | 270 import 'package:newName/myLib.dart'; |
282 export 'package:newName/myLib.dart'; | 271 export 'package:newName/myLib.dart'; |
283 '''); | 272 '''); |
284 } | 273 } |
285 | 274 |
286 /** | 275 /** |
287 * Checks that all conditions are OK. | 276 * Checks that all conditions are OK. |
288 */ | 277 */ |
289 Future _assertSuccessfulRefactoring() async { | 278 Future _assertSuccessfulRefactoring() async { |
290 await assertRefactoringConditionsOK(); | 279 await assertRefactoringConditionsOK(); |
291 refactoringChange = await refactoring.createChange(); | 280 refactoringChange = await refactoring.createChange(); |
292 } | 281 } |
293 | 282 |
294 void _createRefactoring(String newName) { | 283 void _createRefactoring(String newName) { |
295 refactoring = new MoveFileRefactoring( | 284 refactoring = |
296 provider, searchEngine, context, testSource, null); | 285 new MoveFileRefactoring(provider, searchEngine, null, testSource, null); |
297 refactoring.newFile = newName; | 286 refactoring.newFile = newName; |
298 } | 287 } |
299 | |
300 void _performAnalysis() { | |
301 while (true) { | |
302 AnalysisResult result = context.performAnalysisTask(); | |
303 if (!result.hasMoreWork) { | |
304 break; | |
305 } | |
306 for (ChangeNotice notice in result.changeNotices) { | |
307 if (notice.source.fullName.startsWith('/project/')) { | |
308 index.indexUnit(notice.resolvedDartUnit); | |
309 } | |
310 } | |
311 } | |
312 } | |
313 } | 288 } |
OLD | NEW |