| 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.resource; | 5 library test.resource; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io' as io; | 8 import 'dart:io' as io; |
| 9 | 9 |
| 10 import 'mocks.dart'; |
| 11 |
| 10 import 'package:analysis_server/src/resource.dart'; | 12 import 'package:analysis_server/src/resource.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; | 13 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; |
| 12 import 'package:analyzer/src/generated/source_io.dart'; | 14 import 'package:analyzer/src/generated/source_io.dart'; |
| 13 import 'package:path/path.dart'; | 15 import 'package:path/path.dart'; |
| 14 import 'package:unittest/unittest.dart'; | 16 import 'package:unittest/unittest.dart'; |
| 15 import 'package:watcher/watcher.dart'; | 17 import 'package:watcher/watcher.dart'; |
| 16 | 18 |
| 17 main() { | 19 main() { |
| 18 groupSep = ' | '; | 20 groupSep = ' | '; |
| 19 | 21 |
| 20 group('MemoryResourceProvider', () { | 22 group('MemoryResourceProvider', () { |
| 21 MemoryResourceProvider provider; | 23 MemoryResourceProvider provider; |
| 22 | 24 |
| 23 setUp(() { | 25 setUp(() { |
| 24 provider = new MemoryResourceProvider(); | 26 provider = new MemoryResourceProvider(); |
| 25 }); | 27 }); |
| 26 | 28 |
| 27 test('MemoryResourceException', () { | 29 test('MemoryResourceException', () { |
| 28 var exception = new MemoryResourceException('/my/path', 'my message'); | 30 var exception = new MemoryResourceException('/my/path', 'my message'); |
| 29 expect(exception.path, '/my/path'); | 31 expect(exception.path, '/my/path'); |
| 30 expect(exception.message, 'my message'); | 32 expect(exception.message, 'my message'); |
| 31 expect(exception.toString(), | 33 expect(exception.toString(), |
| 32 'MemoryResourceException(path=/my/path; message=my message)'); | 34 'MemoryResourceException(path=/my/path; message=my message)'); |
| 33 }); | 35 }); |
| 34 | 36 |
| 37 group('Watch', () { |
| 38 |
| 39 Future delayed(computation()) { |
| 40 return pumpEventQueue().then((_) => computation()); |
| 41 } |
| 42 |
| 43 watchingFolder(String path, test(List<WatchEvent> changesReceived)) { |
| 44 Folder folder = provider.getResource(path); |
| 45 var changesReceived = <WatchEvent>[]; |
| 46 var subscription = folder.changes.listen(changesReceived.add); |
| 47 return test(changesReceived); |
| 48 } |
| 49 |
| 50 test('create file', () { |
| 51 var rootPath = '/my/path'; |
| 52 provider.newFolder(rootPath); |
| 53 watchingFolder(rootPath, (changesReceived) { |
| 54 expect(changesReceived, hasLength(0)); |
| 55 var path = posix.join(rootPath, 'foo'); |
| 56 provider.newFile(path, 'contents'); |
| 57 return delayed(() { |
| 58 expect(changesReceived, hasLength(1)); |
| 59 expect(changesReceived[0].type, equals(ChangeType.ADD)); |
| 60 expect(changesReceived[0].path, equals(path)); |
| 61 }); |
| 62 }); |
| 63 }); |
| 64 |
| 65 test('modify file', () { |
| 66 var rootPath = '/my/path'; |
| 67 provider.newFolder(rootPath); |
| 68 var path = join(rootPath, 'foo'); |
| 69 provider.newFile(path, 'contents 1'); |
| 70 return watchingFolder(rootPath, (changesReceived) { |
| 71 expect(changesReceived, hasLength(0)); |
| 72 provider.modifyFile(path, 'contents 2'); |
| 73 return delayed(() { |
| 74 expect(changesReceived, hasLength(1)); |
| 75 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); |
| 76 expect(changesReceived[0].path, equals(path)); |
| 77 }); |
| 78 }); |
| 79 }); |
| 80 |
| 81 test('modify file in subdir', () { |
| 82 var rootPath = '/my/path'; |
| 83 provider.newFolder(rootPath); |
| 84 var subdirPath = join(rootPath, 'foo'); |
| 85 provider.newFolder(subdirPath); |
| 86 var path = join(rootPath, 'bar'); |
| 87 provider.newFile(path, 'contents 1'); |
| 88 return watchingFolder(rootPath, (changesReceived) { |
| 89 expect(changesReceived, hasLength(0)); |
| 90 provider.modifyFile(path, 'contents 2'); |
| 91 return delayed(() { |
| 92 expect(changesReceived, hasLength(1)); |
| 93 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); |
| 94 expect(changesReceived[0].path, equals(path)); |
| 95 }); |
| 96 }); |
| 97 }); |
| 98 |
| 99 test('delete file', () { |
| 100 var rootPath = '/my/path'; |
| 101 provider.newFolder(rootPath); |
| 102 var path = join(rootPath, 'foo'); |
| 103 provider.newFile(path, 'contents 1'); |
| 104 return watchingFolder(rootPath, (changesReceived) { |
| 105 expect(changesReceived, hasLength(0)); |
| 106 provider.deleteFile(path); |
| 107 return delayed(() { |
| 108 expect(changesReceived, hasLength(1)); |
| 109 expect(changesReceived[0].type, equals(ChangeType.REMOVE)); |
| 110 expect(changesReceived[0].path, equals(path)); |
| 111 }); |
| 112 }); |
| 113 }); |
| 114 }); |
| 115 |
| 35 group('newFolder', () { | 116 group('newFolder', () { |
| 36 test('empty path', () { | 117 test('empty path', () { |
| 37 expect( | 118 expect( |
| 38 () { | 119 () { |
| 39 provider.newFolder(''); | 120 provider.newFolder(''); |
| 40 }, | 121 }, |
| 41 throwsA(new isInstanceOf<ArgumentError>()) | 122 throwsA(new isInstanceOf<ArgumentError>()) |
| 42 ); | 123 ); |
| 43 }); | 124 }); |
| 44 | 125 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 63 expect( | 144 expect( |
| 64 () { | 145 () { |
| 65 provider.newFolder('/my/file'); | 146 provider.newFolder('/my/file'); |
| 66 }, | 147 }, |
| 67 throwsA(new isInstanceOf<ArgumentError>()) | 148 throwsA(new isInstanceOf<ArgumentError>()) |
| 68 ); | 149 ); |
| 69 }); | 150 }); |
| 70 }); | 151 }); |
| 71 }); | 152 }); |
| 72 | 153 |
| 154 group('modifyFile', () { |
| 155 test('nonexistent', () { |
| 156 var path = '/my/file'; |
| 157 expect(() { provider.modifyFile(path, 'contents'); }, |
| 158 throwsA(new isInstanceOf<ArgumentError>())); |
| 159 var file = provider.getResource(path); |
| 160 expect(file, isNotNull); |
| 161 expect(file.exists, isFalse); |
| 162 }); |
| 163 |
| 164 test('is folder', () { |
| 165 var path = '/my/file'; |
| 166 provider.newFolder(path); |
| 167 expect(() { provider.modifyFile(path, 'contents'); }, |
| 168 throwsA(new isInstanceOf<ArgumentError>())); |
| 169 expect(provider.getResource(path), new isInstanceOf<Folder>()); |
| 170 }); |
| 171 |
| 172 test('successful', () { |
| 173 var path = '/my/file'; |
| 174 provider.newFile(path, 'contents 1'); |
| 175 var file = provider.getResource(path); |
| 176 expect(file, new isInstanceOf<File>()); |
| 177 var source = file.createSource(UriKind.FILE_URI); |
| 178 expect(source.contents.data, equals('contents 1')); |
| 179 provider.modifyFile(path, 'contents 2'); |
| 180 expect(source.contents.data, equals('contents 2')); |
| 181 }); |
| 182 }); |
| 183 |
| 184 group('deleteFile', () { |
| 185 test('nonexistent', () { |
| 186 var path = '/my/file'; |
| 187 expect(() { provider.deleteFile(path); }, |
| 188 throwsA(new isInstanceOf<ArgumentError>())); |
| 189 var file = provider.getResource(path); |
| 190 expect(file, isNotNull); |
| 191 expect(file.exists, isFalse); |
| 192 }); |
| 193 |
| 194 test('is folder', () { |
| 195 var path = '/my/file'; |
| 196 provider.newFolder(path); |
| 197 expect(() { provider.deleteFile(path); }, |
| 198 throwsA(new isInstanceOf<ArgumentError>())); |
| 199 expect(provider.getResource(path), new isInstanceOf<Folder>()); |
| 200 }); |
| 201 |
| 202 test('successful', () { |
| 203 var path = '/my/file'; |
| 204 provider.newFile(path, 'contents'); |
| 205 var file = provider.getResource(path); |
| 206 expect(file, new isInstanceOf<File>()); |
| 207 expect(file.exists, isTrue); |
| 208 provider.deleteFile(path); |
| 209 expect(file.exists, isFalse); |
| 210 }); |
| 211 }); |
| 212 |
| 73 group('File', () { | 213 group('File', () { |
| 74 group('==', () { | 214 group('==', () { |
| 75 test('false', () { | 215 test('false', () { |
| 76 File fileA = provider.getResource('/fileA.txt'); | 216 File fileA = provider.getResource('/fileA.txt'); |
| 77 File fileB = provider.getResource('/fileB.txt'); | 217 File fileB = provider.getResource('/fileB.txt'); |
| 78 expect(fileA == new Object(), isFalse); | 218 expect(fileA == new Object(), isFalse); |
| 79 expect(fileA == fileB, isFalse); | 219 expect(fileA == fileB, isFalse); |
| 80 }); | 220 }); |
| 81 | 221 |
| 82 test('true', () { | 222 test('true', () { |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 expect(children[1], _isFolder); | 623 expect(children[1], _isFolder); |
| 484 expect(children[2], _isFile); | 624 expect(children[2], _isFile); |
| 485 }); | 625 }); |
| 486 }); | 626 }); |
| 487 }); | 627 }); |
| 488 } | 628 } |
| 489 | 629 |
| 490 var _isFile = new isInstanceOf<File>(); | 630 var _isFile = new isInstanceOf<File>(); |
| 491 var _isFolder = new isInstanceOf<Folder>(); | 631 var _isFolder = new isInstanceOf<Folder>(); |
| 492 var _isMemoryResourceException = new isInstanceOf<MemoryResourceException>(); | 632 var _isMemoryResourceException = new isInstanceOf<MemoryResourceException>(); |
| OLD | NEW |