| 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 | 8 |
| 9 class MyListOfOneElement implements List { | 9 class MyListOfOneElement implements List { |
| 10 int _value; | 10 int _value; |
| (...skipping 862 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 873 static void testOpenDirectoryAsFileSync() { | 873 static void testOpenDirectoryAsFileSync() { |
| 874 var f = new File('.'); | 874 var f = new File('.'); |
| 875 try { | 875 try { |
| 876 f.openSync(); | 876 f.openSync(); |
| 877 Expect.fail("Expected exception opening directory as file"); | 877 Expect.fail("Expected exception opening directory as file"); |
| 878 } catch (var e) { | 878 } catch (var e) { |
| 879 Expect.isTrue(e is FileIOException); | 879 Expect.isTrue(e is FileIOException); |
| 880 } | 880 } |
| 881 } | 881 } |
| 882 | 882 |
| 883 static void testReadAsBytes() { |
| 884 var port = new ReceivePort.singleShot(); |
| 885 port.receive((result, replyTo) { |
| 886 Expect.equals(42, result); |
| 887 }); |
| 888 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 889 var f = new File(name); |
| 890 f.readAsBytes(); |
| 891 f.readAsBytesHandler = (bytes) { |
| 892 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
| 893 port.toSendPort().send(bytes.length); |
| 894 }; |
| 895 f.errorHandler = (e) { |
| 896 Expect.fail("No errors expected: $e"); |
| 897 }; |
| 898 } |
| 899 |
| 900 static void testReadAsBytesSync() { |
| 901 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 902 var bytes = new File(name).readAsBytesSync(); |
| 903 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
| 904 Expect.equals(bytes.length, 42); |
| 905 } |
| 906 |
| 907 static void testReadAsText() { |
| 908 var port = new ReceivePort.singleShot(); |
| 909 port.receive((result, replyTo) { |
| 910 Expect.equals(1, result); |
| 911 }); |
| 912 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 913 var f = new File(name); |
| 914 f.readAsText('UTF-8'); |
| 915 f.readAsTextHandler = (text) { |
| 916 Expect.isTrue(text.endsWith("42 bytes.")); |
| 917 Expect.equals(42, text.length); |
| 918 var name = getFilename("tests/standalone/src/read_as_text.dat"); |
| 919 var f = new File(name); |
| 920 f.errorHandler = (e) => Expect.fail("No errors expected"); |
| 921 f.readAsText('UTF-8'); |
| 922 f.readAsTextHandler = (text) { |
| 923 Expect.equals(6, text.length); |
| 924 var expected = [955, 120, 46, 32, 120, 10]; |
| 925 Expect.listEquals(expected, text.charCodes()); |
| 926 f.readAsText('ISO-8859-1'); |
| 927 f.readAsTextHandler = (text) { |
| 928 Expect.equals(7, text.length); |
| 929 var expected = [206, 187, 120, 46, 32, 120, 10]; |
| 930 Expect.listEquals(expected, text.charCodes()); |
| 931 f.readAsText('ASCII'); |
| 932 f.errorHandler = (e) { |
| 933 port.toSendPort().send(1); |
| 934 }; |
| 935 f.readAsTextHandler = (text) { |
| 936 Expect.fail("Non-ascii char should cause error"); |
| 937 }; |
| 938 }; |
| 939 }; |
| 940 }; |
| 941 f.errorHandler = (e) { |
| 942 Expect.fail("No errors expected: $e"); |
| 943 }; |
| 944 } |
| 945 |
| 946 static void testReadAsTextSync() { |
| 947 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 948 var text = new File(name).readAsTextSync(); |
| 949 Expect.isTrue(text.endsWith("42 bytes.")); |
| 950 Expect.equals(42, text.length); |
| 951 name = getFilename("tests/standalone/src/read_as_text.dat"); |
| 952 text = new File(name).readAsTextSync(); |
| 953 Expect.equals(6, text.length); |
| 954 var expected = [955, 120, 46, 32, 120, 10]; |
| 955 Expect.listEquals(expected, text.charCodes()); |
| 956 Expect.throws(() { new File(name).readAsTextSync("ASCII"); }); |
| 957 text = new File(name).readAsTextSync("ISO-8859-1"); |
| 958 expected = [206, 187, 120, 46, 32, 120, 10]; |
| 959 Expect.equals(7, text.length); |
| 960 Expect.listEquals(expected, text.charCodes()); |
| 961 } |
| 962 |
| 963 static void testReadAsLines() { |
| 964 var port = new ReceivePort.singleShot(); |
| 965 port.receive((result, replyTo) { |
| 966 Expect.equals(42, result); |
| 967 }); |
| 968 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 969 var f = new File(name); |
| 970 f.readAsLines('UTF-8'); |
| 971 f.readAsLinesHandler = (lines) { |
| 972 Expect.equals(1, lines.length); |
| 973 var line = lines[0]; |
| 974 Expect.isTrue(line.endsWith("42 bytes.")); |
| 975 port.toSendPort().send(line.length); |
| 976 }; |
| 977 f.errorHandler = (e) { |
| 978 Expect.fail("No errors expected: $e"); |
| 979 }; |
| 980 } |
| 981 |
| 982 static void testReadAsLinesSync() { |
| 983 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 984 var lines = new File(name).readAsLinesSync(); |
| 985 Expect.equals(1, lines.length); |
| 986 var line = lines[0]; |
| 987 Expect.isTrue(line.endsWith("42 bytes.")); |
| 988 Expect.equals(42, line.length); |
| 989 name = getFilename("tests/standalone/src/readline_test1.dat"); |
| 990 lines = new File(name).readAsLinesSync(); |
| 991 Expect.equals(10, lines.length); |
| 992 } |
| 993 |
| 994 static void testReadAsErrors() { |
| 995 var f = new File('.'); |
| 996 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); |
| 997 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); |
| 998 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); |
| 999 // TODO(ager): Add tests of errors in readAsBytes when input |
| 1000 // streams are truely async. |
| 1001 } |
| 883 | 1002 |
| 884 // Test that opens the same file for writing then for appending to test | 1003 // Test that opens the same file for writing then for appending to test |
| 885 // that the file is not truncated when opened for appending. | 1004 // that the file is not truncated when opened for appending. |
| 886 static void testAppend() { | 1005 static void testAppend() { |
| 887 var file = new File('${tempDirectory.path}/out_append'); | 1006 var file = new File('${tempDirectory.path}/out_append'); |
| 888 file.openHandler = (openedFile) { | 1007 file.openHandler = (openedFile) { |
| 889 openedFile.noPendingWriteHandler = () { | 1008 openedFile.noPendingWriteHandler = () { |
| 890 openedFile.closeHandler = () { | 1009 openedFile.closeHandler = () { |
| 891 file.openHandler = (openedFile) { | 1010 file.openHandler = (openedFile) { |
| 892 openedFile.lengthHandler = (length) { | 1011 openedFile.lengthHandler = (length) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 950 testRead(); | 1069 testRead(); |
| 951 testReadSync(); | 1070 testReadSync(); |
| 952 testReadStream(); | 1071 testReadStream(); |
| 953 testLength(); | 1072 testLength(); |
| 954 testLengthSync(); | 1073 testLengthSync(); |
| 955 testPosition(); | 1074 testPosition(); |
| 956 testPositionSync(); | 1075 testPositionSync(); |
| 957 testMixedSyncAndAsync(); | 1076 testMixedSyncAndAsync(); |
| 958 testOpenDirectoryAsFile(); | 1077 testOpenDirectoryAsFile(); |
| 959 testOpenDirectoryAsFileSync(); | 1078 testOpenDirectoryAsFileSync(); |
| 1079 testReadAsBytes(); |
| 1080 testReadAsBytesSync(); |
| 1081 testReadAsText(); |
| 1082 testReadAsTextSync(); |
| 1083 testReadAsLines(); |
| 1084 testReadAsLinesSync(); |
| 1085 testReadAsErrors(); |
| 960 | 1086 |
| 961 createTempDirectory(() { | 1087 createTempDirectory(() { |
| 962 testReadWrite(); | 1088 testReadWrite(); |
| 963 testReadWriteSync(); | 1089 testReadWriteSync(); |
| 964 testReadWriteStream(); | 1090 testReadWriteStream(); |
| 965 testReadEmptyFileSync(); | 1091 testReadEmptyFileSync(); |
| 966 testReadEmptyFile(); | 1092 testReadEmptyFile(); |
| 967 testTruncate(); | 1093 testTruncate(); |
| 968 testTruncateSync(); | 1094 testTruncateSync(); |
| 969 testCloseException(); | 1095 testCloseException(); |
| 970 testCloseExceptionStream(); | 1096 testCloseExceptionStream(); |
| 971 testBufferOutOfBoundsException(); | 1097 testBufferOutOfBoundsException(); |
| 972 testAppend(); | 1098 testAppend(); |
| 973 testAppendSync(); | 1099 testAppendSync(); |
| 974 testWriteAppend(); | 1100 testWriteAppend(); |
| 975 testOutputStreamWriteAppend(); | 1101 testOutputStreamWriteAppend(); |
| 976 testWriteVariousLists(); | 1102 testWriteVariousLists(); |
| 977 testDirectory(); | 1103 testDirectory(); |
| 978 testDirectorySync(); | 1104 testDirectorySync(); |
| 979 }); | 1105 }); |
| 980 } | 1106 } |
| 981 } | 1107 } |
| 982 | 1108 |
| 983 main() { | 1109 main() { |
| 984 FileTest.testMain(); | 1110 FileTest.testMain(); |
| 985 } | 1111 } |
| OLD | NEW |