| OLD | NEW |
| 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 // OtherResources=fixed_length_file |
| 8 // OtherResources=read_as_text.dat |
| 9 // OtherResources=readline_test1.dat |
| 10 |
| 7 import 'dart:async'; | 11 import 'dart:async'; |
| 8 import 'dart:convert'; | 12 import 'dart:convert'; |
| 9 import 'dart:collection'; | 13 import 'dart:collection'; |
| 10 import 'dart:io'; | 14 import 'dart:io'; |
| 11 | 15 |
| 12 import "package:async_helper/async_helper.dart"; | 16 import "package:async_helper/async_helper.dart"; |
| 13 import "package:expect/expect.dart"; | 17 import "package:expect/expect.dart"; |
| 14 import "package:path/path.dart"; | 18 import "package:path/path.dart"; |
| 15 | 19 |
| 16 class MyListOfOneElement | 20 class MyListOfOneElement |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 Expect.equals(116, buffer[11]); // represents 't' in the file. | 87 Expect.equals(116, buffer[11]); // represents 't' in the file. |
| 84 } | 88 } |
| 85 }); | 89 }); |
| 86 } | 90 } |
| 87 | 91 |
| 88 // Test for file read and write functionality. | 92 // Test for file read and write functionality. |
| 89 static void testReadWriteStream() { | 93 static void testReadWriteStream() { |
| 90 asyncTestStarted(); | 94 asyncTestStarted(); |
| 91 | 95 |
| 92 // Read a file. | 96 // Read a file. |
| 93 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 97 String inFilename = getFilename("fixed_length_file"); |
| 94 File file; | 98 File file; |
| 95 int bytesRead; | 99 int bytesRead; |
| 96 | 100 |
| 97 var file1 = new File(inFilename); | 101 var file1 = new File(inFilename); |
| 98 List<int> buffer = new List<int>(); | 102 List<int> buffer = new List<int>(); |
| 99 file1.openRead().listen( | 103 file1.openRead().listen( |
| 100 (d) { | 104 (d) { |
| 101 buffer.addAll(d); | 105 buffer.addAll(d); |
| 102 }, | 106 }, |
| 103 onDone: () { | 107 onDone: () { |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 271 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
| 268 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 272 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
| 269 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 273 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
| 270 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 274 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
| 271 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 275 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
| 272 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 276 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
| 273 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 277 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
| 274 Expect.equals(116, buffer[11]); // represents 't' in the file. | 278 Expect.equals(116, buffer[11]); // represents 't' in the file. |
| 275 raf.closeSync(); | 279 raf.closeSync(); |
| 276 | 280 |
| 277 filename = getFilename("tests/vm/data/fixed_length_file"); | 281 filename = getFilename("fixed_length_file"); |
| 278 File file = new File(filename); | 282 File file = new File(filename); |
| 279 int len = file.lengthSync(); | 283 int len = file.lengthSync(); |
| 280 int read(int length) { | 284 int read(int length) { |
| 281 var f = file.openSync(); | 285 var f = file.openSync(); |
| 282 int res = f.readSync(length).length; | 286 int res = f.readSync(length).length; |
| 283 f.closeSync(); | 287 f.closeSync(); |
| 284 return res; | 288 return res; |
| 285 } | 289 } |
| 286 Expect.equals(0, read(0)); | 290 Expect.equals(0, read(0)); |
| 287 Expect.equals(1, read(1)); | 291 Expect.equals(1, read(1)); |
| 288 Expect.equals(len - 1, read(len - 1)); | 292 Expect.equals(len - 1, read(len - 1)); |
| 289 Expect.equals(len, read(len)); | 293 Expect.equals(len, read(len)); |
| 290 Expect.equals(len, read(len + 1)); | 294 Expect.equals(len, read(len + 1)); |
| 291 Expect.equals(len, read(len * 2)); | 295 Expect.equals(len, read(len * 2)); |
| 292 Expect.equals(len, read(len * 10)); | 296 Expect.equals(len, read(len * 10)); |
| 293 } | 297 } |
| 294 | 298 |
| 295 // Test for file read and write functionality. | 299 // Test for file read and write functionality. |
| 296 static void testReadWrite() { | 300 static void testReadWrite() { |
| 297 asyncTestStarted(); | 301 asyncTestStarted(); |
| 298 // Read a file. | 302 // Read a file. |
| 299 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 303 String inFilename = getFilename("fixed_length_file"); |
| 300 final File file = new File(inFilename); | 304 final File file = new File(inFilename); |
| 301 file.open(mode: READ).then((openedFile) { | 305 file.open(mode: READ).then((openedFile) { |
| 302 List<int> buffer1 = new List<int>(42); | 306 List<int> buffer1 = new List<int>(42); |
| 303 openedFile.readInto(buffer1, 0, 42).then((bytes_read) { | 307 openedFile.readInto(buffer1, 0, 42).then((bytes_read) { |
| 304 Expect.equals(42, bytes_read); | 308 Expect.equals(42, bytes_read); |
| 305 openedFile.close().then((ignore) { | 309 openedFile.close().then((ignore) { |
| 306 // Write the contents of the file just read into another file. | 310 // Write the contents of the file just read into another file. |
| 307 String outFilename = tempDirectory.path + "/out_read_write"; | 311 String outFilename = tempDirectory.path + "/out_read_write"; |
| 308 final File file2 = new File(outFilename); | 312 final File file2 = new File(outFilename); |
| 309 file2.create().then((ignore) { | 313 file2.create().then((ignore) { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 Expect.equals(38, raf.lengthSync()); | 434 Expect.equals(38, raf.lengthSync()); |
| 431 raf.close().then((ignore) { | 435 raf.close().then((ignore) { |
| 432 asyncTestDone("testOutputStreamWriteString"); | 436 asyncTestDone("testOutputStreamWriteString"); |
| 433 }); | 437 }); |
| 434 }); | 438 }); |
| 435 } | 439 } |
| 436 | 440 |
| 437 | 441 |
| 438 static void testReadWriteSync() { | 442 static void testReadWriteSync() { |
| 439 // Read a file. | 443 // Read a file. |
| 440 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 444 String inFilename = getFilename("fixed_length_file"); |
| 441 RandomAccessFile file = (new File(inFilename)).openSync(); | 445 RandomAccessFile file = (new File(inFilename)).openSync(); |
| 442 List<int> buffer1 = new List<int>(42); | 446 List<int> buffer1 = new List<int>(42); |
| 443 int bytes_read = 0; | 447 int bytes_read = 0; |
| 444 int bytes_written = 0; | 448 int bytes_written = 0; |
| 445 bytes_read = file.readIntoSync(buffer1, 0, 42); | 449 bytes_read = file.readIntoSync(buffer1, 0, 42); |
| 446 Expect.equals(42, bytes_read); | 450 Expect.equals(42, bytes_read); |
| 447 file.closeSync(); | 451 file.closeSync(); |
| 448 // Write the contents of the file just read into another file. | 452 // Write the contents of the file just read into another file. |
| 449 String outFilename = tempDirectory.path + "/out_read_write_sync"; | 453 String outFilename = tempDirectory.path + "/out_read_write_sync"; |
| 450 File outFile = new File(outFilename); | 454 File outFile = new File(outFilename); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 468 for (int i = 0; i < buffer1.length; i++) { | 472 for (int i = 0; i < buffer1.length; i++) { |
| 469 Expect.equals(buffer1[i], buffer2[i]); | 473 Expect.equals(buffer1[i], buffer2[i]); |
| 470 } | 474 } |
| 471 // Delete the output file. | 475 // Delete the output file. |
| 472 outFile.deleteSync(); | 476 outFile.deleteSync(); |
| 473 Expect.isFalse(outFile.existsSync()); | 477 Expect.isFalse(outFile.existsSync()); |
| 474 } | 478 } |
| 475 | 479 |
| 476 static void testReadWriteNoArgsSync() { | 480 static void testReadWriteNoArgsSync() { |
| 477 // Read a file. | 481 // Read a file. |
| 478 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 482 String inFilename = getFilename("fixed_length_file"); |
| 479 RandomAccessFile file = (new File(inFilename)).openSync(); | 483 RandomAccessFile file = (new File(inFilename)).openSync(); |
| 480 List<int> buffer1 = new List<int>(42); | 484 List<int> buffer1 = new List<int>(42); |
| 481 int bytes_read = 0; | 485 int bytes_read = 0; |
| 482 int bytes_written = 0; | 486 int bytes_written = 0; |
| 483 bytes_read = file.readIntoSync(buffer1); | 487 bytes_read = file.readIntoSync(buffer1); |
| 484 Expect.equals(42, bytes_read); | 488 Expect.equals(42, bytes_read); |
| 485 file.closeSync(); | 489 file.closeSync(); |
| 486 // Write the contents of the file just read into another file. | 490 // Write the contents of the file just read into another file. |
| 487 String outFilename = tempDirectory.path + "/out_read_write_sync"; | 491 String outFilename = tempDirectory.path + "/out_read_write_sync"; |
| 488 File outFile = new File(outFilename); | 492 File outFile = new File(outFilename); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 Expect.equals("${tempDir}", file.parent.path); | 612 Expect.equals("${tempDir}", file.parent.path); |
| 609 file = new File("foo"); | 613 file = new File("foo"); |
| 610 Expect.equals(".", file.parent.path); | 614 Expect.equals(".", file.parent.path); |
| 611 file = new File("."); | 615 file = new File("."); |
| 612 Expect.equals(".", file.parent.path); | 616 Expect.equals(".", file.parent.path); |
| 613 } | 617 } |
| 614 | 618 |
| 615 // Test for file length functionality. | 619 // Test for file length functionality. |
| 616 static void testLength() { | 620 static void testLength() { |
| 617 asyncTestStarted(); | 621 asyncTestStarted(); |
| 618 String filename = getFilename("tests/vm/data/fixed_length_file"); | 622 String filename = getFilename("fixed_length_file"); |
| 619 File file = new File(filename); | 623 File file = new File(filename); |
| 620 RandomAccessFile openedFile = file.openSync(); | 624 RandomAccessFile openedFile = file.openSync(); |
| 621 openedFile.length().then((length) { | 625 openedFile.length().then((length) { |
| 622 Expect.equals(42, length); | 626 Expect.equals(42, length); |
| 623 openedFile.close().then((ignore) => asyncTestDone("testLength")); | 627 openedFile.close().then((ignore) => asyncTestDone("testLength")); |
| 624 }); | 628 }); |
| 625 file.length().then((length) { | 629 file.length().then((length) { |
| 626 Expect.equals(42, length); | 630 Expect.equals(42, length); |
| 627 }); | 631 }); |
| 628 } | 632 } |
| 629 | 633 |
| 630 static void testLengthSync() { | 634 static void testLengthSync() { |
| 631 String filename = getFilename("tests/vm/data/fixed_length_file"); | 635 String filename = getFilename("fixed_length_file"); |
| 632 File file = new File(filename); | 636 File file = new File(filename); |
| 633 RandomAccessFile openedFile = file.openSync(); | 637 RandomAccessFile openedFile = file.openSync(); |
| 634 Expect.equals(42, file.lengthSync()); | 638 Expect.equals(42, file.lengthSync()); |
| 635 Expect.equals(42, openedFile.lengthSync()); | 639 Expect.equals(42, openedFile.lengthSync()); |
| 636 openedFile.closeSync(); | 640 openedFile.closeSync(); |
| 637 } | 641 } |
| 638 | 642 |
| 639 // Test for file position functionality. | 643 // Test for file position functionality. |
| 640 static void testPosition() { | 644 static void testPosition() { |
| 641 asyncTestStarted(); | 645 asyncTestStarted(); |
| 642 String filename = getFilename("tests/vm/data/fixed_length_file"); | 646 String filename = getFilename("fixed_length_file"); |
| 643 RandomAccessFile input = (new File(filename)).openSync(); | 647 RandomAccessFile input = (new File(filename)).openSync(); |
| 644 input.position().then((position) { | 648 input.position().then((position) { |
| 645 Expect.equals(0, position); | 649 Expect.equals(0, position); |
| 646 List<int> buffer = new List<int>(100); | 650 List<int> buffer = new List<int>(100); |
| 647 input.readInto(buffer, 0, 12).then((bytes_read) { | 651 input.readInto(buffer, 0, 12).then((bytes_read) { |
| 648 input.position().then((position) { | 652 input.position().then((position) { |
| 649 Expect.equals(12, position); | 653 Expect.equals(12, position); |
| 650 input.readInto(buffer, 12, 18).then((bytes_read) { | 654 input.readInto(buffer, 12, 18).then((bytes_read) { |
| 651 input.position().then((position) { | 655 input.position().then((position) { |
| 652 Expect.equals(18, position); | 656 Expect.equals(18, position); |
| 653 input.setPosition(8).then((ignore) { | 657 input.setPosition(8).then((ignore) { |
| 654 input.position().then((position) { | 658 input.position().then((position) { |
| 655 Expect.equals(8, position); | 659 Expect.equals(8, position); |
| 656 input.close().then((ignore) => asyncTestDone("testPosition")); | 660 input.close().then((ignore) => asyncTestDone("testPosition")); |
| 657 }); | 661 }); |
| 658 }); | 662 }); |
| 659 }); | 663 }); |
| 660 }); | 664 }); |
| 661 }); | 665 }); |
| 662 }); | 666 }); |
| 663 }); | 667 }); |
| 664 } | 668 } |
| 665 | 669 |
| 666 static void testPositionSync() { | 670 static void testPositionSync() { |
| 667 String filename = getFilename("tests/vm/data/fixed_length_file"); | 671 String filename = getFilename("fixed_length_file"); |
| 668 RandomAccessFile input = (new File(filename)).openSync(); | 672 RandomAccessFile input = (new File(filename)).openSync(); |
| 669 Expect.equals(0, input.positionSync()); | 673 Expect.equals(0, input.positionSync()); |
| 670 List<int> buffer = new List<int>(100); | 674 List<int> buffer = new List<int>(100); |
| 671 input.readIntoSync(buffer, 0, 12); | 675 input.readIntoSync(buffer, 0, 12); |
| 672 Expect.equals(12, input.positionSync()); | 676 Expect.equals(12, input.positionSync()); |
| 673 input.readIntoSync(buffer, 12, 18); | 677 input.readIntoSync(buffer, 12, 18); |
| 674 Expect.equals(18, input.positionSync()); | 678 Expect.equals(18, input.positionSync()); |
| 675 input.setPositionSync(8); | 679 input.setPositionSync(8); |
| 676 Expect.equals(8, input.positionSync()); | 680 Expect.equals(8, input.positionSync()); |
| 677 input.closeSync(); | 681 input.closeSync(); |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1049 try { | 1053 try { |
| 1050 f.openSync(); | 1054 f.openSync(); |
| 1051 Expect.fail("Expected exception opening directory as file"); | 1055 Expect.fail("Expected exception opening directory as file"); |
| 1052 } catch (e) { | 1056 } catch (e) { |
| 1053 Expect.isTrue(e is FileSystemException); | 1057 Expect.isTrue(e is FileSystemException); |
| 1054 } | 1058 } |
| 1055 } | 1059 } |
| 1056 | 1060 |
| 1057 static void testReadAsBytes() { | 1061 static void testReadAsBytes() { |
| 1058 asyncTestStarted(); | 1062 asyncTestStarted(); |
| 1059 var name = getFilename("tests/vm/data/fixed_length_file"); | 1063 var name = getFilename("fixed_length_file"); |
| 1060 var f = new File(name); | 1064 var f = new File(name); |
| 1061 f.readAsBytes().then((bytes) { | 1065 f.readAsBytes().then((bytes) { |
| 1062 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 1066 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
| 1063 Expect.equals(42, bytes.length); | 1067 Expect.equals(42, bytes.length); |
| 1064 asyncTestDone("testReadAsBytes"); | 1068 asyncTestDone("testReadAsBytes"); |
| 1065 }); | 1069 }); |
| 1066 } | 1070 } |
| 1067 | 1071 |
| 1068 static void testReadAsBytesEmptyFile() { | 1072 static void testReadAsBytesEmptyFile() { |
| 1069 asyncTestStarted(); | 1073 asyncTestStarted(); |
| 1070 var name = getFilename("tests/vm/data/empty_file"); | 1074 var name = getFilename("tests/vm/data/empty_file"); |
| 1071 var f = new File(name); | 1075 var f = new File(name); |
| 1072 f.readAsBytes().then((bytes) { | 1076 f.readAsBytes().then((bytes) { |
| 1073 Expect.equals(0, bytes.length); | 1077 Expect.equals(0, bytes.length); |
| 1074 asyncTestDone("testReadAsBytesEmptyFile"); | 1078 asyncTestDone("testReadAsBytesEmptyFile"); |
| 1075 }); | 1079 }); |
| 1076 } | 1080 } |
| 1077 | 1081 |
| 1078 static void testReadAsBytesSync() { | 1082 static void testReadAsBytesSync() { |
| 1079 var name = getFilename("tests/vm/data/fixed_length_file"); | 1083 var name = getFilename("fixed_length_file"); |
| 1080 var bytes = new File(name).readAsBytesSync(); | 1084 var bytes = new File(name).readAsBytesSync(); |
| 1081 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 1085 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
| 1082 Expect.equals(bytes.length, 42); | 1086 Expect.equals(bytes.length, 42); |
| 1083 } | 1087 } |
| 1084 | 1088 |
| 1085 static void testReadAsBytesSyncEmptyFile() { | 1089 static void testReadAsBytesSyncEmptyFile() { |
| 1086 var name = getFilename("tests/vm/data/empty_file"); | 1090 var name = getFilename("tests/vm/data/empty_file"); |
| 1087 var bytes = new File(name).readAsBytesSync(); | 1091 var bytes = new File(name).readAsBytesSync(); |
| 1088 Expect.equals(bytes.length, 0); | 1092 Expect.equals(bytes.length, 0); |
| 1089 } | 1093 } |
| 1090 | 1094 |
| 1091 static void testReadAsText() { | 1095 static void testReadAsText() { |
| 1092 asyncTestStarted(); | 1096 asyncTestStarted(); |
| 1093 var name = getFilename("tests/vm/data/fixed_length_file"); | 1097 var name = getFilename("fixed_length_file"); |
| 1094 var f = new File(name); | 1098 var f = new File(name); |
| 1095 f.readAsString(encoding: UTF8).then((text) { | 1099 f.readAsString(encoding: UTF8).then((text) { |
| 1096 Expect.isTrue(text.endsWith("42 bytes.")); | 1100 Expect.isTrue(text.endsWith("42 bytes.")); |
| 1097 Expect.equals(42, text.length); | 1101 Expect.equals(42, text.length); |
| 1098 var name = getFilename("tests/standalone/io/read_as_text.dat"); | 1102 var name = getFilename("read_as_text.dat"); |
| 1099 var f = new File(name); | 1103 var f = new File(name); |
| 1100 f.readAsString(encoding: UTF8).then((text) { | 1104 f.readAsString(encoding: UTF8).then((text) { |
| 1101 Expect.equals(6, text.length); | 1105 Expect.equals(6, text.length); |
| 1102 var expected = [955, 120, 46, 32, 120, 10]; | 1106 var expected = [955, 120, 46, 32, 120, 10]; |
| 1103 Expect.listEquals(expected, text.codeUnits); | 1107 Expect.listEquals(expected, text.codeUnits); |
| 1104 f.readAsString(encoding: LATIN1).then((text) { | 1108 f.readAsString(encoding: LATIN1).then((text) { |
| 1105 Expect.equals(7, text.length); | 1109 Expect.equals(7, text.length); |
| 1106 var expected = [206, 187, 120, 46, 32, 120, 10]; | 1110 var expected = [206, 187, 120, 46, 32, 120, 10]; |
| 1107 Expect.listEquals(expected, text.codeUnits); | 1111 Expect.listEquals(expected, text.codeUnits); |
| 1108 var readAsStringFuture = f.readAsString(encoding: ASCII); | 1112 var readAsStringFuture = f.readAsString(encoding: ASCII); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1121 var name = getFilename("tests/vm/data/empty_file"); | 1125 var name = getFilename("tests/vm/data/empty_file"); |
| 1122 var f = new File(name); | 1126 var f = new File(name); |
| 1123 f.readAsString(encoding: UTF8).then((text) { | 1127 f.readAsString(encoding: UTF8).then((text) { |
| 1124 Expect.equals(0, text.length); | 1128 Expect.equals(0, text.length); |
| 1125 asyncTestDone("testReadAsTextEmptyFile"); | 1129 asyncTestDone("testReadAsTextEmptyFile"); |
| 1126 return true; | 1130 return true; |
| 1127 }); | 1131 }); |
| 1128 } | 1132 } |
| 1129 | 1133 |
| 1130 static void testReadAsTextSync() { | 1134 static void testReadAsTextSync() { |
| 1131 var name = getFilename("tests/vm/data/fixed_length_file"); | 1135 var name = getFilename("fixed_length_file"); |
| 1132 var text = new File(name).readAsStringSync(); | 1136 var text = new File(name).readAsStringSync(); |
| 1133 Expect.isTrue(text.endsWith("42 bytes.")); | 1137 Expect.isTrue(text.endsWith("42 bytes.")); |
| 1134 Expect.equals(42, text.length); | 1138 Expect.equals(42, text.length); |
| 1135 name = getFilename("tests/standalone/io/read_as_text.dat"); | 1139 name = getFilename("read_as_text.dat"); |
| 1136 text = new File(name).readAsStringSync(); | 1140 text = new File(name).readAsStringSync(); |
| 1137 Expect.equals(6, text.length); | 1141 Expect.equals(6, text.length); |
| 1138 var expected = [955, 120, 46, 32, 120, 10]; | 1142 var expected = [955, 120, 46, 32, 120, 10]; |
| 1139 Expect.listEquals(expected, text.codeUnits); | 1143 Expect.listEquals(expected, text.codeUnits); |
| 1140 // First character is not ASCII. The default ASCII decoder will throw. | 1144 // First character is not ASCII. The default ASCII decoder will throw. |
| 1141 Expect.throws(() => new File(name).readAsStringSync(encoding: ASCII), | 1145 Expect.throws(() => new File(name).readAsStringSync(encoding: ASCII), |
| 1142 (e) => e is FileSystemException); | 1146 (e) => e is FileSystemException); |
| 1143 // We can use an ASCII decoder that inserts the replacement character. | 1147 // We can use an ASCII decoder that inserts the replacement character. |
| 1144 var lenientAscii = const AsciiCodec(allowInvalid: true); | 1148 var lenientAscii = const AsciiCodec(allowInvalid: true); |
| 1145 text = new File(name).readAsStringSync(encoding: lenientAscii); | 1149 text = new File(name).readAsStringSync(encoding: lenientAscii); |
| 1146 // Default replacement character is the Unicode replacement character. | 1150 // Default replacement character is the Unicode replacement character. |
| 1147 expected = [UNICODE_REPLACEMENT_CHARACTER_RUNE, | 1151 expected = [UNICODE_REPLACEMENT_CHARACTER_RUNE, |
| 1148 UNICODE_REPLACEMENT_CHARACTER_RUNE, | 1152 UNICODE_REPLACEMENT_CHARACTER_RUNE, |
| 1149 120, 46, 32, 120, 10]; | 1153 120, 46, 32, 120, 10]; |
| 1150 Expect.listEquals(expected, text.codeUnits); | 1154 Expect.listEquals(expected, text.codeUnits); |
| 1151 text = new File(name).readAsStringSync(encoding: LATIN1); | 1155 text = new File(name).readAsStringSync(encoding: LATIN1); |
| 1152 expected = [206, 187, 120, 46, 32, 120, 10]; | 1156 expected = [206, 187, 120, 46, 32, 120, 10]; |
| 1153 Expect.equals(7, text.length); | 1157 Expect.equals(7, text.length); |
| 1154 Expect.listEquals(expected, text.codeUnits); | 1158 Expect.listEquals(expected, text.codeUnits); |
| 1155 } | 1159 } |
| 1156 | 1160 |
| 1157 static void testReadAsTextSyncEmptyFile() { | 1161 static void testReadAsTextSyncEmptyFile() { |
| 1158 var name = getFilename("tests/vm/data/empty_file"); | 1162 var name = getFilename("tests/vm/data/empty_file"); |
| 1159 var text = new File(name).readAsStringSync(); | 1163 var text = new File(name).readAsStringSync(); |
| 1160 Expect.equals(0, text.length); | 1164 Expect.equals(0, text.length); |
| 1161 } | 1165 } |
| 1162 | 1166 |
| 1163 static void testReadAsLines() { | 1167 static void testReadAsLines() { |
| 1164 asyncTestStarted(); | 1168 asyncTestStarted(); |
| 1165 var name = getFilename("tests/vm/data/fixed_length_file"); | 1169 var name = getFilename("fixed_length_file"); |
| 1166 var f = new File(name); | 1170 var f = new File(name); |
| 1167 f.readAsLines(encoding: UTF8).then((lines) { | 1171 f.readAsLines(encoding: UTF8).then((lines) { |
| 1168 Expect.equals(1, lines.length); | 1172 Expect.equals(1, lines.length); |
| 1169 var line = lines[0]; | 1173 var line = lines[0]; |
| 1170 Expect.isTrue(line.endsWith("42 bytes.")); | 1174 Expect.isTrue(line.endsWith("42 bytes.")); |
| 1171 Expect.equals(42, line.length); | 1175 Expect.equals(42, line.length); |
| 1172 asyncTestDone("testReadAsLines"); | 1176 asyncTestDone("testReadAsLines"); |
| 1173 }); | 1177 }); |
| 1174 } | 1178 } |
| 1175 | 1179 |
| 1176 static void testReadAsLinesSync() { | 1180 static void testReadAsLinesSync() { |
| 1177 var name = getFilename("tests/vm/data/fixed_length_file"); | 1181 var name = getFilename("fixed_length_file"); |
| 1178 var lines = new File(name).readAsLinesSync(); | 1182 var lines = new File(name).readAsLinesSync(); |
| 1179 Expect.equals(1, lines.length); | 1183 Expect.equals(1, lines.length); |
| 1180 var line = lines[0]; | 1184 var line = lines[0]; |
| 1181 Expect.isTrue(line.endsWith("42 bytes.")); | 1185 Expect.isTrue(line.endsWith("42 bytes.")); |
| 1182 Expect.equals(42, line.length); | 1186 Expect.equals(42, line.length); |
| 1183 name = getFilename("tests/standalone/io/readline_test1.dat"); | 1187 name = getFilename("readline_test1.dat"); |
| 1184 lines = new File(name).readAsLinesSync(); | 1188 lines = new File(name).readAsLinesSync(); |
| 1185 Expect.equals(10, lines.length); | 1189 Expect.equals(10, lines.length); |
| 1186 } | 1190 } |
| 1187 | 1191 |
| 1188 | 1192 |
| 1189 static void testReadAsErrors() { | 1193 static void testReadAsErrors() { |
| 1190 asyncTestStarted(); | 1194 asyncTestStarted(); |
| 1191 var f = new File('.'); | 1195 var f = new File('.'); |
| 1192 Expect.throws(f.readAsBytesSync, (e) => e is FileSystemException); | 1196 Expect.throws(f.readAsBytesSync, (e) => e is FileSystemException); |
| 1193 Expect.throws(f.readAsStringSync, (e) => e is FileSystemException); | 1197 Expect.throws(f.readAsStringSync, (e) => e is FileSystemException); |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1394 var brokenLink = new Link(source); | 1398 var brokenLink = new Link(source); |
| 1395 brokenLink.createSync(dest); | 1399 brokenLink.createSync(dest); |
| 1396 Expect.throws(() => file.renameSync('xxx')); | 1400 Expect.throws(() => file.renameSync('xxx')); |
| 1397 brokenLink.deleteSync(); | 1401 brokenLink.deleteSync(); |
| 1398 } | 1402 } |
| 1399 } | 1403 } |
| 1400 | 1404 |
| 1401 // Helper method to be able to run the test from the runtime | 1405 // Helper method to be able to run the test from the runtime |
| 1402 // directory, or the top directory. | 1406 // directory, or the top directory. |
| 1403 static String getFilename(String path) { | 1407 static String getFilename(String path) { |
| 1404 var testPath = Platform.script.resolve('../../../$path'); | 1408 var testPath = Platform.script.resolve(path); |
| 1405 return new File.fromUri(testPath).existsSync() | 1409 return new File.fromUri(testPath).existsSync() |
| 1406 ? testPath.toFilePath() | 1410 ? testPath.toFilePath() |
| 1407 : Platform.script.resolve('../../../runtime/$path').toFilePath(); | 1411 : Uri.parse(Platform.executable).resolve('../../runtime/$path').toFilePa
th(); |
| 1408 } | 1412 } |
| 1409 | 1413 |
| 1410 // Main test entrypoint. | 1414 // Main test entrypoint. |
| 1411 static testMain() { | 1415 static testMain() { |
| 1412 asyncStart(); | 1416 asyncStart(); |
| 1413 | 1417 |
| 1414 testRead(); | 1418 testRead(); |
| 1415 testReadSync(); | 1419 testReadSync(); |
| 1416 testReadStream(); | 1420 testReadStream(); |
| 1417 testLengthSync(); | 1421 testLengthSync(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1467 testLastModified(); | 1471 testLastModified(); |
| 1468 testDoubleAsyncOperation(); | 1472 testDoubleAsyncOperation(); |
| 1469 asyncEnd(); | 1473 asyncEnd(); |
| 1470 }); | 1474 }); |
| 1471 } | 1475 } |
| 1472 } | 1476 } |
| 1473 | 1477 |
| 1474 main() { | 1478 main() { |
| 1475 FileTest.testMain(); | 1479 FileTest.testMain(); |
| 1476 } | 1480 } |
| OLD | NEW |