Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: tests/standalone/io/file_test.dart

Issue 10392023: Change dart:io to use Future for one-shot operations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Adding stable test binaries Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 {
11 int _value; 11 int _value;
12 MyListOfOneElement(this._value); 12 MyListOfOneElement(this._value);
13 int get length() => 1; 13 int get length() => 1;
14 operator [](int index) => _value; 14 operator [](int index) => _value;
15 } 15 }
16 16
17 class FileTest { 17 class FileTest {
18 static Directory tempDirectory; 18 static Directory tempDirectory;
19 static int numLiveAsyncTests = 0; 19 static int numLiveAsyncTests = 0;
20 static ReceivePort port;
20 21
21 static void asyncTestStarted() { ++numLiveAsyncTests; } 22 static void asyncTestStarted() { ++numLiveAsyncTests; }
22 static void asyncTestDone(String name) { 23 static void asyncTestDone(String name) {
23 --numLiveAsyncTests; 24 --numLiveAsyncTests;
24 if (numLiveAsyncTests == 0) { 25 if (numLiveAsyncTests == 0) {
25 deleteTempDirectory(); 26 deleteTempDirectory();
27 port.close();
26 } 28 }
27 } 29 }
28 30
29 static void createTempDirectory(Function doNext) { 31 static void createTempDirectory(Function doNext) {
30 tempDirectory = new Directory(''); 32 new Directory('').createTemp().then((temp) {
31 tempDirectory.onError = (e) { 33 tempDirectory = temp;
32 Expect.fail("Failed creating temporary directory"); 34 doNext();
33 }; 35 });
34 tempDirectory.createTemp(doNext);
35 } 36 }
36 37
37 static void deleteTempDirectory() { 38 static void deleteTempDirectory() {
38 tempDirectory.deleteRecursivelySync(); 39 tempDirectory.deleteRecursivelySync();
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");
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
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() {
153 ReceivePort port = new ReceivePort();
152 // Read a file and check part of it's contents. 154 // Read a file and check part of it's contents.
153 String filename = getFilename("bin/file_test.cc"); 155 String filename = getFilename("bin/file_test.cc");
154 File file = new File(filename); 156 File file = new File(filename);
155 file.onError = (e) { 157 file.open(FileMode.READ).then((RandomAccessFile file) {
156 Expect.fail("No errors expected : $e");
157 };
158 file.open(FileMode.READ, (RandomAccessFile file) {
159 List<int> buffer = new List<int>(10); 158 List<int> buffer = new List<int>(10);
160 file.readList(buffer, 0, 5, (bytes_read) { 159 file.readList(buffer, 0, 5).then((bytes_read) {
161 Expect.equals(5, bytes_read); 160 Expect.equals(5, bytes_read);
162 file.readList(buffer, 5, 5, (bytes_read) { 161 file.readList(buffer, 5, 5).then((bytes_read) {
163 Expect.equals(5, bytes_read); 162 Expect.equals(5, bytes_read);
164 Expect.equals(47, buffer[0]); // represents '/' in the file. 163 Expect.equals(47, buffer[0]); // represents '/' in the file.
165 Expect.equals(47, buffer[1]); // represents '/' in the file. 164 Expect.equals(47, buffer[1]); // represents '/' in the file.
166 Expect.equals(32, buffer[2]); // represents ' ' in the file. 165 Expect.equals(32, buffer[2]); // represents ' ' in the file.
167 Expect.equals(67, buffer[3]); // represents 'C' in the file. 166 Expect.equals(67, buffer[3]); // represents 'C' in the file.
168 Expect.equals(111, buffer[4]); // represents 'o' in the file. 167 Expect.equals(111, buffer[4]); // represents 'o' in the file.
169 Expect.equals(112, buffer[5]); // represents 'p' in the file. 168 Expect.equals(112, buffer[5]); // represents 'p' in the file.
170 Expect.equals(121, buffer[6]); // represents 'y' in the file. 169 Expect.equals(121, buffer[6]); // represents 'y' in the file.
171 Expect.equals(114, buffer[7]); // represents 'r' in the file. 170 Expect.equals(114, buffer[7]); // represents 'r' in the file.
172 Expect.equals(105, buffer[8]); // represents 'i' in the file. 171 Expect.equals(105, buffer[8]); // represents 'i' in the file.
173 Expect.equals(103, buffer[9]); // represents 'g' in the file. 172 Expect.equals(103, buffer[9]); // represents 'g' in the file.
174 file.close(() => null); 173 file.close().then((ignore) => port.close());
175 }); 174 });
176 }); 175 });
177 }); 176 });
178 } 177 }
179 178
180 static void testReadSync() { 179 static void testReadSync() {
181 // Read a file and check part of it's contents. 180 // Read a file and check part of it's contents.
182 String filename = getFilename("bin/file_test.cc"); 181 String filename = getFilename("bin/file_test.cc");
183 RandomAccessFile file = (new File(filename)).openSync(); 182 RandomAccessFile file = (new File(filename)).openSync();
184 List<int> buffer = new List<int>(42); 183 List<int> buffer = new List<int>(42);
(...skipping 14 matching lines...) Expand all
199 Expect.equals(103, buffer[9]); // represents 'g' in the file. 198 Expect.equals(103, buffer[9]); // represents 'g' in the file.
200 Expect.equals(104, buffer[10]); // represents 'h' in the file. 199 Expect.equals(104, buffer[10]); // represents 'h' in the file.
201 Expect.equals(116, buffer[11]); // represents 't' in the file. 200 Expect.equals(116, buffer[11]); // represents 't' in the file.
202 } 201 }
203 202
204 // Test for file read and write functionality. 203 // Test for file read and write functionality.
205 static void testReadWrite() { 204 static void testReadWrite() {
206 // Read a file. 205 // Read a file.
207 String inFilename = getFilename("tests/vm/data/fixed_length_file"); 206 String inFilename = getFilename("tests/vm/data/fixed_length_file");
208 final File file = new File(inFilename); 207 final File file = new File(inFilename);
209 file.onError = (e) { 208 file.open(FileMode.READ).then((openedFile) {
210 Expect.fail("No errors expected : $e");
211 };
212 file.open(FileMode.READ, (RandomAccessFile openedFile) {
213 openedFile.onError = (s) {
214 Expect.fail("No errors expected : $s");
215 };
216 List<int> buffer1 = new List<int>(42); 209 List<int> buffer1 = new List<int>(42);
217 openedFile.readList(buffer1, 0, 42, (bytes_read) { 210 openedFile.readList(buffer1, 0, 42).then((bytes_read) {
218 Expect.equals(42, bytes_read); 211 Expect.equals(42, bytes_read);
219 openedFile.close(() { 212 openedFile.close().then((ignore) {
220 // Write the contents of the file just read into another file. 213 // Write the contents of the file just read into another file.
221 String outFilename = tempDirectory.path + "/out_read_write"; 214 String outFilename = tempDirectory.path + "/out_read_write";
222 final File file2 = new File(outFilename); 215 final File file2 = new File(outFilename);
223 file2.onError = (e) { 216 file2.create().then((ignore) {
224 Expect.fail("No errors expected : $e"); 217 file2.fullPath().then((s) {
225 };
226 file2.create(() {
227 file2.fullPath((s) {
228 Expect.isTrue(new File(s).existsSync()); 218 Expect.isTrue(new File(s).existsSync());
229 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { 219 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') {
230 Expect.fail("Not a full path"); 220 Expect.fail("Not a full path");
231 } 221 }
232 file2.open(FileMode.WRITE, (RandomAccessFile openedFile2) { 222 file2.open(FileMode.WRITE).then((openedFile2) {
233 openedFile2.onError = (s) { 223 openedFile2.writeList(buffer1, 0, bytes_read).then((ignore) {
234 Expect.fail("No errors expected : $s"); 224 openedFile2.close().then((ignore) {
235 };
236 openedFile2.writeList(buffer1, 0, bytes_read);
237 openedFile2.onNoPendingWrites = () {
238 openedFile2.close(() {
239 List<int> buffer2 = new List<int>(bytes_read); 225 List<int> buffer2 = new List<int>(bytes_read);
240 final File file3 = new File(outFilename); 226 final File file3 = new File(outFilename);
241 file3.onError = (e) { 227 file3.open(FileMode.READ).then((openedFile3) {
242 Expect.fail("No errors expected : $e"); 228 openedFile3.readList(buffer2, 0, 42).then((bytes_read) {
243 }; 229 Expect.equals(42, bytes_read);
244 file3.open(FileMode.READ, (RandomAccessFile openedFile3) { 230 openedFile3.close().then((ignore) {
245 openedFile3.onError = (s) {
246 Expect.fail("No errors expected : $s");
247 };
248 openedFile3.readList(buffer2, 0, 42, (bytes_read) {
249 Expect.equals(42, bytes_read);
250 openedFile3.close(() {
251 // Now compare the two buffers to check if they 231 // Now compare the two buffers to check if they
252 // are identical. 232 // are identical.
253 Expect.equals(buffer1.length, buffer2.length); 233 Expect.equals(buffer1.length, buffer2.length);
254 for (int i = 0; i < buffer1.length; i++) { 234 for (int i = 0; i < buffer1.length; i++) {
255 Expect.equals(buffer1[i], buffer2[i]); 235 Expect.equals(buffer1[i], buffer2[i]);
256 } 236 }
257 // Delete the output file. 237 // Delete the output file.
258 final file4 = file3; 238 final file4 = file3;
259 file4.delete(() { 239 file4.delete().then((ignore) {
260 file4.exists((exists) { 240 file4.exists().then((exists) {
261 Expect.isFalse(exists); 241 Expect.isFalse(exists);
262 asyncTestDone("testReadWrite"); 242 asyncTestDone("testReadWrite");
263 }); 243 });
264 }); 244 });
265 }); 245 });
266 }); 246 });
267 }); 247 });
268 }); 248 });
269 }; 249 });
270 }); 250 });
271 }); 251 });
272 }); 252 });
273 }); 253 });
274 }); 254 });
275 }); 255 });
276 asyncTestStarted(); 256 asyncTestStarted();
277 } 257 }
278 258
279 static void testWriteAppend() { 259 static void testWriteAppend() {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 outStream.close(); 292 outStream.close();
313 outStream.onClosed = () { 293 outStream.onClosed = () {
314 File file2 = new File(filename); 294 File file2 = new File(filename);
315 OutputStream appendingOutput = 295 OutputStream appendingOutput =
316 file2.openOutputStream(FileMode.APPEND); 296 file2.openOutputStream(FileMode.APPEND);
317 appendingOutput.write(buffer); 297 appendingOutput.write(buffer);
318 appendingOutput.onNoPendingWrites = () { 298 appendingOutput.onNoPendingWrites = () {
319 appendingOutput.close(); 299 appendingOutput.close();
320 appendingOutput.onClosed = () { 300 appendingOutput.onClosed = () {
321 File file3 = new File(filename); 301 File file3 = new File(filename);
322 file3.open(FileMode.READ, (RandomAccessFile openedFile) { 302 file3.open(FileMode.READ).then((RandomAccessFile openedFile) {
323 openedFile.length((int length) { 303 openedFile.length().then((int length) {
324 Expect.equals(content.length * 2, length); 304 Expect.equals(content.length * 2, length);
325 openedFile.close(() { 305 openedFile.close().then((ignore) {
326 file3.delete(() { 306 file3.delete().then((ignore) {
327 asyncTestDone("testOutputStreamWriteAppend"); 307 asyncTestDone("testOutputStreamWriteAppend");
328 }); 308 });
329 }); 309 });
330 }); 310 });
331 }); 311 });
332 }; 312 };
333 }; 313 };
334 }; 314 };
335 }; 315 };
336 asyncTestStarted(); 316 asyncTestStarted();
(...skipping 10 matching lines...) Expand all
347 outStream.writeString("abcdABCD"); 327 outStream.writeString("abcdABCD");
348 outStream.writeString("abcdABCD", Encoding.UTF_8); 328 outStream.writeString("abcdABCD", Encoding.UTF_8);
349 outStream.writeString("abcdABCD", Encoding.ISO_8859_1); 329 outStream.writeString("abcdABCD", Encoding.ISO_8859_1);
350 outStream.writeString("abcdABCD", Encoding.ASCII); 330 outStream.writeString("abcdABCD", Encoding.ASCII);
351 outStream.writeString("æøå", Encoding.UTF_8); 331 outStream.writeString("æøå", Encoding.UTF_8);
352 outStream.onNoPendingWrites = () { 332 outStream.onNoPendingWrites = () {
353 outStream.close(); 333 outStream.close();
354 outStream.onClosed = () { 334 outStream.onClosed = () {
355 RandomAccessFile raf = file.openSync(); 335 RandomAccessFile raf = file.openSync();
356 Expect.equals(38, raf.lengthSync()); 336 Expect.equals(38, raf.lengthSync());
337 raf.close().then((ignore) {
338 asyncTestDone("testOutputStreamWriteString");
339 });
357 }; 340 };
358 }; 341 };
359 asyncTestStarted(); 342 asyncTestStarted();
360 } 343 }
361 344
362 345
363 static void testReadWriteSync() { 346 static void testReadWriteSync() {
364 // Read a file. 347 // Read a file.
365 String inFilename = getFilename("tests/vm/data/fixed_length_file"); 348 String inFilename = getFilename("tests/vm/data/fixed_length_file");
366 RandomAccessFile file = (new File(inFilename)).openSync(); 349 RandomAccessFile file = (new File(inFilename)).openSync();
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 file.createSync(); 387 file.createSync();
405 RandomAccessFile openedFile = file.openSync(); 388 RandomAccessFile openedFile = file.openSync();
406 Expect.equals(-1, openedFile.readByteSync()); 389 Expect.equals(-1, openedFile.readByteSync());
407 openedFile.closeSync(); 390 openedFile.closeSync();
408 file.deleteSync(); 391 file.deleteSync();
409 } 392 }
410 393
411 static void testReadEmptyFile() { 394 static void testReadEmptyFile() {
412 String fileName = tempDirectory.path + "/empty_file"; 395 String fileName = tempDirectory.path + "/empty_file";
413 File file = new File(fileName); 396 File file = new File(fileName);
414 file.onError = (e) {
415 Expect.fail("No errors expected : $e");
416 };
417 asyncTestStarted(); 397 asyncTestStarted();
418 file.create(() { 398 file.create().then((ignore) {
419 file.open(FileMode.READ, (RandomAccessFile openedFile) { 399 file.open(FileMode.READ).then((RandomAccessFile openedFile) {
420 openedFile.readByte((int byte) { 400 var readByteFuture = openedFile.readByte();
401 readByteFuture.then((int byte) {
421 Expect.equals(-1, byte); 402 Expect.equals(-1, byte);
403 openedFile.close().then((ignore) {
404 asyncTestDone("testReadEmptyFile");
405 });
422 }); 406 });
423 openedFile.onError = (e) {
424 Expect.isTrue(e is FileIOException);
425 openedFile.close(() {
426 file.delete(() {
427 asyncTestDone("testReadEmptyFile");
428 });
429 });
430 };
431 }); 407 });
432 }); 408 });
433 } 409 }
434 410
435 // Test for file write of different types of lists. 411 // Test for file write of different types of lists.
436 static void testWriteVariousLists() { 412 static void testWriteVariousLists() {
437 asyncTestStarted(); 413 asyncTestStarted();
438 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; 414 final String fileName = "${tempDirectory.path}/testWriteVariousLists";
439 final File file = new File(fileName); 415 final File file = new File(fileName);
440 file.onError = (e) => Expect.fail("No errors expected : $e"); 416 file.create().then((ignore) {
441 file.create(() { 417 file.open(FileMode.WRITE).then((RandomAccessFile openedFile) {
442 file.open(FileMode.WRITE, (RandomAccessFile openedFile) {
443 // Write bytes from 0 to 7. 418 // Write bytes from 0 to 7.
444 openedFile.writeList([0], 0, 1); 419 openedFile.writeList([0], 0, 1);
445 openedFile.writeList(const [1], 0, 1); 420 openedFile.writeList(const [1], 0, 1);
446 openedFile.writeList(new MyListOfOneElement(2), 0, 1); 421 openedFile.writeList(new MyListOfOneElement(2), 0, 1);
447 var x = 12345678901234567890123456789012345678901234567890; 422 var x = 12345678901234567890123456789012345678901234567890;
448 var y = 12345678901234567890123456789012345678901234567893; 423 var y = 12345678901234567890123456789012345678901234567893;
449 openedFile.writeList([y - x], 0, 1); 424 openedFile.writeList([y - x], 0, 1);
450 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. 425 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104.
451 openedFile.writeList(const [261], 0, 1); 426 openedFile.writeList(const [261], 0, 1);
452 openedFile.writeList(new MyListOfOneElement(262), 0, 1); 427 openedFile.writeList(new MyListOfOneElement(262), 0, 1);
453 x = 12345678901234567890123456789012345678901234567890; 428 x = 12345678901234567890123456789012345678901234567890;
454 y = 12345678901234567890123456789012345678901234568153; 429 y = 12345678901234567890123456789012345678901234568153;
455 openedFile.writeList([y - x], 0, 1); 430 openedFile.writeList([y - x], 0, 1).then((ignore) {
456 431 openedFile.close().then((ignore) {
457 openedFile.onError = (e) => Expect.fail("No errors expected : $e");
458 openedFile.onNoPendingWrites = () {
459 openedFile.close(() {
460 // Check the written bytes. 432 // Check the written bytes.
461 final File file2 = new File(fileName); 433 final File file2 = new File(fileName);
462 var openedFile2 = file2.openSync(); 434 var openedFile2 = file2.openSync();
463 var length = openedFile2.lengthSync(); 435 var length = openedFile2.lengthSync();
464 Expect.equals(8, length); 436 Expect.equals(8, length);
465 List data = new List(length); 437 List data = new List(length);
466 openedFile2.readListSync(data, 0, length); 438 openedFile2.readListSync(data, 0, length);
467 for (var i = 0; i < data.length; i++) { 439 for (var i = 0; i < data.length; i++) {
468 Expect.equals(i, data[i]); 440 Expect.equals(i, data[i]);
469 } 441 }
470 openedFile2.closeSync(); 442 openedFile2.closeSync();
471 file2.deleteSync(); 443 file2.deleteSync();
472 asyncTestDone("testWriteVariousLists"); 444 asyncTestDone("testWriteVariousLists");
473 }); 445 });
474 }; 446 });
475 }); 447 });
476 }); 448 });
477 } 449 }
478 450
479 static void testDirectory() { 451 static void testDirectory() {
480 asyncTestStarted(); 452 asyncTestStarted();
481 453
482 // Port to verify that the test completes. 454 // Port to verify that the test completes.
483 var port = new ReceivePort(); 455 var port = new ReceivePort();
484 port.receive((message, replyTo) { 456 port.receive((message, replyTo) {
485 port.close(); 457 port.close();
486 Expect.equals(1, message); 458 Expect.equals(1, message);
487 asyncTestDone("testDirectory"); 459 asyncTestDone("testDirectory");
488 }); 460 });
489 461
490 var tempDir = tempDirectory.path; 462 var tempDir = tempDirectory.path;
491 var file = new File("${tempDir}/testDirectory"); 463 var file = new File("${tempDir}/testDirectory");
492 var errors = 0; 464 var errors = 0;
493 file.directory((d) => Expect.fail("non-existing file")); 465 var dirFuture = file.directory();
494 file.onError = (e) { 466 dirFuture.then((d) => Expect.fail("non-existing file"));
495 file.onError = (e) => Expect.fail("no error expected"); 467 dirFuture.handleException((e) {
496 file.create(() { 468 file.create().then((ignore) {
497 file.directory((Directory d) { 469 file.directory().then((Directory d) {
498 d.onError = (s) => Expect.fail("no error expected"); 470 d.exists().then((exists) {
499 d.exists((exists) {
500 Expect.isTrue(exists); 471 Expect.isTrue(exists);
501 Expect.isTrue(d.path.endsWith(tempDir)); 472 Expect.isTrue(d.path.endsWith(tempDir));
502 file.delete(() { 473 file.delete().then((ignore) {
503 var file_dir = new File("."); 474 var fileDir = new File(".");
504 file_dir.directory((d) => Expect.fail("non-existing file")); 475 var dirFuture2 = fileDir.directory();
505 file_dir.onError = (e) { 476 dirFuture2.then((d) => Expect.fail("non-existing file"));
506 var file_dir = new File(tempDir); 477 dirFuture2.handleException((e) {
507 file_dir.directory((d) => Expect.fail("non-existing file")); 478 var fileDir = new File(tempDir);
508 file_dir.onError = (e) => port.toSendPort().send(1); 479 var dirFuture3 = fileDir.directory();
509 }; 480 dirFuture3.then((d) => Expect.fail("non-existing file"));
481 dirFuture3.handleException((e) {
482 port.toSendPort().send(1);
483 return true;
484 });
485 return true;
486 });
510 }); 487 });
511 }); 488 });
512 }); 489 });
513 }); 490 });
514 }; 491 return true;
492 });
515 } 493 }
516 494
517 static void testDirectorySync() { 495 static void testDirectorySync() {
518 var tempDir = tempDirectory.path; 496 var tempDir = tempDirectory.path;
519 var file = new File("${tempDir}/testDirectorySync"); 497 var file = new File("${tempDir}/testDirectorySync");
520 // Non-existing file should throw exception. 498 // Non-existing file should throw exception.
521 Expect.throws(file.directorySync, (e) { return e is FileIOException; }); 499 Expect.throws(file.directorySync, (e) { return e is FileIOException; });
522 file.createSync(); 500 file.createSync();
523 // 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.
524 Directory d = file.directorySync(); 502 Directory d = file.directorySync();
525 Expect.isTrue(d.existsSync()); 503 Expect.isTrue(d.existsSync());
526 Expect.isTrue(d.path.endsWith(tempDir)); 504 Expect.isTrue(d.path.endsWith(tempDir));
527 file.deleteSync(); 505 file.deleteSync();
528 // Directories should throw exception. 506 // Directories should throw exception.
529 var file_dir = new File("."); 507 var file_dir = new File(".");
530 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); 508 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
531 file_dir = new File(tempDir); 509 file_dir = new File(tempDir);
532 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); 510 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
533 } 511 }
534 512
535 // Test for file length functionality. 513 // Test for file length functionality.
536 static void testLength() { 514 static void testLength() {
515 var port = new ReceivePort();
537 String filename = getFilename("tests/vm/data/fixed_length_file"); 516 String filename = getFilename("tests/vm/data/fixed_length_file");
538 File file = new File(filename); 517 File file = new File(filename);
539 RandomAccessFile openedFile = file.openSync(); 518 RandomAccessFile openedFile = file.openSync();
540 openedFile.onError = (e) => Expect.fail("No errors expected"); 519 openedFile.length().then((length) {
541 file.onError = (e) => Expect.fail("No errors expected");
542 openedFile.length((length) {
543 Expect.equals(42, length); 520 Expect.equals(42, length);
544 openedFile.close(() => null); 521 openedFile.close().then((ignore) => port.close());
545 }); 522 });
546 file.length((length) { 523 file.length().then((length) {
547 Expect.equals(42, length); 524 Expect.equals(42, length);
548 }); 525 });
549 } 526 }
550 527
551 static void testLengthSync() { 528 static void testLengthSync() {
552 String filename = getFilename("tests/vm/data/fixed_length_file"); 529 String filename = getFilename("tests/vm/data/fixed_length_file");
553 File file = new File(filename); 530 File file = new File(filename);
554 RandomAccessFile openedFile = file.openSync(); 531 RandomAccessFile openedFile = file.openSync();
555 Expect.equals(42, file.lengthSync()); 532 Expect.equals(42, file.lengthSync());
556 Expect.equals(42, openedFile.lengthSync()); 533 Expect.equals(42, openedFile.lengthSync());
557 openedFile.closeSync(); 534 openedFile.closeSync();
558 } 535 }
559 536
560 // Test for file position functionality. 537 // Test for file position functionality.
561 static void testPosition() { 538 static void testPosition() {
539 var port = new ReceivePort();
562 String filename = getFilename("tests/vm/data/fixed_length_file"); 540 String filename = getFilename("tests/vm/data/fixed_length_file");
563 RandomAccessFile input = (new File(filename)).openSync(); 541 RandomAccessFile input = (new File(filename)).openSync();
564 input.onError = (e) => Expect.fail("No errors expected"); 542 input.position().then((position) {
565 input.position((position) {
566 Expect.equals(0, position); 543 Expect.equals(0, position);
567 List<int> buffer = new List<int>(100); 544 List<int> buffer = new List<int>(100);
568 input.readList(buffer, 0, 12, (bytes_read) { 545 input.readList(buffer, 0, 12).then((bytes_read) {
569 input.position((position) { 546 input.position().then((position) {
570 Expect.equals(12, position); 547 Expect.equals(12, position);
571 input.readList(buffer, 12, 6, (bytes_read) { 548 input.readList(buffer, 12, 6).then((bytes_read) {
572 input.position((position) { 549 input.position().then((position) {
573 Expect.equals(18, position); 550 Expect.equals(18, position);
574 input.setPosition(8, () { 551 input.setPosition(8).then((ignore) {
575 input.position((position) { 552 input.position().then((position) {
576 Expect.equals(8, position); 553 Expect.equals(8, position);
577 input.close(() => null); 554 input.close().then((ignore) => port.close());
578 }); 555 });
579 }); 556 });
580 }); 557 });
581 }); 558 });
582 }); 559 });
583 }); 560 });
584 }); 561 });
585 } 562 }
586 563
587 static void testPositionSync() { 564 static void testPositionSync() {
588 String filename = getFilename("tests/vm/data/fixed_length_file"); 565 String filename = getFilename("tests/vm/data/fixed_length_file");
589 RandomAccessFile input = (new File(filename)).openSync(); 566 RandomAccessFile input = (new File(filename)).openSync();
590 Expect.equals(0, input.positionSync()); 567 Expect.equals(0, input.positionSync());
591 List<int> buffer = new List<int>(100); 568 List<int> buffer = new List<int>(100);
592 input.readListSync(buffer, 0, 12); 569 input.readListSync(buffer, 0, 12);
593 Expect.equals(12, input.positionSync()); 570 Expect.equals(12, input.positionSync());
594 input.readListSync(buffer, 12, 6); 571 input.readListSync(buffer, 12, 6);
595 Expect.equals(18, input.positionSync()); 572 Expect.equals(18, input.positionSync());
596 input.setPositionSync(8); 573 input.setPositionSync(8);
597 Expect.equals(8, input.positionSync()); 574 Expect.equals(8, input.positionSync());
598 input.closeSync(); 575 input.closeSync();
599 } 576 }
600 577
601 static void testTruncate() { 578 static void testTruncate() {
602 File file = new File(tempDirectory.path + "/out_truncate"); 579 File file = new File(tempDirectory.path + "/out_truncate");
603 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; 580 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
604 file.onError = (e) => Expect.fail("No errors expected: $e"); 581 file.open(FileMode.WRITE).then((RandomAccessFile openedFile) {
605 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { 582 openedFile.writeList(buffer, 0, 10).then((ignore) {
606 openedFile.writeList(buffer, 0, 10); 583 openedFile.length().then((length) {
607 openedFile.onNoPendingWrites = () {
608 openedFile.length((length) {
609 Expect.equals(10, length); 584 Expect.equals(10, length);
610 openedFile.truncate(5, () { 585 openedFile.truncate(5).then((ignore) {
611 openedFile.length((length) { 586 openedFile.length().then((length) {
612 Expect.equals(5, length); 587 Expect.equals(5, length);
613 openedFile.close(() { 588 openedFile.close().then((ignore) {
614 file.delete(() { 589 file.delete().then((ignore) {
615 file.exists((exists) { 590 file.exists().then((exists) {
616 Expect.isFalse(exists); 591 Expect.isFalse(exists);
617 asyncTestDone("testTruncate"); 592 asyncTestDone("testTruncate");
618 }); 593 });
619 }); 594 });
620 }); 595 });
621 }); 596 });
622 }); 597 });
623 }); 598 });
624 }; 599 });
625 }); 600 });
626 asyncTestStarted(); 601 asyncTestStarted();
627 } 602 }
628 603
629 static void testTruncateSync() { 604 static void testTruncateSync() {
630 File file = new File(tempDirectory.path + "/out_truncate_sync"); 605 File file = new File(tempDirectory.path + "/out_truncate_sync");
631 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; 606 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
632 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); 607 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
633 openedFile.writeListSync(buffer, 0, 10); 608 openedFile.writeListSync(buffer, 0, 10);
634 Expect.equals(10, openedFile.lengthSync()); 609 Expect.equals(10, openedFile.lengthSync());
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 wrongExceptionCaught = true; 817 wrongExceptionCaught = true;
843 } 818 }
844 Expect.equals(true, exceptionCaught); 819 Expect.equals(true, exceptionCaught);
845 Expect.equals(true, !wrongExceptionCaught); 820 Expect.equals(true, !wrongExceptionCaught);
846 openedFile.closeSync(); 821 openedFile.closeSync();
847 file.deleteSync(); 822 file.deleteSync();
848 } 823 }
849 824
850 static void testOpenDirectoryAsFile() { 825 static void testOpenDirectoryAsFile() {
851 var f = new File('.'); 826 var f = new File('.');
852 f.open(FileMode.READ, (r) => Expect.fail('Directory opened as file')); 827 var future = f.open(FileMode.READ);
853 f.onError = (e) => null; 828 future.then((r) => Expect.fail('Directory opened as file'));
829 future.handleException((e) => true);
854 } 830 }
855 831
856 static void testOpenDirectoryAsFileSync() { 832 static void testOpenDirectoryAsFileSync() {
857 var f = new File('.'); 833 var f = new File('.');
858 try { 834 try {
859 f.openSync(); 835 f.openSync();
860 Expect.fail("Expected exception opening directory as file"); 836 Expect.fail("Expected exception opening directory as file");
861 } catch (var e) { 837 } catch (var e) {
862 Expect.isTrue(e is FileIOException); 838 Expect.isTrue(e is FileIOException);
863 } 839 }
864 } 840 }
865 841
866 static void testReadAsBytes() { 842 static void testReadAsBytes() {
867 var port = new ReceivePort(); 843 var port = new ReceivePort();
868 port.receive((result, replyTo) { 844 port.receive((result, replyTo) {
869 port.close(); 845 port.close();
870 Expect.equals(42, result); 846 Expect.equals(42, result);
871 }); 847 });
872 var name = getFilename("tests/vm/data/fixed_length_file"); 848 var name = getFilename("tests/vm/data/fixed_length_file");
873 var f = new File(name); 849 var f = new File(name);
874 f.readAsBytes((bytes) { 850 f.readAsBytes().then((bytes) {
875 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); 851 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes."));
876 port.toSendPort().send(bytes.length); 852 port.toSendPort().send(bytes.length);
877 }); 853 });
878 f.onError = (e) => Expect.fail("No errors expected: $e");
879 } 854 }
880 855
881 static void testReadAsBytesSync() { 856 static void testReadAsBytesSync() {
882 var name = getFilename("tests/vm/data/fixed_length_file"); 857 var name = getFilename("tests/vm/data/fixed_length_file");
883 var bytes = new File(name).readAsBytesSync(); 858 var bytes = new File(name).readAsBytesSync();
884 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); 859 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes."));
885 Expect.equals(bytes.length, 42); 860 Expect.equals(bytes.length, 42);
886 } 861 }
887 862
888 static void testReadAsText() { 863 static void testReadAsText() {
889 var port = new ReceivePort(); 864 var port = new ReceivePort();
890 port.receive((result, replyTo) { 865 port.receive((result, replyTo) {
891 port.close(); 866 port.close();
892 Expect.equals(1, result); 867 Expect.equals(1, result);
893 }); 868 });
894 var name = getFilename("tests/vm/data/fixed_length_file"); 869 var name = getFilename("tests/vm/data/fixed_length_file");
895 var f = new File(name); 870 var f = new File(name);
896 f.readAsText(Encoding.UTF_8, (text) { 871 f.readAsText(Encoding.UTF_8).then((text) {
897 Expect.isTrue(text.endsWith("42 bytes.")); 872 Expect.isTrue(text.endsWith("42 bytes."));
898 Expect.equals(42, text.length); 873 Expect.equals(42, text.length);
899 var name = getDataFilename("tests/standalone/io/read_as_text.dat"); 874 var name = getDataFilename("tests/standalone/io/read_as_text.dat");
900 var f = new File(name); 875 var f = new File(name);
901 f.onError = (e) => Expect.fail("No errors expected: $e"); 876 f.readAsText(Encoding.UTF_8).then((text) {
902 f.readAsText(Encoding.UTF_8, (text) {
903 Expect.equals(6, text.length); 877 Expect.equals(6, text.length);
904 var expected = [955, 120, 46, 32, 120, 10]; 878 var expected = [955, 120, 46, 32, 120, 10];
905 Expect.listEquals(expected, text.charCodes()); 879 Expect.listEquals(expected, text.charCodes());
906 f.readAsText(Encoding.ISO_8859_1, (text) { 880 f.readAsText(Encoding.ISO_8859_1).then((text) {
907 Expect.equals(7, text.length); 881 Expect.equals(7, text.length);
908 var expected = [206, 187, 120, 46, 32, 120, 10]; 882 var expected = [206, 187, 120, 46, 32, 120, 10];
909 Expect.listEquals(expected, text.charCodes()); 883 Expect.listEquals(expected, text.charCodes());
910 f.onError = (e) { 884 var readAsTextFuture = f.readAsText(Encoding.ASCII);
885 readAsTextFuture.then((text) {
886 Expect.fail("Non-ascii char should cause error");
887 });
888 readAsTextFuture.handleException((e) {
911 port.toSendPort().send(1); 889 port.toSendPort().send(1);
912 }; 890 return true;
913 f.readAsText(Encoding.ASCII, (text) {
914 Expect.fail("Non-ascii char should cause error");
915 }); 891 });
916 }); 892 });
917 }); 893 });
918 }); 894 });
919 f.onError = (e) {
920 Expect.fail("No errors expected: $e");
921 };
922 } 895 }
923 896
924 static void testReadAsTextSync() { 897 static void testReadAsTextSync() {
925 var name = getFilename("tests/vm/data/fixed_length_file"); 898 var name = getFilename("tests/vm/data/fixed_length_file");
926 var text = new File(name).readAsTextSync(); 899 var text = new File(name).readAsTextSync();
927 Expect.isTrue(text.endsWith("42 bytes.")); 900 Expect.isTrue(text.endsWith("42 bytes."));
928 Expect.equals(42, text.length); 901 Expect.equals(42, text.length);
929 name = getDataFilename("tests/standalone/io/read_as_text.dat"); 902 name = getDataFilename("tests/standalone/io/read_as_text.dat");
930 text = new File(name).readAsTextSync(); 903 text = new File(name).readAsTextSync();
931 Expect.equals(6, text.length); 904 Expect.equals(6, text.length);
932 var expected = [955, 120, 46, 32, 120, 10]; 905 var expected = [955, 120, 46, 32, 120, 10];
933 Expect.listEquals(expected, text.charCodes()); 906 Expect.listEquals(expected, text.charCodes());
934 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); }); 907 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); });
935 text = new File(name).readAsTextSync(Encoding.ISO_8859_1); 908 text = new File(name).readAsTextSync(Encoding.ISO_8859_1);
936 expected = [206, 187, 120, 46, 32, 120, 10]; 909 expected = [206, 187, 120, 46, 32, 120, 10];
937 Expect.equals(7, text.length); 910 Expect.equals(7, text.length);
938 Expect.listEquals(expected, text.charCodes()); 911 Expect.listEquals(expected, text.charCodes());
939 } 912 }
940 913
941 static void testReadAsLines() { 914 static void testReadAsLines() {
942 var port = new ReceivePort(); 915 var port = new ReceivePort();
943 port.receive((result, replyTo) { 916 port.receive((result, replyTo) {
944 port.close(); 917 port.close();
945 Expect.equals(42, result); 918 Expect.equals(42, result);
946 }); 919 });
947 var name = getFilename("tests/vm/data/fixed_length_file"); 920 var name = getFilename("tests/vm/data/fixed_length_file");
948 var f = new File(name); 921 var f = new File(name);
949 f.readAsLines(Encoding.UTF_8, (lines) { 922 f.readAsLines(Encoding.UTF_8).then((lines) {
950 Expect.equals(1, lines.length); 923 Expect.equals(1, lines.length);
951 var line = lines[0]; 924 var line = lines[0];
952 Expect.isTrue(line.endsWith("42 bytes.")); 925 Expect.isTrue(line.endsWith("42 bytes."));
953 port.toSendPort().send(line.length); 926 port.toSendPort().send(line.length);
954 }); 927 });
955 f.onError = (e) => Expect.fail("No errors expected: $e");
956 } 928 }
957 929
958 static void testReadAsLinesSync() { 930 static void testReadAsLinesSync() {
959 var name = getFilename("tests/vm/data/fixed_length_file"); 931 var name = getFilename("tests/vm/data/fixed_length_file");
960 var lines = new File(name).readAsLinesSync(); 932 var lines = new File(name).readAsLinesSync();
961 Expect.equals(1, lines.length); 933 Expect.equals(1, lines.length);
962 var line = lines[0]; 934 var line = lines[0];
963 Expect.isTrue(line.endsWith("42 bytes.")); 935 Expect.isTrue(line.endsWith("42 bytes."));
964 Expect.equals(42, line.length); 936 Expect.equals(42, line.length);
965 name = getDataFilename("tests/standalone/io/readline_test1.dat"); 937 name = getDataFilename("tests/standalone/io/readline_test1.dat");
966 lines = new File(name).readAsLinesSync(); 938 lines = new File(name).readAsLinesSync();
967 Expect.equals(10, lines.length); 939 Expect.equals(10, lines.length);
968 } 940 }
969 941
970 942
971 static void testReadAsErrors() { 943 static void testReadAsErrors() {
972 var port = new ReceivePort(); 944 var port = new ReceivePort();
973 port.receive((message, _) { 945 port.receive((message, _) {
974 port.close(); 946 port.close();
975 Expect.equals(1, message); 947 Expect.equals(1, message);
976 }); 948 });
977 var f = new File('.'); 949 var f = new File('.');
978 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); 950 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException);
979 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); 951 Expect.throws(f.readAsTextSync, (e) => e is FileIOException);
980 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); 952 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException);
981 f.readAsBytes((bytes) => Expect.fail("no bytes expected")); 953 var readAsBytesFuture = f.readAsBytes();
982 f.onError = (e) { 954 readAsBytesFuture.then((bytes) => Expect.fail("no bytes expected"));
983 f.readAsText(Encoding.UTF_8, (text) => Expect.fail("no text expected")); 955 readAsBytesFuture.handleException((e) {
984 f.onError = (e) { 956 var readAsTextFuture = f.readAsText(Encoding.UTF_8);
985 f.readAsLines(Encoding.UTF_8, 957 readAsTextFuture.then((text) => Expect.fail("no text expected"));
986 (lines) => Expect.fail("no lines expected")); 958 readAsTextFuture.handleException((e) {
987 f.onError = (e) => port.toSendPort().send(1); 959 var readAsLinesFuture = f.readAsLines(Encoding.UTF_8);
988 }; 960 readAsLinesFuture.then((lines) => Expect.fail("no lines expected"));
989 }; 961 readAsLinesFuture.handleException((e) {
962 port.toSendPort().send(1);
963 return true;
964 });
965 return true;
966 });
967 return true;
968 });
990 } 969 }
991 970
992 // Test that opens the same file for writing then for appending to test 971 // Test that opens the same file for writing then for appending to test
993 // that the file is not truncated when opened for appending. 972 // that the file is not truncated when opened for appending.
994 static void testAppend() { 973 static void testAppend() {
995 var file = new File('${tempDirectory.path}/out_append'); 974 var file = new File('${tempDirectory.path}/out_append');
996 file.open(FileMode.WRITE, (openedFile) { 975 file.open(FileMode.WRITE).then((openedFile) {
997 openedFile.writeString("asdf"); 976 openedFile.writeString("asdf").then((ignore) {
998 openedFile.onNoPendingWrites = () { 977 openedFile.close().then((ignore) {
999 openedFile.close(() { 978 file.open(FileMode.APPEND).then((openedFile) {
1000 file.open(FileMode.APPEND, (openedFile) { 979 openedFile.length().then((length) {
1001 openedFile.length((length) {
1002 Expect.equals(4, length); 980 Expect.equals(4, length);
1003 openedFile.writeString("asdf"); 981 openedFile.writeString("asdf").then((ignore) {
1004 openedFile.onNoPendingWrites = () { 982 openedFile.length().then((length) {
1005 openedFile.length((length) {
1006 Expect.equals(8, length); 983 Expect.equals(8, length);
1007 openedFile.close(() { 984 openedFile.close().then((ignore) {
1008 file.delete(() { 985 file.delete().then((ignore) {
1009 file.exists((exists) { 986 file.exists().then((exists) {
1010 Expect.isFalse(exists); 987 Expect.isFalse(exists);
1011 asyncTestDone("testAppend"); 988 asyncTestDone("testAppend");
1012 }); 989 });
1013 }); 990 });
1014 }); 991 });
1015 }); 992 });
1016 }; 993 });
1017 }); 994 });
1018 }); 995 });
1019 }); 996 });
1020 }; 997 });
1021 }); 998 });
1022 asyncTestStarted(); 999 asyncTestStarted();
1023 } 1000 }
1024 1001
1025 static void testAppendSync() { 1002 static void testAppendSync() {
1026 var file = new File('${tempDirectory.path}/out_append_sync'); 1003 var file = new File('${tempDirectory.path}/out_append_sync');
1027 var openedFile = file.openSync(FileMode.WRITE); 1004 var openedFile = file.openSync(FileMode.WRITE);
1028 openedFile.writeStringSync("asdf"); 1005 openedFile.writeStringSync("asdf");
1029 Expect.equals(4, openedFile.lengthSync()); 1006 Expect.equals(4, openedFile.lengthSync());
1030 openedFile.closeSync(); 1007 openedFile.closeSync();
1031 openedFile = file.openSync(FileMode.WRITE); 1008 openedFile = file.openSync(FileMode.WRITE);
1032 openedFile.setPositionSync(4); 1009 openedFile.setPositionSync(4);
1033 openedFile.writeStringSync("asdf"); 1010 openedFile.writeStringSync("asdf");
1034 Expect.equals(8, openedFile.lengthSync()); 1011 Expect.equals(8, openedFile.lengthSync());
1035 openedFile.closeSync(); 1012 openedFile.closeSync();
1036 file.deleteSync(); 1013 file.deleteSync();
1037 Expect.isFalse(file.existsSync()); 1014 Expect.isFalse(file.existsSync());
1038 } 1015 }
1039 1016
1040 // Helper method to be able to run the test from the runtime 1017 // Helper method to be able to run the test from the runtime
1041 // directory, or the top directory. 1018 // directory, or the top directory.
1042 static String getFilename(String path) => 1019 static String getFilename(String path) =>
1043 new File(path).existsSync() ? path : 'runtime/' + path; 1020 new File(path).existsSync() ? path : 'runtime/' + path;
1044 1021
1045 static String getDataFilename(String path) => 1022 static String getDataFilename(String path) =>
1046 new File(path).existsSync() ? path : '../' + path; 1023 new File(path).existsSync() ? path : '../' + path;
1047 1024
1048 // Main test entrypoint. 1025 // Main test entrypoint.
1049 static testMain() { 1026 static testMain() {
1027 port = new ReceivePort();
1050 testRead(); 1028 testRead();
1051 testReadSync(); 1029 testReadSync();
1052 testReadStream(); 1030 testReadStream();
1053 testLength(); 1031 testLength();
1054 testLengthSync(); 1032 testLengthSync();
1055 testPosition(); 1033 testPosition();
1056 testPositionSync(); 1034 testPositionSync();
1057 testOpenDirectoryAsFile(); 1035 testOpenDirectoryAsFile();
1058 testOpenDirectoryAsFileSync(); 1036 testOpenDirectoryAsFileSync();
1059 testReadAsBytes(); 1037 testReadAsBytes();
(...skipping 23 matching lines...) Expand all
1083 testWriteVariousLists(); 1061 testWriteVariousLists();
1084 testDirectory(); 1062 testDirectory();
1085 testDirectorySync(); 1063 testDirectorySync();
1086 }); 1064 });
1087 } 1065 }
1088 } 1066 }
1089 1067
1090 main() { 1068 main() {
1091 FileTest.testMain(); 1069 FileTest.testMain();
1092 } 1070 }
OLDNEW
« no previous file with comments | « tests/standalone/io/file_system_links_test.dart ('k') | tests/standalone/io/many_directory_operations_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698