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

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

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

Powered by Google App Engine
This is Rietveld 408576698