Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: packages/analyzer/test/file_system/memory_file_system_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.memory_file_system;
6
7 import 'dart:async';
8 import 'dart:core' hide Resource;
9
10 import 'package:analyzer/file_system/file_system.dart';
11 import 'package:analyzer/file_system/memory_file_system.dart';
12 import 'package:analyzer/src/generated/engine.dart' show TimestampedData;
13 import 'package:analyzer/src/generated/source.dart';
14 import 'package:path/path.dart';
15 import 'package:unittest/unittest.dart';
16 import 'package:watcher/watcher.dart';
17
18 import '../reflective_tests.dart';
19 import '../utils.dart';
20
21 main() {
22 initializeTestEnvironment();
23 runReflectiveTests(FileSystemExceptionTest);
24 runReflectiveTests(FileTest);
25 runReflectiveTests(FolderTest);
26 runReflectiveTests(MemoryFileSourceExistingTest);
27 runReflectiveTests(MemoryFileSourceNotExistingTest);
28 runReflectiveTests(MemoryResourceProviderTest);
29 }
30
31 var _isFile = new isInstanceOf<File>();
32 var _isFileSystemException = new isInstanceOf<FileSystemException>();
33 var _isFolder = new isInstanceOf<Folder>();
34
35 @reflectiveTest
36 class FileSystemExceptionTest {
37 void test_constructor() {
38 var exception = new FileSystemException('/my/path', 'my message');
39 expect(exception.path, '/my/path');
40 expect(exception.message, 'my message');
41 expect(exception.toString(),
42 'FileSystemException(path=/my/path; message=my message)');
43 }
44 }
45
46 @reflectiveTest
47 class FileTest {
48 MemoryResourceProvider provider = new MemoryResourceProvider();
49
50 void test_equals_beforeAndAfterCreate() {
51 String path = '/file.txt';
52 File file1 = provider.getResource(path);
53 provider.newFile(path, 'contents');
54 File file2 = provider.getResource(path);
55 expect(file1 == file2, isTrue);
56 }
57
58 void test_equals_false() {
59 File fileA = provider.getResource('/fileA.txt');
60 File fileB = provider.getResource('/fileB.txt');
61 expect(fileA == new Object(), isFalse);
62 expect(fileA == fileB, isFalse);
63 }
64
65 void test_equals_true() {
66 File file = provider.getResource('/file.txt');
67 expect(file == file, isTrue);
68 }
69
70 void test_exists_false() {
71 File file = provider.getResource('/file.txt');
72 expect(file, isNotNull);
73 expect(file.exists, isFalse);
74 }
75
76 void test_exists_true() {
77 provider.newFile('/foo/file.txt', 'qwerty');
78 File file = provider.getResource('/foo/file.txt');
79 expect(file, isNotNull);
80 expect(file.exists, isTrue);
81 }
82
83 void test_fullName() {
84 File file = provider.getResource('/foo/bar/file.txt');
85 expect(file.path, '/foo/bar/file.txt');
86 }
87
88 void test_hashCode() {
89 String path = '/foo/bar/file.txt';
90 File file1 = provider.getResource(path);
91 provider.newFile(path, 'contents');
92 File file2 = provider.getResource(path);
93 expect(file1.hashCode, equals(file2.hashCode));
94 }
95
96 void test_isOrContains() {
97 String path = '/foo/bar/file.txt';
98 File file = provider.getResource(path);
99 expect(file.isOrContains(path), isTrue);
100 expect(file.isOrContains('/foo/bar'), isFalse);
101 }
102
103 void test_modificationStamp_doesNotExist() {
104 String path = '/foo/bar/file.txt';
105 File file = provider.newFile(path, 'qwerty');
106 provider.deleteFile(path);
107 expect(() {
108 file.modificationStamp;
109 }, throwsA(_isFileSystemException));
110 }
111
112 void test_modificationStamp_exists() {
113 String path = '/foo/bar/file.txt';
114 File file = provider.newFile(path, 'qwerty');
115 expect(file.modificationStamp, isNonNegative);
116 }
117
118 void test_parent() {
119 provider.newFile('/foo/bar/file.txt', 'content');
120 File file = provider.getResource('/foo/bar/file.txt');
121 Resource parent = file.parent;
122 expect(parent, new isInstanceOf<Folder>());
123 expect(parent.path, equals('/foo/bar'));
124 }
125
126 void test_readAsStringSync_doesNotExist() {
127 File file = provider.getResource('/test.txt');
128 expect(() {
129 file.readAsStringSync();
130 }, throwsA(_isFileSystemException));
131 }
132
133 void test_readAsStringSync_exists() {
134 File file = provider.newFile('/file.txt', 'abc');
135 expect(file.readAsStringSync(), 'abc');
136 }
137
138 void test_shortName() {
139 File file = provider.getResource('/foo/bar/file.txt');
140 expect(file.shortName, 'file.txt');
141 }
142
143 void test_toString() {
144 File file = provider.getResource('/foo/bar/file.txt');
145 expect(file.toString(), '/foo/bar/file.txt');
146 }
147 }
148
149 @reflectiveTest
150 class FolderTest {
151 static const String path = '/foo/bar';
152
153 MemoryResourceProvider provider = new MemoryResourceProvider();
154 Folder folder;
155
156 void setUp() {
157 folder = provider.newFolder(path);
158 }
159
160 void test_canonicalizePath() {
161 expect(folder.canonicalizePath('baz'), equals('/foo/bar/baz'));
162 expect(folder.canonicalizePath('/baz'), equals('/baz'));
163 expect(folder.canonicalizePath('../baz'), equals('/foo/baz'));
164 expect(folder.canonicalizePath('/a/b/../c'), equals('/a/c'));
165 expect(folder.canonicalizePath('./baz'), equals('/foo/bar/baz'));
166 expect(folder.canonicalizePath('/a/b/./c'), equals('/a/b/c'));
167 }
168
169 void test_contains() {
170 expect(folder.contains('/foo/bar/aaa.txt'), isTrue);
171 expect(folder.contains('/foo/bar/aaa/bbb.txt'), isTrue);
172 expect(folder.contains('/baz.txt'), isFalse);
173 expect(folder.contains('/foo/bar'), isFalse);
174 }
175
176 void test_equal_false() {
177 String path2 = '/foo/baz';
178 Folder folder2 = provider.newFolder(path2);
179 expect(folder == folder2, isFalse);
180 }
181
182 void test_equal_true() {
183 Folder folder2 = provider.getResource(path);
184 expect(folder == folder2, isTrue);
185 }
186
187 void test_getChild_doesNotExist() {
188 File file = folder.getChild('file.txt');
189 expect(file, isNotNull);
190 expect(file.exists, isFalse);
191 }
192
193 void test_getChild_file() {
194 provider.newFile('/foo/bar/file.txt', 'content');
195 File child = folder.getChild('file.txt');
196 expect(child, isNotNull);
197 expect(child.exists, isTrue);
198 }
199
200 void test_getChild_folder() {
201 provider.newFolder('/foo/bar/baz');
202 Folder child = folder.getChild('baz');
203 expect(child, isNotNull);
204 expect(child.exists, isTrue);
205 }
206
207 void test_getChildAssumingFolder_doesNotExist() {
208 Folder child = folder.getChildAssumingFolder('foldername');
209 expect(child, isNotNull);
210 expect(child.exists, isFalse);
211 }
212
213 void test_getChildAssumingFolder_file() {
214 provider.newFile('/foo/bar/foldername', 'content');
215 Folder child = folder.getChildAssumingFolder('foldername');
216 expect(child, isNotNull);
217 expect(child.exists, isFalse);
218 }
219
220 void test_getChildAssumingFolder_folder() {
221 provider.newFolder('/foo/bar/foldername');
222 Folder child = folder.getChildAssumingFolder('foldername');
223 expect(child, isNotNull);
224 expect(child.exists, isTrue);
225 }
226
227 void test_getChildren_doesNotExist() {
228 folder = folder.getChildAssumingFolder('no-such-folder');
229 expect(() {
230 folder.getChildren();
231 }, throwsA(_isFileSystemException));
232 }
233
234 void test_getChildren_exists() {
235 provider.newFile('/foo/bar/a.txt', 'aaa');
236 provider.newFolder('/foo/bar/bFolder');
237 provider.newFile('/foo/bar/c.txt', 'ccc');
238 // prepare 3 children
239 List<Resource> children = folder.getChildren();
240 expect(children, hasLength(3));
241 children.sort((a, b) => a.shortName.compareTo(b.shortName));
242 // check that each child exists
243 children.forEach((child) {
244 expect(child.exists, true);
245 });
246 // check names
247 expect(children[0].shortName, 'a.txt');
248 expect(children[1].shortName, 'bFolder');
249 expect(children[2].shortName, 'c.txt');
250 // check types
251 expect(children[0], _isFile);
252 expect(children[1], _isFolder);
253 expect(children[2], _isFile);
254 }
255
256 void test_hashCode() {
257 Folder folder2 = provider.getResource(path);
258 expect(folder.hashCode, folder2.hashCode);
259 }
260
261 void test_isOrContains() {
262 expect(folder.isOrContains('/foo/bar'), isTrue);
263 expect(folder.isOrContains('/foo/bar/aaa.txt'), isTrue);
264 expect(folder.isOrContains('/foo/bar/aaa/bbb.txt'), isTrue);
265 expect(folder.isOrContains('/baz.txt'), isFalse);
266 }
267
268 void test_parent() {
269 Resource parent1 = folder.parent;
270 expect(parent1, new isInstanceOf<Folder>());
271 expect(parent1.path, equals('/foo'));
272 Resource parent2 = parent1.parent;
273 expect(parent2, new isInstanceOf<Folder>());
274 expect(parent2.path, equals('/'));
275 expect(parent2.parent, isNull);
276 }
277 }
278
279 @reflectiveTest
280 class MemoryFileSourceExistingTest {
281 MemoryResourceProvider provider = new MemoryResourceProvider();
282 Source source;
283
284 setUp() {
285 File file = provider.newFile('/foo/test.dart', 'library test;');
286 source = file.createSource();
287 }
288
289 void test_contents() {
290 TimestampedData<String> contents = source.contents;
291 expect(contents.data, 'library test;');
292 }
293
294 void test_encoding() {
295 expect(source.encoding, 'file:///foo/test.dart');
296 }
297
298 void test_equals_false_differentFile() {
299 File fileA = provider.newFile('/foo/a.dart', '');
300 File fileB = provider.newFile('/foo/b.dart', '');
301 Source sourceA = fileA.createSource();
302 Source sourceB = fileB.createSource();
303 expect(sourceA == sourceB, isFalse);
304 }
305
306 void test_equals_false_notMemorySource() {
307 File file = provider.newFile('/foo/test.dart', '');
308 Source source = file.createSource();
309 expect(source == new Object(), isFalse);
310 }
311
312 void test_equals_true_sameFile() {
313 File file = provider.newFile('/foo/test.dart', '');
314 Source sourceA = file.createSource();
315 Source sourceB = file.createSource();
316 expect(sourceA == sourceB, isTrue);
317 }
318
319 void test_equals_true_self() {
320 File file = provider.newFile('/foo/test.dart', '');
321 Source source = file.createSource();
322 expect(source == source, isTrue);
323 }
324
325 void test_exists() {
326 expect(source.exists(), isTrue);
327 }
328
329 void test_fullName() {
330 expect(source.fullName, '/foo/test.dart');
331 }
332
333 void test_hashCode() {
334 source.hashCode;
335 }
336
337 void test_resolveRelative() {
338 Uri relative = source.resolveRelativeUri(new Uri.file('bar/baz.dart'));
339 expect(relative.path, '/foo/bar/baz.dart');
340 }
341
342 void test_shortName() {
343 expect(source.shortName, 'test.dart');
344 }
345 }
346
347 @reflectiveTest
348 class MemoryFileSourceNotExistingTest {
349 MemoryResourceProvider provider = new MemoryResourceProvider();
350 Source source;
351
352 setUp() {
353 File file = provider.getResource('/foo/test.dart');
354 source = file.createSource();
355 }
356
357 void test_contents() {
358 expect(() {
359 source.contents;
360 }, throwsA(_isFileSystemException));
361 }
362
363 void test_encoding() {
364 expect(source.encoding, 'file:///foo/test.dart');
365 }
366
367 void test_exists() {
368 expect(source.exists(), isFalse);
369 }
370
371 void test_fullName() {
372 expect(source.fullName, '/foo/test.dart');
373 }
374
375 void test_modificationStamp() {
376 expect(source.modificationStamp, -1);
377 }
378
379 void test_resolveRelative() {
380 Uri relative = source.resolveRelativeUri(new Uri.file('bar/baz.dart'));
381 expect(relative.path, '/foo/bar/baz.dart');
382 }
383
384 void test_shortName() {
385 expect(source.shortName, 'test.dart');
386 }
387 }
388
389 @reflectiveTest
390 class MemoryResourceProviderTest {
391 MemoryResourceProvider provider = new MemoryResourceProvider();
392
393 void test_deleteFile_folder() {
394 String path = '/my/file';
395 provider.newFolder(path);
396 expect(() {
397 provider.deleteFile(path);
398 }, throwsA(new isInstanceOf<ArgumentError>()));
399 expect(provider.getResource(path), new isInstanceOf<Folder>());
400 }
401
402 void test_deleteFile_notExistent() {
403 String path = '/my/file';
404 expect(() {
405 provider.deleteFile(path);
406 }, throwsA(new isInstanceOf<ArgumentError>()));
407 Resource file = provider.getResource(path);
408 expect(file, isNotNull);
409 expect(file.exists, isFalse);
410 }
411
412 void test_deleteFile_success() {
413 String path = '/my/file';
414 provider.newFile(path, 'contents');
415 Resource file = provider.getResource(path);
416 expect(file, new isInstanceOf<File>());
417 expect(file.exists, isTrue);
418 provider.deleteFile(path);
419 expect(file.exists, isFalse);
420 }
421
422 void test_getStateLocation_uniqueness() {
423 String idOne = 'one';
424 Folder folderOne = provider.getStateLocation(idOne);
425 expect(folderOne, isNotNull);
426 String idTwo = 'two';
427 Folder folderTwo = provider.getStateLocation(idTwo);
428 expect(folderTwo, isNotNull);
429 expect(folderTwo, isNot(equals(folderOne)));
430 expect(provider.getStateLocation(idOne), equals(folderOne));
431 }
432
433 void test_modifyFile_isFolder() {
434 String path = '/my/file';
435 provider.newFolder(path);
436 expect(() {
437 provider.modifyFile(path, 'contents');
438 }, throwsA(new isInstanceOf<ArgumentError>()));
439 expect(provider.getResource(path), new isInstanceOf<Folder>());
440 }
441
442 void test_modifyFile_notExistent() {
443 String path = '/my/file';
444 expect(() {
445 provider.modifyFile(path, 'contents');
446 }, throwsA(new isInstanceOf<ArgumentError>()));
447 Resource file = provider.getResource(path);
448 expect(file, isNotNull);
449 expect(file.exists, isFalse);
450 }
451
452 void test_modifyFile_success() {
453 String path = '/my/file';
454 provider.newFile(path, 'contents 1');
455 Resource file = provider.getResource(path);
456 expect(file, new isInstanceOf<File>());
457 Source source = (file as File).createSource();
458 expect(source.contents.data, equals('contents 1'));
459 provider.modifyFile(path, 'contents 2');
460 expect(source.contents.data, equals('contents 2'));
461 }
462
463 void test_newFolder_aleadyExists_asFile() {
464 provider.newFile('/my/file', 'qwerty');
465 expect(() {
466 provider.newFolder('/my/file');
467 }, throwsA(new isInstanceOf<ArgumentError>()));
468 }
469
470 void test_newFolder_aleadyExists_asFolder() {
471 Folder folder = provider.newFolder('/my/folder');
472 Folder newFolder = provider.newFolder('/my/folder');
473 expect(newFolder, folder);
474 }
475
476 void test_newFolder_emptyPath() {
477 expect(() {
478 provider.newFolder('');
479 }, throwsA(new isInstanceOf<ArgumentError>()));
480 }
481
482 void test_newFolder_notAbsolute() {
483 expect(() {
484 provider.newFolder('not/absolute');
485 }, throwsA(new isInstanceOf<ArgumentError>()));
486 }
487
488 test_watch_createFile() {
489 String rootPath = '/my/path';
490 provider.newFolder(rootPath);
491 return _watchingFolder(rootPath, (changesReceived) {
492 expect(changesReceived, hasLength(0));
493 String path = posix.join(rootPath, 'foo');
494 provider.newFile(path, 'contents');
495 return _delayed(() {
496 expect(changesReceived, hasLength(1));
497 expect(changesReceived[0].type, equals(ChangeType.ADD));
498 expect(changesReceived[0].path, equals(path));
499 });
500 });
501 }
502
503 test_watch_deleteFile() {
504 String rootPath = '/my/path';
505 provider.newFolder(rootPath);
506 String path = posix.join(rootPath, 'foo');
507 provider.newFile(path, 'contents 1');
508 return _watchingFolder(rootPath, (changesReceived) {
509 expect(changesReceived, hasLength(0));
510 provider.deleteFile(path);
511 return _delayed(() {
512 expect(changesReceived, hasLength(1));
513 expect(changesReceived[0].type, equals(ChangeType.REMOVE));
514 expect(changesReceived[0].path, equals(path));
515 });
516 });
517 }
518
519 test_watch_modifyFile() {
520 String rootPath = '/my/path';
521 provider.newFolder(rootPath);
522 String path = posix.join(rootPath, 'foo');
523 provider.newFile(path, 'contents 1');
524 return _watchingFolder(rootPath, (changesReceived) {
525 expect(changesReceived, hasLength(0));
526 provider.modifyFile(path, 'contents 2');
527 return _delayed(() {
528 expect(changesReceived, hasLength(1));
529 expect(changesReceived[0].type, equals(ChangeType.MODIFY));
530 expect(changesReceived[0].path, equals(path));
531 });
532 });
533 }
534
535 test_watch_modifyFile_inSubDir() {
536 String rootPath = '/my/path';
537 provider.newFolder(rootPath);
538 String subdirPath = posix.join(rootPath, 'foo');
539 provider.newFolder(subdirPath);
540 String path = posix.join(rootPath, 'bar');
541 provider.newFile(path, 'contents 1');
542 return _watchingFolder(rootPath, (changesReceived) {
543 expect(changesReceived, hasLength(0));
544 provider.modifyFile(path, 'contents 2');
545 return _delayed(() {
546 expect(changesReceived, hasLength(1));
547 expect(changesReceived[0].type, equals(ChangeType.MODIFY));
548 expect(changesReceived[0].path, equals(path));
549 });
550 });
551 }
552
553 Future _delayed(computation()) {
554 return new Future.delayed(Duration.ZERO, computation);
555 }
556
557 _watchingFolder(String path, test(List<WatchEvent> changesReceived)) {
558 Folder folder = provider.getResource(path);
559 var changesReceived = <WatchEvent>[];
560 folder.changes.listen(changesReceived.add);
561 return test(changesReceived);
562 }
563 }
OLDNEW
« no previous file with comments | « packages/analyzer/test/enum_test.dart ('k') | packages/analyzer/test/file_system/physical_resource_provider_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698