| 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 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import "package:path/path.dart"; | 8 import "package:path/path.dart"; |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import 'dart:convert'; |
| 10 import 'dart:collection'; | 11 import 'dart:collection'; |
| 11 import 'dart:io'; | 12 import 'dart:io'; |
| 12 import 'dart:isolate'; | 13 import 'dart:isolate'; |
| 13 | 14 |
| 14 class MyListOfOneElement | 15 class MyListOfOneElement |
| 15 extends Object with ListMixin<int> implements List<int> { | 16 extends Object with ListMixin<int> implements List<int> { |
| 16 int _value; | 17 int _value; |
| 17 MyListOfOneElement(this._value); | 18 MyListOfOneElement(this._value); |
| 18 int get length => 1; | 19 int get length => 1; |
| 19 operator [](int index) => _value; | 20 operator [](int index) => _value; |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 | 387 |
| 387 // Test for file read and write functionality. | 388 // Test for file read and write functionality. |
| 388 static void testOutputStreamWriteString() { | 389 static void testOutputStreamWriteString() { |
| 389 String content = "foobar"; | 390 String content = "foobar"; |
| 390 String filename = tempDirectory.path + "/outstream_write_string"; | 391 String filename = tempDirectory.path + "/outstream_write_string"; |
| 391 File file = new File(filename); | 392 File file = new File(filename); |
| 392 file.createSync(); | 393 file.createSync(); |
| 393 List<int> buffer = content.codeUnits; | 394 List<int> buffer = content.codeUnits; |
| 394 var output = file.openWrite(); | 395 var output = file.openWrite(); |
| 395 output.write("abcdABCD"); | 396 output.write("abcdABCD"); |
| 396 output.encoding = UTF_8; | 397 output.encoding = UTF8; |
| 397 output.write("abcdABCD"); | 398 output.write("abcdABCD"); |
| 398 output.encoding = ISO_8859_1; | 399 output.encoding = LATIN1; |
| 399 output.write("abcdABCD"); | 400 output.write("abcdABCD"); |
| 400 output.encoding = ASCII; | 401 output.encoding = ASCII; |
| 401 output.write("abcdABCD"); | 402 output.write("abcdABCD"); |
| 402 output.encoding = UTF_8; | 403 output.encoding = UTF8; |
| 403 output.write("æøå"); | 404 output.write("æøå"); |
| 404 output.close(); | 405 output.close(); |
| 405 output.done.then((_) { | 406 output.done.then((_) { |
| 406 RandomAccessFile raf = file.openSync(); | 407 RandomAccessFile raf = file.openSync(); |
| 407 Expect.equals(38, raf.lengthSync()); | 408 Expect.equals(38, raf.lengthSync()); |
| 408 raf.close().then((ignore) { | 409 raf.close().then((ignore) { |
| 409 asyncTestDone("testOutputStreamWriteString"); | 410 asyncTestDone("testOutputStreamWriteString"); |
| 410 }); | 411 }); |
| 411 }); | 412 }); |
| 412 asyncTestStarted(); | 413 asyncTestStarted(); |
| (...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 978 } | 979 } |
| 979 | 980 |
| 980 static void testReadAsText() { | 981 static void testReadAsText() { |
| 981 var port = new ReceivePort(); | 982 var port = new ReceivePort(); |
| 982 port.receive((result, replyTo) { | 983 port.receive((result, replyTo) { |
| 983 port.close(); | 984 port.close(); |
| 984 Expect.equals(1, result); | 985 Expect.equals(1, result); |
| 985 }); | 986 }); |
| 986 var name = getFilename("tests/vm/data/fixed_length_file"); | 987 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 987 var f = new File(name); | 988 var f = new File(name); |
| 988 f.readAsString(encoding: UTF_8).then((text) { | 989 f.readAsString(encoding: UTF8).then((text) { |
| 989 Expect.isTrue(text.endsWith("42 bytes.")); | 990 Expect.isTrue(text.endsWith("42 bytes.")); |
| 990 Expect.equals(42, text.length); | 991 Expect.equals(42, text.length); |
| 991 var name = getDataFilename("tests/standalone/io/read_as_text.dat"); | 992 var name = getDataFilename("tests/standalone/io/read_as_text.dat"); |
| 992 var f = new File(name); | 993 var f = new File(name); |
| 993 f.readAsString(encoding: UTF_8).then((text) { | 994 f.readAsString(encoding: UTF8).then((text) { |
| 994 Expect.equals(6, text.length); | 995 Expect.equals(6, text.length); |
| 995 var expected = [955, 120, 46, 32, 120, 10]; | 996 var expected = [955, 120, 46, 32, 120, 10]; |
| 996 Expect.listEquals(expected, text.codeUnits); | 997 Expect.listEquals(expected, text.codeUnits); |
| 997 f.readAsString(encoding: ISO_8859_1).then((text) { | 998 f.readAsString(encoding: LATIN1).then((text) { |
| 998 Expect.equals(7, text.length); | 999 Expect.equals(7, text.length); |
| 999 var expected = [206, 187, 120, 46, 32, 120, 10]; | 1000 var expected = [206, 187, 120, 46, 32, 120, 10]; |
| 1000 Expect.listEquals(expected, text.codeUnits); | 1001 Expect.listEquals(expected, text.codeUnits); |
| 1001 var readAsStringFuture = f.readAsString(encoding: ASCII); | 1002 var readAsStringFuture = f.readAsString(encoding: ASCII); |
| 1002 readAsStringFuture.then((text) { | 1003 readAsStringFuture.then((text) { |
| 1003 Expect.fail("Non-ascii char should cause error"); | 1004 Expect.fail("Non-ascii char should cause error"); |
| 1004 }).catchError((e) { | 1005 }).catchError((e) { |
| 1005 port.toSendPort().send(1); | 1006 port.toSendPort().send(1); |
| 1006 }); | 1007 }); |
| 1007 }); | 1008 }); |
| 1008 }); | 1009 }); |
| 1009 }); | 1010 }); |
| 1010 } | 1011 } |
| 1011 | 1012 |
| 1012 static void testReadAsTextEmptyFile() { | 1013 static void testReadAsTextEmptyFile() { |
| 1013 var port = new ReceivePort(); | 1014 var port = new ReceivePort(); |
| 1014 port.receive((result, replyTo) { | 1015 port.receive((result, replyTo) { |
| 1015 port.close(); | 1016 port.close(); |
| 1016 Expect.equals(0, result); | 1017 Expect.equals(0, result); |
| 1017 }); | 1018 }); |
| 1018 var name = getFilename("tests/vm/data/empty_file"); | 1019 var name = getFilename("tests/vm/data/empty_file"); |
| 1019 var f = new File(name); | 1020 var f = new File(name); |
| 1020 f.readAsString(encoding: UTF_8).then((text) { | 1021 f.readAsString(encoding: UTF8).then((text) { |
| 1021 port.toSendPort().send(text.length); | 1022 port.toSendPort().send(text.length); |
| 1022 return true; | 1023 return true; |
| 1023 }); | 1024 }); |
| 1024 } | 1025 } |
| 1025 | 1026 |
| 1026 static void testReadAsTextSync() { | 1027 static void testReadAsTextSync() { |
| 1027 var name = getFilename("tests/vm/data/fixed_length_file"); | 1028 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 1028 var text = new File(name).readAsStringSync(); | 1029 var text = new File(name).readAsStringSync(); |
| 1029 Expect.isTrue(text.endsWith("42 bytes.")); | 1030 Expect.isTrue(text.endsWith("42 bytes.")); |
| 1030 Expect.equals(42, text.length); | 1031 Expect.equals(42, text.length); |
| 1031 name = getDataFilename("tests/standalone/io/read_as_text.dat"); | 1032 name = getDataFilename("tests/standalone/io/read_as_text.dat"); |
| 1032 text = new File(name).readAsStringSync(); | 1033 text = new File(name).readAsStringSync(); |
| 1033 Expect.equals(6, text.length); | 1034 Expect.equals(6, text.length); |
| 1034 var expected = [955, 120, 46, 32, 120, 10]; | 1035 var expected = [955, 120, 46, 32, 120, 10]; |
| 1035 Expect.listEquals(expected, text.codeUnits); | 1036 Expect.listEquals(expected, text.codeUnits); |
| 1036 text = new File(name).readAsStringSync(encoding: ASCII); | 1037 // First character is not ASCII. The default ASCII decoder will throw. |
| 1037 // Default replacement character is '?', char code 63. | 1038 Expect.throws(() => new File(name).readAsStringSync(encoding: ASCII), |
| 1038 expected = [63, 63, 120, 46, 32, 120, 10]; | 1039 (e) => e is FormatError); |
| 1040 // We can use an ASCII decoder that inserts the replacement character. |
| 1041 var lenientAscii = const AsciiCodec(allowInvalid: true); |
| 1042 text = new File(name).readAsStringSync(encoding: lenientAscii); |
| 1043 // Default replacement character is the Unicode replacement character. |
| 1044 expected = [UNICODE_REPLACEMENT_CHARACTER_RUNE, |
| 1045 UNICODE_REPLACEMENT_CHARACTER_RUNE, |
| 1046 120, 46, 32, 120, 10]; |
| 1039 Expect.listEquals(expected, text.codeUnits); | 1047 Expect.listEquals(expected, text.codeUnits); |
| 1040 text = new File(name).readAsStringSync(encoding: ISO_8859_1); | 1048 text = new File(name).readAsStringSync(encoding: LATIN1); |
| 1041 expected = [206, 187, 120, 46, 32, 120, 10]; | 1049 expected = [206, 187, 120, 46, 32, 120, 10]; |
| 1042 Expect.equals(7, text.length); | 1050 Expect.equals(7, text.length); |
| 1043 Expect.listEquals(expected, text.codeUnits); | 1051 Expect.listEquals(expected, text.codeUnits); |
| 1044 } | 1052 } |
| 1045 | 1053 |
| 1046 static void testReadAsTextSyncEmptyFile() { | 1054 static void testReadAsTextSyncEmptyFile() { |
| 1047 var name = getFilename("tests/vm/data/empty_file"); | 1055 var name = getFilename("tests/vm/data/empty_file"); |
| 1048 var text = new File(name).readAsStringSync(); | 1056 var text = new File(name).readAsStringSync(); |
| 1049 Expect.equals(0, text.length); | 1057 Expect.equals(0, text.length); |
| 1050 } | 1058 } |
| 1051 | 1059 |
| 1052 static void testReadAsLines() { | 1060 static void testReadAsLines() { |
| 1053 var port = new ReceivePort(); | 1061 var port = new ReceivePort(); |
| 1054 port.receive((result, replyTo) { | 1062 port.receive((result, replyTo) { |
| 1055 port.close(); | 1063 port.close(); |
| 1056 Expect.equals(42, result); | 1064 Expect.equals(42, result); |
| 1057 }); | 1065 }); |
| 1058 var name = getFilename("tests/vm/data/fixed_length_file"); | 1066 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 1059 var f = new File(name); | 1067 var f = new File(name); |
| 1060 f.readAsLines(encoding: UTF_8).then((lines) { | 1068 f.readAsLines(encoding: UTF8).then((lines) { |
| 1061 Expect.equals(1, lines.length); | 1069 Expect.equals(1, lines.length); |
| 1062 var line = lines[0]; | 1070 var line = lines[0]; |
| 1063 Expect.isTrue(line.endsWith("42 bytes.")); | 1071 Expect.isTrue(line.endsWith("42 bytes.")); |
| 1064 port.toSendPort().send(line.length); | 1072 port.toSendPort().send(line.length); |
| 1065 }); | 1073 }); |
| 1066 } | 1074 } |
| 1067 | 1075 |
| 1068 static void testReadAsLinesSync() { | 1076 static void testReadAsLinesSync() { |
| 1069 var name = getFilename("tests/vm/data/fixed_length_file"); | 1077 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 1070 var lines = new File(name).readAsLinesSync(); | 1078 var lines = new File(name).readAsLinesSync(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1084 port.close(); | 1092 port.close(); |
| 1085 Expect.equals(1, message); | 1093 Expect.equals(1, message); |
| 1086 }); | 1094 }); |
| 1087 var f = new File('.'); | 1095 var f = new File('.'); |
| 1088 Expect.throws(f.readAsBytesSync, (e) => e is FileException); | 1096 Expect.throws(f.readAsBytesSync, (e) => e is FileException); |
| 1089 Expect.throws(f.readAsStringSync, (e) => e is FileException); | 1097 Expect.throws(f.readAsStringSync, (e) => e is FileException); |
| 1090 Expect.throws(f.readAsLinesSync, (e) => e is FileException); | 1098 Expect.throws(f.readAsLinesSync, (e) => e is FileException); |
| 1091 var readAsBytesFuture = f.readAsBytes(); | 1099 var readAsBytesFuture = f.readAsBytes(); |
| 1092 readAsBytesFuture.then((bytes) => Expect.fail("no bytes expected")) | 1100 readAsBytesFuture.then((bytes) => Expect.fail("no bytes expected")) |
| 1093 .catchError((e) { | 1101 .catchError((e) { |
| 1094 var readAsStringFuture = f.readAsString(encoding: UTF_8); | 1102 var readAsStringFuture = f.readAsString(encoding: UTF8); |
| 1095 readAsStringFuture.then((text) => Expect.fail("no text expected")) | 1103 readAsStringFuture.then((text) => Expect.fail("no text expected")) |
| 1096 .catchError((e) { | 1104 .catchError((e) { |
| 1097 var readAsLinesFuture = f.readAsLines(encoding: UTF_8); | 1105 var readAsLinesFuture = f.readAsLines(encoding: UTF8); |
| 1098 readAsLinesFuture.then((lines) => Expect.fail("no lines expected")) | 1106 readAsLinesFuture.then((lines) => Expect.fail("no lines expected")) |
| 1099 .catchError((e) { | 1107 .catchError((e) { |
| 1100 port.toSendPort().send(1); | 1108 port.toSendPort().send(1); |
| 1101 }); | 1109 }); |
| 1102 }); | 1110 }); |
| 1103 }); | 1111 }); |
| 1104 } | 1112 } |
| 1105 | 1113 |
| 1106 static void testLastModified() { | 1114 static void testLastModified() { |
| 1107 var port = new ReceivePort(); | 1115 var port = new ReceivePort(); |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1330 testWriteStringUtf8Sync(); | 1338 testWriteStringUtf8Sync(); |
| 1331 testRename(); | 1339 testRename(); |
| 1332 testRenameSync(); | 1340 testRenameSync(); |
| 1333 }); | 1341 }); |
| 1334 } | 1342 } |
| 1335 } | 1343 } |
| 1336 | 1344 |
| 1337 main() { | 1345 main() { |
| 1338 FileTest.testMain(); | 1346 FileTest.testMain(); |
| 1339 } | 1347 } |
| OLD | NEW |