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

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

Issue 12609004: Change all File APIs to make the mode and encoding arguments named (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments and changed other use places Created 7 years, 9 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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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:async'; 7 import 'dart:async';
8 import 'dart:io'; 8 import 'dart:io';
9 import 'dart:isolate'; 9 import 'dart:isolate';
10 10
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 }); 193 });
194 }); 194 });
195 return done.future; 195 return done.future;
196 } 196 }
197 197
198 static void testRead() { 198 static void testRead() {
199 ReceivePort port = new ReceivePort(); 199 ReceivePort port = new ReceivePort();
200 // Read a file and check part of it's contents. 200 // Read a file and check part of it's contents.
201 String filename = getFilename("bin/file_test.cc"); 201 String filename = getFilename("bin/file_test.cc");
202 File file = new File(filename); 202 File file = new File(filename);
203 file.open(FileMode.READ).then((RandomAccessFile file) { 203 file.open(mode: FileMode.READ).then((RandomAccessFile file) {
204 List<int> buffer = new List<int>(10); 204 List<int> buffer = new List<int>(10);
205 file.readList(buffer, 0, 5).then((bytes_read) { 205 file.readList(buffer, 0, 5).then((bytes_read) {
206 Expect.equals(5, bytes_read); 206 Expect.equals(5, bytes_read);
207 file.readList(buffer, 5, 5).then((bytes_read) { 207 file.readList(buffer, 5, 5).then((bytes_read) {
208 Expect.equals(5, bytes_read); 208 Expect.equals(5, bytes_read);
209 Expect.equals(47, buffer[0]); // represents '/' in the file. 209 Expect.equals(47, buffer[0]); // represents '/' in the file.
210 Expect.equals(47, buffer[1]); // represents '/' in the file. 210 Expect.equals(47, buffer[1]); // represents '/' in the file.
211 Expect.equals(32, buffer[2]); // represents ' ' in the file. 211 Expect.equals(32, buffer[2]); // represents ' ' in the file.
212 Expect.equals(67, buffer[3]); // represents 'C' in the file. 212 Expect.equals(67, buffer[3]); // represents 'C' in the file.
213 Expect.equals(111, buffer[4]); // represents 'o' in the file. 213 Expect.equals(111, buffer[4]); // represents 'o' in the file.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 Expect.equals(len, file.openSync().readSync(len + 1).length); 255 Expect.equals(len, file.openSync().readSync(len + 1).length);
256 Expect.equals(len, file.openSync().readSync(len * 2).length); 256 Expect.equals(len, file.openSync().readSync(len * 2).length);
257 Expect.equals(len, file.openSync().readSync(len * 10).length); 257 Expect.equals(len, file.openSync().readSync(len * 10).length);
258 } 258 }
259 259
260 // Test for file read and write functionality. 260 // Test for file read and write functionality.
261 static void testReadWrite() { 261 static void testReadWrite() {
262 // Read a file. 262 // Read a file.
263 String inFilename = getFilename("tests/vm/data/fixed_length_file"); 263 String inFilename = getFilename("tests/vm/data/fixed_length_file");
264 final File file = new File(inFilename); 264 final File file = new File(inFilename);
265 file.open(FileMode.READ).then((openedFile) { 265 file.open(mode: FileMode.READ).then((openedFile) {
266 List<int> buffer1 = new List<int>(42); 266 List<int> buffer1 = new List<int>(42);
267 openedFile.readList(buffer1, 0, 42).then((bytes_read) { 267 openedFile.readList(buffer1, 0, 42).then((bytes_read) {
268 Expect.equals(42, bytes_read); 268 Expect.equals(42, bytes_read);
269 openedFile.close().then((ignore) { 269 openedFile.close().then((ignore) {
270 // Write the contents of the file just read into another file. 270 // Write the contents of the file just read into another file.
271 String outFilename = tempDirectory.path + "/out_read_write"; 271 String outFilename = tempDirectory.path + "/out_read_write";
272 final File file2 = new File(outFilename); 272 final File file2 = new File(outFilename);
273 file2.create().then((ignore) { 273 file2.create().then((ignore) {
274 file2.fullPath().then((s) { 274 file2.fullPath().then((s) {
275 Expect.isTrue(new File(s).existsSync()); 275 Expect.isTrue(new File(s).existsSync());
276 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { 276 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') {
277 Expect.fail("Not a full path"); 277 Expect.fail("Not a full path");
278 } 278 }
279 file2.open(FileMode.WRITE).then((openedFile2) { 279 file2.open(mode: FileMode.WRITE).then((openedFile2) {
280 openedFile2.writeList(buffer1, 0, bytes_read).then((ignore) { 280 openedFile2.writeList(buffer1, 0, bytes_read).then((ignore) {
281 openedFile2.close().then((ignore) { 281 openedFile2.close().then((ignore) {
282 List<int> buffer2 = new List<int>(bytes_read); 282 List<int> buffer2 = new List<int>(bytes_read);
283 final File file3 = new File(outFilename); 283 final File file3 = new File(outFilename);
284 file3.open(FileMode.READ).then((openedFile3) { 284 file3.open(mode: FileMode.READ).then((openedFile3) {
285 openedFile3.readList(buffer2, 0, 42).then((bytes_read) { 285 openedFile3.readList(buffer2, 0, 42).then((bytes_read) {
286 Expect.equals(42, bytes_read); 286 Expect.equals(42, bytes_read);
287 openedFile3.close().then((ignore) { 287 openedFile3.close().then((ignore) {
288 // Now compare the two buffers to check if they 288 // Now compare the two buffers to check if they
289 // are identical. 289 // are identical.
290 Expect.equals(buffer1.length, buffer2.length); 290 Expect.equals(buffer1.length, buffer2.length);
291 for (int i = 0; i < buffer1.length; i++) { 291 for (int i = 0; i < buffer1.length; i++) {
292 Expect.equals(buffer1[i], buffer2[i]); 292 Expect.equals(buffer1[i], buffer2[i]);
293 } 293 }
294 // Delete the output file. 294 // Delete the output file.
(...skipping 18 matching lines...) Expand all
313 asyncTestStarted(); 313 asyncTestStarted();
314 } 314 }
315 315
316 static void testWriteAppend() { 316 static void testWriteAppend() {
317 String content = "foobar"; 317 String content = "foobar";
318 String filename = tempDirectory.path + "/write_append"; 318 String filename = tempDirectory.path + "/write_append";
319 File file = new File(filename); 319 File file = new File(filename);
320 file.createSync(); 320 file.createSync();
321 Expect.isTrue(new File(filename).existsSync()); 321 Expect.isTrue(new File(filename).existsSync());
322 List<int> buffer = content.codeUnits; 322 List<int> buffer = content.codeUnits;
323 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); 323 RandomAccessFile openedFile = file.openSync(mode: FileMode.WRITE);
324 openedFile.writeListSync(buffer, 0, buffer.length); 324 openedFile.writeListSync(buffer, 0, buffer.length);
325 openedFile.closeSync(); 325 openedFile.closeSync();
326 // Reopen the file in write mode to ensure that we overwrite the content. 326 // Reopen the file in write mode to ensure that we overwrite the content.
327 openedFile = (new File(filename)).openSync(FileMode.WRITE); 327 openedFile = (new File(filename)).openSync(mode: FileMode.WRITE);
328 openedFile.writeListSync(buffer, 0, buffer.length); 328 openedFile.writeListSync(buffer, 0, buffer.length);
329 Expect.equals(content.length, openedFile.lengthSync()); 329 Expect.equals(content.length, openedFile.lengthSync());
330 openedFile.closeSync(); 330 openedFile.closeSync();
331 // Open the file in append mode and ensure that we do not overwrite 331 // Open the file in append mode and ensure that we do not overwrite
332 // the existing content. 332 // the existing content.
333 openedFile = (new File(filename)).openSync(FileMode.APPEND); 333 openedFile = (new File(filename)).openSync(mode: FileMode.APPEND);
334 openedFile.writeListSync(buffer, 0, buffer.length); 334 openedFile.writeListSync(buffer, 0, buffer.length);
335 Expect.equals(content.length * 2, openedFile.lengthSync()); 335 Expect.equals(content.length * 2, openedFile.lengthSync());
336 openedFile.closeSync(); 336 openedFile.closeSync();
337 file.deleteSync(); 337 file.deleteSync();
338 } 338 }
339 339
340 static void testOutputStreamWriteAppend() { 340 static void testOutputStreamWriteAppend() {
341 String content = "foobar"; 341 String content = "foobar";
342 String filename = tempDirectory.path + "/outstream_write_append"; 342 String filename = tempDirectory.path + "/outstream_write_append";
343 File file = new File(filename); 343 File file = new File(filename);
344 file.createSync(); 344 file.createSync();
345 List<int> buffer = content.codeUnits; 345 List<int> buffer = content.codeUnits;
346 var output = file.openWrite(); 346 var output = file.openWrite();
347 output.writeBytes(buffer); 347 output.writeBytes(buffer);
348 output.close(); 348 output.close();
349 output.done.then((_) { 349 output.done.then((_) {
350 File file2 = new File(filename); 350 File file2 = new File(filename);
351 var appendingOutput = file2.openWrite(mode: FileMode.APPEND); 351 var appendingOutput = file2.openWrite(mode: FileMode.APPEND);
352 appendingOutput.writeBytes(buffer); 352 appendingOutput.writeBytes(buffer);
353 appendingOutput.close(); 353 appendingOutput.close();
354 appendingOutput.done.then((_) { 354 appendingOutput.done.then((_) {
355 File file3 = new File(filename); 355 File file3 = new File(filename);
356 file3.open(FileMode.READ).then((RandomAccessFile openedFile) { 356 file3.open(mode: FileMode.READ).then((RandomAccessFile openedFile) {
357 openedFile.length().then((int length) { 357 openedFile.length().then((int length) {
358 Expect.equals(content.length * 2, length); 358 Expect.equals(content.length * 2, length);
359 openedFile.close().then((ignore) { 359 openedFile.close().then((ignore) {
360 file3.delete().then((ignore) { 360 file3.delete().then((ignore) {
361 asyncTestDone("testOutputStreamWriteAppend"); 361 asyncTestDone("testOutputStreamWriteAppend");
362 }); 362 });
363 }); 363 });
364 }); 364 });
365 }); 365 });
366 }); 366 });
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 file.closeSync(); 409 file.closeSync();
410 // Write the contents of the file just read into another file. 410 // Write the contents of the file just read into another file.
411 String outFilename = tempDirectory.path + "/out_read_write_sync"; 411 String outFilename = tempDirectory.path + "/out_read_write_sync";
412 File outFile = new File(outFilename); 412 File outFile = new File(outFilename);
413 outFile.createSync(); 413 outFile.createSync();
414 String path = outFile.fullPathSync(); 414 String path = outFile.fullPathSync();
415 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') { 415 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') {
416 Expect.fail("Not a full path"); 416 Expect.fail("Not a full path");
417 } 417 }
418 Expect.isTrue(new File(path).existsSync()); 418 Expect.isTrue(new File(path).existsSync());
419 RandomAccessFile openedFile = outFile.openSync(FileMode.WRITE); 419 RandomAccessFile openedFile = outFile.openSync(mode: FileMode.WRITE);
420 openedFile.writeListSync(buffer1, 0, bytes_read); 420 openedFile.writeListSync(buffer1, 0, bytes_read);
421 openedFile.closeSync(); 421 openedFile.closeSync();
422 // Now read the contents of the file just written. 422 // Now read the contents of the file just written.
423 List<int> buffer2 = new List<int>(bytes_read); 423 List<int> buffer2 = new List<int>(bytes_read);
424 openedFile = (new File(outFilename)).openSync(); 424 openedFile = (new File(outFilename)).openSync();
425 bytes_read = openedFile.readListSync(buffer2, 0, 42); 425 bytes_read = openedFile.readListSync(buffer2, 0, 42);
426 Expect.equals(42, bytes_read); 426 Expect.equals(42, bytes_read);
427 openedFile.closeSync(); 427 openedFile.closeSync();
428 // Now compare the two buffers to check if they are identical. 428 // Now compare the two buffers to check if they are identical.
429 Expect.equals(buffer1.length, buffer2.length); 429 Expect.equals(buffer1.length, buffer2.length);
(...skipping 13 matching lines...) Expand all
443 Expect.equals(-1, openedFile.readByteSync()); 443 Expect.equals(-1, openedFile.readByteSync());
444 openedFile.closeSync(); 444 openedFile.closeSync();
445 file.deleteSync(); 445 file.deleteSync();
446 } 446 }
447 447
448 static void testReadEmptyFile() { 448 static void testReadEmptyFile() {
449 String fileName = tempDirectory.path + "/empty_file"; 449 String fileName = tempDirectory.path + "/empty_file";
450 File file = new File(fileName); 450 File file = new File(fileName);
451 asyncTestStarted(); 451 asyncTestStarted();
452 file.create().then((ignore) { 452 file.create().then((ignore) {
453 file.open(FileMode.READ).then((RandomAccessFile openedFile) { 453 file.open(mode: FileMode.READ).then((RandomAccessFile openedFile) {
454 var readByteFuture = openedFile.readByte(); 454 var readByteFuture = openedFile.readByte();
455 readByteFuture.then((int byte) { 455 readByteFuture.then((int byte) {
456 Expect.equals(-1, byte); 456 Expect.equals(-1, byte);
457 openedFile.close().then((ignore) { 457 openedFile.close().then((ignore) {
458 asyncTestDone("testReadEmptyFile"); 458 asyncTestDone("testReadEmptyFile");
459 }); 459 });
460 }); 460 });
461 }); 461 });
462 }); 462 });
463 } 463 }
464 464
465 // Test for file write of different types of lists. 465 // Test for file write of different types of lists.
466 static void testWriteVariousLists() { 466 static void testWriteVariousLists() {
467 asyncTestStarted(); 467 asyncTestStarted();
468 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; 468 final String fileName = "${tempDirectory.path}/testWriteVariousLists";
469 final File file = new File(fileName); 469 final File file = new File(fileName);
470 file.create().then((ignore) { 470 file.create().then((ignore) {
471 file.open(FileMode.WRITE).then((RandomAccessFile openedFile) { 471 file.open(mode: FileMode.WRITE).then((RandomAccessFile openedFile) {
472 // Write bytes from 0 to 7. 472 // Write bytes from 0 to 7.
473 openedFile.writeList([0], 0, 1); 473 openedFile.writeList([0], 0, 1);
474 openedFile.writeList(const [1], 0, 1); 474 openedFile.writeList(const [1], 0, 1);
475 openedFile.writeList(new MyListOfOneElement(2), 0, 1); 475 openedFile.writeList(new MyListOfOneElement(2), 0, 1);
476 var x = 12345678901234567890123456789012345678901234567890; 476 var x = 12345678901234567890123456789012345678901234567890;
477 var y = 12345678901234567890123456789012345678901234567893; 477 var y = 12345678901234567890123456789012345678901234567893;
478 openedFile.writeList([y - x], 0, 1); 478 openedFile.writeList([y - x], 0, 1);
479 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. 479 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104.
480 openedFile.writeList(const [261], 0, 1); 480 openedFile.writeList(const [261], 0, 1);
481 openedFile.writeList(new MyListOfOneElement(262), 0, 1); 481 openedFile.writeList(new MyListOfOneElement(262), 0, 1);
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 input.readListSync(buffer, 12, 6); 622 input.readListSync(buffer, 12, 6);
623 Expect.equals(18, input.positionSync()); 623 Expect.equals(18, input.positionSync());
624 input.setPositionSync(8); 624 input.setPositionSync(8);
625 Expect.equals(8, input.positionSync()); 625 Expect.equals(8, input.positionSync());
626 input.closeSync(); 626 input.closeSync();
627 } 627 }
628 628
629 static void testTruncate() { 629 static void testTruncate() {
630 File file = new File(tempDirectory.path + "/out_truncate"); 630 File file = new File(tempDirectory.path + "/out_truncate");
631 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; 631 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
632 file.open(FileMode.WRITE).then((RandomAccessFile openedFile) { 632 file.open(mode: FileMode.WRITE).then((RandomAccessFile openedFile) {
633 openedFile.writeList(buffer, 0, 10).then((ignore) { 633 openedFile.writeList(buffer, 0, 10).then((ignore) {
634 openedFile.length().then((length) { 634 openedFile.length().then((length) {
635 Expect.equals(10, length); 635 Expect.equals(10, length);
636 openedFile.truncate(5).then((ignore) { 636 openedFile.truncate(5).then((ignore) {
637 openedFile.length().then((length) { 637 openedFile.length().then((length) {
638 Expect.equals(5, length); 638 Expect.equals(5, length);
639 openedFile.close().then((ignore) { 639 openedFile.close().then((ignore) {
640 file.delete().then((ignore) { 640 file.delete().then((ignore) {
641 file.exists().then((exists) { 641 file.exists().then((exists) {
642 Expect.isFalse(exists); 642 Expect.isFalse(exists);
643 asyncTestDone("testTruncate"); 643 asyncTestDone("testTruncate");
644 }); 644 });
645 }); 645 });
646 }); 646 });
647 }); 647 });
648 }); 648 });
649 }); 649 });
650 }); 650 });
651 }); 651 });
652 asyncTestStarted(); 652 asyncTestStarted();
653 } 653 }
654 654
655 static void testTruncateSync() { 655 static void testTruncateSync() {
656 File file = new File(tempDirectory.path + "/out_truncate_sync"); 656 File file = new File(tempDirectory.path + "/out_truncate_sync");
657 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; 657 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
658 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); 658 RandomAccessFile openedFile = file.openSync(mode: FileMode.WRITE);
659 openedFile.writeListSync(buffer, 0, 10); 659 openedFile.writeListSync(buffer, 0, 10);
660 Expect.equals(10, openedFile.lengthSync()); 660 Expect.equals(10, openedFile.lengthSync());
661 openedFile.truncateSync(5); 661 openedFile.truncateSync(5);
662 Expect.equals(5, openedFile.lengthSync()); 662 Expect.equals(5, openedFile.lengthSync());
663 openedFile.closeSync(); 663 openedFile.closeSync();
664 file.deleteSync(); 664 file.deleteSync();
665 Expect.isFalse(file.existsSync()); 665 Expect.isFalse(file.existsSync());
666 } 666 }
667 667
668 // Tests exception handling after file was closed. 668 // Tests exception handling after file was closed.
669 static void testCloseException() { 669 static void testCloseException() {
670 bool exceptionCaught = false; 670 bool exceptionCaught = false;
671 bool wrongExceptionCaught = false; 671 bool wrongExceptionCaught = false;
672 File input = new File(tempDirectory.path + "/out_close_exception"); 672 File input = new File(tempDirectory.path + "/out_close_exception");
673 RandomAccessFile openedFile = input.openSync(FileMode.WRITE); 673 RandomAccessFile openedFile = input.openSync(mode: FileMode.WRITE);
674 openedFile.closeSync(); 674 openedFile.closeSync();
675 try { 675 try {
676 openedFile.readByteSync(); 676 openedFile.readByteSync();
677 } on FileIOException catch (ex) { 677 } on FileIOException catch (ex) {
678 exceptionCaught = true; 678 exceptionCaught = true;
679 } on Exception catch (ex) { 679 } on Exception catch (ex) {
680 wrongExceptionCaught = true; 680 wrongExceptionCaught = true;
681 } 681 }
682 Expect.equals(true, exceptionCaught); 682 Expect.equals(true, exceptionCaught);
683 Expect.equals(true, !wrongExceptionCaught); 683 Expect.equals(true, !wrongExceptionCaught);
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 asyncTestDone("testCloseExceptionStream"); 771 asyncTestDone("testCloseExceptionStream");
772 }); 772 });
773 } 773 }
774 774
775 // Tests buffer out of bounds exception. 775 // Tests buffer out of bounds exception.
776 static void testBufferOutOfBoundsException() { 776 static void testBufferOutOfBoundsException() {
777 bool exceptionCaught = false; 777 bool exceptionCaught = false;
778 bool wrongExceptionCaught = false; 778 bool wrongExceptionCaught = false;
779 File file = 779 File file =
780 new File(tempDirectory.path + "/out_buffer_out_of_bounds"); 780 new File(tempDirectory.path + "/out_buffer_out_of_bounds");
781 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); 781 RandomAccessFile openedFile = file.openSync(mode: FileMode.WRITE);
782 try { 782 try {
783 List<int> buffer = new List<int>(10); 783 List<int> buffer = new List<int>(10);
784 openedFile.readListSync(buffer, 0, 12); 784 openedFile.readListSync(buffer, 0, 12);
785 } on RangeError catch (ex) { 785 } on RangeError catch (ex) {
786 exceptionCaught = true; 786 exceptionCaught = true;
787 } on Exception catch (ex) { 787 } on Exception catch (ex) {
788 wrongExceptionCaught = true; 788 wrongExceptionCaught = true;
789 } 789 }
790 Expect.equals(true, exceptionCaught); 790 Expect.equals(true, exceptionCaught);
791 Expect.equals(true, !wrongExceptionCaught); 791 Expect.equals(true, !wrongExceptionCaught);
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 wrongExceptionCaught = true; 865 wrongExceptionCaught = true;
866 } 866 }
867 Expect.equals(true, exceptionCaught); 867 Expect.equals(true, exceptionCaught);
868 Expect.equals(true, !wrongExceptionCaught); 868 Expect.equals(true, !wrongExceptionCaught);
869 openedFile.closeSync(); 869 openedFile.closeSync();
870 file.deleteSync(); 870 file.deleteSync();
871 } 871 }
872 872
873 static void testOpenDirectoryAsFile() { 873 static void testOpenDirectoryAsFile() {
874 var f = new File('.'); 874 var f = new File('.');
875 var future = f.open(FileMode.READ); 875 var future = f.open(mode: FileMode.READ);
876 future.then((r) => Expect.fail('Directory opened as file')) 876 future.then((r) => Expect.fail('Directory opened as file'))
877 .catchError((e) {}); 877 .catchError((e) {});
878 } 878 }
879 879
880 static void testOpenDirectoryAsFileSync() { 880 static void testOpenDirectoryAsFileSync() {
881 var f = new File('.'); 881 var f = new File('.');
882 try { 882 try {
883 f.openSync(); 883 f.openSync();
884 Expect.fail("Expected exception opening directory as file"); 884 Expect.fail("Expected exception opening directory as file");
885 } catch (e) { 885 } catch (e) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
940 } 940 }
941 941
942 static void testReadAsText() { 942 static void testReadAsText() {
943 var port = new ReceivePort(); 943 var port = new ReceivePort();
944 port.receive((result, replyTo) { 944 port.receive((result, replyTo) {
945 port.close(); 945 port.close();
946 Expect.equals(1, result); 946 Expect.equals(1, result);
947 }); 947 });
948 var name = getFilename("tests/vm/data/fixed_length_file"); 948 var name = getFilename("tests/vm/data/fixed_length_file");
949 var f = new File(name); 949 var f = new File(name);
950 f.readAsString(Encoding.UTF_8).then((text) { 950 f.readAsString(encoding: Encoding.UTF_8).then((text) {
951 Expect.isTrue(text.endsWith("42 bytes.")); 951 Expect.isTrue(text.endsWith("42 bytes."));
952 Expect.equals(42, text.length); 952 Expect.equals(42, text.length);
953 var name = getDataFilename("tests/standalone/io/read_as_text.dat"); 953 var name = getDataFilename("tests/standalone/io/read_as_text.dat");
954 var f = new File(name); 954 var f = new File(name);
955 f.readAsString(Encoding.UTF_8).then((text) { 955 f.readAsString(encoding: Encoding.UTF_8).then((text) {
956 Expect.equals(6, text.length); 956 Expect.equals(6, text.length);
957 var expected = [955, 120, 46, 32, 120, 10]; 957 var expected = [955, 120, 46, 32, 120, 10];
958 Expect.listEquals(expected, text.codeUnits); 958 Expect.listEquals(expected, text.codeUnits);
959 f.readAsString(Encoding.ISO_8859_1).then((text) { 959 f.readAsString(encoding: Encoding.ISO_8859_1).then((text) {
960 Expect.equals(7, text.length); 960 Expect.equals(7, text.length);
961 var expected = [206, 187, 120, 46, 32, 120, 10]; 961 var expected = [206, 187, 120, 46, 32, 120, 10];
962 Expect.listEquals(expected, text.codeUnits); 962 Expect.listEquals(expected, text.codeUnits);
963 var readAsStringFuture = f.readAsString(Encoding.ASCII); 963 var readAsStringFuture = f.readAsString(encoding: Encoding.ASCII);
964 readAsStringFuture.then((text) { 964 readAsStringFuture.then((text) {
965 Expect.fail("Non-ascii char should cause error"); 965 Expect.fail("Non-ascii char should cause error");
966 }).catchError((e) { 966 }).catchError((e) {
967 port.toSendPort().send(1); 967 port.toSendPort().send(1);
968 }); 968 });
969 }); 969 });
970 }); 970 });
971 }); 971 });
972 } 972 }
973 973
974 static void testReadAsTextEmptyFile() { 974 static void testReadAsTextEmptyFile() {
975 var port = new ReceivePort(); 975 var port = new ReceivePort();
976 port.receive((result, replyTo) { 976 port.receive((result, replyTo) {
977 port.close(); 977 port.close();
978 Expect.equals(0, result); 978 Expect.equals(0, result);
979 }); 979 });
980 var name = getFilename("tests/vm/data/empty_file"); 980 var name = getFilename("tests/vm/data/empty_file");
981 var f = new File(name); 981 var f = new File(name);
982 f.readAsString(Encoding.UTF_8).then((text) { 982 f.readAsString(encoding: Encoding.UTF_8).then((text) {
983 port.toSendPort().send(text.length); 983 port.toSendPort().send(text.length);
984 return true; 984 return true;
985 }); 985 });
986 } 986 }
987 987
988 static void testReadAsTextSync() { 988 static void testReadAsTextSync() {
989 var name = getFilename("tests/vm/data/fixed_length_file"); 989 var name = getFilename("tests/vm/data/fixed_length_file");
990 var text = new File(name).readAsStringSync(); 990 var text = new File(name).readAsStringSync();
991 Expect.isTrue(text.endsWith("42 bytes.")); 991 Expect.isTrue(text.endsWith("42 bytes."));
992 Expect.equals(42, text.length); 992 Expect.equals(42, text.length);
993 name = getDataFilename("tests/standalone/io/read_as_text.dat"); 993 name = getDataFilename("tests/standalone/io/read_as_text.dat");
994 text = new File(name).readAsStringSync(); 994 text = new File(name).readAsStringSync();
995 Expect.equals(6, text.length); 995 Expect.equals(6, text.length);
996 var expected = [955, 120, 46, 32, 120, 10]; 996 var expected = [955, 120, 46, 32, 120, 10];
997 Expect.listEquals(expected, text.codeUnits); 997 Expect.listEquals(expected, text.codeUnits);
998 text = new File(name).readAsStringSync(Encoding.ASCII); 998 text = new File(name).readAsStringSync(encoding: Encoding.ASCII);
999 // Default replacement character is '?', char code 63. 999 // Default replacement character is '?', char code 63.
1000 expected = [63, 63, 120, 46, 32, 120, 10]; 1000 expected = [63, 63, 120, 46, 32, 120, 10];
1001 Expect.listEquals(expected, text.codeUnits); 1001 Expect.listEquals(expected, text.codeUnits);
1002 text = new File(name).readAsStringSync(Encoding.ISO_8859_1); 1002 text = new File(name).readAsStringSync(encoding: Encoding.ISO_8859_1);
1003 expected = [206, 187, 120, 46, 32, 120, 10]; 1003 expected = [206, 187, 120, 46, 32, 120, 10];
1004 Expect.equals(7, text.length); 1004 Expect.equals(7, text.length);
1005 Expect.listEquals(expected, text.codeUnits); 1005 Expect.listEquals(expected, text.codeUnits);
1006 } 1006 }
1007 1007
1008 static void testReadAsTextSyncEmptyFile() { 1008 static void testReadAsTextSyncEmptyFile() {
1009 var name = getFilename("tests/vm/data/empty_file"); 1009 var name = getFilename("tests/vm/data/empty_file");
1010 var text = new File(name).readAsStringSync(); 1010 var text = new File(name).readAsStringSync();
1011 Expect.equals(0, text.length); 1011 Expect.equals(0, text.length);
1012 } 1012 }
1013 1013
1014 static void testReadAsLines() { 1014 static void testReadAsLines() {
1015 var port = new ReceivePort(); 1015 var port = new ReceivePort();
1016 port.receive((result, replyTo) { 1016 port.receive((result, replyTo) {
1017 port.close(); 1017 port.close();
1018 Expect.equals(42, result); 1018 Expect.equals(42, result);
1019 }); 1019 });
1020 var name = getFilename("tests/vm/data/fixed_length_file"); 1020 var name = getFilename("tests/vm/data/fixed_length_file");
1021 var f = new File(name); 1021 var f = new File(name);
1022 f.readAsLines(Encoding.UTF_8).then((lines) { 1022 f.readAsLines(encoding: Encoding.UTF_8).then((lines) {
1023 Expect.equals(1, lines.length); 1023 Expect.equals(1, lines.length);
1024 var line = lines[0]; 1024 var line = lines[0];
1025 Expect.isTrue(line.endsWith("42 bytes.")); 1025 Expect.isTrue(line.endsWith("42 bytes."));
1026 port.toSendPort().send(line.length); 1026 port.toSendPort().send(line.length);
1027 }); 1027 });
1028 } 1028 }
1029 1029
1030 static void testReadAsLinesSync() { 1030 static void testReadAsLinesSync() {
1031 var name = getFilename("tests/vm/data/fixed_length_file"); 1031 var name = getFilename("tests/vm/data/fixed_length_file");
1032 var lines = new File(name).readAsLinesSync(); 1032 var lines = new File(name).readAsLinesSync();
(...skipping 13 matching lines...) Expand all
1046 port.close(); 1046 port.close();
1047 Expect.equals(1, message); 1047 Expect.equals(1, message);
1048 }); 1048 });
1049 var f = new File('.'); 1049 var f = new File('.');
1050 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); 1050 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException);
1051 Expect.throws(f.readAsStringSync, (e) => e is FileIOException); 1051 Expect.throws(f.readAsStringSync, (e) => e is FileIOException);
1052 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); 1052 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException);
1053 var readAsBytesFuture = f.readAsBytes(); 1053 var readAsBytesFuture = f.readAsBytes();
1054 readAsBytesFuture.then((bytes) => Expect.fail("no bytes expected")) 1054 readAsBytesFuture.then((bytes) => Expect.fail("no bytes expected"))
1055 .catchError((e) { 1055 .catchError((e) {
1056 var readAsStringFuture = f.readAsString(Encoding.UTF_8); 1056 var readAsStringFuture = f.readAsString(encoding: Encoding.UTF_8);
1057 readAsStringFuture.then((text) => Expect.fail("no text expected")) 1057 readAsStringFuture.then((text) => Expect.fail("no text expected"))
1058 .catchError((e) { 1058 .catchError((e) {
1059 var readAsLinesFuture = f.readAsLines(Encoding.UTF_8); 1059 var readAsLinesFuture = f.readAsLines(encoding: Encoding.UTF_8);
1060 readAsLinesFuture.then((lines) => Expect.fail("no lines expected")) 1060 readAsLinesFuture.then((lines) => Expect.fail("no lines expected"))
1061 .catchError((e) { 1061 .catchError((e) {
1062 port.toSendPort().send(1); 1062 port.toSendPort().send(1);
1063 }); 1063 });
1064 }); 1064 });
1065 }); 1065 });
1066 } 1066 }
1067 1067
1068 static void testLastModified() { 1068 static void testLastModified() {
1069 var port = new ReceivePort(); 1069 var port = new ReceivePort();
1070 new File(new Options().executable).lastModified().then((modified) { 1070 new File(new Options().executable).lastModified().then((modified) {
1071 Expect.isTrue(modified is DateTime); 1071 Expect.isTrue(modified is DateTime);
1072 Expect.isTrue(modified < new DateTime.now()); 1072 Expect.isTrue(modified < new DateTime.now());
1073 port.close(); 1073 port.close();
1074 }); 1074 });
1075 } 1075 }
1076 1076
1077 static void testLastModifiedSync() { 1077 static void testLastModifiedSync() {
1078 var modified = new File(new Options().executable).lastModifiedSync(); 1078 var modified = new File(new Options().executable).lastModifiedSync();
1079 Expect.isTrue(modified is DateTime); 1079 Expect.isTrue(modified is DateTime);
1080 Expect.isTrue(modified < new DateTime.now()); 1080 Expect.isTrue(modified < new DateTime.now());
1081 } 1081 }
1082 1082
1083 // Test that opens the same file for writing then for appending to test 1083 // Test that opens the same file for writing then for appending to test
1084 // that the file is not truncated when opened for appending. 1084 // that the file is not truncated when opened for appending.
1085 static void testAppend() { 1085 static void testAppend() {
1086 var file = new File('${tempDirectory.path}/out_append'); 1086 var file = new File('${tempDirectory.path}/out_append');
1087 file.open(FileMode.WRITE).then((openedFile) { 1087 file.open(mode: FileMode.WRITE).then((openedFile) {
1088 openedFile.writeString("asdf").then((ignore) { 1088 openedFile.writeString("asdf").then((ignore) {
1089 openedFile.close().then((ignore) { 1089 openedFile.close().then((ignore) {
1090 file.open(FileMode.APPEND).then((openedFile) { 1090 file.open(mode: FileMode.APPEND).then((openedFile) {
1091 openedFile.length().then((length) { 1091 openedFile.length().then((length) {
1092 Expect.equals(4, length); 1092 Expect.equals(4, length);
1093 openedFile.writeString("asdf").then((ignore) { 1093 openedFile.writeString("asdf").then((ignore) {
1094 openedFile.length().then((length) { 1094 openedFile.length().then((length) {
1095 Expect.equals(8, length); 1095 Expect.equals(8, length);
1096 openedFile.close().then((ignore) { 1096 openedFile.close().then((ignore) {
1097 file.delete().then((ignore) { 1097 file.delete().then((ignore) {
1098 file.exists().then((exists) { 1098 file.exists().then((exists) {
1099 Expect.isFalse(exists); 1099 Expect.isFalse(exists);
1100 asyncTestDone("testAppend"); 1100 asyncTestDone("testAppend");
1101 }); 1101 });
1102 }); 1102 });
1103 }); 1103 });
1104 }); 1104 });
1105 }); 1105 });
1106 }); 1106 });
1107 }); 1107 });
1108 }); 1108 });
1109 }); 1109 });
1110 }); 1110 });
1111 asyncTestStarted(); 1111 asyncTestStarted();
1112 } 1112 }
1113 1113
1114 static void testAppendSync() { 1114 static void testAppendSync() {
1115 var file = new File('${tempDirectory.path}/out_append_sync'); 1115 var file = new File('${tempDirectory.path}/out_append_sync');
1116 var openedFile = file.openSync(FileMode.WRITE); 1116 var openedFile = file.openSync(mode: FileMode.WRITE);
1117 openedFile.writeStringSync("asdf"); 1117 openedFile.writeStringSync("asdf");
1118 Expect.equals(4, openedFile.lengthSync()); 1118 Expect.equals(4, openedFile.lengthSync());
1119 openedFile.closeSync(); 1119 openedFile.closeSync();
1120 openedFile = file.openSync(FileMode.WRITE); 1120 openedFile = file.openSync(mode: FileMode.WRITE);
1121 openedFile.setPositionSync(4); 1121 openedFile.setPositionSync(4);
1122 openedFile.writeStringSync("asdf"); 1122 openedFile.writeStringSync("asdf");
1123 Expect.equals(8, openedFile.lengthSync()); 1123 Expect.equals(8, openedFile.lengthSync());
1124 openedFile.closeSync(); 1124 openedFile.closeSync();
1125 file.deleteSync(); 1125 file.deleteSync();
1126 Expect.isFalse(file.existsSync()); 1126 Expect.isFalse(file.existsSync());
1127 } 1127 }
1128 1128
1129 static void testWriteStringUtf8() { 1129 static void testWriteStringUtf8() {
1130 var file = new File('${tempDirectory.path}/out_write_string'); 1130 var file = new File('${tempDirectory.path}/out_write_string');
1131 var string = new String.fromCharCodes([0x192]); 1131 var string = new String.fromCharCodes([0x192]);
1132 file.open(FileMode.WRITE).then((openedFile) { 1132 file.open(mode: FileMode.WRITE).then((openedFile) {
1133 openedFile.writeString(string).then((_) { 1133 openedFile.writeString(string).then((_) {
1134 openedFile.length().then((l) { 1134 openedFile.length().then((l) {
1135 Expect.equals(2, l); 1135 Expect.equals(2, l);
1136 openedFile.close().then((_) { 1136 openedFile.close().then((_) {
1137 file.open(FileMode.APPEND).then((openedFile) { 1137 file.open(mode: FileMode.APPEND).then((openedFile) {
1138 openedFile.setPosition(2).then((_) { 1138 openedFile.setPosition(2).then((_) {
1139 openedFile.writeString(string).then((_) { 1139 openedFile.writeString(string).then((_) {
1140 openedFile.length().then((l) { 1140 openedFile.length().then((l) {
1141 Expect.equals(4, l); 1141 Expect.equals(4, l);
1142 openedFile.close().then((_) { 1142 openedFile.close().then((_) {
1143 file.readAsString().then((readBack) { 1143 file.readAsString().then((readBack) {
1144 Expect.stringEquals(readBack, '$string$string'); 1144 Expect.stringEquals(readBack, '$string$string');
1145 file.delete().then((_) { 1145 file.delete().then((_) {
1146 file.exists().then((e) { 1146 file.exists().then((e) {
1147 Expect.isFalse(e); 1147 Expect.isFalse(e);
1148 asyncTestDone("testWriteStringUtf8"); 1148 asyncTestDone("testWriteStringUtf8");
1149 }); 1149 });
1150 }); 1150 });
1151 }); 1151 });
1152 }); 1152 });
1153 }); 1153 });
1154 }); 1154 });
1155 }); 1155 });
1156 }); 1156 });
1157 }); 1157 });
1158 }); 1158 });
1159 }); 1159 });
1160 }); 1160 });
1161 asyncTestStarted(); 1161 asyncTestStarted();
1162 } 1162 }
1163 1163
1164 static void testWriteStringUtf8Sync() { 1164 static void testWriteStringUtf8Sync() {
1165 var file = new File('${tempDirectory.path}/out_write_string_sync'); 1165 var file = new File('${tempDirectory.path}/out_write_string_sync');
1166 var string = new String.fromCharCodes([0x192]); 1166 var string = new String.fromCharCodes([0x192]);
1167 var openedFile = file.openSync(FileMode.WRITE); 1167 var openedFile = file.openSync(mode: FileMode.WRITE);
1168 openedFile.writeStringSync(string); 1168 openedFile.writeStringSync(string);
1169 Expect.equals(2, openedFile.lengthSync()); 1169 Expect.equals(2, openedFile.lengthSync());
1170 openedFile.closeSync(); 1170 openedFile.closeSync();
1171 openedFile = file.openSync(FileMode.APPEND); 1171 openedFile = file.openSync(mode: FileMode.APPEND);
1172 openedFile.setPositionSync(2); 1172 openedFile.setPositionSync(2);
1173 openedFile.writeStringSync(string); 1173 openedFile.writeStringSync(string);
1174 Expect.equals(4, openedFile.lengthSync()); 1174 Expect.equals(4, openedFile.lengthSync());
1175 openedFile.closeSync(); 1175 openedFile.closeSync();
1176 var readBack = file.readAsStringSync(); 1176 var readBack = file.readAsStringSync();
1177 Expect.stringEquals(readBack, '$string$string'); 1177 Expect.stringEquals(readBack, '$string$string');
1178 file.deleteSync(); 1178 file.deleteSync();
1179 Expect.isFalse(file.existsSync()); 1179 Expect.isFalse(file.existsSync());
1180 } 1180 }
1181 1181
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1236 testDirectorySync(); 1236 testDirectorySync();
1237 testWriteStringUtf8(); 1237 testWriteStringUtf8();
1238 testWriteStringUtf8Sync(); 1238 testWriteStringUtf8Sync();
1239 }); 1239 });
1240 } 1240 }
1241 } 1241 }
1242 1242
1243 main() { 1243 main() {
1244 FileTest.testMain(); 1244 FileTest.testMain();
1245 } 1245 }
OLDNEW
« no previous file with comments | « tests/standalone/io/file_invalid_arguments_test.dart ('k') | tests/standalone/io/file_write_as_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698