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 { |
(...skipping 10 matching lines...) Expand all Loading... |
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 tempDirectory = new Directory(''); |
31 tempDirectory.onError = (e) { | 31 tempDirectory.errorHandler = (e) { |
32 Expect.fail("Failed creating temporary directory"); | 32 Expect.fail("Failed creating temporary directory"); |
33 }; | 33 }; |
34 tempDirectory.createTemp(doNext); | 34 tempDirectory.createTempHandler = doNext; |
| 35 tempDirectory.createTemp(); |
35 } | 36 } |
36 | 37 |
37 static void deleteTempDirectory() { | 38 static void deleteTempDirectory() { |
38 tempDirectory.deleteRecursivelySync(); | 39 tempDirectory.deleteSync(recursive: true); |
39 } | 40 } |
40 | 41 |
41 // Test for file read functionality. | 42 // Test for file read functionality. |
42 static void testReadStream() { | 43 static void testReadStream() { |
43 // Read a file and check part of it's contents. | 44 // Read a file and check part of it's contents. |
44 String filename = getFilename("bin/file_test.cc"); | 45 String filename = getFilename("bin/file_test.cc"); |
45 File file = new File(filename); | 46 File file = new File(filename); |
46 InputStream input = file.openInputStream(); | 47 InputStream input = file.openInputStream(); |
47 input.onData = () { | 48 input.dataHandler = () { |
48 List<int> buffer = new List<int>(42); | 49 List<int> buffer = new List<int>(42); |
49 int bytesRead = input.readInto(buffer, 0, 12); | 50 int bytesRead = input.readInto(buffer, 0, 12); |
50 Expect.equals(12, bytesRead); | 51 Expect.equals(12, bytesRead); |
51 bytesRead = input.readInto(buffer, 12, 30); | 52 bytesRead = input.readInto(buffer, 12, 30); |
52 input.close(); | 53 input.close(); |
53 Expect.equals(30, bytesRead); | 54 Expect.equals(30, bytesRead); |
54 Expect.equals(47, buffer[0]); // represents '/' in the file. | 55 Expect.equals(47, buffer[0]); // represents '/' in the file. |
55 Expect.equals(47, buffer[1]); // represents '/' in the file. | 56 Expect.equals(47, buffer[1]); // represents '/' in the file. |
56 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 57 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
57 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 58 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
(...skipping 14 matching lines...) Expand all Loading... |
72 | 73 |
73 // Read a file. | 74 // Read a file. |
74 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 75 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
75 File file; | 76 File file; |
76 InputStream input; | 77 InputStream input; |
77 int bytesRead; | 78 int bytesRead; |
78 | 79 |
79 // Test reading all using readInto. | 80 // Test reading all using readInto. |
80 file = new File(inFilename); | 81 file = new File(inFilename); |
81 input = file.openInputStream(); | 82 input = file.openInputStream(); |
82 input.onData = () { | 83 input.dataHandler = () { |
83 List<int> buffer1 = new List<int>(42); | 84 List<int> buffer1 = new List<int>(42); |
84 bytesRead = input.readInto(buffer1, 0, 42); | 85 bytesRead = input.readInto(buffer1, 0, 42); |
85 Expect.equals(42, bytesRead); | 86 Expect.equals(42, bytesRead); |
86 Expect.isTrue(input.closed); | 87 Expect.isTrue(input.closed); |
87 | 88 |
88 // Test reading all using readInto and read. | 89 // Test reading all using readInto and read. |
89 file = new File(inFilename); | 90 file = new File(inFilename); |
90 input = file.openInputStream(); | 91 input = file.openInputStream(); |
91 input.onData = () { | 92 input.dataHandler = () { |
92 bytesRead = input.readInto(buffer1, 0, 21); | 93 bytesRead = input.readInto(buffer1, 0, 21); |
93 Expect.equals(21, bytesRead); | 94 Expect.equals(21, bytesRead); |
94 buffer1 = input.read(); | 95 buffer1 = input.read(); |
95 Expect.equals(21, buffer1.length); | 96 Expect.equals(21, buffer1.length); |
96 Expect.isTrue(input.closed); | 97 Expect.isTrue(input.closed); |
97 | 98 |
98 // Test reading all using read and readInto. | 99 // Test reading all using read and readInto. |
99 file = new File(inFilename); | 100 file = new File(inFilename); |
100 input = file.openInputStream(); | 101 input = file.openInputStream(); |
101 input.onData = () { | 102 input.dataHandler = () { |
102 buffer1 = input.read(21); | 103 buffer1 = input.read(21); |
103 Expect.equals(21, buffer1.length); | 104 Expect.equals(21, buffer1.length); |
104 bytesRead = input.readInto(buffer1, 0, 21); | 105 bytesRead = input.readInto(buffer1, 0, 21); |
105 Expect.equals(21, bytesRead); | 106 Expect.equals(21, bytesRead); |
106 Expect.isTrue(input.closed); | 107 Expect.isTrue(input.closed); |
107 | 108 |
108 // Test reading all using read. | 109 // Test reading all using read. |
109 file = new File(inFilename); | 110 file = new File(inFilename); |
110 input = file.openInputStream(); | 111 input = file.openInputStream(); |
111 input.onData = () { | 112 input.dataHandler = () { |
112 buffer1 = input.read(); | 113 buffer1 = input.read(); |
113 Expect.equals(42, buffer1.length); | 114 Expect.equals(42, buffer1.length); |
114 Expect.isTrue(input.closed); | 115 Expect.isTrue(input.closed); |
115 | 116 |
116 // Write the contents of the file just read into another file. | 117 // Write the contents of the file just read into another file. |
117 String outFilename = tempDirectory.path + "/out_read_write_stream"; | 118 String outFilename = tempDirectory.path + "/out_read_write_stream"; |
118 file = new File(outFilename); | 119 file = new File(outFilename); |
119 OutputStream output = file.openOutputStream(); | 120 OutputStream output = file.openOutputStream(); |
120 bool writeDone = output.writeFrom(buffer1, 0, 42); | 121 bool writeDone = output.writeFrom(buffer1, 0, 42); |
121 Expect.equals(false, writeDone); | 122 Expect.equals(false, writeDone); |
122 output.onNoPendingWrites = () { | 123 output.noPendingWriteHandler = () { |
123 output.close(); | 124 output.close(); |
124 output.onClosed = () { | 125 output.closeHandler = () { |
125 // Now read the contents of the file just written. | 126 // Now read the contents of the file just written. |
126 List<int> buffer2 = new List<int>(42); | 127 List<int> buffer2 = new List<int>(42); |
127 file = new File(outFilename); | 128 file = new File(outFilename); |
128 input = file.openInputStream(); | 129 input = file.openInputStream(); |
129 input.onData = () { | 130 input.dataHandler = () { |
130 bytesRead = input.readInto(buffer2, 0, 42); | 131 bytesRead = input.readInto(buffer2, 0, 42); |
131 Expect.equals(42, bytesRead); | 132 Expect.equals(42, bytesRead); |
132 // Now compare the two buffers to check if they are identical. | 133 // Now compare the two buffers to check if they are identical. |
133 for (int i = 0; i < buffer1.length; i++) { | 134 for (int i = 0; i < buffer1.length; i++) { |
134 Expect.equals(buffer1[i], buffer2[i]); | 135 Expect.equals(buffer1[i], buffer2[i]); |
135 } | 136 } |
136 }; | 137 }; |
137 input.onClosed = () { | 138 input.closeHandler = () { |
138 // Delete the output file. | 139 // Delete the output file. |
139 file.deleteSync(); | 140 file.deleteSync(); |
140 Expect.isFalse(file.existsSync()); | 141 Expect.isFalse(file.existsSync()); |
141 asyncTestDone("testReadWriteStream"); | 142 asyncTestDone("testReadWriteStream"); |
142 }; | 143 }; |
143 }; | 144 }; |
144 }; | 145 }; |
145 }; | 146 }; |
146 }; | 147 }; |
147 }; | 148 }; |
148 }; | 149 }; |
149 } | 150 } |
150 | 151 |
151 static void testRead() { | 152 static void testRead() { |
152 // Read a file and check part of it's contents. | 153 // Read a file and check part of it's contents. |
153 String filename = getFilename("bin/file_test.cc"); | 154 String filename = getFilename("bin/file_test.cc"); |
154 File file = new File(filename); | 155 File file = new File(filename); |
155 file.onError = (s) { | 156 file.errorHandler = (s) { |
156 Expect.fail("No errors expected : $s"); | 157 Expect.fail("No errors expected : $s"); |
157 }; | 158 }; |
158 file.open(FileMode.READ, (RandomAccessFile file) { | 159 file.openHandler = (RandomAccessFile file) { |
159 List<int> buffer = new List<int>(10); | 160 List<int> buffer = new List<int>(10); |
160 file.readList(buffer, 0, 5, (bytes_read) { | 161 file.readListHandler = (bytes_read) { |
161 Expect.equals(5, bytes_read); | 162 Expect.equals(5, bytes_read); |
162 file.readList(buffer, 5, 5, (bytes_read) { | 163 file.readListHandler = (bytes_read) { |
163 Expect.equals(5, bytes_read); | 164 Expect.equals(5, bytes_read); |
164 Expect.equals(47, buffer[0]); // represents '/' in the file. | 165 Expect.equals(47, buffer[0]); // represents '/' in the file. |
165 Expect.equals(47, buffer[1]); // represents '/' in the file. | 166 Expect.equals(47, buffer[1]); // represents '/' in the file. |
166 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 167 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
167 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 168 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
168 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 169 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
169 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 170 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
170 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 171 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
171 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 172 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
172 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 173 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
173 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 174 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
174 file.close(() => null); | 175 file.close(); |
175 }); | 176 }; |
176 }); | 177 file.readList(buffer, 5, 5); |
177 }); | 178 }; |
| 179 file.readList(buffer, 0, 5); |
| 180 }; |
| 181 file.open(); |
178 } | 182 } |
179 | 183 |
180 static void testReadSync() { | 184 static void testReadSync() { |
181 // Read a file and check part of it's contents. | 185 // Read a file and check part of it's contents. |
182 String filename = getFilename("bin/file_test.cc"); | 186 String filename = getFilename("bin/file_test.cc"); |
183 RandomAccessFile file = (new File(filename)).openSync(); | 187 RandomAccessFile file = (new File(filename)).openSync(); |
184 List<int> buffer = new List<int>(42); | 188 List<int> buffer = new List<int>(42); |
185 int bytes_read = 0; | 189 int bytes_read = 0; |
186 bytes_read = file.readListSync(buffer, 0, 12); | 190 bytes_read = file.readListSync(buffer, 0, 12); |
187 Expect.equals(12, bytes_read); | 191 Expect.equals(12, bytes_read); |
(...skipping 11 matching lines...) Expand all Loading... |
199 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 203 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
200 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 204 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
201 Expect.equals(116, buffer[11]); // represents 't' in the file. | 205 Expect.equals(116, buffer[11]); // represents 't' in the file. |
202 } | 206 } |
203 | 207 |
204 // Test for file read and write functionality. | 208 // Test for file read and write functionality. |
205 static void testReadWrite() { | 209 static void testReadWrite() { |
206 // Read a file. | 210 // Read a file. |
207 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 211 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
208 final File file = new File(inFilename); | 212 final File file = new File(inFilename); |
209 file.onError = (s) { | 213 file.errorHandler = (s) { |
210 Expect.fail("No errors expected : $s"); | 214 Expect.fail("No errors expected : $s"); |
211 }; | 215 }; |
212 file.open(FileMode.READ, (RandomAccessFile openedFile) { | 216 file.openHandler = (RandomAccessFile openedFile) { |
213 openedFile.onError = (s) { | 217 openedFile.errorHandler = (s) { |
214 Expect.fail("No errors expected : $s"); | 218 Expect.fail("No errors expected : $s"); |
215 }; | 219 }; |
216 List<int> buffer1 = new List<int>(42); | 220 List<int> buffer1 = new List<int>(42); |
217 openedFile.readList(buffer1, 0, 42, (bytes_read) { | 221 openedFile.readListHandler = (bytes_read) { |
218 Expect.equals(42, bytes_read); | 222 Expect.equals(42, bytes_read); |
219 openedFile.close(() { | 223 openedFile.closeHandler = () { |
220 // Write the contents of the file just read into another file. | 224 // Write the contents of the file just read into another file. |
221 String outFilename = tempDirectory.path + "/out_read_write"; | 225 String outFilename = tempDirectory.path + "/out_read_write"; |
222 final File file2 = new File(outFilename); | 226 final File file2 = new File(outFilename); |
223 file2.onError = (s) { | 227 file2.errorHandler = (s) { |
224 Expect.fail("No errors expected : $s"); | 228 Expect.fail("No errors expected : $s"); |
225 }; | 229 }; |
226 file2.create(() { | 230 file2.createHandler = () { |
227 file2.fullPath((s) { | 231 file2.fullPathHandler = (s) { |
228 Expect.isTrue(new File(s).existsSync()); | 232 Expect.isTrue(new File(s).existsSync()); |
229 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { | 233 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { |
230 Expect.fail("Not a full path"); | 234 Expect.fail("Not a full path"); |
231 } | 235 } |
232 file2.open(FileMode.WRITE, (RandomAccessFile openedFile2) { | 236 file2.openHandler = (RandomAccessFile openedFile2) { |
233 openedFile2.onError = (s) { | 237 openedFile2.errorHandler = (s) { |
234 Expect.fail("No errors expected : $s"); | 238 Expect.fail("No errors expected : $s"); |
235 }; | 239 }; |
236 openedFile2.writeList(buffer1, 0, bytes_read); | 240 openedFile2.noPendingWriteHandler = () { |
237 openedFile2.onNoPendingWrites = () { | 241 openedFile2.closeHandler = () { |
238 openedFile2.close(() { | |
239 List<int> buffer2 = new List<int>(bytes_read); | 242 List<int> buffer2 = new List<int>(bytes_read); |
240 final File file3 = new File(outFilename); | 243 final File file3 = new File(outFilename); |
241 file3.onError = (s) { | 244 file3.errorHandler = (s) { |
242 Expect.fail("No errors expected : $s"); | 245 Expect.fail("No errors expected : $s"); |
243 }; | 246 }; |
244 file3.open(FileMode.READ, (RandomAccessFile openedFile3) { | 247 file3.openHandler = (RandomAccessFile openedFile3) { |
245 openedFile3.onError = (s) { | 248 openedFile3.errorHandler = (s) { |
246 Expect.fail("No errors expected : $s"); | 249 Expect.fail("No errors expected : $s"); |
247 }; | 250 }; |
248 openedFile3.readList(buffer2, 0, 42, (bytes_read) { | 251 openedFile3.readListHandler = (bytes_read) { |
249 Expect.equals(42, bytes_read); | 252 Expect.equals(42, bytes_read); |
250 openedFile3.close(() { | 253 openedFile3.closeHandler = () { |
251 // Now compare the two buffers to check if they | 254 // Now compare the two buffers to check if they |
252 // are identical. | 255 // are identical. |
253 Expect.equals(buffer1.length, buffer2.length); | 256 Expect.equals(buffer1.length, buffer2.length); |
254 for (int i = 0; i < buffer1.length; i++) { | 257 for (int i = 0; i < buffer1.length; i++) { |
255 Expect.equals(buffer1[i], buffer2[i]); | 258 Expect.equals(buffer1[i], buffer2[i]); |
256 } | 259 } |
257 // Delete the output file. | 260 // Delete the output file. |
258 final file4 = file3; | 261 final file4 = file3; |
259 file4.delete(() { | 262 file4.deleteHandler = () { |
260 file4.exists((exists) { | 263 file4.existsHandler = (exists) { |
261 Expect.isFalse(exists); | 264 Expect.isFalse(exists); |
262 asyncTestDone("testReadWrite"); | 265 asyncTestDone("testReadWrite"); |
263 }); | 266 }; |
264 }); | 267 file4.exists(); |
265 }); | 268 }; |
266 }); | 269 file4.delete(); |
267 }); | 270 }; |
268 }); | 271 openedFile3.close(); |
| 272 }; |
| 273 openedFile3.readList(buffer2, 0, 42); |
| 274 }; |
| 275 file3.open(); |
| 276 }; |
| 277 openedFile2.close(); |
269 }; | 278 }; |
270 }); | 279 openedFile2.writeList(buffer1, 0, bytes_read); |
271 }); | 280 }; |
272 }); | 281 file2.open(FileMode.WRITE); |
273 }); | 282 }; |
274 }); | 283 file2.fullPath(); |
275 }); | 284 }; |
| 285 file2.create(); |
| 286 }; |
| 287 openedFile.close(); |
| 288 }; |
| 289 openedFile.readList(buffer1, 0, 42); |
| 290 }; |
276 asyncTestStarted(); | 291 asyncTestStarted(); |
| 292 file.open(); |
277 } | 293 } |
278 | 294 |
279 static void testWriteAppend() { | 295 static void testWriteAppend() { |
280 String content = "foobar"; | 296 String content = "foobar"; |
281 String filename = tempDirectory.path + "/write_append"; | 297 String filename = tempDirectory.path + "/write_append"; |
282 File file = new File(filename); | 298 File file = new File(filename); |
283 file.createSync(); | 299 file.createSync(); |
284 Expect.isTrue(new File(filename).existsSync()); | 300 Expect.isTrue(new File(filename).existsSync()); |
285 List<int> buffer = content.charCodes(); | 301 List<int> buffer = content.charCodes(); |
286 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); | 302 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); |
(...skipping 14 matching lines...) Expand all Loading... |
301 } | 317 } |
302 | 318 |
303 static void testOutputStreamWriteAppend() { | 319 static void testOutputStreamWriteAppend() { |
304 String content = "foobar"; | 320 String content = "foobar"; |
305 String filename = tempDirectory.path + "/outstream_write_append"; | 321 String filename = tempDirectory.path + "/outstream_write_append"; |
306 File file = new File(filename); | 322 File file = new File(filename); |
307 file.createSync(); | 323 file.createSync(); |
308 List<int> buffer = content.charCodes(); | 324 List<int> buffer = content.charCodes(); |
309 OutputStream outStream = file.openOutputStream(); | 325 OutputStream outStream = file.openOutputStream(); |
310 outStream.write(buffer); | 326 outStream.write(buffer); |
311 outStream.onNoPendingWrites = () { | 327 outStream.noPendingWriteHandler = () { |
312 outStream.close(); | 328 outStream.close(); |
313 File file2 = new File(filename); | 329 File file2 = new File(filename); |
314 OutputStream appendingOutput = | 330 OutputStream appendingOutput = |
315 file2.openOutputStream(FileMode.APPEND); | 331 file2.openOutputStream(FileMode.APPEND); |
316 appendingOutput.write(buffer); | 332 appendingOutput.write(buffer); |
317 appendingOutput.onNoPendingWrites = () { | 333 appendingOutput.noPendingWriteHandler = () { |
318 appendingOutput.close(); | 334 appendingOutput.close(); |
319 File file3 = new File(filename); | 335 File file3 = new File(filename); |
320 file3.open(FileMode.READ, (RandomAccessFile openedFile) { | 336 file3.openHandler = (RandomAccessFile openedFile) { |
321 openedFile.length((int length) { | 337 openedFile.lengthHandler = (int length) { |
322 Expect.equals(content.length * 2, length); | 338 Expect.equals(content.length * 2, length); |
323 openedFile.close(() { | 339 openedFile.closeHandler = () { |
324 file3.delete(() { | 340 file3.deleteHandler = () { |
325 asyncTestDone("testOutputStreamWriteAppend"); | 341 asyncTestDone("testOutputStreamWriteAppend"); |
326 }); | 342 }; |
327 }); | 343 file3.delete(); |
328 }); | 344 }; |
329 }); | 345 openedFile.close(); |
| 346 }; |
| 347 openedFile.length(); |
| 348 }; |
| 349 file3.open(); |
330 }; | 350 }; |
331 }; | 351 }; |
332 asyncTestStarted(); | 352 asyncTestStarted(); |
333 } | 353 } |
334 | 354 |
335 | 355 |
336 static void testReadWriteSync() { | 356 static void testReadWriteSync() { |
337 // Read a file. | 357 // Read a file. |
338 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 358 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
339 RandomAccessFile file = (new File(inFilename)).openSync(); | 359 RandomAccessFile file = (new File(inFilename)).openSync(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 file.createSync(); | 397 file.createSync(); |
378 RandomAccessFile openedFile = file.openSync(); | 398 RandomAccessFile openedFile = file.openSync(); |
379 Expect.throws(() => openedFile.readByteSync(), (e) => e is FileIOException); | 399 Expect.throws(() => openedFile.readByteSync(), (e) => e is FileIOException); |
380 openedFile.closeSync(); | 400 openedFile.closeSync(); |
381 file.deleteSync(); | 401 file.deleteSync(); |
382 } | 402 } |
383 | 403 |
384 static void testReadEmptyFile() { | 404 static void testReadEmptyFile() { |
385 String fileName = tempDirectory.path + "/empty_file"; | 405 String fileName = tempDirectory.path + "/empty_file"; |
386 File file = new File(fileName); | 406 File file = new File(fileName); |
387 file.onError = (s) { | 407 file.errorHandler = (s) { |
388 Expect.fail("No errors expected : $s"); | 408 Expect.fail("No errors expected : $s"); |
389 }; | 409 }; |
| 410 file.createHandler = () { |
| 411 file.openHandler = (RandomAccessFile openedFile) { |
| 412 openedFile.readByteHandler = (int byte) { |
| 413 Expect.fail("Read byte from empty file"); |
| 414 }; |
| 415 openedFile.errorHandler = (String err) { |
| 416 Expect.isTrue(err.indexOf("failed") != -1); |
| 417 openedFile.closeHandler = () { |
| 418 file.deleteHandler = () { |
| 419 asyncTestDone("testReadEmptyFile"); |
| 420 }; |
| 421 file.delete(); |
| 422 }; |
| 423 openedFile.close(); |
| 424 }; |
| 425 openedFile.readByte(); |
| 426 }; |
| 427 file.open(); |
| 428 }; |
390 asyncTestStarted(); | 429 asyncTestStarted(); |
391 file.create(() { | 430 file.create(); |
392 file.open(FileMode.READ, (RandomAccessFile openedFile) { | |
393 openedFile.readByte((int byte) { | |
394 Expect.fail("Read byte from empty file"); | |
395 }); | |
396 openedFile.onError = (String err) { | |
397 Expect.isTrue(err.indexOf("failed") != -1); | |
398 openedFile.close(() { | |
399 file.delete(() { | |
400 asyncTestDone("testReadEmptyFile"); | |
401 }); | |
402 }); | |
403 }; | |
404 }); | |
405 }); | |
406 } | 431 } |
407 | 432 |
408 // Test for file write of different types of lists. | 433 // Test for file write of different types of lists. |
409 static void testWriteVariousLists() { | 434 static void testWriteVariousLists() { |
410 asyncTestStarted(); | 435 asyncTestStarted(); |
411 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; | 436 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; |
412 final File file = new File(fileName); | 437 final File file = new File(fileName); |
413 file.onError = (s) { | 438 file.create(); |
414 Expect.fail("No errors expected : $s"); | 439 file.createHandler = () { |
415 }; | 440 file.open(FileMode.WRITE); |
416 file.create(() { | 441 file.openHandler = (RandomAccessFile openedFile) { |
417 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { | |
418 // Write bytes from 0 to 7. | 442 // Write bytes from 0 to 7. |
419 openedFile.writeList([0], 0, 1); | 443 openedFile.writeList([0], 0, 1); |
420 openedFile.writeList(const [1], 0, 1); | 444 openedFile.writeList(const [1], 0, 1); |
421 openedFile.writeList(new MyListOfOneElement(2), 0, 1); | 445 openedFile.writeList(new MyListOfOneElement(2), 0, 1); |
422 var x = 12345678901234567890123456789012345678901234567890; | 446 var x = 12345678901234567890123456789012345678901234567890; |
423 var y = 12345678901234567890123456789012345678901234567893; | 447 var y = 12345678901234567890123456789012345678901234567893; |
424 openedFile.writeList([y - x], 0, 1); | 448 openedFile.writeList([y - x], 0, 1); |
425 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. | 449 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. |
426 openedFile.writeList(const [261], 0, 1); | 450 openedFile.writeList(const [261], 0, 1); |
427 openedFile.writeList(new MyListOfOneElement(262), 0, 1); | 451 openedFile.writeList(new MyListOfOneElement(262), 0, 1); |
428 x = 12345678901234567890123456789012345678901234567890; | 452 x = 12345678901234567890123456789012345678901234567890; |
429 y = 12345678901234567890123456789012345678901234568153; | 453 y = 12345678901234567890123456789012345678901234568153; |
430 openedFile.writeList([y - x], 0, 1); | 454 openedFile.writeList([y - x], 0, 1); |
431 | 455 |
432 openedFile.onError = (s) { | 456 openedFile.errorHandler = (s) { |
433 Expect.fail("No errors expected : $s"); | 457 Expect.fail("No errors expected : $s"); |
434 }; | 458 }; |
435 openedFile.onNoPendingWrites = () { | 459 openedFile.noPendingWriteHandler = () { |
436 openedFile.close(() { | 460 openedFile.close(); |
437 // Check the written bytes. | |
438 final File file2 = new File(fileName); | |
439 var openedFile2 = file2.openSync(); | |
440 var length = openedFile2.lengthSync(); | |
441 Expect.equals(8, length); | |
442 List data = new List(length); | |
443 openedFile2.readListSync(data, 0, length); | |
444 for (var i = 0; i < data.length; i++) { | |
445 Expect.equals(i, data[i]); | |
446 } | |
447 openedFile2.closeSync(); | |
448 file2.deleteSync(); | |
449 asyncTestDone("testWriteVariousLists"); | |
450 }); | |
451 }; | 461 }; |
452 }); | 462 openedFile.closeHandler = () { |
453 }); | 463 // Check the written bytes. |
| 464 final File file2 = new File(fileName); |
| 465 var openedFile2 = file2.openSync(); |
| 466 var length = openedFile2.lengthSync(); |
| 467 Expect.equals(8, length); |
| 468 List data = new List(length); |
| 469 openedFile2.readListSync(data, 0, length); |
| 470 for (var i = 0; i < data.length; i++) { |
| 471 Expect.equals(i, data[i]); |
| 472 } |
| 473 openedFile2.closeSync(); |
| 474 file2.deleteSync(); |
| 475 asyncTestDone("testWriteVariousLists"); |
| 476 }; |
| 477 }; |
| 478 file.errorHandler = (s) { |
| 479 Expect.fail("No errors expected : $s"); |
| 480 }; |
| 481 }; |
454 } | 482 } |
455 | 483 |
456 static void testDirectory() { | 484 static void testDirectory() { |
457 asyncTestStarted(); | 485 asyncTestStarted(); |
458 | 486 |
459 // Port to verify that the test completes. | 487 // Port to verify that the test completes. |
460 var port = new ReceivePort.singleShot(); | 488 var port = new ReceivePort.singleShot(); |
461 port.receive((message, replyTo) { | 489 port.receive((message, replyTo) { |
462 Expect.equals(1, message); | 490 Expect.equals(1, message); |
463 asyncTestDone("testDirectory"); | 491 asyncTestDone("testDirectory"); |
464 }); | 492 }); |
465 | 493 |
466 var tempDir = tempDirectory.path; | 494 var tempDir = tempDirectory.path; |
467 var file = new File("${tempDir}/testDirectory"); | 495 var file = new File("${tempDir}/testDirectory"); |
468 var errors = 0; | 496 var errors = 0; |
469 file.directory((d) => Expect.fail("non-existing file")); | 497 file.directory(); |
470 file.onError = (s) { | 498 file.directoryHandler = (d) => Expect.fail("non-existing file"); |
471 file.onError = (s) => Expect.fail("no error expected"); | 499 file.errorHandler = (s) { |
472 file.create(() { | 500 file.errorHandler = (s) => Expect.fail("no error expected"); |
473 file.directory((Directory d) { | 501 file.create(); |
474 d.onError = (s) => Expect.fail("no error expected"); | 502 file.createHandler = () { |
475 d.exists((exists) { | 503 file.directory(); |
| 504 file.directoryHandler = (Directory d) { |
| 505 d.exists(); |
| 506 d.errorHandler = (s) => Expect.fail("no error expected"); |
| 507 d.existsHandler = (exists) { |
476 Expect.isTrue(exists); | 508 Expect.isTrue(exists); |
477 Expect.isTrue(d.path.endsWith(tempDir)); | 509 Expect.isTrue(d.path.endsWith(tempDir)); |
478 file.delete(() { | 510 file.delete(); |
| 511 file.deleteHandler = () { |
479 var file_dir = new File("."); | 512 var file_dir = new File("."); |
480 file_dir.directory((d) => Expect.fail("non-existing file")); | 513 file_dir.directory(); |
481 file_dir.onError = (s) { | 514 file_dir.directoryHandler = (d) { |
| 515 Expect.fail("non-existing file"); |
| 516 }; |
| 517 file_dir.errorHandler = (s) { |
482 var file_dir = new File(tempDir); | 518 var file_dir = new File(tempDir); |
483 file_dir.directory((d) => Expect.fail("non-existing file")); | 519 file_dir.directory(); |
484 file_dir.onError = (s) { | 520 file_dir.directoryHandler = (d) { |
| 521 Expect.fail("non-existing file"); |
| 522 }; |
| 523 file_dir.errorHandler = (s) { |
485 port.toSendPort().send(1); | 524 port.toSendPort().send(1); |
486 }; | 525 }; |
487 }; | 526 }; |
488 }); | 527 }; |
489 }); | 528 }; |
490 }); | 529 }; |
491 }); | 530 }; |
492 }; | 531 }; |
493 } | 532 } |
494 | 533 |
495 static void testDirectorySync() { | 534 static void testDirectorySync() { |
496 var tempDir = tempDirectory.path; | 535 var tempDir = tempDirectory.path; |
497 var file = new File("${tempDir}/testDirectorySync"); | 536 var file = new File("${tempDir}/testDirectorySync"); |
498 // Non-existing file should throw exception. | 537 // Non-existing file should throw exception. |
499 Expect.throws(file.directorySync, (e) { return e is FileIOException; }); | 538 Expect.throws(file.directorySync, (e) { return e is FileIOException; }); |
500 file.createSync(); | 539 file.createSync(); |
501 // Check that the path of the returned directory is the temp directory. | 540 // Check that the path of the returned directory is the temp directory. |
502 Directory d = file.directorySync(); | 541 Directory d = file.directorySync(); |
503 Expect.isTrue(d.existsSync()); | 542 Expect.isTrue(d.existsSync()); |
504 Expect.isTrue(d.path.endsWith(tempDir)); | 543 Expect.isTrue(d.path.endsWith(tempDir)); |
505 file.deleteSync(); | 544 file.deleteSync(); |
506 // Directories should throw exception. | 545 // Directories should throw exception. |
507 var file_dir = new File("."); | 546 var file_dir = new File("."); |
508 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); | 547 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); |
509 file_dir = new File(tempDir); | 548 file_dir = new File(tempDir); |
510 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); | 549 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); |
511 } | 550 } |
512 | 551 |
513 // Test for file length functionality. | 552 // Test for file length functionality. |
514 static void testLength() { | 553 static void testLength() { |
515 String filename = getFilename("tests/vm/data/fixed_length_file"); | 554 String filename = getFilename("tests/vm/data/fixed_length_file"); |
516 RandomAccessFile input = (new File(filename)).openSync(); | 555 RandomAccessFile input = (new File(filename)).openSync(); |
517 input.onError = (s) { | 556 input.errorHandler = (s) { |
518 Expect.fail("No errors expected"); | 557 Expect.fail("No errors expected"); |
519 }; | 558 }; |
520 input.length((length) { | 559 input.lengthHandler = (length) { |
521 Expect.equals(42, length); | 560 Expect.equals(42, length); |
522 input.close(() => null); | 561 input.close(); |
523 }); | 562 }; |
| 563 input.length(); |
524 } | 564 } |
525 | 565 |
526 static void testLengthSync() { | 566 static void testLengthSync() { |
527 String filename = getFilename("tests/vm/data/fixed_length_file"); | 567 String filename = getFilename("tests/vm/data/fixed_length_file"); |
528 RandomAccessFile input = (new File(filename)).openSync(); | 568 RandomAccessFile input = (new File(filename)).openSync(); |
529 Expect.equals(42, input.lengthSync()); | 569 Expect.equals(42, input.lengthSync()); |
530 input.closeSync(); | 570 input.closeSync(); |
531 } | 571 } |
532 | 572 |
533 // Test for file position functionality. | 573 // Test for file position functionality. |
534 static void testPosition() { | 574 static void testPosition() { |
535 String filename = getFilename("tests/vm/data/fixed_length_file"); | 575 String filename = getFilename("tests/vm/data/fixed_length_file"); |
536 RandomAccessFile input = (new File(filename)).openSync(); | 576 RandomAccessFile input = (new File(filename)).openSync(); |
537 input.onError = (s) { | 577 input.errorHandler = (s) { |
538 Expect.fail("No errors expected"); | 578 Expect.fail("No errors expected"); |
539 }; | 579 }; |
540 input.position((position) { | 580 input.positionHandler = (position) { |
541 Expect.equals(0, position); | 581 Expect.equals(0, position); |
542 List<int> buffer = new List<int>(100); | 582 List<int> buffer = new List<int>(100); |
543 input.readList(buffer, 0, 12, (bytes_read) { | 583 input.readListHandler = (bytes_read) { |
544 input.position((position) { | 584 input.positionHandler = (position) { |
545 Expect.equals(12, position); | 585 Expect.equals(12, position); |
546 input.readList(buffer, 12, 6, (bytes_read) { | 586 input.readListHandler = (bytes_read) { |
547 input.position((position) { | 587 input.positionHandler = (position) { |
548 Expect.equals(18, position); | 588 Expect.equals(18, position); |
549 input.setPosition(8, () { | 589 input.setPositionHandler = () { |
550 input.position((position) { | 590 input.positionHandler = (position) { |
551 Expect.equals(8, position); | 591 Expect.equals(8, position); |
552 input.close(() => null); | 592 input.close(); |
553 }); | 593 }; |
554 }); | 594 input.position(); |
555 }); | 595 }; |
556 }); | 596 input.setPosition(8); |
557 }); | 597 }; |
558 }); | 598 }; |
559 }); | 599 input.readList(buffer, 12, 6); |
| 600 }; |
| 601 input.position(); |
| 602 }; |
| 603 input.readList(buffer, 0, 12); |
| 604 }; |
| 605 input.position(); |
560 } | 606 } |
561 | 607 |
562 static void testPositionSync() { | 608 static void testPositionSync() { |
563 String filename = getFilename("tests/vm/data/fixed_length_file"); | 609 String filename = getFilename("tests/vm/data/fixed_length_file"); |
564 RandomAccessFile input = (new File(filename)).openSync(); | 610 RandomAccessFile input = (new File(filename)).openSync(); |
565 Expect.equals(0, input.positionSync()); | 611 Expect.equals(0, input.positionSync()); |
566 List<int> buffer = new List<int>(100); | 612 List<int> buffer = new List<int>(100); |
567 input.readListSync(buffer, 0, 12); | 613 input.readListSync(buffer, 0, 12); |
568 Expect.equals(12, input.positionSync()); | 614 Expect.equals(12, input.positionSync()); |
569 input.readListSync(buffer, 12, 6); | 615 input.readListSync(buffer, 12, 6); |
570 Expect.equals(18, input.positionSync()); | 616 Expect.equals(18, input.positionSync()); |
571 input.setPositionSync(8); | 617 input.setPositionSync(8); |
572 Expect.equals(8, input.positionSync()); | 618 Expect.equals(8, input.positionSync()); |
573 input.closeSync(); | 619 input.closeSync(); |
574 } | 620 } |
575 | 621 |
576 static void testTruncate() { | 622 static void testTruncate() { |
577 File file = new File(tempDirectory.path + "/out_truncate"); | 623 File file = new File(tempDirectory.path + "/out_truncate"); |
578 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 624 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
579 file.onError = (error) { | 625 file.errorHandler = (error) { |
580 Expect.fail("testTruncate: No errors expected"); | 626 Expect.fail("testTruncate: No errors expected"); |
581 }; | 627 }; |
582 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { | 628 file.openHandler = (RandomAccessFile openedFile) { |
583 openedFile.writeList(buffer, 0, 10); | 629 openedFile.noPendingWriteHandler = () { |
584 openedFile.onNoPendingWrites = () { | 630 openedFile.lengthHandler = (length) { |
585 openedFile.length((length) { | |
586 Expect.equals(10, length); | 631 Expect.equals(10, length); |
587 openedFile.truncate(5, () { | 632 openedFile.truncateHandler = () { |
588 openedFile.length((length) { | 633 openedFile.lengthHandler = (length) { |
589 Expect.equals(5, length); | 634 Expect.equals(5, length); |
590 openedFile.close(() { | 635 openedFile.closeHandler = () { |
591 file.delete(() { | 636 file.deleteHandler = () { |
592 file.exists((exists) { | 637 file.existsHandler = (exists) { |
593 Expect.isFalse(exists); | 638 Expect.isFalse(exists); |
594 asyncTestDone("testTruncate"); | 639 asyncTestDone("testTruncate"); |
595 }); | 640 }; |
596 }); | 641 file.exists(); |
597 }); | 642 }; |
598 }); | 643 file.delete(); |
599 }); | 644 }; |
600 }); | 645 openedFile.close(); |
| 646 }; |
| 647 openedFile.length(); |
| 648 }; |
| 649 openedFile.truncate(5); |
| 650 }; |
| 651 openedFile.length(); |
601 }; | 652 }; |
602 }); | 653 openedFile.writeList(buffer, 0, 10); |
| 654 }; |
603 asyncTestStarted(); | 655 asyncTestStarted(); |
| 656 file.open(FileMode.WRITE); |
604 } | 657 } |
605 | 658 |
606 static void testTruncateSync() { | 659 static void testTruncateSync() { |
607 File file = new File(tempDirectory.path + "/out_truncate_sync"); | 660 File file = new File(tempDirectory.path + "/out_truncate_sync"); |
608 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 661 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
609 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); | 662 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); |
610 openedFile.writeListSync(buffer, 0, 10); | 663 openedFile.writeListSync(buffer, 0, 10); |
611 Expect.equals(10, openedFile.lengthSync()); | 664 Expect.equals(10, openedFile.lengthSync()); |
612 openedFile.truncateSync(5); | 665 openedFile.truncateSync(5); |
613 Expect.equals(5, openedFile.lengthSync()); | 666 Expect.equals(5, openedFile.lengthSync()); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 input.deleteSync(); | 760 input.deleteSync(); |
708 } | 761 } |
709 | 762 |
710 // Tests stream exception handling after file was closed. | 763 // Tests stream exception handling after file was closed. |
711 static void testCloseExceptionStream() { | 764 static void testCloseExceptionStream() { |
712 asyncTestStarted(); | 765 asyncTestStarted(); |
713 List<int> buffer = new List<int>(42); | 766 List<int> buffer = new List<int>(42); |
714 File file = new File(tempDirectory.path + "/out_close_exception_stream"); | 767 File file = new File(tempDirectory.path + "/out_close_exception_stream"); |
715 file.createSync(); | 768 file.createSync(); |
716 InputStream input = file.openInputStream(); | 769 InputStream input = file.openInputStream(); |
717 input.onClosed = () { | 770 input.closeHandler = () { |
718 Expect.isTrue(input.closed); | 771 Expect.isTrue(input.closed); |
719 Expect.isNull(input.readInto(buffer, 0, 12)); | 772 Expect.isNull(input.readInto(buffer, 0, 12)); |
720 OutputStream output = file.openOutputStream(); | 773 OutputStream output = file.openOutputStream(); |
721 output.close(); | 774 output.close(); |
722 Expect.throws(() => output.writeFrom(buffer, 0, 12)); | 775 Expect.throws(() => output.writeFrom(buffer, 0, 12)); |
723 output.onClosed = () { | 776 output.closeHandler = () { |
724 file.deleteSync(); | 777 file.deleteSync(); |
725 asyncTestDone("testCloseExceptionStream"); | 778 asyncTestDone("testCloseExceptionStream"); |
726 }; | 779 }; |
727 }; | 780 }; |
728 } | 781 } |
729 | 782 |
730 // Tests buffer out of bounds exception. | 783 // Tests buffer out of bounds exception. |
731 static void testBufferOutOfBoundsException() { | 784 static void testBufferOutOfBoundsException() { |
732 bool exceptionCaught = false; | 785 bool exceptionCaught = false; |
733 bool wrongExceptionCaught = false; | 786 bool wrongExceptionCaught = false; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 } | 873 } |
821 Expect.equals(true, exceptionCaught); | 874 Expect.equals(true, exceptionCaught); |
822 Expect.equals(true, !wrongExceptionCaught); | 875 Expect.equals(true, !wrongExceptionCaught); |
823 openedFile.closeSync(); | 876 openedFile.closeSync(); |
824 file.deleteSync(); | 877 file.deleteSync(); |
825 } | 878 } |
826 | 879 |
827 static void testMixedSyncAndAsync() { | 880 static void testMixedSyncAndAsync() { |
828 var name = getFilename("tests/vm/data/fixed_length_file"); | 881 var name = getFilename("tests/vm/data/fixed_length_file"); |
829 var f = new File(name); | 882 var f = new File(name); |
830 f.onError = (s) { | 883 f.errorHandler = (s) { |
831 Expect.fail("No errors expected"); | 884 Expect.fail("No errors expected"); |
832 }; | 885 }; |
833 f.exists((exists) { | 886 f.existsHandler = (exists) { |
834 try { | 887 try { |
835 f.existsSync(); | 888 f.existsSync(); |
836 Expect.fail("Expected exception"); | 889 Expect.fail("Expected exception"); |
837 } catch (var e) { | 890 } catch (var e) { |
838 Expect.isTrue(e is FileIOException); | 891 Expect.isTrue(e is FileIOException); |
839 } | 892 } |
840 }); | 893 }; |
| 894 f.exists(); |
841 } | 895 } |
842 | 896 |
843 static void testOpenDirectoryAsFile() { | 897 static void testOpenDirectoryAsFile() { |
844 var f = new File('.'); | 898 var f = new File('.'); |
845 f.open(FileMode.READ, (r) => Expect.fail('Directory opened as file')); | 899 f.open(); |
| 900 f.openHandler = (r) => Expect.fail('Directory opened as file'); |
846 } | 901 } |
847 | 902 |
848 static void testOpenDirectoryAsFileSync() { | 903 static void testOpenDirectoryAsFileSync() { |
849 var f = new File('.'); | 904 var f = new File('.'); |
850 try { | 905 try { |
851 f.openSync(); | 906 f.openSync(); |
852 Expect.fail("Expected exception opening directory as file"); | 907 Expect.fail("Expected exception opening directory as file"); |
853 } catch (var e) { | 908 } catch (var e) { |
854 Expect.isTrue(e is FileIOException); | 909 Expect.isTrue(e is FileIOException); |
855 } | 910 } |
856 } | 911 } |
857 | 912 |
858 static void testReadAsBytes() { | 913 static void testReadAsBytes() { |
859 var port = new ReceivePort.singleShot(); | 914 var port = new ReceivePort.singleShot(); |
860 port.receive((result, replyTo) { | 915 port.receive((result, replyTo) { |
861 Expect.equals(42, result); | 916 Expect.equals(42, result); |
862 }); | 917 }); |
863 var name = getFilename("tests/vm/data/fixed_length_file"); | 918 var name = getFilename("tests/vm/data/fixed_length_file"); |
864 var f = new File(name); | 919 var f = new File(name); |
865 f.readAsBytes((bytes) { | 920 f.readAsBytes(); |
| 921 f.readAsBytesHandler = (bytes) { |
866 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 922 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
867 port.toSendPort().send(bytes.length); | 923 port.toSendPort().send(bytes.length); |
868 }); | 924 }; |
869 f.onError = (e) { | 925 f.errorHandler = (e) { |
870 Expect.fail("No errors expected: $e"); | 926 Expect.fail("No errors expected: $e"); |
871 }; | 927 }; |
872 } | 928 } |
873 | 929 |
874 static void testReadAsBytesSync() { | 930 static void testReadAsBytesSync() { |
875 var name = getFilename("tests/vm/data/fixed_length_file"); | 931 var name = getFilename("tests/vm/data/fixed_length_file"); |
876 var bytes = new File(name).readAsBytesSync(); | 932 var bytes = new File(name).readAsBytesSync(); |
877 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 933 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
878 Expect.equals(bytes.length, 42); | 934 Expect.equals(bytes.length, 42); |
879 } | 935 } |
880 | 936 |
881 static void testReadAsText() { | 937 static void testReadAsText() { |
882 var port = new ReceivePort.singleShot(); | 938 var port = new ReceivePort.singleShot(); |
883 port.receive((result, replyTo) { | 939 port.receive((result, replyTo) { |
884 Expect.equals(1, result); | 940 Expect.equals(1, result); |
885 }); | 941 }); |
886 var name = getFilename("tests/vm/data/fixed_length_file"); | 942 var name = getFilename("tests/vm/data/fixed_length_file"); |
887 var f = new File(name); | 943 var f = new File(name); |
888 f.readAsText('UTF-8', (text) { | 944 f.readAsText('UTF-8'); |
| 945 f.readAsTextHandler = (text) { |
889 Expect.isTrue(text.endsWith("42 bytes.")); | 946 Expect.isTrue(text.endsWith("42 bytes.")); |
890 Expect.equals(42, text.length); | 947 Expect.equals(42, text.length); |
891 var name = getDataFilename("tests/standalone/src/read_as_text.dat"); | 948 var name = getDataFilename("tests/standalone/src/read_as_text.dat"); |
892 var f = new File(name); | 949 var f = new File(name); |
893 f.onError = (e) => Expect.fail("No errors expected"); | 950 f.errorHandler = (e) => Expect.fail("No errors expected"); |
894 f.readAsText('UTF-8', (text) { | 951 f.readAsText('UTF-8'); |
| 952 f.readAsTextHandler = (text) { |
895 Expect.equals(6, text.length); | 953 Expect.equals(6, text.length); |
896 var expected = [955, 120, 46, 32, 120, 10]; | 954 var expected = [955, 120, 46, 32, 120, 10]; |
897 Expect.listEquals(expected, text.charCodes()); | 955 Expect.listEquals(expected, text.charCodes()); |
898 f.readAsText('ISO-8859-1', (text) { | 956 f.readAsText('ISO-8859-1'); |
| 957 f.readAsTextHandler = (text) { |
899 Expect.equals(7, text.length); | 958 Expect.equals(7, text.length); |
900 var expected = [206, 187, 120, 46, 32, 120, 10]; | 959 var expected = [206, 187, 120, 46, 32, 120, 10]; |
901 Expect.listEquals(expected, text.charCodes()); | 960 Expect.listEquals(expected, text.charCodes()); |
902 f.onError = (e) { | 961 f.readAsText('ASCII'); |
| 962 f.errorHandler = (e) { |
903 port.toSendPort().send(1); | 963 port.toSendPort().send(1); |
904 }; | 964 }; |
905 f.readAsText('ASCII', (text) { | 965 f.readAsTextHandler = (text) { |
906 Expect.fail("Non-ascii char should cause error"); | 966 Expect.fail("Non-ascii char should cause error"); |
907 }); | 967 }; |
908 }); | 968 }; |
909 }); | 969 }; |
910 }); | 970 }; |
911 f.onError = (e) { | 971 f.errorHandler = (e) { |
912 Expect.fail("No errors expected: $e"); | 972 Expect.fail("No errors expected: $e"); |
913 }; | 973 }; |
914 } | 974 } |
915 | 975 |
916 static void testReadAsTextSync() { | 976 static void testReadAsTextSync() { |
917 var name = getFilename("tests/vm/data/fixed_length_file"); | 977 var name = getFilename("tests/vm/data/fixed_length_file"); |
918 var text = new File(name).readAsTextSync(); | 978 var text = new File(name).readAsTextSync(); |
919 Expect.isTrue(text.endsWith("42 bytes.")); | 979 Expect.isTrue(text.endsWith("42 bytes.")); |
920 Expect.equals(42, text.length); | 980 Expect.equals(42, text.length); |
921 name = getDataFilename("tests/standalone/src/read_as_text.dat"); | 981 name = getDataFilename("tests/standalone/src/read_as_text.dat"); |
922 text = new File(name).readAsTextSync(); | 982 text = new File(name).readAsTextSync(); |
923 Expect.equals(6, text.length); | 983 Expect.equals(6, text.length); |
924 var expected = [955, 120, 46, 32, 120, 10]; | 984 var expected = [955, 120, 46, 32, 120, 10]; |
925 Expect.listEquals(expected, text.charCodes()); | 985 Expect.listEquals(expected, text.charCodes()); |
926 Expect.throws(() { new File(name).readAsTextSync("ASCII"); }); | 986 Expect.throws(() { new File(name).readAsTextSync("ASCII"); }); |
927 text = new File(name).readAsTextSync("ISO-8859-1"); | 987 text = new File(name).readAsTextSync("ISO-8859-1"); |
928 expected = [206, 187, 120, 46, 32, 120, 10]; | 988 expected = [206, 187, 120, 46, 32, 120, 10]; |
929 Expect.equals(7, text.length); | 989 Expect.equals(7, text.length); |
930 Expect.listEquals(expected, text.charCodes()); | 990 Expect.listEquals(expected, text.charCodes()); |
931 } | 991 } |
932 | 992 |
933 static void testReadAsLines() { | 993 static void testReadAsLines() { |
934 var port = new ReceivePort.singleShot(); | 994 var port = new ReceivePort.singleShot(); |
935 port.receive((result, replyTo) { | 995 port.receive((result, replyTo) { |
936 Expect.equals(42, result); | 996 Expect.equals(42, result); |
937 }); | 997 }); |
938 var name = getFilename("tests/vm/data/fixed_length_file"); | 998 var name = getFilename("tests/vm/data/fixed_length_file"); |
939 var f = new File(name); | 999 var f = new File(name); |
940 f.readAsLines('UTF-8', (lines) { | 1000 f.readAsLines('UTF-8'); |
| 1001 f.readAsLinesHandler = (lines) { |
941 Expect.equals(1, lines.length); | 1002 Expect.equals(1, lines.length); |
942 var line = lines[0]; | 1003 var line = lines[0]; |
943 Expect.isTrue(line.endsWith("42 bytes.")); | 1004 Expect.isTrue(line.endsWith("42 bytes.")); |
944 port.toSendPort().send(line.length); | 1005 port.toSendPort().send(line.length); |
945 }); | 1006 }; |
946 f.onError = (e) { | 1007 f.errorHandler = (e) { |
947 Expect.fail("No errors expected: $e"); | 1008 Expect.fail("No errors expected: $e"); |
948 }; | 1009 }; |
949 } | 1010 } |
950 | 1011 |
951 static void testReadAsLinesSync() { | 1012 static void testReadAsLinesSync() { |
952 var name = getFilename("tests/vm/data/fixed_length_file"); | 1013 var name = getFilename("tests/vm/data/fixed_length_file"); |
953 var lines = new File(name).readAsLinesSync(); | 1014 var lines = new File(name).readAsLinesSync(); |
954 Expect.equals(1, lines.length); | 1015 Expect.equals(1, lines.length); |
955 var line = lines[0]; | 1016 var line = lines[0]; |
956 Expect.isTrue(line.endsWith("42 bytes.")); | 1017 Expect.isTrue(line.endsWith("42 bytes.")); |
957 Expect.equals(42, line.length); | 1018 Expect.equals(42, line.length); |
958 name = getDataFilename("tests/standalone/src/readline_test1.dat"); | 1019 name = getDataFilename("tests/standalone/src/readline_test1.dat"); |
959 lines = new File(name).readAsLinesSync(); | 1020 lines = new File(name).readAsLinesSync(); |
960 Expect.equals(10, lines.length); | 1021 Expect.equals(10, lines.length); |
961 } | 1022 } |
962 | 1023 |
963 | 1024 |
964 static void testReadAsErrors() { | 1025 static void testReadAsErrors() { |
965 var port = new ReceivePort.singleShot(); | 1026 var port = new ReceivePort.singleShot(); |
966 port.receive((message, _) { | 1027 port.receive((message, _) { |
967 Expect.equals(1, message); | 1028 Expect.equals(1, message); |
968 }); | 1029 }); |
969 var f = new File('.'); | 1030 var f = new File('.'); |
970 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); | 1031 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); |
971 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); | 1032 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); |
972 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); | 1033 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); |
973 f.readAsBytes((bytes) => Expect.fail("no bytes expected")); | 1034 f.readAsBytes(); |
974 f.onError = (e) { | 1035 f.errorHandler = (e) { |
975 f.readAsText('UTF-8', (text) => Expect.fail("no text expected")); | 1036 f.readAsText(); |
976 f.onError = (e) { | 1037 f.errorHandler = (e) { |
977 f.readAsLines('UTF-8', (lines) => Expect.fail("no lines expected")); | 1038 f.readAsLines(); |
978 f.onError = (e) { | 1039 f.errorHandler = (e) { |
979 port.toSendPort().send(1); | 1040 port.toSendPort().send(1); |
980 }; | 1041 }; |
981 }; | 1042 }; |
982 }; | 1043 }; |
983 } | 1044 } |
984 | 1045 |
985 // Test that opens the same file for writing then for appending to test | 1046 // Test that opens the same file for writing then for appending to test |
986 // that the file is not truncated when opened for appending. | 1047 // that the file is not truncated when opened for appending. |
987 static void testAppend() { | 1048 static void testAppend() { |
988 var file = new File('${tempDirectory.path}/out_append'); | 1049 var file = new File('${tempDirectory.path}/out_append'); |
989 file.open(FileMode.WRITE, (openedFile) { | 1050 file.openHandler = (openedFile) { |
| 1051 openedFile.noPendingWriteHandler = () { |
| 1052 openedFile.closeHandler = () { |
| 1053 file.openHandler = (openedFile) { |
| 1054 openedFile.lengthHandler = (length) { |
| 1055 Expect.equals(4, length); |
| 1056 openedFile.setPositionHandler = () { |
| 1057 openedFile.noPendingWriteHandler = () { |
| 1058 openedFile.lengthHandler = (length) { |
| 1059 Expect.equals(8, length); |
| 1060 openedFile.closeHandler = () { |
| 1061 file.deleteHandler = () { |
| 1062 file.existsHandler = (exists) { |
| 1063 Expect.isFalse(exists); |
| 1064 asyncTestDone("testAppend"); |
| 1065 }; |
| 1066 file.exists(); |
| 1067 }; |
| 1068 file.delete(); |
| 1069 }; |
| 1070 openedFile.close(); |
| 1071 }; |
| 1072 openedFile.length(); |
| 1073 }; |
| 1074 openedFile.writeString("asdf"); |
| 1075 }; |
| 1076 openedFile.setPosition(4); |
| 1077 }; |
| 1078 openedFile.length(); |
| 1079 }; |
| 1080 file.open(FileMode.APPEND); |
| 1081 }; |
| 1082 openedFile.close(); |
| 1083 }; |
990 openedFile.writeString("asdf"); | 1084 openedFile.writeString("asdf"); |
991 openedFile.onNoPendingWrites = () { | 1085 }; |
992 openedFile.close(() { | |
993 file.open(FileMode.APPEND, (openedFile) { | |
994 openedFile.length((length) { | |
995 Expect.equals(4, length); | |
996 openedFile.writeString("asdf"); | |
997 openedFile.onNoPendingWrites = () { | |
998 openedFile.length((length) { | |
999 Expect.equals(8, length); | |
1000 openedFile.close(() { | |
1001 file.delete(() { | |
1002 file.exists((exists) { | |
1003 Expect.isFalse(exists); | |
1004 asyncTestDone("testAppend"); | |
1005 }); | |
1006 }); | |
1007 }); | |
1008 }); | |
1009 }; | |
1010 }); | |
1011 }); | |
1012 }); | |
1013 }; | |
1014 }); | |
1015 asyncTestStarted(); | 1086 asyncTestStarted(); |
| 1087 file.open(FileMode.WRITE); |
1016 } | 1088 } |
1017 | 1089 |
1018 static void testAppendSync() { | 1090 static void testAppendSync() { |
1019 var file = new File('${tempDirectory.path}/out_append_sync'); | 1091 var file = new File('${tempDirectory.path}/out_append_sync'); |
1020 var openedFile = file.openSync(FileMode.WRITE); | 1092 var openedFile = file.openSync(FileMode.WRITE); |
1021 openedFile.writeStringSync("asdf"); | 1093 openedFile.writeStringSync("asdf"); |
1022 Expect.equals(4, openedFile.lengthSync()); | 1094 Expect.equals(4, openedFile.lengthSync()); |
1023 openedFile.closeSync(); | 1095 openedFile.closeSync(); |
1024 openedFile = file.openSync(FileMode.WRITE); | 1096 openedFile = file.openSync(FileMode.WRITE); |
1025 openedFile.setPositionSync(4); | 1097 openedFile.setPositionSync(4); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1076 testWriteVariousLists(); | 1148 testWriteVariousLists(); |
1077 testDirectory(); | 1149 testDirectory(); |
1078 testDirectorySync(); | 1150 testDirectorySync(); |
1079 }); | 1151 }); |
1080 } | 1152 } |
1081 } | 1153 } |
1082 | 1154 |
1083 main() { | 1155 main() { |
1084 FileTest.testMain(); | 1156 FileTest.testMain(); |
1085 } | 1157 } |
OLD | NEW |