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