OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Dart test program for testing file I/O. | 5 // Dart test program for testing file I/O. |
6 | 6 |
7 #import("dart:io"); | 7 #import("dart:io"); |
8 #import("dart:isolate"); | 8 #import("dart:isolate"); |
9 | 9 |
10 class MyListOfOneElement implements List { | 10 class MyListOfOneElement implements List { |
11 int _value; | 11 int _value; |
12 MyListOfOneElement(this._value); | 12 MyListOfOneElement(this._value); |
13 int get length() => 1; | 13 int get length() => 1; |
14 operator [](int index) => _value; | 14 operator [](int index) => _value; |
15 } | 15 } |
16 | 16 |
17 class FileTest { | 17 class FileTest { |
18 static Directory tempDirectory; | 18 static Directory tempDirectory; |
19 static int numLiveAsyncTests = 0; | 19 static int numLiveAsyncTests = 0; |
20 | 20 |
21 static void asyncTestStarted() { ++numLiveAsyncTests; } | 21 static void asyncTestStarted() { ++numLiveAsyncTests; } |
22 static void asyncTestDone(String name) { | 22 static void asyncTestDone(String name) { |
23 --numLiveAsyncTests; | 23 --numLiveAsyncTests; |
24 if (numLiveAsyncTests == 0) { | 24 if (numLiveAsyncTests == 0) { |
25 deleteTempDirectory(); | 25 deleteTempDirectory(); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 static void createTempDirectory(Function doNext) { | 29 static void createTempDirectory(Function doNext) { |
30 tempDirectory = new Directory(''); | 30 new Directory('').createTemp().then((temp) { |
31 tempDirectory.onError = (e) { | 31 tempDirectory = temp; |
32 Expect.fail("Failed creating temporary directory"); | 32 doNext(); |
33 }; | 33 }); |
34 tempDirectory.createTemp(doNext); | |
35 } | 34 } |
36 | 35 |
37 static void deleteTempDirectory() { | 36 static void deleteTempDirectory() { |
38 tempDirectory.deleteRecursivelySync(); | 37 tempDirectory.deleteRecursivelySync(); |
39 } | 38 } |
40 | 39 |
41 // Test for file read functionality. | 40 // Test for file read functionality. |
42 static void testReadStream() { | 41 static void testReadStream() { |
43 // Read a file and check part of it's contents. | 42 // Read a file and check part of it's contents. |
44 String filename = getFilename("bin/file_test.cc"); | 43 String filename = getFilename("bin/file_test.cc"); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 }; | 141 }; |
143 }; | 142 }; |
144 }; | 143 }; |
145 }; | 144 }; |
146 }; | 145 }; |
147 }; | 146 }; |
148 }; | 147 }; |
149 } | 148 } |
150 | 149 |
151 static void testRead() { | 150 static void testRead() { |
151 ReceivePort port = new ReceivePort(); | |
152 // Read a file and check part of it's contents. | 152 // Read a file and check part of it's contents. |
153 String filename = getFilename("bin/file_test.cc"); | 153 String filename = getFilename("bin/file_test.cc"); |
154 File file = new File(filename); | 154 File file = new File(filename); |
155 file.onError = (e) { | 155 file.open(FileMode.READ).then((RandomAccessFile file) { |
156 Expect.fail("No errors expected : $e"); | |
157 }; | |
158 file.open(FileMode.READ, (RandomAccessFile file) { | |
159 List<int> buffer = new List<int>(10); | 156 List<int> buffer = new List<int>(10); |
160 file.readList(buffer, 0, 5, (bytes_read) { | 157 file.readList(buffer, 0, 5).then((bytes_read) { |
161 Expect.equals(5, bytes_read); | 158 Expect.equals(5, bytes_read); |
162 file.readList(buffer, 5, 5, (bytes_read) { | 159 file.readList(buffer, 5, 5).then((bytes_read) { |
163 Expect.equals(5, bytes_read); | 160 Expect.equals(5, bytes_read); |
164 Expect.equals(47, buffer[0]); // represents '/' in the file. | 161 Expect.equals(47, buffer[0]); // represents '/' in the file. |
165 Expect.equals(47, buffer[1]); // represents '/' in the file. | 162 Expect.equals(47, buffer[1]); // represents '/' in the file. |
166 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 163 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
167 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 164 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
168 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 165 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
169 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 166 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
170 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 167 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
171 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 168 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
172 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 169 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
173 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 170 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
174 file.close(() => null); | 171 file.close().then((ignore) => port.close()); |
175 }); | 172 }); |
176 }); | 173 }); |
177 }); | 174 }); |
178 } | 175 } |
179 | 176 |
180 static void testReadSync() { | 177 static void testReadSync() { |
181 // Read a file and check part of it's contents. | 178 // Read a file and check part of it's contents. |
182 String filename = getFilename("bin/file_test.cc"); | 179 String filename = getFilename("bin/file_test.cc"); |
183 RandomAccessFile file = (new File(filename)).openSync(); | 180 RandomAccessFile file = (new File(filename)).openSync(); |
184 List<int> buffer = new List<int>(42); | 181 List<int> buffer = new List<int>(42); |
(...skipping 14 matching lines...) Expand all Loading... | |
199 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 196 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
200 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 197 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
201 Expect.equals(116, buffer[11]); // represents 't' in the file. | 198 Expect.equals(116, buffer[11]); // represents 't' in the file. |
202 } | 199 } |
203 | 200 |
204 // Test for file read and write functionality. | 201 // Test for file read and write functionality. |
205 static void testReadWrite() { | 202 static void testReadWrite() { |
206 // Read a file. | 203 // Read a file. |
207 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 204 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
208 final File file = new File(inFilename); | 205 final File file = new File(inFilename); |
209 file.onError = (e) { | 206 file.open(FileMode.READ).then((RandomAccessFile openedFile) { |
210 Expect.fail("No errors expected : $e"); | |
211 }; | |
212 file.open(FileMode.READ, (RandomAccessFile openedFile) { | |
213 openedFile.onError = (s) { | |
214 Expect.fail("No errors expected : $s"); | |
215 }; | |
216 List<int> buffer1 = new List<int>(42); | 207 List<int> buffer1 = new List<int>(42); |
217 openedFile.readList(buffer1, 0, 42, (bytes_read) { | 208 openedFile.readList(buffer1, 0, 42).then((bytes_read) { |
218 Expect.equals(42, bytes_read); | 209 Expect.equals(42, bytes_read); |
219 openedFile.close(() { | 210 openedFile.close().then((ignore) { |
220 // Write the contents of the file just read into another file. | 211 // Write the contents of the file just read into another file. |
221 String outFilename = tempDirectory.path + "/out_read_write"; | 212 String outFilename = tempDirectory.path + "/out_read_write"; |
222 final File file2 = new File(outFilename); | 213 final File file2 = new File(outFilename); |
223 file2.onError = (e) { | 214 file2.create().then((ignore) { |
224 Expect.fail("No errors expected : $e"); | 215 file2.fullPath().then((s) { |
225 }; | |
226 file2.create(() { | |
227 file2.fullPath((s) { | |
228 Expect.isTrue(new File(s).existsSync()); | 216 Expect.isTrue(new File(s).existsSync()); |
229 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { | 217 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { |
230 Expect.fail("Not a full path"); | 218 Expect.fail("Not a full path"); |
231 } | 219 } |
232 file2.open(FileMode.WRITE, (RandomAccessFile openedFile2) { | 220 file2.open(FileMode.WRITE).then((RandomAccessFile openedFile2) { |
233 openedFile2.onError = (s) { | 221 openedFile2.writeList(buffer1, 0, bytes_read).then((ignore) { |
234 Expect.fail("No errors expected : $s"); | 222 openedFile2.close().then((ignore) { |
235 }; | |
236 openedFile2.writeList(buffer1, 0, bytes_read); | |
237 openedFile2.onNoPendingWrites = () { | |
238 openedFile2.close(() { | |
239 List<int> buffer2 = new List<int>(bytes_read); | 223 List<int> buffer2 = new List<int>(bytes_read); |
240 final File file3 = new File(outFilename); | 224 final File file3 = new File(outFilename); |
241 file3.onError = (e) { | 225 file3.open(FileMode.READ).then((RandomAccessFile openedFile3 ) { |
ricow1
2012/05/10 11:48:01
long line
Mads Ager (google)
2012/05/10 12:42:38
Done.
| |
242 Expect.fail("No errors expected : $e"); | 226 openedFile3.readList(buffer2, 0, 42).then((bytes_read) { |
243 }; | 227 Expect.equals(42, bytes_read); |
244 file3.open(FileMode.READ, (RandomAccessFile openedFile3) { | 228 openedFile3.close().then((ignore) { |
245 openedFile3.onError = (s) { | |
246 Expect.fail("No errors expected : $s"); | |
247 }; | |
248 openedFile3.readList(buffer2, 0, 42, (bytes_read) { | |
249 Expect.equals(42, bytes_read); | |
250 openedFile3.close(() { | |
251 // Now compare the two buffers to check if they | 229 // Now compare the two buffers to check if they |
252 // are identical. | 230 // are identical. |
253 Expect.equals(buffer1.length, buffer2.length); | 231 Expect.equals(buffer1.length, buffer2.length); |
254 for (int i = 0; i < buffer1.length; i++) { | 232 for (int i = 0; i < buffer1.length; i++) { |
255 Expect.equals(buffer1[i], buffer2[i]); | 233 Expect.equals(buffer1[i], buffer2[i]); |
256 } | 234 } |
257 // Delete the output file. | 235 // Delete the output file. |
258 final file4 = file3; | 236 final file4 = file3; |
259 file4.delete(() { | 237 file4.delete().then((ignore) { |
260 file4.exists((exists) { | 238 file4.exists().then((exists) { |
261 Expect.isFalse(exists); | 239 Expect.isFalse(exists); |
262 asyncTestDone("testReadWrite"); | 240 asyncTestDone("testReadWrite"); |
263 }); | 241 }); |
264 }); | 242 }); |
265 }); | 243 }); |
266 }); | 244 }); |
267 }); | 245 }); |
268 }); | 246 }); |
269 }; | 247 }); |
270 }); | 248 }); |
271 }); | 249 }); |
272 }); | 250 }); |
273 }); | 251 }); |
274 }); | 252 }); |
275 }); | 253 }); |
276 asyncTestStarted(); | 254 asyncTestStarted(); |
277 } | 255 } |
278 | 256 |
279 static void testWriteAppend() { | 257 static void testWriteAppend() { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 outStream.close(); | 290 outStream.close(); |
313 outStream.onClosed = () { | 291 outStream.onClosed = () { |
314 File file2 = new File(filename); | 292 File file2 = new File(filename); |
315 OutputStream appendingOutput = | 293 OutputStream appendingOutput = |
316 file2.openOutputStream(FileMode.APPEND); | 294 file2.openOutputStream(FileMode.APPEND); |
317 appendingOutput.write(buffer); | 295 appendingOutput.write(buffer); |
318 appendingOutput.onNoPendingWrites = () { | 296 appendingOutput.onNoPendingWrites = () { |
319 appendingOutput.close(); | 297 appendingOutput.close(); |
320 appendingOutput.onClosed = () { | 298 appendingOutput.onClosed = () { |
321 File file3 = new File(filename); | 299 File file3 = new File(filename); |
322 file3.open(FileMode.READ, (RandomAccessFile openedFile) { | 300 file3.open(FileMode.READ).then((RandomAccessFile openedFile) { |
323 openedFile.length((int length) { | 301 openedFile.length().then((int length) { |
324 Expect.equals(content.length * 2, length); | 302 Expect.equals(content.length * 2, length); |
325 openedFile.close(() { | 303 openedFile.close().then((ignore) { |
326 file3.delete(() { | 304 file3.delete().then((ignore) { |
327 asyncTestDone("testOutputStreamWriteAppend"); | 305 asyncTestDone("testOutputStreamWriteAppend"); |
328 }); | 306 }); |
329 }); | 307 }); |
330 }); | 308 }); |
331 }); | 309 }); |
332 }; | 310 }; |
333 }; | 311 }; |
334 }; | 312 }; |
335 }; | 313 }; |
336 asyncTestStarted(); | 314 asyncTestStarted(); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
404 file.createSync(); | 382 file.createSync(); |
405 RandomAccessFile openedFile = file.openSync(); | 383 RandomAccessFile openedFile = file.openSync(); |
406 Expect.equals(-1, openedFile.readByteSync()); | 384 Expect.equals(-1, openedFile.readByteSync()); |
407 openedFile.closeSync(); | 385 openedFile.closeSync(); |
408 file.deleteSync(); | 386 file.deleteSync(); |
409 } | 387 } |
410 | 388 |
411 static void testReadEmptyFile() { | 389 static void testReadEmptyFile() { |
412 String fileName = tempDirectory.path + "/empty_file"; | 390 String fileName = tempDirectory.path + "/empty_file"; |
413 File file = new File(fileName); | 391 File file = new File(fileName); |
414 file.onError = (e) { | |
415 Expect.fail("No errors expected : $e"); | |
416 }; | |
417 asyncTestStarted(); | 392 asyncTestStarted(); |
418 file.create(() { | 393 file.create().then((ignore) { |
419 file.open(FileMode.READ, (RandomAccessFile openedFile) { | 394 file.open(FileMode.READ).then((RandomAccessFile openedFile) { |
420 openedFile.readByte((int byte) { | 395 var readByteFuture = openedFile.readByte(); |
396 readByteFuture.then((int byte) { | |
421 Expect.equals(-1, byte); | 397 Expect.equals(-1, byte); |
422 }); | 398 }); |
423 openedFile.onError = (e) { | 399 readByteFuture.handleException((e) { |
424 Expect.isTrue(e is FileIOException); | 400 Expect.isTrue(e is FileIOException); |
ricow1
2012/05/10 11:48:01
Don't we need to return true from this closure
Mads Ager (google)
2012/05/10 12:42:38
Yes, thank you!!!
This test is completely wrong.
| |
425 openedFile.close(() { | 401 openedFile.close().then((ignore) { |
426 file.delete(() { | 402 file.delete().then((ignore) { |
427 asyncTestDone("testReadEmptyFile"); | 403 asyncTestDone("testReadEmptyFile"); |
428 }); | 404 }); |
429 }); | 405 }); |
430 }; | 406 }); |
431 }); | 407 }); |
432 }); | 408 }); |
433 } | 409 } |
434 | 410 |
435 // Test for file write of different types of lists. | 411 // Test for file write of different types of lists. |
436 static void testWriteVariousLists() { | 412 static void testWriteVariousLists() { |
437 asyncTestStarted(); | 413 asyncTestStarted(); |
438 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; | 414 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; |
439 final File file = new File(fileName); | 415 final File file = new File(fileName); |
440 file.onError = (e) => Expect.fail("No errors expected : $e"); | 416 file.create().then((ignore) { |
441 file.create(() { | 417 file.open(FileMode.WRITE).then((RandomAccessFile openedFile) { |
442 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { | |
443 // Write bytes from 0 to 7. | 418 // Write bytes from 0 to 7. |
444 openedFile.writeList([0], 0, 1); | 419 openedFile.writeList([0], 0, 1); |
445 openedFile.writeList(const [1], 0, 1); | 420 openedFile.writeList(const [1], 0, 1); |
446 openedFile.writeList(new MyListOfOneElement(2), 0, 1); | 421 openedFile.writeList(new MyListOfOneElement(2), 0, 1); |
447 var x = 12345678901234567890123456789012345678901234567890; | 422 var x = 12345678901234567890123456789012345678901234567890; |
448 var y = 12345678901234567890123456789012345678901234567893; | 423 var y = 12345678901234567890123456789012345678901234567893; |
449 openedFile.writeList([y - x], 0, 1); | 424 openedFile.writeList([y - x], 0, 1); |
450 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. | 425 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. |
451 openedFile.writeList(const [261], 0, 1); | 426 openedFile.writeList(const [261], 0, 1); |
452 openedFile.writeList(new MyListOfOneElement(262), 0, 1); | 427 openedFile.writeList(new MyListOfOneElement(262), 0, 1); |
453 x = 12345678901234567890123456789012345678901234567890; | 428 x = 12345678901234567890123456789012345678901234567890; |
454 y = 12345678901234567890123456789012345678901234568153; | 429 y = 12345678901234567890123456789012345678901234568153; |
455 openedFile.writeList([y - x], 0, 1); | 430 openedFile.writeList([y - x], 0, 1).then((ignore) { |
456 | 431 openedFile.close().then((ignore) { |
457 openedFile.onError = (e) => Expect.fail("No errors expected : $e"); | |
458 openedFile.onNoPendingWrites = () { | |
459 openedFile.close(() { | |
460 // Check the written bytes. | 432 // Check the written bytes. |
461 final File file2 = new File(fileName); | 433 final File file2 = new File(fileName); |
462 var openedFile2 = file2.openSync(); | 434 var openedFile2 = file2.openSync(); |
463 var length = openedFile2.lengthSync(); | 435 var length = openedFile2.lengthSync(); |
464 Expect.equals(8, length); | 436 Expect.equals(8, length); |
465 List data = new List(length); | 437 List data = new List(length); |
466 openedFile2.readListSync(data, 0, length); | 438 openedFile2.readListSync(data, 0, length); |
467 for (var i = 0; i < data.length; i++) { | 439 for (var i = 0; i < data.length; i++) { |
468 Expect.equals(i, data[i]); | 440 Expect.equals(i, data[i]); |
469 } | 441 } |
470 openedFile2.closeSync(); | 442 openedFile2.closeSync(); |
471 file2.deleteSync(); | 443 file2.deleteSync(); |
472 asyncTestDone("testWriteVariousLists"); | 444 asyncTestDone("testWriteVariousLists"); |
473 }); | 445 }); |
474 }; | 446 }); |
475 }); | 447 }); |
476 }); | 448 }); |
477 } | 449 } |
478 | 450 |
479 static void testDirectory() { | 451 static void testDirectory() { |
480 asyncTestStarted(); | 452 asyncTestStarted(); |
481 | 453 |
482 // Port to verify that the test completes. | 454 // Port to verify that the test completes. |
483 var port = new ReceivePort(); | 455 var port = new ReceivePort(); |
484 port.receive((message, replyTo) { | 456 port.receive((message, replyTo) { |
485 port.close(); | 457 port.close(); |
486 Expect.equals(1, message); | 458 Expect.equals(1, message); |
487 asyncTestDone("testDirectory"); | 459 asyncTestDone("testDirectory"); |
488 }); | 460 }); |
489 | 461 |
490 var tempDir = tempDirectory.path; | 462 var tempDir = tempDirectory.path; |
491 var file = new File("${tempDir}/testDirectory"); | 463 var file = new File("${tempDir}/testDirectory"); |
492 var errors = 0; | 464 var errors = 0; |
493 file.directory((d) => Expect.fail("non-existing file")); | 465 var dirFuture = file.directory(); |
494 file.onError = (e) { | 466 dirFuture.then((d) => Expect.fail("non-existing file")); |
495 file.onError = (e) => Expect.fail("no error expected"); | 467 dirFuture.handleException((e) { |
ricow1
2012/05/10 11:48:01
this is really hard to read, could we call dirFutu
Mads Ager (google)
2012/05/10 12:42:38
Done.
| |
496 file.create(() { | 468 file.create().then((ignore) { |
497 file.directory((Directory d) { | 469 file.directory().then((Directory d) { |
498 d.onError = (s) => Expect.fail("no error expected"); | 470 d.exists().then((exists) { |
499 d.exists((exists) { | |
500 Expect.isTrue(exists); | 471 Expect.isTrue(exists); |
501 Expect.isTrue(d.path.endsWith(tempDir)); | 472 Expect.isTrue(d.path.endsWith(tempDir)); |
502 file.delete(() { | 473 file.delete().then((ignore) { |
503 var file_dir = new File("."); | 474 var fileDir = new File("."); |
504 file_dir.directory((d) => Expect.fail("non-existing file")); | 475 dirFuture = fileDir.directory(); |
505 file_dir.onError = (e) { | 476 dirFuture.then((d) => Expect.fail("non-existing file")); |
506 var file_dir = new File(tempDir); | 477 dirFuture.handleException((e) { |
507 file_dir.directory((d) => Expect.fail("non-existing file")); | 478 var fileDir = new File(tempDir); |
508 file_dir.onError = (e) => port.toSendPort().send(1); | 479 dirFuture = fileDir.directory(); |
509 }; | 480 dirFuture.then((d) => Expect.fail("non-existing file")); |
481 dirFuture.handleException((e) { | |
482 port.toSendPort().send(1); | |
483 return true; | |
484 }); | |
485 return true; | |
486 }); | |
510 }); | 487 }); |
511 }); | 488 }); |
512 }); | 489 }); |
513 }); | 490 }); |
514 }; | 491 return true; |
492 }); | |
515 } | 493 } |
516 | 494 |
517 static void testDirectorySync() { | 495 static void testDirectorySync() { |
518 var tempDir = tempDirectory.path; | 496 var tempDir = tempDirectory.path; |
519 var file = new File("${tempDir}/testDirectorySync"); | 497 var file = new File("${tempDir}/testDirectorySync"); |
520 // Non-existing file should throw exception. | 498 // Non-existing file should throw exception. |
521 Expect.throws(file.directorySync, (e) { return e is FileIOException; }); | 499 Expect.throws(file.directorySync, (e) { return e is FileIOException; }); |
522 file.createSync(); | 500 file.createSync(); |
523 // Check that the path of the returned directory is the temp directory. | 501 // Check that the path of the returned directory is the temp directory. |
524 Directory d = file.directorySync(); | 502 Directory d = file.directorySync(); |
525 Expect.isTrue(d.existsSync()); | 503 Expect.isTrue(d.existsSync()); |
526 Expect.isTrue(d.path.endsWith(tempDir)); | 504 Expect.isTrue(d.path.endsWith(tempDir)); |
527 file.deleteSync(); | 505 file.deleteSync(); |
528 // Directories should throw exception. | 506 // Directories should throw exception. |
529 var file_dir = new File("."); | 507 var file_dir = new File("."); |
530 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); | 508 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); |
531 file_dir = new File(tempDir); | 509 file_dir = new File(tempDir); |
532 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); | 510 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); |
533 } | 511 } |
534 | 512 |
535 // Test for file length functionality. | 513 // Test for file length functionality. |
536 static void testLength() { | 514 static void testLength() { |
515 var port = new ReceivePort(); | |
537 String filename = getFilename("tests/vm/data/fixed_length_file"); | 516 String filename = getFilename("tests/vm/data/fixed_length_file"); |
538 File file = new File(filename); | 517 File file = new File(filename); |
539 RandomAccessFile openedFile = file.openSync(); | 518 RandomAccessFile openedFile = file.openSync(); |
540 openedFile.onError = (e) => Expect.fail("No errors expected"); | 519 openedFile.length().then((length) { |
541 file.onError = (e) => Expect.fail("No errors expected"); | |
542 openedFile.length((length) { | |
543 Expect.equals(42, length); | 520 Expect.equals(42, length); |
544 openedFile.close(() => null); | 521 openedFile.close().then((ignore) => port.close()); |
545 }); | 522 }); |
546 file.length((length) { | 523 file.length().then((length) { |
547 Expect.equals(42, length); | 524 Expect.equals(42, length); |
548 }); | 525 }); |
549 } | 526 } |
550 | 527 |
551 static void testLengthSync() { | 528 static void testLengthSync() { |
552 String filename = getFilename("tests/vm/data/fixed_length_file"); | 529 String filename = getFilename("tests/vm/data/fixed_length_file"); |
553 File file = new File(filename); | 530 File file = new File(filename); |
554 RandomAccessFile openedFile = file.openSync(); | 531 RandomAccessFile openedFile = file.openSync(); |
555 Expect.equals(42, file.lengthSync()); | 532 Expect.equals(42, file.lengthSync()); |
556 Expect.equals(42, openedFile.lengthSync()); | 533 Expect.equals(42, openedFile.lengthSync()); |
557 openedFile.closeSync(); | 534 openedFile.closeSync(); |
558 } | 535 } |
559 | 536 |
560 // Test for file position functionality. | 537 // Test for file position functionality. |
561 static void testPosition() { | 538 static void testPosition() { |
539 var port = new ReceivePort(); | |
562 String filename = getFilename("tests/vm/data/fixed_length_file"); | 540 String filename = getFilename("tests/vm/data/fixed_length_file"); |
563 RandomAccessFile input = (new File(filename)).openSync(); | 541 RandomAccessFile input = (new File(filename)).openSync(); |
564 input.onError = (e) => Expect.fail("No errors expected"); | 542 input.position().then((position) { |
565 input.position((position) { | |
566 Expect.equals(0, position); | 543 Expect.equals(0, position); |
567 List<int> buffer = new List<int>(100); | 544 List<int> buffer = new List<int>(100); |
568 input.readList(buffer, 0, 12, (bytes_read) { | 545 input.readList(buffer, 0, 12).then((bytes_read) { |
569 input.position((position) { | 546 input.position().then((position) { |
570 Expect.equals(12, position); | 547 Expect.equals(12, position); |
571 input.readList(buffer, 12, 6, (bytes_read) { | 548 input.readList(buffer, 12, 6).then((bytes_read) { |
572 input.position((position) { | 549 input.position().then((position) { |
573 Expect.equals(18, position); | 550 Expect.equals(18, position); |
574 input.setPosition(8, () { | 551 input.setPosition(8).then((ignore) { |
575 input.position((position) { | 552 input.position().then((position) { |
576 Expect.equals(8, position); | 553 Expect.equals(8, position); |
577 input.close(() => null); | 554 input.close().then((ignore) => port.close()); |
578 }); | 555 }); |
579 }); | 556 }); |
580 }); | 557 }); |
581 }); | 558 }); |
582 }); | 559 }); |
583 }); | 560 }); |
584 }); | 561 }); |
585 } | 562 } |
586 | 563 |
587 static void testPositionSync() { | 564 static void testPositionSync() { |
588 String filename = getFilename("tests/vm/data/fixed_length_file"); | 565 String filename = getFilename("tests/vm/data/fixed_length_file"); |
589 RandomAccessFile input = (new File(filename)).openSync(); | 566 RandomAccessFile input = (new File(filename)).openSync(); |
590 Expect.equals(0, input.positionSync()); | 567 Expect.equals(0, input.positionSync()); |
591 List<int> buffer = new List<int>(100); | 568 List<int> buffer = new List<int>(100); |
592 input.readListSync(buffer, 0, 12); | 569 input.readListSync(buffer, 0, 12); |
593 Expect.equals(12, input.positionSync()); | 570 Expect.equals(12, input.positionSync()); |
594 input.readListSync(buffer, 12, 6); | 571 input.readListSync(buffer, 12, 6); |
595 Expect.equals(18, input.positionSync()); | 572 Expect.equals(18, input.positionSync()); |
596 input.setPositionSync(8); | 573 input.setPositionSync(8); |
597 Expect.equals(8, input.positionSync()); | 574 Expect.equals(8, input.positionSync()); |
598 input.closeSync(); | 575 input.closeSync(); |
599 } | 576 } |
600 | 577 |
601 static void testTruncate() { | 578 static void testTruncate() { |
602 File file = new File(tempDirectory.path + "/out_truncate"); | 579 File file = new File(tempDirectory.path + "/out_truncate"); |
603 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 580 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
604 file.onError = (e) => Expect.fail("No errors expected: $e"); | 581 file.open(FileMode.WRITE).then((RandomAccessFile openedFile) { |
605 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { | 582 openedFile.writeList(buffer, 0, 10).then((ignore) { |
606 openedFile.writeList(buffer, 0, 10); | 583 openedFile.length().then((length) { |
607 openedFile.onNoPendingWrites = () { | |
608 openedFile.length((length) { | |
609 Expect.equals(10, length); | 584 Expect.equals(10, length); |
610 openedFile.truncate(5, () { | 585 openedFile.truncate(5).then((ignore) { |
611 openedFile.length((length) { | 586 openedFile.length().then((length) { |
612 Expect.equals(5, length); | 587 Expect.equals(5, length); |
613 openedFile.close(() { | 588 openedFile.close().then((ignore) { |
614 file.delete(() { | 589 file.delete().then((ignore) { |
615 file.exists((exists) { | 590 file.exists().then((exists) { |
616 Expect.isFalse(exists); | 591 Expect.isFalse(exists); |
617 asyncTestDone("testTruncate"); | 592 asyncTestDone("testTruncate"); |
618 }); | 593 }); |
619 }); | 594 }); |
620 }); | 595 }); |
621 }); | 596 }); |
622 }); | 597 }); |
623 }); | 598 }); |
624 }; | 599 }); |
625 }); | 600 }); |
626 asyncTestStarted(); | 601 asyncTestStarted(); |
627 } | 602 } |
628 | 603 |
629 static void testTruncateSync() { | 604 static void testTruncateSync() { |
630 File file = new File(tempDirectory.path + "/out_truncate_sync"); | 605 File file = new File(tempDirectory.path + "/out_truncate_sync"); |
631 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 606 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
632 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); | 607 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); |
633 openedFile.writeListSync(buffer, 0, 10); | 608 openedFile.writeListSync(buffer, 0, 10); |
634 Expect.equals(10, openedFile.lengthSync()); | 609 Expect.equals(10, openedFile.lengthSync()); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
842 wrongExceptionCaught = true; | 817 wrongExceptionCaught = true; |
843 } | 818 } |
844 Expect.equals(true, exceptionCaught); | 819 Expect.equals(true, exceptionCaught); |
845 Expect.equals(true, !wrongExceptionCaught); | 820 Expect.equals(true, !wrongExceptionCaught); |
846 openedFile.closeSync(); | 821 openedFile.closeSync(); |
847 file.deleteSync(); | 822 file.deleteSync(); |
848 } | 823 } |
849 | 824 |
850 static void testOpenDirectoryAsFile() { | 825 static void testOpenDirectoryAsFile() { |
851 var f = new File('.'); | 826 var f = new File('.'); |
852 f.open(FileMode.READ, (r) => Expect.fail('Directory opened as file')); | 827 var future = f.open(FileMode.READ); |
853 f.onError = (e) => null; | 828 future.then((r) => Expect.fail('Directory opened as file')); |
829 future.handleException((e) => true); | |
854 } | 830 } |
855 | 831 |
856 static void testOpenDirectoryAsFileSync() { | 832 static void testOpenDirectoryAsFileSync() { |
857 var f = new File('.'); | 833 var f = new File('.'); |
858 try { | 834 try { |
859 f.openSync(); | 835 f.openSync(); |
860 Expect.fail("Expected exception opening directory as file"); | 836 Expect.fail("Expected exception opening directory as file"); |
861 } catch (var e) { | 837 } catch (var e) { |
862 Expect.isTrue(e is FileIOException); | 838 Expect.isTrue(e is FileIOException); |
863 } | 839 } |
864 } | 840 } |
865 | 841 |
866 static void testReadAsBytes() { | 842 static void testReadAsBytes() { |
867 var port = new ReceivePort(); | 843 var port = new ReceivePort(); |
868 port.receive((result, replyTo) { | 844 port.receive((result, replyTo) { |
869 port.close(); | 845 port.close(); |
870 Expect.equals(42, result); | 846 Expect.equals(42, result); |
871 }); | 847 }); |
872 var name = getFilename("tests/vm/data/fixed_length_file"); | 848 var name = getFilename("tests/vm/data/fixed_length_file"); |
873 var f = new File(name); | 849 var f = new File(name); |
874 f.readAsBytes((bytes) { | 850 f.readAsBytes().then((bytes) { |
875 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 851 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
876 port.toSendPort().send(bytes.length); | 852 port.toSendPort().send(bytes.length); |
877 }); | 853 }); |
878 f.onError = (e) => Expect.fail("No errors expected: $e"); | |
879 } | 854 } |
880 | 855 |
881 static void testReadAsBytesSync() { | 856 static void testReadAsBytesSync() { |
882 var name = getFilename("tests/vm/data/fixed_length_file"); | 857 var name = getFilename("tests/vm/data/fixed_length_file"); |
883 var bytes = new File(name).readAsBytesSync(); | 858 var bytes = new File(name).readAsBytesSync(); |
884 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 859 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
885 Expect.equals(bytes.length, 42); | 860 Expect.equals(bytes.length, 42); |
886 } | 861 } |
887 | 862 |
888 static void testReadAsText() { | 863 static void testReadAsText() { |
889 var port = new ReceivePort(); | 864 var port = new ReceivePort(); |
890 port.receive((result, replyTo) { | 865 port.receive((result, replyTo) { |
891 port.close(); | 866 port.close(); |
892 Expect.equals(1, result); | 867 Expect.equals(1, result); |
893 }); | 868 }); |
894 var name = getFilename("tests/vm/data/fixed_length_file"); | 869 var name = getFilename("tests/vm/data/fixed_length_file"); |
895 var f = new File(name); | 870 var f = new File(name); |
896 f.readAsText(Encoding.UTF_8, (text) { | 871 f.readAsText(Encoding.UTF_8).then((text) { |
897 Expect.isTrue(text.endsWith("42 bytes.")); | 872 Expect.isTrue(text.endsWith("42 bytes.")); |
898 Expect.equals(42, text.length); | 873 Expect.equals(42, text.length); |
899 var name = getDataFilename("tests/standalone/io/read_as_text.dat"); | 874 var name = getDataFilename("tests/standalone/io/read_as_text.dat"); |
900 var f = new File(name); | 875 var f = new File(name); |
901 f.onError = (e) => Expect.fail("No errors expected: $e"); | 876 f.readAsText(Encoding.UTF_8).then((text) { |
902 f.readAsText(Encoding.UTF_8, (text) { | |
903 Expect.equals(6, text.length); | 877 Expect.equals(6, text.length); |
904 var expected = [955, 120, 46, 32, 120, 10]; | 878 var expected = [955, 120, 46, 32, 120, 10]; |
905 Expect.listEquals(expected, text.charCodes()); | 879 Expect.listEquals(expected, text.charCodes()); |
906 f.readAsText(Encoding.ISO_8859_1, (text) { | 880 f.readAsText(Encoding.ISO_8859_1).then((text) { |
907 Expect.equals(7, text.length); | 881 Expect.equals(7, text.length); |
908 var expected = [206, 187, 120, 46, 32, 120, 10]; | 882 var expected = [206, 187, 120, 46, 32, 120, 10]; |
909 Expect.listEquals(expected, text.charCodes()); | 883 Expect.listEquals(expected, text.charCodes()); |
910 f.onError = (e) { | 884 var readAsTextFuture = f.readAsText(Encoding.ASCII); |
885 readAsTextFuture.then((text) { | |
886 Expect.fail("Non-ascii char should cause error"); | |
887 }); | |
888 readAsTextFuture.handleException((e) { | |
911 port.toSendPort().send(1); | 889 port.toSendPort().send(1); |
912 }; | 890 return true; |
913 f.readAsText(Encoding.ASCII, (text) { | |
914 Expect.fail("Non-ascii char should cause error"); | |
915 }); | 891 }); |
916 }); | 892 }); |
917 }); | 893 }); |
918 }); | 894 }); |
919 f.onError = (e) { | |
920 Expect.fail("No errors expected: $e"); | |
921 }; | |
922 } | 895 } |
923 | 896 |
924 static void testReadAsTextSync() { | 897 static void testReadAsTextSync() { |
925 var name = getFilename("tests/vm/data/fixed_length_file"); | 898 var name = getFilename("tests/vm/data/fixed_length_file"); |
926 var text = new File(name).readAsTextSync(); | 899 var text = new File(name).readAsTextSync(); |
927 Expect.isTrue(text.endsWith("42 bytes.")); | 900 Expect.isTrue(text.endsWith("42 bytes.")); |
928 Expect.equals(42, text.length); | 901 Expect.equals(42, text.length); |
929 name = getDataFilename("tests/standalone/io/read_as_text.dat"); | 902 name = getDataFilename("tests/standalone/io/read_as_text.dat"); |
930 text = new File(name).readAsTextSync(); | 903 text = new File(name).readAsTextSync(); |
931 Expect.equals(6, text.length); | 904 Expect.equals(6, text.length); |
932 var expected = [955, 120, 46, 32, 120, 10]; | 905 var expected = [955, 120, 46, 32, 120, 10]; |
933 Expect.listEquals(expected, text.charCodes()); | 906 Expect.listEquals(expected, text.charCodes()); |
934 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); }); | 907 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); }); |
935 text = new File(name).readAsTextSync(Encoding.ISO_8859_1); | 908 text = new File(name).readAsTextSync(Encoding.ISO_8859_1); |
936 expected = [206, 187, 120, 46, 32, 120, 10]; | 909 expected = [206, 187, 120, 46, 32, 120, 10]; |
937 Expect.equals(7, text.length); | 910 Expect.equals(7, text.length); |
938 Expect.listEquals(expected, text.charCodes()); | 911 Expect.listEquals(expected, text.charCodes()); |
939 } | 912 } |
940 | 913 |
941 static void testReadAsLines() { | 914 static void testReadAsLines() { |
942 var port = new ReceivePort(); | 915 var port = new ReceivePort(); |
943 port.receive((result, replyTo) { | 916 port.receive((result, replyTo) { |
944 port.close(); | 917 port.close(); |
945 Expect.equals(42, result); | 918 Expect.equals(42, result); |
946 }); | 919 }); |
947 var name = getFilename("tests/vm/data/fixed_length_file"); | 920 var name = getFilename("tests/vm/data/fixed_length_file"); |
948 var f = new File(name); | 921 var f = new File(name); |
949 f.readAsLines(Encoding.UTF_8, (lines) { | 922 f.readAsLines(Encoding.UTF_8).then((lines) { |
950 Expect.equals(1, lines.length); | 923 Expect.equals(1, lines.length); |
951 var line = lines[0]; | 924 var line = lines[0]; |
952 Expect.isTrue(line.endsWith("42 bytes.")); | 925 Expect.isTrue(line.endsWith("42 bytes.")); |
953 port.toSendPort().send(line.length); | 926 port.toSendPort().send(line.length); |
954 }); | 927 }); |
955 f.onError = (e) => Expect.fail("No errors expected: $e"); | |
956 } | 928 } |
957 | 929 |
958 static void testReadAsLinesSync() { | 930 static void testReadAsLinesSync() { |
959 var name = getFilename("tests/vm/data/fixed_length_file"); | 931 var name = getFilename("tests/vm/data/fixed_length_file"); |
960 var lines = new File(name).readAsLinesSync(); | 932 var lines = new File(name).readAsLinesSync(); |
961 Expect.equals(1, lines.length); | 933 Expect.equals(1, lines.length); |
962 var line = lines[0]; | 934 var line = lines[0]; |
963 Expect.isTrue(line.endsWith("42 bytes.")); | 935 Expect.isTrue(line.endsWith("42 bytes.")); |
964 Expect.equals(42, line.length); | 936 Expect.equals(42, line.length); |
965 name = getDataFilename("tests/standalone/io/readline_test1.dat"); | 937 name = getDataFilename("tests/standalone/io/readline_test1.dat"); |
966 lines = new File(name).readAsLinesSync(); | 938 lines = new File(name).readAsLinesSync(); |
967 Expect.equals(10, lines.length); | 939 Expect.equals(10, lines.length); |
968 } | 940 } |
969 | 941 |
970 | 942 |
971 static void testReadAsErrors() { | 943 static void testReadAsErrors() { |
972 var port = new ReceivePort(); | 944 var port = new ReceivePort(); |
973 port.receive((message, _) { | 945 port.receive((message, _) { |
974 port.close(); | 946 port.close(); |
975 Expect.equals(1, message); | 947 Expect.equals(1, message); |
976 }); | 948 }); |
977 var f = new File('.'); | 949 var f = new File('.'); |
978 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); | 950 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); |
979 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); | 951 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); |
980 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); | 952 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); |
981 f.readAsBytes((bytes) => Expect.fail("no bytes expected")); | 953 var readAsBytesFuture = f.readAsBytes(); |
982 f.onError = (e) { | 954 readAsBytesFuture.then((bytes) => Expect.fail("no bytes expected")); |
983 f.readAsText(Encoding.UTF_8, (text) => Expect.fail("no text expected")); | 955 readAsBytesFuture.handleException((e) { |
984 f.onError = (e) { | 956 var readAsTextFuture = f.readAsText(Encoding.UTF_8); |
985 f.readAsLines(Encoding.UTF_8, | 957 readAsTextFuture.then((text) => Expect.fail("no text expected")); |
986 (lines) => Expect.fail("no lines expected")); | 958 readAsTextFuture.handleException((e) { |
987 f.onError = (e) => port.toSendPort().send(1); | 959 var readAsLinesFuture = f.readAsLines(Encoding.UTF_8); |
988 }; | 960 readAsLinesFuture.then((lines) => Expect.fail("no lines expected")); |
989 }; | 961 readAsLinesFuture.handleException((e) { |
962 port.toSendPort().send(1); | |
963 return true; | |
964 }); | |
965 return true; | |
966 }); | |
967 return true; | |
968 }); | |
990 } | 969 } |
991 | 970 |
992 // Test that opens the same file for writing then for appending to test | 971 // Test that opens the same file for writing then for appending to test |
993 // that the file is not truncated when opened for appending. | 972 // that the file is not truncated when opened for appending. |
994 static void testAppend() { | 973 static void testAppend() { |
995 var file = new File('${tempDirectory.path}/out_append'); | 974 var file = new File('${tempDirectory.path}/out_append'); |
996 file.open(FileMode.WRITE, (openedFile) { | 975 file.open(FileMode.WRITE).then((openedFile) { |
997 openedFile.writeString("asdf"); | 976 openedFile.writeString("asdf").then((ignore) { |
998 openedFile.onNoPendingWrites = () { | 977 openedFile.close().then((ignore) { |
999 openedFile.close(() { | 978 file.open(FileMode.APPEND).then((openedFile) { |
1000 file.open(FileMode.APPEND, (openedFile) { | 979 openedFile.length().then((length) { |
1001 openedFile.length((length) { | |
1002 Expect.equals(4, length); | 980 Expect.equals(4, length); |
1003 openedFile.writeString("asdf"); | 981 openedFile.writeString("asdf").then((ignore) { |
1004 openedFile.onNoPendingWrites = () { | 982 openedFile.length().then((length) { |
1005 openedFile.length((length) { | |
1006 Expect.equals(8, length); | 983 Expect.equals(8, length); |
1007 openedFile.close(() { | 984 openedFile.close().then((ignore) { |
1008 file.delete(() { | 985 file.delete().then((ignore) { |
1009 file.exists((exists) { | 986 file.exists().then((exists) { |
1010 Expect.isFalse(exists); | 987 Expect.isFalse(exists); |
1011 asyncTestDone("testAppend"); | 988 asyncTestDone("testAppend"); |
1012 }); | 989 }); |
1013 }); | 990 }); |
1014 }); | 991 }); |
1015 }); | 992 }); |
1016 }; | 993 }); |
1017 }); | 994 }); |
1018 }); | 995 }); |
1019 }); | 996 }); |
1020 }; | 997 }); |
1021 }); | 998 }); |
1022 asyncTestStarted(); | 999 asyncTestStarted(); |
1023 } | 1000 } |
1024 | 1001 |
1025 static void testAppendSync() { | 1002 static void testAppendSync() { |
1026 var file = new File('${tempDirectory.path}/out_append_sync'); | 1003 var file = new File('${tempDirectory.path}/out_append_sync'); |
1027 var openedFile = file.openSync(FileMode.WRITE); | 1004 var openedFile = file.openSync(FileMode.WRITE); |
1028 openedFile.writeStringSync("asdf"); | 1005 openedFile.writeStringSync("asdf"); |
1029 Expect.equals(4, openedFile.lengthSync()); | 1006 Expect.equals(4, openedFile.lengthSync()); |
1030 openedFile.closeSync(); | 1007 openedFile.closeSync(); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1083 testWriteVariousLists(); | 1060 testWriteVariousLists(); |
1084 testDirectory(); | 1061 testDirectory(); |
1085 testDirectorySync(); | 1062 testDirectorySync(); |
1086 }); | 1063 }); |
1087 } | 1064 } |
1088 } | 1065 } |
1089 | 1066 |
1090 main() { | 1067 main() { |
1091 FileTest.testMain(); | 1068 FileTest.testMain(); |
1092 } | 1069 } |
OLD | NEW |