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

Side by Side Diff: tests/standalone/src/FileTest.dart

Issue 9452014: Implement File.{readAsBytes, readAsText, readAsLines}. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 10 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
« runtime/bin/file_impl.dart ('K') | « runtime/bin/file_impl.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) 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
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() {
Søren Gjesse 2012/02/24 07:24:51 I know that the decoders are tested elsewhere, but
Mads Ager (google) 2012/02/24 13:11:17 Done.
908 var port = new ReceivePort.singleShot();
909 port.receive((result, replyTo) {
910 Expect.equals(42, 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 port.toSendPort().send(text.length);
918 };
919 f.errorHandler = (e) {
920 Expect.fail("No errors expected: $e");
921 };
922 }
923
924 static void testReadAsTextSync() {
925 var name = getFilename("tests/vm/data/fixed_length_file");
926 var text = new File(name).readAsTextSync();
927 Expect.isTrue(text.endsWith("42 bytes."));
928 Expect.equals(42, text.length);
929 }
930
931 static void testReadAsLines() {
Søren Gjesse 2012/02/24 07:24:51 Again I know that the line-splitting is tested els
Mads Ager (google) 2012/02/24 13:11:17 Done.
932 var port = new ReceivePort.singleShot();
933 port.receive((result, replyTo) {
934 Expect.equals(42, result);
935 });
936 var name = getFilename("tests/vm/data/fixed_length_file");
937 var f = new File(name);
938 f.readAsLines('UTF-8');
939 f.readAsLinesHandler = (lines) {
940 Expect.equals(1, lines.length);
941 var line = lines[0];
942 Expect.isTrue(line.endsWith("42 bytes."));
943 port.toSendPort().send(line.length);
944 };
945 f.errorHandler = (e) {
946 Expect.fail("No errors expected: $e");
947 };
948 }
949
950 static void testReadAsLinesSync() {
951 var name = getFilename("tests/vm/data/fixed_length_file");
952 var lines = new File(name).readAsLinesSync();
953 Expect.equals(1, lines.length);
954 var line = lines[0];
955 Expect.isTrue(line.endsWith("42 bytes."));
956 Expect.equals(42, line.length);
957 }
958
959 static void testReadAsErrors() {
960 var f = new File('.');
961 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException);
962 Expect.throws(f.readAsTextSync, (e) => e is FileIOException);
963 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException);
964 // TODO(ager): Add tests of errors in readAsBytes when input
965 // streams are truely async.
966 }
883 967
884 // Test that opens the same file for writing then for appending to test 968 // Test that opens the same file for writing then for appending to test
885 // that the file is not truncated when opened for appending. 969 // that the file is not truncated when opened for appending.
886 static void testAppend() { 970 static void testAppend() {
887 var file = new File('${tempDirectory.path}/out_append'); 971 var file = new File('${tempDirectory.path}/out_append');
888 file.openHandler = (openedFile) { 972 file.openHandler = (openedFile) {
889 openedFile.noPendingWriteHandler = () { 973 openedFile.noPendingWriteHandler = () {
890 openedFile.closeHandler = () { 974 openedFile.closeHandler = () {
891 file.openHandler = (openedFile) { 975 file.openHandler = (openedFile) {
892 openedFile.lengthHandler = (length) { 976 openedFile.lengthHandler = (length) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 testRead(); 1034 testRead();
951 testReadSync(); 1035 testReadSync();
952 testReadStream(); 1036 testReadStream();
953 testLength(); 1037 testLength();
954 testLengthSync(); 1038 testLengthSync();
955 testPosition(); 1039 testPosition();
956 testPositionSync(); 1040 testPositionSync();
957 testMixedSyncAndAsync(); 1041 testMixedSyncAndAsync();
958 testOpenDirectoryAsFile(); 1042 testOpenDirectoryAsFile();
959 testOpenDirectoryAsFileSync(); 1043 testOpenDirectoryAsFileSync();
1044 testReadAsBytes();
1045 testReadAsBytesSync();
1046 testReadAsText();
1047 testReadAsTextSync();
1048 testReadAsLines();
1049 testReadAsLinesSync();
1050 testReadAsErrors();
960 1051
961 createTempDirectory(() { 1052 createTempDirectory(() {
962 testReadWrite(); 1053 testReadWrite();
963 testReadWriteSync(); 1054 testReadWriteSync();
964 testReadWriteStream(); 1055 testReadWriteStream();
965 testReadEmptyFileSync(); 1056 testReadEmptyFileSync();
966 testReadEmptyFile(); 1057 testReadEmptyFile();
967 testTruncate(); 1058 testTruncate();
968 testTruncateSync(); 1059 testTruncateSync();
969 testCloseException(); 1060 testCloseException();
970 testCloseExceptionStream(); 1061 testCloseExceptionStream();
971 testBufferOutOfBoundsException(); 1062 testBufferOutOfBoundsException();
972 testAppend(); 1063 testAppend();
973 testAppendSync(); 1064 testAppendSync();
974 testWriteAppend(); 1065 testWriteAppend();
975 testOutputStreamWriteAppend(); 1066 testOutputStreamWriteAppend();
976 testWriteVariousLists(); 1067 testWriteVariousLists();
977 testDirectory(); 1068 testDirectory();
978 testDirectorySync(); 1069 testDirectorySync();
979 }); 1070 });
980 } 1071 }
981 } 1072 }
982 1073
983 main() { 1074 main() {
984 FileTest.testMain(); 1075 FileTest.testMain();
985 } 1076 }
OLDNEW
« runtime/bin/file_impl.dart ('K') | « runtime/bin/file_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698