Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Directory listing test. | 5 // Directory listing test. |
| 6 | 6 |
| 7 class DirectoryTest { | 7 class DirectoryTest { |
| 8 static void testListing() { | 8 static void testListing() { |
| 9 bool listedSomething = false; | 9 bool listedSomething = false; |
| 10 Directory directory = new Directory("."); | 10 Directory directory = new Directory("."); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 directory.list(); | 29 directory.list(); |
| 30 | 30 |
| 31 // Listing is asynchronous, so nothing should be listed at this | 31 // Listing is asynchronous, so nothing should be listed at this |
| 32 // point. | 32 // point. |
| 33 Expect.isFalse(listedSomething); | 33 Expect.isFalse(listedSomething); |
| 34 } | 34 } |
| 35 | 35 |
| 36 static void testExistsCreateDelete() { | 36 static void testExistsCreateDelete() { |
| 37 // TODO(ager): This should be creating temporary directories. | |
| 38 Directory d = new Directory("____DIRECTORY_TEST_DIRECTORY____"); | 37 Directory d = new Directory("____DIRECTORY_TEST_DIRECTORY____"); |
| 39 Expect.isFalse(d.existsSync()); | 38 Expect.isFalse(d.existsSync()); |
| 40 d.createSync(); | 39 d.createSync(); |
| 41 Expect.isTrue(d.existsSync()); | 40 Expect.isTrue(d.existsSync()); |
| 42 d.deleteSync(); | 41 d.deleteSync(); |
| 43 Expect.isFalse(d.existsSync()); | 42 Expect.isFalse(d.existsSync()); |
| 43 | |
| 44 Directory tempDir1 = new Directory("/tmp/dart_temp_dir_"); | |
| 45 Directory tempDir2 = new Directory("/tmp/dart_temp_dir_"); | |
| 46 bool stage1aDone = false; | |
| 47 bool stage1bDone = false; | |
| 48 bool emptyTemplateTestDone = false; | |
| 49 | |
| 50 // Stages 0 through 2 run twice, the second time with an empty path. | |
| 51 Function stage0; | |
| 52 Function stage1a; | |
| 53 Function stage1b; | |
| 54 Function stage2; | |
| 55 Function stage3; // Loops to stage 0. | |
| 56 | |
| 57 Function error(String message) { | |
| 58 Expect.fail("Directory errorHandler: $message"); | |
| 59 } | |
| 60 | |
| 61 stage0 = () { | |
| 62 tempDir1.createTempHandler = stage1a; | |
| 63 tempDir1.errorHandler = error; | |
| 64 tempDir1.createTemp(); | |
| 65 tempDir2.createTempHandler = stage1b; | |
| 66 tempDir2.errorHandler = error; | |
| 67 tempDir2.createTemp(); | |
| 68 }; | |
| 69 | |
| 70 stage1a = () { | |
| 71 stage1aDone = true; | |
| 72 Expect.isTrue(tempDir1.existsSync()); | |
| 73 if (stage1bDone) { | |
| 74 stage2(); | |
| 75 } | |
| 76 }; | |
| 77 | |
| 78 stage1b = () { | |
| 79 stage1bDone = true; | |
| 80 Expect.isTrue(tempDir2.existsSync()); | |
| 81 if (stage1aDone) { | |
| 82 stage2(); | |
| 83 } | |
| 84 }; | |
| 85 | |
| 86 stage2 = () { | |
| 87 Expect.notEquals(tempDir1.path, tempDir2.path); | |
| 88 tempDir1.deleteSync(); | |
| 89 tempDir2.deleteSync(); | |
| 90 Expect.isFalse(tempDir1.existsSync()); | |
| 91 Expect.isFalse(tempDir2.existsSync()); | |
| 92 if (!emptyTemplateTestDone) { | |
| 93 emptyTemplateTestDone = true; | |
| 94 stage3(); | |
| 95 } else { | |
| 96 // Done with test. | |
| 97 } | |
| 98 }; | |
| 99 | |
| 100 stage3 = () { | |
| 101 tempDir1 = new Directory(""); | |
| 102 tempDir2 = new Directory(""); | |
| 103 stage1aDone = false; | |
| 104 stage1bDone = false; | |
| 105 stage0(); | |
| 106 }; | |
| 107 | |
| 108 stage0(); | |
| 44 } | 109 } |
| 45 | 110 |
| 111 static void testCreateTemp() { | |
| 112 Directory tempDirectory = new Directory(""); | |
| 113 tempDirectory.createTempHandler = () { | |
| 114 String filename = tempDirectory.path + | |
| 115 new Platform().pathSeparator() + "dart_testfile"; | |
| 116 File file = new File(filename); | |
| 117 Expect.isFalse(file.existsSync()); | |
| 118 file.errorHandler = (error) { | |
| 119 Expect.fail("testCreateTemp file.errorHandler called: $error"); | |
| 120 }; | |
| 121 file.createHandler = () { | |
| 122 file.open(writable: true); | |
| 123 }; | |
| 124 file.openHandler = () { | |
| 125 file.writeList([65, 66, 67, 13], 0, 4); | |
| 126 }; | |
| 127 file.noPendingWriteHandler = () { | |
| 128 file.length(); | |
| 129 }; | |
| 130 file.lengthHandler = (int length) { | |
| 131 Expect.equals(4, length); | |
| 132 file.close(); | |
| 133 }; | |
| 134 file.closeHandler = () { | |
| 135 file.exists(); | |
| 136 }; | |
| 137 file.existsHandler = (bool exists) { | |
| 138 Expect.isTrue(exists); | |
| 139 // Try to delete the directory containing the file - should throw. | |
| 140 bool threw_exception = false; | |
| 141 try { | |
| 142 tempDirectory.deleteSync(); | |
| 143 } catch (var e) { | |
| 144 Expect.isTrue(tempDirectory.existsSync()); | |
| 145 threw_exception = true; | |
| 146 } | |
| 147 Expect.isTrue(threw_exception); | |
| 148 Expect.isTrue(tempDirectory.existsSync()); | |
| 149 | |
| 150 // Delete the file, and then delete the directory. | |
| 151 file.delete(); | |
| 152 }; | |
| 153 file.deleteHandler = () { | |
| 154 tempDirectory.deleteSync(); | |
| 155 Expect.isFalse(tempDirectory.existsSync()); | |
| 156 }; | |
| 157 | |
| 158 file.create(); | |
| 159 }; | |
| 160 tempDirectory.createTemp(); | |
| 161 } | |
| 162 | |
| 163 static void testNestedTempDirectory() { | |
| 164 var test = new NestedTempDirectoryTest(); | |
| 165 } | |
| 166 | |
| 167 | |
| 46 static void testMain() { | 168 static void testMain() { |
| 47 testListing(); | 169 testListing(); |
| 48 testExistsCreateDelete(); | 170 testExistsCreateDelete(); |
| 171 testCreateTemp(); | |
| 172 testNestedTempDirectory(); | |
| 49 } | 173 } |
| 50 } | 174 } |
| 51 | 175 |
| 176 | |
| 177 class NestedTempDirectoryTest { | |
| 178 List<Directory> createdDirectories; | |
| 179 static final int nestingDepth = 6; | |
| 180 Directory current; | |
| 181 | |
| 182 void errorCallback(error) { | |
| 183 Expect.fail("Error callback called in NestedTempDirectoryTest: $error"); | |
| 184 } | |
| 185 | |
| 186 void createPhaseCallback() { | |
| 187 createdDirectories.add(current); | |
| 188 if (createdDirectories.length < nestingDepth) { | |
| 189 current = new Directory( | |
| 190 current.path + "/nested_temp_dir_${createdDirectories.length}_"); | |
| 191 current.errorHandler = errorCallback; | |
| 192 current.createTempHandler = createPhaseCallback; | |
| 193 current.createTemp(); | |
| 194 } else { | |
| 195 deletePhaseCallback(); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 void deletePhaseCallback() { | |
| 200 if (!createdDirectories.isEmpty()) { | |
| 201 current = createdDirectories.removeLast(); | |
| 202 current.deleteSync(); | |
| 203 deletePhaseCallback(); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 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.
| |
| 208 createdDirectories = new List<Directory>(); | |
| 209 current = new Directory("/tmp/nested_dir_test"); | |
| 210 current.createSync(); | |
| 211 createPhaseCallback(); | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 | |
| 52 main() { | 216 main() { |
| 53 DirectoryTest.testMain(); | 217 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.
| |
| 54 } | 218 } |
| OLD | NEW |