OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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:convert'; | 5 import 'dart:convert'; |
6 import 'dart:typed_data'; | 6 import 'dart:typed_data'; |
7 | 7 |
8 import 'package:analyzer/file_system/file_system.dart'; | 8 import 'package:analyzer/file_system/file_system.dart'; |
9 import 'package:analyzer/file_system/memory_file_system.dart'; | 9 import 'package:analyzer/file_system/memory_file_system.dart'; |
10 import 'package:analyzer/source/package_map_resolver.dart'; | 10 import 'package:analyzer/source/package_map_resolver.dart'; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 test_getFileForPath_doesNotExist() { | 60 test_getFileForPath_doesNotExist() { |
61 String path = _p('/aaa/lib/a.dart'); | 61 String path = _p('/aaa/lib/a.dart'); |
62 FileState file = fileSystemState.getFileForPath(path); | 62 FileState file = fileSystemState.getFileForPath(path); |
63 expect(file.path, path); | 63 expect(file.path, path); |
64 expect(file.uri, FastUri.parse('package:aaa/a.dart')); | 64 expect(file.uri, FastUri.parse('package:aaa/a.dart')); |
65 expect(file.content, ''); | 65 expect(file.content, ''); |
66 expect(file.contentHash, _md5('')); | 66 expect(file.contentHash, _md5('')); |
67 expect(file.importedFiles, isEmpty); | 67 expect(file.importedFiles, isEmpty); |
68 expect(file.exportedFiles, isEmpty); | 68 expect(file.exportedFiles, isEmpty); |
69 expect(file.partedFiles, isEmpty); | 69 expect(file.partedFiles, isEmpty); |
70 expect(file.dependencies, isEmpty); | 70 expect(file.directReferencedFiles, isEmpty); |
71 expect(file.isPart, isFalse); | 71 expect(file.isPart, isFalse); |
72 expect(file.library, isNull); | 72 expect(file.library, isNull); |
73 expect(file.unlinked, isNotNull); | 73 expect(file.unlinked, isNotNull); |
74 expect(file.unlinked.classes, isEmpty); | 74 expect(file.unlinked.classes, isEmpty); |
75 } | 75 } |
76 | 76 |
77 test_getFileForPath_library() { | 77 test_getFileForPath_library() { |
78 String a1 = _p('/aaa/lib/a1.dart'); | 78 String a1 = _p('/aaa/lib/a1.dart'); |
79 String a2 = _p('/aaa/lib/a2.dart'); | 79 String a2 = _p('/aaa/lib/a2.dart'); |
80 String a3 = _p('/aaa/lib/a3.dart'); | 80 String a3 = _p('/aaa/lib/a3.dart'); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 expect(file.exportedFiles[0].uri, FastUri.parse('package:bbb/b2.dart')); | 116 expect(file.exportedFiles[0].uri, FastUri.parse('package:bbb/b2.dart')); |
117 expect(file.exportedFiles[0].source, isNotNull); | 117 expect(file.exportedFiles[0].source, isNotNull); |
118 expect(file.exportedFiles[1].path, a3); | 118 expect(file.exportedFiles[1].path, a3); |
119 expect(file.exportedFiles[1].uri, FastUri.parse('package:aaa/a3.dart')); | 119 expect(file.exportedFiles[1].uri, FastUri.parse('package:aaa/a3.dart')); |
120 expect(file.exportedFiles[1].source, isNotNull); | 120 expect(file.exportedFiles[1].source, isNotNull); |
121 | 121 |
122 expect(file.partedFiles, hasLength(1)); | 122 expect(file.partedFiles, hasLength(1)); |
123 expect(file.partedFiles[0].path, a4); | 123 expect(file.partedFiles[0].path, a4); |
124 expect(file.partedFiles[0].uri, FastUri.parse('package:aaa/a4.dart')); | 124 expect(file.partedFiles[0].uri, FastUri.parse('package:aaa/a4.dart')); |
125 | 125 |
126 expect(file.dependencies, hasLength(5)); | 126 expect(file.directReferencedFiles, hasLength(5)); |
127 | 127 |
128 expect(fileSystemState.getFilesForPath(a1), [file]); | 128 expect(fileSystemState.getFilesForPath(a1), [file]); |
129 } | 129 } |
130 | 130 |
131 test_getFileForPath_part() { | 131 test_getFileForPath_part() { |
132 String a1 = _p('/aaa/lib/a1.dart'); | 132 String a1 = _p('/aaa/lib/a1.dart'); |
133 String a2 = _p('/aaa/lib/a2.dart'); | 133 String a2 = _p('/aaa/lib/a2.dart'); |
134 provider.newFile( | 134 provider.newFile( |
135 a1, | 135 a1, |
136 r''' | 136 r''' |
(...skipping 11 matching lines...) Expand all Loading... |
148 expect(file_a2.path, a2); | 148 expect(file_a2.path, a2); |
149 expect(file_a2.uri, FastUri.parse('package:aaa/a2.dart')); | 149 expect(file_a2.uri, FastUri.parse('package:aaa/a2.dart')); |
150 | 150 |
151 expect(file_a2.unlinked, isNotNull); | 151 expect(file_a2.unlinked, isNotNull); |
152 expect(file_a2.unlinked.classes, hasLength(1)); | 152 expect(file_a2.unlinked.classes, hasLength(1)); |
153 expect(file_a2.unlinked.classes[0].name, 'A2'); | 153 expect(file_a2.unlinked.classes[0].name, 'A2'); |
154 | 154 |
155 expect(file_a2.importedFiles, isEmpty); | 155 expect(file_a2.importedFiles, isEmpty); |
156 expect(file_a2.exportedFiles, isEmpty); | 156 expect(file_a2.exportedFiles, isEmpty); |
157 expect(file_a2.partedFiles, isEmpty); | 157 expect(file_a2.partedFiles, isEmpty); |
158 expect(file_a2.dependencies, isEmpty); | 158 expect(file_a2.directReferencedFiles, isEmpty); |
159 | 159 |
160 // The library is not known yet. | 160 // The library is not known yet. |
161 expect(file_a2.isPart, isTrue); | 161 expect(file_a2.isPart, isTrue); |
162 expect(file_a2.library, isNull); | 162 expect(file_a2.library, isNull); |
163 | 163 |
164 // Ask for the library. | 164 // Ask for the library. |
165 FileState file_a1 = fileSystemState.getFileForPath(a1); | 165 FileState file_a1 = fileSystemState.getFileForPath(a1); |
166 expect(file_a1.partedFiles, hasLength(1)); | 166 expect(file_a1.partedFiles, hasLength(1)); |
167 expect(file_a1.partedFiles[0], same(file_a2)); | 167 expect(file_a1.partedFiles[0], same(file_a2)); |
168 expect(file_a1.dependencies, unorderedEquals([file_a2])); | 168 expect(file_a1.directReferencedFiles, unorderedEquals([file_a2])); |
169 | 169 |
170 // Now the part knows its library. | 170 // Now the part knows its library. |
171 expect(file_a2.library, same(file_a1)); | 171 expect(file_a2.library, same(file_a1)); |
172 | 172 |
173 // Now update the library, and refresh its file. | 173 // Now update the library, and refresh its file. |
174 // The 'a2.dart' is not referenced anymore. | 174 // The 'a2.dart' is not referenced anymore. |
175 // So the part file does not have the library anymore. | 175 // So the part file does not have the library anymore. |
176 provider.newFile( | 176 provider.newFile( |
177 a1, | 177 a1, |
178 r''' | 178 r''' |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 print(222); | 258 print(222); |
259 } | 259 } |
260 } | 260 } |
261 '''); | 261 '''); |
262 bool apiSignatureChanged = file.refresh(); | 262 bool apiSignatureChanged = file.refresh(); |
263 expect(apiSignatureChanged, isFalse); | 263 expect(apiSignatureChanged, isFalse); |
264 | 264 |
265 expect(file.apiSignature, signature); | 265 expect(file.apiSignature, signature); |
266 } | 266 } |
267 | 267 |
| 268 test_transitiveFiles() { |
| 269 String pa = _p('/aaa/lib/a.dart'); |
| 270 String pb = _p('/aaa/lib/b.dart'); |
| 271 String pc = _p('/aaa/lib/c.dart'); |
| 272 String pd = _p('/aaa/lib/d.dart'); |
| 273 |
| 274 FileState fa = fileSystemState.getFileForPath(pa); |
| 275 FileState fb = fileSystemState.getFileForPath(pb); |
| 276 FileState fc = fileSystemState.getFileForPath(pc); |
| 277 FileState fd = fileSystemState.getFileForPath(pd); |
| 278 |
| 279 // Compute transitive closures for all files. |
| 280 fa.transitiveFiles; |
| 281 fb.transitiveFiles; |
| 282 fc.transitiveFiles; |
| 283 fd.transitiveFiles; |
| 284 expect(fileSystemState.test.filesWithoutTransitive, isEmpty); |
| 285 |
| 286 // No imports, so just a single file. |
| 287 provider.newFile(pa, ""); |
| 288 _assertTransitiveFiles(pa, [pa]); |
| 289 |
| 290 // Import b.dart into a.dart, two files now. |
| 291 provider.newFile(pa, "import 'b.dart';"); |
| 292 fa.refresh(); |
| 293 _assertFilesWithoutTransitive([fa]); |
| 294 _assertTransitiveFiles(pa, [pa, pb]); |
| 295 |
| 296 // Update b.dart so that it imports c.dart now. |
| 297 provider.newFile(pb, "import 'c.dart';"); |
| 298 fb.refresh(); |
| 299 _assertFilesWithoutTransitive([fa, fb]); |
| 300 _assertTransitiveFiles(pa, [pa, pb, pc]); |
| 301 _assertTransitiveFiles(pb, [pb, pc]); |
| 302 _assertFilesWithoutTransitive([]); |
| 303 |
| 304 // Update b.dart so that it exports d.dart instead. |
| 305 provider.newFile(pb, "export 'd.dart';"); |
| 306 fb.refresh(); |
| 307 _assertFilesWithoutTransitive([fa, fb]); |
| 308 _assertTransitiveFiles(pa, [pa, pb, pd]); |
| 309 _assertTransitiveFiles(pb, [pb, pd]); |
| 310 _assertFilesWithoutTransitive([]); |
| 311 |
| 312 // Update a.dart so that it does not import b.dart anymore. |
| 313 provider.newFile(pa, ""); |
| 314 fa.refresh(); |
| 315 _assertFilesWithoutTransitive([fa]); |
| 316 _assertTransitiveFiles(pa, [pa]); |
| 317 } |
| 318 |
| 319 test_transitiveFiles_cycle() { |
| 320 String pa = _p('/aaa/lib/a.dart'); |
| 321 String pb = _p('/aaa/lib/b.dart'); |
| 322 |
| 323 provider.newFile(pa, "import 'b.dart';"); |
| 324 provider.newFile(pb, "import 'a.dart';"); |
| 325 |
| 326 FileState fa = fileSystemState.getFileForPath(pa); |
| 327 FileState fb = fileSystemState.getFileForPath(pb); |
| 328 |
| 329 // Compute transitive closures for all files. |
| 330 fa.transitiveFiles; |
| 331 fb.transitiveFiles; |
| 332 _assertFilesWithoutTransitive([]); |
| 333 |
| 334 // It's a cycle. |
| 335 _assertTransitiveFiles(pa, [pa, pb]); |
| 336 _assertTransitiveFiles(pb, [pa, pb]); |
| 337 |
| 338 // Update a.dart so that it does not import b.dart anymore. |
| 339 provider.newFile(pa, ""); |
| 340 fa.refresh(); |
| 341 _assertFilesWithoutTransitive([fa, fb]); |
| 342 _assertTransitiveFiles(pa, [pa]); |
| 343 _assertTransitiveFiles(pb, [pa, pb]); |
| 344 } |
| 345 |
| 346 void _assertFilesWithoutTransitive(List<FileState> expected) { |
| 347 var actual = fileSystemState.test.filesWithoutTransitive; |
| 348 expect(actual, unorderedEquals(expected)); |
| 349 } |
| 350 |
| 351 void _assertTransitiveFiles(String path, List<String> expectedPaths) { |
| 352 FileState file = fileSystemState.getFileForPath(path); |
| 353 expect(file.transitiveFiles, |
| 354 unorderedEquals(expectedPaths.map(fileSystemState.getFileForPath))); |
| 355 } |
| 356 |
268 String _p(String path) => provider.convertPath(path); | 357 String _p(String path) => provider.convertPath(path); |
269 | 358 |
270 static String _md5(String content) { | 359 static String _md5(String content) { |
271 return hex.encode(md5.convert(UTF8.encode(content)).bytes); | 360 return hex.encode(md5.convert(UTF8.encode(content)).bytes); |
272 } | 361 } |
273 } | 362 } |
OLD | NEW |