| 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 | 8 |
| 9 class MyListOfOneElement implements List { | 9 class MyListOfOneElement implements List { |
| 10 int _value; | 10 int _value; |
| 11 MyListOfOneElement(this._value); | 11 MyListOfOneElement(this._value); |
| 12 int get length() => 1; | 12 int get length() => 1; |
| 13 operator [](int index) => _value; | 13 operator [](int index) => _value; |
| 14 } | 14 } |
| 15 | 15 |
| 16 class FileTest { | 16 class FileTest { |
| 17 static Directory tempDirectory; | 17 static Directory tempDirectory; |
| 18 static int numLiveAsyncTests = 0; | 18 static int numLiveAsyncTests = 0; |
| 19 | 19 |
| 20 static void asyncTestStarted() { ++numLiveAsyncTests; } | 20 static void asyncTestStarted() { ++numLiveAsyncTests; } |
| 21 static void asyncTestDone(String name) { | 21 static void asyncTestDone(String name) { |
| 22 --numLiveAsyncTests; | 22 --numLiveAsyncTests; |
| 23 if (numLiveAsyncTests == 0) { | 23 if (numLiveAsyncTests == 0) { |
| 24 deleteTempDirectory(); | 24 deleteTempDirectory(); |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 | 27 |
| 28 static void createTempDirectory(Function doNext) { | 28 static void createTempDirectory(Function doNext) { |
| 29 tempDirectory = new Directory(''); | 29 tempDirectory = new Directory(''); |
| 30 tempDirectory.errorHandler = (e) { |
| 31 Expect.fail("Failed creating temporary directory"); |
| 32 }; |
| 30 tempDirectory.createTempHandler = doNext; | 33 tempDirectory.createTempHandler = doNext; |
| 31 tempDirectory.createTemp(); | 34 tempDirectory.createTemp(); |
| 32 } | 35 } |
| 33 | 36 |
| 34 static void deleteTempDirectory() { | 37 static void deleteTempDirectory() { |
| 35 tempDirectory.deleteSync(); | 38 tempDirectory.deleteSync(recursive: true); |
| 36 } | 39 } |
| 37 | 40 |
| 38 // Test for file read functionality. | 41 // Test for file read functionality. |
| 39 static void testReadStream() { | 42 static void testReadStream() { |
| 40 // Read a file and check part of it's contents. | 43 // Read a file and check part of it's contents. |
| 41 String filename = getFilename("bin/file_test.cc"); | 44 String filename = getFilename("bin/file_test.cc"); |
| 42 File file = new File(filename); | 45 File file = new File(filename); |
| 43 InputStream input = file.openInputStreamSync(); | 46 InputStream input = file.openInputStreamSync(); |
| 44 List<int> buffer = new List<int>(42); | 47 List<int> buffer = new List<int>(42); |
| 45 int bytesRead = input.readInto(buffer, 0, 12); | 48 int bytesRead = input.readInto(buffer, 0, 12); |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 openedFile.readByte(); | 400 openedFile.readByte(); |
| 398 }; | 401 }; |
| 399 file.open(); | 402 file.open(); |
| 400 }; | 403 }; |
| 401 asyncTestStarted(); | 404 asyncTestStarted(); |
| 402 file.create(); | 405 file.create(); |
| 403 } | 406 } |
| 404 | 407 |
| 405 // Test for file write of different types of lists. | 408 // Test for file write of different types of lists. |
| 406 static void testWriteVariousLists() { | 409 static void testWriteVariousLists() { |
| 410 asyncTestStarted(); |
| 407 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; | 411 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; |
| 408 final File file = new File(fileName); | 412 final File file = new File(fileName); |
| 409 file.create(); | 413 file.create(); |
| 410 file.createHandler = () { | 414 file.createHandler = () { |
| 411 file.open(FileMode.WRITE); | 415 file.open(FileMode.WRITE); |
| 412 file.openHandler = (RandomAccessFile openedFile) { | 416 file.openHandler = (RandomAccessFile openedFile) { |
| 413 // Write bytes from 0 to 7. | 417 // Write bytes from 0 to 7. |
| 414 openedFile.writeList([0], 0, 1); | 418 openedFile.writeList([0], 0, 1); |
| 415 openedFile.writeList(const [1], 0, 1); | 419 openedFile.writeList(const [1], 0, 1); |
| 416 openedFile.writeList(new MyListOfOneElement(2), 0, 1); | 420 openedFile.writeList(new MyListOfOneElement(2), 0, 1); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 436 var openedFile2 = file2.openSync(); | 440 var openedFile2 = file2.openSync(); |
| 437 var length = openedFile2.lengthSync(); | 441 var length = openedFile2.lengthSync(); |
| 438 Expect.equals(8, length); | 442 Expect.equals(8, length); |
| 439 List data = new List(length); | 443 List data = new List(length); |
| 440 openedFile2.readListSync(data, 0, length); | 444 openedFile2.readListSync(data, 0, length); |
| 441 for (var i = 0; i < data.length; i++) { | 445 for (var i = 0; i < data.length; i++) { |
| 442 Expect.equals(i, data[i]); | 446 Expect.equals(i, data[i]); |
| 443 } | 447 } |
| 444 openedFile2.closeSync(); | 448 openedFile2.closeSync(); |
| 445 file2.deleteSync(); | 449 file2.deleteSync(); |
| 450 asyncTestDone("testWriteVariousLists"); |
| 446 }; | 451 }; |
| 447 }; | 452 }; |
| 448 file.errorHandler = (s) { | 453 file.errorHandler = (s) { |
| 449 Expect.fail("No errors expected : $s"); | 454 Expect.fail("No errors expected : $s"); |
| 450 }; | 455 }; |
| 451 }; | 456 }; |
| 452 } | 457 } |
| 453 | 458 |
| 454 static void testDirectory() { | 459 static void testDirectory() { |
| 460 asyncTestStarted(); |
| 461 |
| 455 // Port to verify that the test completes. | 462 // Port to verify that the test completes. |
| 456 var port = new ReceivePort.singleShot(); | 463 var port = new ReceivePort.singleShot(); |
| 457 port.receive((message, replyTo) => Expect.equals(1, message)); | 464 port.receive((message, replyTo) { |
| 465 Expect.equals(1, message); |
| 466 asyncTestDone("testDirectory"); |
| 467 }); |
| 458 | 468 |
| 459 var tempDir = tempDirectory.path; | 469 var tempDir = tempDirectory.path; |
| 460 var file = new File("${tempDir}/file"); | 470 var file = new File("${tempDir}/testDirectory"); |
| 461 var errors = 0; | 471 var errors = 0; |
| 462 file.directory(); | 472 file.directory(); |
| 463 file.directoryHandler = (d) => Expect.fail("non-existing file"); | 473 file.directoryHandler = (d) => Expect.fail("non-existing file"); |
| 464 file.errorHandler = (s) { | 474 file.errorHandler = (s) { |
| 465 file.errorHandler = (s) => Expect.fail("no error expected"); | 475 file.errorHandler = (s) => Expect.fail("no error expected"); |
| 466 file.create(); | 476 file.create(); |
| 467 file.createHandler = () { | 477 file.createHandler = () { |
| 468 file.directory(); | 478 file.directory(); |
| 469 file.directoryHandler = (Directory d) { | 479 file.directoryHandler = (Directory d) { |
| 470 d.exists(); | 480 d.exists(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 491 }; | 501 }; |
| 492 }; | 502 }; |
| 493 }; | 503 }; |
| 494 }; | 504 }; |
| 495 }; | 505 }; |
| 496 }; | 506 }; |
| 497 } | 507 } |
| 498 | 508 |
| 499 static void testDirectorySync() { | 509 static void testDirectorySync() { |
| 500 var tempDir = tempDirectory.path; | 510 var tempDir = tempDirectory.path; |
| 501 var file = new File("${tempDir}/file"); | 511 var file = new File("${tempDir}/testDirectorySync"); |
| 502 // Non-existing file should throw exception. | 512 // Non-existing file should throw exception. |
| 503 Expect.throws(file.directorySync, (e) { return e is FileIOException; }); | 513 Expect.throws(file.directorySync, (e) { return e is FileIOException; }); |
| 504 file.createSync(); | 514 file.createSync(); |
| 505 // Check that the path of the returned directory is the temp directory. | 515 // Check that the path of the returned directory is the temp directory. |
| 506 Directory d = file.directorySync(); | 516 Directory d = file.directorySync(); |
| 507 Expect.isTrue(d.existsSync()); | 517 Expect.isTrue(d.existsSync()); |
| 508 Expect.isTrue(d.path.endsWith(tempDir)); | 518 Expect.isTrue(d.path.endsWith(tempDir)); |
| 509 file.deleteSync(); | 519 file.deleteSync(); |
| 510 // Directories should throw exception. | 520 // Directories should throw exception. |
| 511 var file_dir = new File("."); | 521 var file_dir = new File("."); |
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 966 testWriteVariousLists(); | 976 testWriteVariousLists(); |
| 967 testDirectory(); | 977 testDirectory(); |
| 968 testDirectorySync(); | 978 testDirectorySync(); |
| 969 }); | 979 }); |
| 970 } | 980 } |
| 971 } | 981 } |
| 972 | 982 |
| 973 main() { | 983 main() { |
| 974 FileTest.testMain(); | 984 FileTest.testMain(); |
| 975 } | 985 } |
| OLD | NEW |