Chromium Code Reviews| Index: tests/standalone/src/DirectoryTest.dart |
| diff --git a/tests/standalone/src/DirectoryTest.dart b/tests/standalone/src/DirectoryTest.dart |
| index 6056aea8ebca75151865d9ced37719bf86323efc..e8f69bc843e158d1608e291ca1e7e1f574775192 100644 |
| --- a/tests/standalone/src/DirectoryTest.dart |
| +++ b/tests/standalone/src/DirectoryTest.dart |
| @@ -34,21 +34,185 @@ class DirectoryTest { |
| } |
| static void testExistsCreateDelete() { |
| - // TODO(ager): This should be creating temporary directories. |
| Directory d = new Directory("____DIRECTORY_TEST_DIRECTORY____"); |
| Expect.isFalse(d.existsSync()); |
| d.createSync(); |
| Expect.isTrue(d.existsSync()); |
| d.deleteSync(); |
| Expect.isFalse(d.existsSync()); |
| + |
| + Directory tempDir1 = new Directory("/tmp/dart_temp_dir_"); |
| + Directory tempDir2 = new Directory("/tmp/dart_temp_dir_"); |
| + bool stage1aDone = false; |
| + bool stage1bDone = false; |
| + bool emptyTemplateTestDone = false; |
| + |
| + // Stages 0 through 2 run twice, the second time with an empty path. |
| + Function stage0; |
| + Function stage1a; |
| + Function stage1b; |
| + Function stage2; |
| + Function stage3; // Loops to stage 0. |
| + |
| + Function error(String message) { |
| + Expect.fail("Directory errorHandler: $message"); |
| + } |
| + |
| + stage0 = () { |
| + tempDir1.createTempHandler = stage1a; |
| + tempDir1.errorHandler = error; |
| + tempDir1.createTemp(); |
| + tempDir2.createTempHandler = stage1b; |
| + tempDir2.errorHandler = error; |
| + tempDir2.createTemp(); |
| + }; |
| + |
| + stage1a = () { |
| + stage1aDone = true; |
| + Expect.isTrue(tempDir1.existsSync()); |
| + if (stage1bDone) { |
| + stage2(); |
| + } |
| + }; |
| + |
| + stage1b = () { |
| + stage1bDone = true; |
| + Expect.isTrue(tempDir2.existsSync()); |
| + if (stage1aDone) { |
| + stage2(); |
| + } |
| + }; |
| + |
| + stage2 = () { |
| + Expect.notEquals(tempDir1.path, tempDir2.path); |
| + tempDir1.deleteSync(); |
| + tempDir2.deleteSync(); |
| + Expect.isFalse(tempDir1.existsSync()); |
| + Expect.isFalse(tempDir2.existsSync()); |
| + if (!emptyTemplateTestDone) { |
| + emptyTemplateTestDone = true; |
| + stage3(); |
| + } else { |
| + // Done with test. |
| + } |
| + }; |
| + |
| + stage3 = () { |
| + tempDir1 = new Directory(""); |
| + tempDir2 = new Directory(""); |
| + stage1aDone = false; |
| + stage1bDone = false; |
| + stage0(); |
| + }; |
| + |
| + stage0(); |
| + } |
| + |
| + static void testCreateTemp() { |
| + Directory tempDirectory = new Directory(""); |
| + tempDirectory.createTempHandler = () { |
| + String filename = tempDirectory.path + |
| + new Platform().pathSeparator() + "dart_testfile"; |
| + File file = new File(filename); |
| + Expect.isFalse(file.existsSync()); |
| + file.errorHandler = (error) { |
| + Expect.fail("testCreateTemp file.errorHandler called: $error"); |
| + }; |
| + file.createHandler = () { |
| + file.open(writable: true); |
| + }; |
| + file.openHandler = () { |
| + file.writeList([65, 66, 67, 13], 0, 4); |
| + }; |
| + file.noPendingWriteHandler = () { |
| + file.length(); |
| + }; |
| + file.lengthHandler = (int length) { |
| + Expect.equals(4, length); |
| + file.close(); |
| + }; |
| + file.closeHandler = () { |
| + file.exists(); |
| + }; |
| + file.existsHandler = (bool exists) { |
| + Expect.isTrue(exists); |
| + // Try to delete the directory containing the file - should throw. |
| + bool threw_exception = false; |
| + try { |
| + tempDirectory.deleteSync(); |
| + } catch (var e) { |
| + Expect.isTrue(tempDirectory.existsSync()); |
| + threw_exception = true; |
| + } |
| + Expect.isTrue(threw_exception); |
| + Expect.isTrue(tempDirectory.existsSync()); |
| + |
| + // Delete the file, and then delete the directory. |
| + file.delete(); |
| + }; |
| + file.deleteHandler = () { |
| + tempDirectory.deleteSync(); |
| + Expect.isFalse(tempDirectory.existsSync()); |
| + }; |
| + |
| + file.create(); |
| + }; |
| + tempDirectory.createTemp(); |
| } |
| + static void testNestedTempDirectory() { |
| + var test = new NestedTempDirectoryTest(); |
| + } |
| + |
| + |
| static void testMain() { |
| testListing(); |
| testExistsCreateDelete(); |
| + testCreateTemp(); |
| + testNestedTempDirectory(); |
| } |
| } |
| + |
| +class NestedTempDirectoryTest { |
| + List<Directory> createdDirectories; |
| + static final int nestingDepth = 6; |
| + Directory current; |
| + |
| + void errorCallback(error) { |
| + Expect.fail("Error callback called in NestedTempDirectoryTest: $error"); |
| + } |
| + |
| + void createPhaseCallback() { |
| + createdDirectories.add(current); |
| + if (createdDirectories.length < nestingDepth) { |
| + current = new Directory( |
| + current.path + "/nested_temp_dir_${createdDirectories.length}_"); |
| + current.errorHandler = errorCallback; |
| + current.createTempHandler = createPhaseCallback; |
| + current.createTemp(); |
| + } else { |
| + deletePhaseCallback(); |
| + } |
| + } |
| + |
| + void deletePhaseCallback() { |
| + if (!createdDirectories.isEmpty()) { |
| + current = createdDirectories.removeLast(); |
| + current.deleteSync(); |
| + deletePhaseCallback(); |
| + } |
| + } |
| + |
| + NestedTempDirectoryTest() { |
|
Mads Ager (google)
2011/11/21 16:29:37
I would put the constructor before the instance me
Bill Hesse
2011/11/22 12:49:39
Done.
|
| + createdDirectories = new List<Directory>(); |
| + current = new Directory("/tmp/nested_dir_test"); |
| + current.createSync(); |
| + createPhaseCallback(); |
| + } |
| +} |
| + |
| + |
| main() { |
| DirectoryTest.testMain(); |
|
Mads Ager (google)
2011/11/21 16:29:37
Why not create a testMain method on NestedTempDire
Bill Hesse
2011/11/22 12:49:39
Done.
|
| } |