| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Dart test program for testing file I/O. | 5 // Dart test program for testing file I/O. |
| 6 | 6 |
| 7 #import("dart:io"); | 7 #import("dart:io"); |
| 8 #import("dart:isolate"); | 8 #import("dart:isolate"); |
| 9 | 9 |
| 10 class MyListOfOneElement implements List { | 10 class MyListOfOneElement implements List { |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 asyncTestDone("testOutputStreamWriteAppend"); | 325 asyncTestDone("testOutputStreamWriteAppend"); |
| 326 }); | 326 }); |
| 327 }); | 327 }); |
| 328 }); | 328 }); |
| 329 }); | 329 }); |
| 330 }; | 330 }; |
| 331 }; | 331 }; |
| 332 asyncTestStarted(); | 332 asyncTestStarted(); |
| 333 } | 333 } |
| 334 | 334 |
| 335 // Test for file read and write functionality. |
| 336 static void testOutputStreamWriteString() { |
| 337 String content = "foobar"; |
| 338 String filename = tempDirectory.path + "/outstream_write_string"; |
| 339 File file = new File(filename); |
| 340 file.createSync(); |
| 341 List<int> buffer = content.charCodes(); |
| 342 OutputStream outStream = file.openOutputStream(); |
| 343 outStream.writeString("abcdABCD"); |
| 344 outStream.writeString("abcdABCD", Encoding.UTF_8); |
| 345 outStream.writeString("abcdABCD", Encoding.ISO_8859_1); |
| 346 outStream.writeString("abcdABCD", Encoding.ASCII); |
| 347 outStream.writeString("æøå", Encoding.UTF_8); |
| 348 outStream.onNoPendingWrites = () { |
| 349 outStream.close(); |
| 350 outStream.onClosed = () { |
| 351 RandomAccessFile raf = file.openSync(); |
| 352 Expect.equals(38, raf.lengthSync()); |
| 353 }; |
| 354 }; |
| 355 asyncTestStarted(); |
| 356 } |
| 357 |
| 335 | 358 |
| 336 static void testReadWriteSync() { | 359 static void testReadWriteSync() { |
| 337 // Read a file. | 360 // Read a file. |
| 338 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 361 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 339 RandomAccessFile file = (new File(inFilename)).openSync(); | 362 RandomAccessFile file = (new File(inFilename)).openSync(); |
| 340 List<int> buffer1 = new List<int>(42); | 363 List<int> buffer1 = new List<int>(42); |
| 341 int bytes_read = 0; | 364 int bytes_read = 0; |
| 342 int bytes_written = 0; | 365 int bytes_written = 0; |
| 343 bytes_read = file.readListSync(buffer1, 0, 42); | 366 bytes_read = file.readListSync(buffer1, 0, 42); |
| 344 Expect.equals(42, bytes_read); | 367 Expect.equals(42, bytes_read); |
| (...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 Expect.equals(bytes.length, 42); | 901 Expect.equals(bytes.length, 42); |
| 879 } | 902 } |
| 880 | 903 |
| 881 static void testReadAsText() { | 904 static void testReadAsText() { |
| 882 var port = new ReceivePort.singleShot(); | 905 var port = new ReceivePort.singleShot(); |
| 883 port.receive((result, replyTo) { | 906 port.receive((result, replyTo) { |
| 884 Expect.equals(1, result); | 907 Expect.equals(1, result); |
| 885 }); | 908 }); |
| 886 var name = getFilename("tests/vm/data/fixed_length_file"); | 909 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 887 var f = new File(name); | 910 var f = new File(name); |
| 888 f.readAsText('UTF-8', (text) { | 911 f.readAsText(Encoding.UTF_8, (text) { |
| 889 Expect.isTrue(text.endsWith("42 bytes.")); | 912 Expect.isTrue(text.endsWith("42 bytes.")); |
| 890 Expect.equals(42, text.length); | 913 Expect.equals(42, text.length); |
| 891 var name = getDataFilename("tests/standalone/src/io/read_as_text.dat"); | 914 var name = getDataFilename("tests/standalone/src/io/read_as_text.dat"); |
| 892 var f = new File(name); | 915 var f = new File(name); |
| 893 f.onError = (e) => Expect.fail("No errors expected"); | 916 f.onError = (e) => Expect.fail("No errors expected"); |
| 894 f.readAsText('UTF-8', (text) { | 917 f.readAsText(Encoding.UTF_8, (text) { |
| 895 Expect.equals(6, text.length); | 918 Expect.equals(6, text.length); |
| 896 var expected = [955, 120, 46, 32, 120, 10]; | 919 var expected = [955, 120, 46, 32, 120, 10]; |
| 897 Expect.listEquals(expected, text.charCodes()); | 920 Expect.listEquals(expected, text.charCodes()); |
| 898 f.readAsText('ISO-8859-1', (text) { | 921 f.readAsText(Encoding.ISO_8859_1, (text) { |
| 899 Expect.equals(7, text.length); | 922 Expect.equals(7, text.length); |
| 900 var expected = [206, 187, 120, 46, 32, 120, 10]; | 923 var expected = [206, 187, 120, 46, 32, 120, 10]; |
| 901 Expect.listEquals(expected, text.charCodes()); | 924 Expect.listEquals(expected, text.charCodes()); |
| 902 f.onError = (e) { | 925 f.onError = (e) { |
| 903 port.toSendPort().send(1); | 926 port.toSendPort().send(1); |
| 904 }; | 927 }; |
| 905 f.readAsText('ASCII', (text) { | 928 f.readAsText(Encoding.ASCII, (text) { |
| 906 Expect.fail("Non-ascii char should cause error"); | 929 Expect.fail("Non-ascii char should cause error"); |
| 907 }); | 930 }); |
| 908 }); | 931 }); |
| 909 }); | 932 }); |
| 910 }); | 933 }); |
| 911 f.onError = (e) { | 934 f.onError = (e) { |
| 912 Expect.fail("No errors expected: $e"); | 935 Expect.fail("No errors expected: $e"); |
| 913 }; | 936 }; |
| 914 } | 937 } |
| 915 | 938 |
| 916 static void testReadAsTextSync() { | 939 static void testReadAsTextSync() { |
| 917 var name = getFilename("tests/vm/data/fixed_length_file"); | 940 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 918 var text = new File(name).readAsTextSync(); | 941 var text = new File(name).readAsTextSync(); |
| 919 Expect.isTrue(text.endsWith("42 bytes.")); | 942 Expect.isTrue(text.endsWith("42 bytes.")); |
| 920 Expect.equals(42, text.length); | 943 Expect.equals(42, text.length); |
| 921 name = getDataFilename("tests/standalone/src/io/read_as_text.dat"); | 944 name = getDataFilename("tests/standalone/src/io/read_as_text.dat"); |
| 922 text = new File(name).readAsTextSync(); | 945 text = new File(name).readAsTextSync(); |
| 923 Expect.equals(6, text.length); | 946 Expect.equals(6, text.length); |
| 924 var expected = [955, 120, 46, 32, 120, 10]; | 947 var expected = [955, 120, 46, 32, 120, 10]; |
| 925 Expect.listEquals(expected, text.charCodes()); | 948 Expect.listEquals(expected, text.charCodes()); |
| 926 Expect.throws(() { new File(name).readAsTextSync("ASCII"); }); | 949 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); }); |
| 927 text = new File(name).readAsTextSync("ISO-8859-1"); | 950 text = new File(name).readAsTextSync(Encoding.ISO_8859_1); |
| 928 expected = [206, 187, 120, 46, 32, 120, 10]; | 951 expected = [206, 187, 120, 46, 32, 120, 10]; |
| 929 Expect.equals(7, text.length); | 952 Expect.equals(7, text.length); |
| 930 Expect.listEquals(expected, text.charCodes()); | 953 Expect.listEquals(expected, text.charCodes()); |
| 931 } | 954 } |
| 932 | 955 |
| 933 static void testReadAsLines() { | 956 static void testReadAsLines() { |
| 934 var port = new ReceivePort.singleShot(); | 957 var port = new ReceivePort.singleShot(); |
| 935 port.receive((result, replyTo) { | 958 port.receive((result, replyTo) { |
| 936 Expect.equals(42, result); | 959 Expect.equals(42, result); |
| 937 }); | 960 }); |
| 938 var name = getFilename("tests/vm/data/fixed_length_file"); | 961 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 939 var f = new File(name); | 962 var f = new File(name); |
| 940 f.readAsLines('UTF-8', (lines) { | 963 f.readAsLines(Encoding.UTF_8, (lines) { |
| 941 Expect.equals(1, lines.length); | 964 Expect.equals(1, lines.length); |
| 942 var line = lines[0]; | 965 var line = lines[0]; |
| 943 Expect.isTrue(line.endsWith("42 bytes.")); | 966 Expect.isTrue(line.endsWith("42 bytes.")); |
| 944 port.toSendPort().send(line.length); | 967 port.toSendPort().send(line.length); |
| 945 }); | 968 }); |
| 946 f.onError = (e) { | 969 f.onError = (e) { |
| 947 Expect.fail("No errors expected: $e"); | 970 Expect.fail("No errors expected: $e"); |
| 948 }; | 971 }; |
| 949 } | 972 } |
| 950 | 973 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 965 var port = new ReceivePort.singleShot(); | 988 var port = new ReceivePort.singleShot(); |
| 966 port.receive((message, _) { | 989 port.receive((message, _) { |
| 967 Expect.equals(1, message); | 990 Expect.equals(1, message); |
| 968 }); | 991 }); |
| 969 var f = new File('.'); | 992 var f = new File('.'); |
| 970 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); | 993 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); |
| 971 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); | 994 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); |
| 972 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); | 995 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); |
| 973 f.readAsBytes((bytes) => Expect.fail("no bytes expected")); | 996 f.readAsBytes((bytes) => Expect.fail("no bytes expected")); |
| 974 f.onError = (e) { | 997 f.onError = (e) { |
| 975 f.readAsText('UTF-8', (text) => Expect.fail("no text expected")); | 998 f.readAsText(Encoding.UTF_8, (text) => Expect.fail("no text expected")); |
| 976 f.onError = (e) { | 999 f.onError = (e) { |
| 977 f.readAsLines('UTF-8', (lines) => Expect.fail("no lines expected")); | 1000 f.readAsLines(Encoding.UTF_8, |
| 1001 (lines) => Expect.fail("no lines expected")); |
| 978 f.onError = (e) { | 1002 f.onError = (e) { |
| 979 port.toSendPort().send(1); | 1003 port.toSendPort().send(1); |
| 980 }; | 1004 }; |
| 981 }; | 1005 }; |
| 982 }; | 1006 }; |
| 983 } | 1007 } |
| 984 | 1008 |
| 985 // Test that opens the same file for writing then for appending to test | 1009 // Test that opens the same file for writing then for appending to test |
| 986 // that the file is not truncated when opened for appending. | 1010 // that the file is not truncated when opened for appending. |
| 987 static void testAppend() { | 1011 static void testAppend() { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1066 testReadEmptyFile(); | 1090 testReadEmptyFile(); |
| 1067 testTruncate(); | 1091 testTruncate(); |
| 1068 testTruncateSync(); | 1092 testTruncateSync(); |
| 1069 testCloseException(); | 1093 testCloseException(); |
| 1070 testCloseExceptionStream(); | 1094 testCloseExceptionStream(); |
| 1071 testBufferOutOfBoundsException(); | 1095 testBufferOutOfBoundsException(); |
| 1072 testAppend(); | 1096 testAppend(); |
| 1073 testAppendSync(); | 1097 testAppendSync(); |
| 1074 testWriteAppend(); | 1098 testWriteAppend(); |
| 1075 testOutputStreamWriteAppend(); | 1099 testOutputStreamWriteAppend(); |
| 1100 testOutputStreamWriteString(); |
| 1076 testWriteVariousLists(); | 1101 testWriteVariousLists(); |
| 1077 testDirectory(); | 1102 testDirectory(); |
| 1078 testDirectorySync(); | 1103 testDirectorySync(); |
| 1079 }); | 1104 }); |
| 1080 } | 1105 } |
| 1081 } | 1106 } |
| 1082 | 1107 |
| 1083 main() { | 1108 main() { |
| 1084 FileTest.testMain(); | 1109 FileTest.testMain(); |
| 1085 } | 1110 } |
| OLD | NEW |