| 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 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 }); | 473 }); |
| 474 }; | 474 }; |
| 475 }); | 475 }); |
| 476 }); | 476 }); |
| 477 } | 477 } |
| 478 | 478 |
| 479 static void testDirectory() { | 479 static void testDirectory() { |
| 480 asyncTestStarted(); | 480 asyncTestStarted(); |
| 481 | 481 |
| 482 // Port to verify that the test completes. | 482 // Port to verify that the test completes. |
| 483 var port = new ReceivePort.singleShot(); | 483 var port = new ReceivePort(); |
| 484 port.receive((message, replyTo) { | 484 port.receive((message, replyTo) { |
| 485 port.close(); |
| 485 Expect.equals(1, message); | 486 Expect.equals(1, message); |
| 486 asyncTestDone("testDirectory"); | 487 asyncTestDone("testDirectory"); |
| 487 }); | 488 }); |
| 488 | 489 |
| 489 var tempDir = tempDirectory.path; | 490 var tempDir = tempDirectory.path; |
| 490 var file = new File("${tempDir}/testDirectory"); | 491 var file = new File("${tempDir}/testDirectory"); |
| 491 var errors = 0; | 492 var errors = 0; |
| 492 file.directory((d) => Expect.fail("non-existing file")); | 493 file.directory((d) => Expect.fail("non-existing file")); |
| 493 file.onError = (s) { | 494 file.onError = (s) { |
| 494 file.onError = (s) => Expect.fail("no error expected"); | 495 file.onError = (s) => Expect.fail("no error expected"); |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 872 var f = new File('.'); | 873 var f = new File('.'); |
| 873 try { | 874 try { |
| 874 f.openSync(); | 875 f.openSync(); |
| 875 Expect.fail("Expected exception opening directory as file"); | 876 Expect.fail("Expected exception opening directory as file"); |
| 876 } catch (var e) { | 877 } catch (var e) { |
| 877 Expect.isTrue(e is FileIOException); | 878 Expect.isTrue(e is FileIOException); |
| 878 } | 879 } |
| 879 } | 880 } |
| 880 | 881 |
| 881 static void testReadAsBytes() { | 882 static void testReadAsBytes() { |
| 882 var port = new ReceivePort.singleShot(); | 883 var port = new ReceivePort(); |
| 883 port.receive((result, replyTo) { | 884 port.receive((result, replyTo) { |
| 885 port.close(); |
| 884 Expect.equals(42, result); | 886 Expect.equals(42, result); |
| 885 }); | 887 }); |
| 886 var name = getFilename("tests/vm/data/fixed_length_file"); | 888 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 887 var f = new File(name); | 889 var f = new File(name); |
| 888 f.readAsBytes((bytes) { | 890 f.readAsBytes((bytes) { |
| 889 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 891 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
| 890 port.toSendPort().send(bytes.length); | 892 port.toSendPort().send(bytes.length); |
| 891 }); | 893 }); |
| 892 f.onError = (e) { | 894 f.onError = (e) { |
| 893 Expect.fail("No errors expected: $e"); | 895 Expect.fail("No errors expected: $e"); |
| 894 }; | 896 }; |
| 895 } | 897 } |
| 896 | 898 |
| 897 static void testReadAsBytesSync() { | 899 static void testReadAsBytesSync() { |
| 898 var name = getFilename("tests/vm/data/fixed_length_file"); | 900 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 899 var bytes = new File(name).readAsBytesSync(); | 901 var bytes = new File(name).readAsBytesSync(); |
| 900 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 902 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
| 901 Expect.equals(bytes.length, 42); | 903 Expect.equals(bytes.length, 42); |
| 902 } | 904 } |
| 903 | 905 |
| 904 static void testReadAsText() { | 906 static void testReadAsText() { |
| 905 var port = new ReceivePort.singleShot(); | 907 var port = new ReceivePort(); |
| 906 port.receive((result, replyTo) { | 908 port.receive((result, replyTo) { |
| 909 port.close(); |
| 907 Expect.equals(1, result); | 910 Expect.equals(1, result); |
| 908 }); | 911 }); |
| 909 var name = getFilename("tests/vm/data/fixed_length_file"); | 912 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 910 var f = new File(name); | 913 var f = new File(name); |
| 911 f.readAsText(Encoding.UTF_8, (text) { | 914 f.readAsText(Encoding.UTF_8, (text) { |
| 912 Expect.isTrue(text.endsWith("42 bytes.")); | 915 Expect.isTrue(text.endsWith("42 bytes.")); |
| 913 Expect.equals(42, text.length); | 916 Expect.equals(42, text.length); |
| 914 var name = getDataFilename("tests/standalone/src/io/read_as_text.dat"); | 917 var name = getDataFilename("tests/standalone/src/io/read_as_text.dat"); |
| 915 var f = new File(name); | 918 var f = new File(name); |
| 916 f.onError = (e) => Expect.fail("No errors expected"); | 919 f.onError = (e) => Expect.fail("No errors expected"); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 947 var expected = [955, 120, 46, 32, 120, 10]; | 950 var expected = [955, 120, 46, 32, 120, 10]; |
| 948 Expect.listEquals(expected, text.charCodes()); | 951 Expect.listEquals(expected, text.charCodes()); |
| 949 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); }); | 952 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); }); |
| 950 text = new File(name).readAsTextSync(Encoding.ISO_8859_1); | 953 text = new File(name).readAsTextSync(Encoding.ISO_8859_1); |
| 951 expected = [206, 187, 120, 46, 32, 120, 10]; | 954 expected = [206, 187, 120, 46, 32, 120, 10]; |
| 952 Expect.equals(7, text.length); | 955 Expect.equals(7, text.length); |
| 953 Expect.listEquals(expected, text.charCodes()); | 956 Expect.listEquals(expected, text.charCodes()); |
| 954 } | 957 } |
| 955 | 958 |
| 956 static void testReadAsLines() { | 959 static void testReadAsLines() { |
| 957 var port = new ReceivePort.singleShot(); | 960 var port = new ReceivePort(); |
| 958 port.receive((result, replyTo) { | 961 port.receive((result, replyTo) { |
| 962 port.close(); |
| 959 Expect.equals(42, result); | 963 Expect.equals(42, result); |
| 960 }); | 964 }); |
| 961 var name = getFilename("tests/vm/data/fixed_length_file"); | 965 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 962 var f = new File(name); | 966 var f = new File(name); |
| 963 f.readAsLines(Encoding.UTF_8, (lines) { | 967 f.readAsLines(Encoding.UTF_8, (lines) { |
| 964 Expect.equals(1, lines.length); | 968 Expect.equals(1, lines.length); |
| 965 var line = lines[0]; | 969 var line = lines[0]; |
| 966 Expect.isTrue(line.endsWith("42 bytes.")); | 970 Expect.isTrue(line.endsWith("42 bytes.")); |
| 967 port.toSendPort().send(line.length); | 971 port.toSendPort().send(line.length); |
| 968 }); | 972 }); |
| 969 f.onError = (e) { | 973 f.onError = (e) { |
| 970 Expect.fail("No errors expected: $e"); | 974 Expect.fail("No errors expected: $e"); |
| 971 }; | 975 }; |
| 972 } | 976 } |
| 973 | 977 |
| 974 static void testReadAsLinesSync() { | 978 static void testReadAsLinesSync() { |
| 975 var name = getFilename("tests/vm/data/fixed_length_file"); | 979 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 976 var lines = new File(name).readAsLinesSync(); | 980 var lines = new File(name).readAsLinesSync(); |
| 977 Expect.equals(1, lines.length); | 981 Expect.equals(1, lines.length); |
| 978 var line = lines[0]; | 982 var line = lines[0]; |
| 979 Expect.isTrue(line.endsWith("42 bytes.")); | 983 Expect.isTrue(line.endsWith("42 bytes.")); |
| 980 Expect.equals(42, line.length); | 984 Expect.equals(42, line.length); |
| 981 name = getDataFilename("tests/standalone/src/io/readline_test1.dat"); | 985 name = getDataFilename("tests/standalone/src/io/readline_test1.dat"); |
| 982 lines = new File(name).readAsLinesSync(); | 986 lines = new File(name).readAsLinesSync(); |
| 983 Expect.equals(10, lines.length); | 987 Expect.equals(10, lines.length); |
| 984 } | 988 } |
| 985 | 989 |
| 986 | 990 |
| 987 static void testReadAsErrors() { | 991 static void testReadAsErrors() { |
| 988 var port = new ReceivePort.singleShot(); | 992 var port = new ReceivePort(); |
| 989 port.receive((message, _) { | 993 port.receive((message, _) { |
| 994 port.close(); |
| 990 Expect.equals(1, message); | 995 Expect.equals(1, message); |
| 991 }); | 996 }); |
| 992 var f = new File('.'); | 997 var f = new File('.'); |
| 993 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); | 998 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); |
| 994 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); | 999 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); |
| 995 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); | 1000 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); |
| 996 f.readAsBytes((bytes) => Expect.fail("no bytes expected")); | 1001 f.readAsBytes((bytes) => Expect.fail("no bytes expected")); |
| 997 f.onError = (e) { | 1002 f.onError = (e) { |
| 998 f.readAsText(Encoding.UTF_8, (text) => Expect.fail("no text expected")); | 1003 f.readAsText(Encoding.UTF_8, (text) => Expect.fail("no text expected")); |
| 999 f.onError = (e) { | 1004 f.onError = (e) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1101 testWriteVariousLists(); | 1106 testWriteVariousLists(); |
| 1102 testDirectory(); | 1107 testDirectory(); |
| 1103 testDirectorySync(); | 1108 testDirectorySync(); |
| 1104 }); | 1109 }); |
| 1105 } | 1110 } |
| 1106 } | 1111 } |
| 1107 | 1112 |
| 1108 main() { | 1113 main() { |
| 1109 FileTest.testMain(); | 1114 FileTest.testMain(); |
| 1110 } | 1115 } |
| OLD | NEW |