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:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 | 10 |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 }); | 392 }); |
393 asyncTestStarted(); | 393 asyncTestStarted(); |
394 } | 394 } |
395 | 395 |
396 static void testWriteAppend() { | 396 static void testWriteAppend() { |
397 String content = "foobar"; | 397 String content = "foobar"; |
398 String filename = tempDirectory.path.concat("/write_append"); | 398 String filename = tempDirectory.path.concat("/write_append"); |
399 File file = new File(filename); | 399 File file = new File(filename); |
400 file.createSync(); | 400 file.createSync(); |
401 Expect.isTrue(new File(filename).existsSync()); | 401 Expect.isTrue(new File(filename).existsSync()); |
402 List<int> buffer = content.charCodes; | 402 List<int> buffer = content.codeUnits; |
403 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); | 403 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); |
404 openedFile.writeListSync(buffer, 0, buffer.length); | 404 openedFile.writeListSync(buffer, 0, buffer.length); |
405 openedFile.closeSync(); | 405 openedFile.closeSync(); |
406 // Reopen the file in write mode to ensure that we overwrite the content. | 406 // Reopen the file in write mode to ensure that we overwrite the content. |
407 openedFile = (new File(filename)).openSync(FileMode.WRITE); | 407 openedFile = (new File(filename)).openSync(FileMode.WRITE); |
408 openedFile.writeListSync(buffer, 0, buffer.length); | 408 openedFile.writeListSync(buffer, 0, buffer.length); |
409 Expect.equals(content.length, openedFile.lengthSync()); | 409 Expect.equals(content.length, openedFile.lengthSync()); |
410 openedFile.closeSync(); | 410 openedFile.closeSync(); |
411 // Open the file in append mode and ensure that we do not overwrite | 411 // Open the file in append mode and ensure that we do not overwrite |
412 // the existing content. | 412 // the existing content. |
413 openedFile = (new File(filename)).openSync(FileMode.APPEND); | 413 openedFile = (new File(filename)).openSync(FileMode.APPEND); |
414 openedFile.writeListSync(buffer, 0, buffer.length); | 414 openedFile.writeListSync(buffer, 0, buffer.length); |
415 Expect.equals(content.length * 2, openedFile.lengthSync()); | 415 Expect.equals(content.length * 2, openedFile.lengthSync()); |
416 openedFile.closeSync(); | 416 openedFile.closeSync(); |
417 file.deleteSync(); | 417 file.deleteSync(); |
418 } | 418 } |
419 | 419 |
420 static void testOutputStreamWriteAppend() { | 420 static void testOutputStreamWriteAppend() { |
421 String content = "foobar"; | 421 String content = "foobar"; |
422 String filename = tempDirectory.path.concat("/outstream_write_append"); | 422 String filename = tempDirectory.path.concat("/outstream_write_append"); |
423 File file = new File(filename); | 423 File file = new File(filename); |
424 file.createSync(); | 424 file.createSync(); |
425 List<int> buffer = content.charCodes; | 425 List<int> buffer = content.codeUnits; |
426 OutputStream outStream = file.openOutputStream(); | 426 OutputStream outStream = file.openOutputStream(); |
427 outStream.write(buffer); | 427 outStream.write(buffer); |
428 outStream.onNoPendingWrites = () { | 428 outStream.onNoPendingWrites = () { |
429 outStream.close(); | 429 outStream.close(); |
430 outStream.onClosed = () { | 430 outStream.onClosed = () { |
431 File file2 = new File(filename); | 431 File file2 = new File(filename); |
432 OutputStream appendingOutput = | 432 OutputStream appendingOutput = |
433 file2.openOutputStream(FileMode.APPEND); | 433 file2.openOutputStream(FileMode.APPEND); |
434 appendingOutput.write(buffer); | 434 appendingOutput.write(buffer); |
435 appendingOutput.onNoPendingWrites = () { | 435 appendingOutput.onNoPendingWrites = () { |
(...skipping 16 matching lines...) Expand all Loading... |
452 }; | 452 }; |
453 asyncTestStarted(); | 453 asyncTestStarted(); |
454 } | 454 } |
455 | 455 |
456 // Test for file read and write functionality. | 456 // Test for file read and write functionality. |
457 static void testOutputStreamWriteString() { | 457 static void testOutputStreamWriteString() { |
458 String content = "foobar"; | 458 String content = "foobar"; |
459 String filename = tempDirectory.path.concat("/outstream_write_string"); | 459 String filename = tempDirectory.path.concat("/outstream_write_string"); |
460 File file = new File(filename); | 460 File file = new File(filename); |
461 file.createSync(); | 461 file.createSync(); |
462 List<int> buffer = content.charCodes; | 462 List<int> buffer = content.codeUnits; |
463 OutputStream outStream = file.openOutputStream(); | 463 OutputStream outStream = file.openOutputStream(); |
464 outStream.writeString("abcdABCD"); | 464 outStream.writeString("abcdABCD"); |
465 outStream.writeString("abcdABCD", Encoding.UTF_8); | 465 outStream.writeString("abcdABCD", Encoding.UTF_8); |
466 outStream.writeString("abcdABCD", Encoding.ISO_8859_1); | 466 outStream.writeString("abcdABCD", Encoding.ISO_8859_1); |
467 outStream.writeString("abcdABCD", Encoding.ASCII); | 467 outStream.writeString("abcdABCD", Encoding.ASCII); |
468 outStream.writeString("æøå", Encoding.UTF_8); | 468 outStream.writeString("æøå", Encoding.UTF_8); |
469 outStream.onNoPendingWrites = () { | 469 outStream.onNoPendingWrites = () { |
470 outStream.close(); | 470 outStream.close(); |
471 outStream.onClosed = () { | 471 outStream.onClosed = () { |
472 RandomAccessFile raf = file.openSync(); | 472 RandomAccessFile raf = file.openSync(); |
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1036 var name = getFilename("tests/vm/data/fixed_length_file"); | 1036 var name = getFilename("tests/vm/data/fixed_length_file"); |
1037 var f = new File(name); | 1037 var f = new File(name); |
1038 f.readAsString(Encoding.UTF_8).then((text) { | 1038 f.readAsString(Encoding.UTF_8).then((text) { |
1039 Expect.isTrue(text.endsWith("42 bytes.")); | 1039 Expect.isTrue(text.endsWith("42 bytes.")); |
1040 Expect.equals(42, text.length); | 1040 Expect.equals(42, text.length); |
1041 var name = getDataFilename("tests/standalone/io/read_as_text.dat"); | 1041 var name = getDataFilename("tests/standalone/io/read_as_text.dat"); |
1042 var f = new File(name); | 1042 var f = new File(name); |
1043 f.readAsString(Encoding.UTF_8).then((text) { | 1043 f.readAsString(Encoding.UTF_8).then((text) { |
1044 Expect.equals(6, text.length); | 1044 Expect.equals(6, text.length); |
1045 var expected = [955, 120, 46, 32, 120, 10]; | 1045 var expected = [955, 120, 46, 32, 120, 10]; |
1046 Expect.listEquals(expected, text.charCodes); | 1046 Expect.listEquals(expected, text.codeUnits); |
1047 f.readAsString(Encoding.ISO_8859_1).then((text) { | 1047 f.readAsString(Encoding.ISO_8859_1).then((text) { |
1048 Expect.equals(7, text.length); | 1048 Expect.equals(7, text.length); |
1049 var expected = [206, 187, 120, 46, 32, 120, 10]; | 1049 var expected = [206, 187, 120, 46, 32, 120, 10]; |
1050 Expect.listEquals(expected, text.charCodes); | 1050 Expect.listEquals(expected, text.codeUnits); |
1051 var readAsStringFuture = f.readAsString(Encoding.ASCII); | 1051 var readAsStringFuture = f.readAsString(Encoding.ASCII); |
1052 readAsStringFuture.then((text) { | 1052 readAsStringFuture.then((text) { |
1053 Expect.fail("Non-ascii char should cause error"); | 1053 Expect.fail("Non-ascii char should cause error"); |
1054 }).catchError((e) { | 1054 }).catchError((e) { |
1055 port.toSendPort().send(1); | 1055 port.toSendPort().send(1); |
1056 }); | 1056 }); |
1057 }); | 1057 }); |
1058 }); | 1058 }); |
1059 }); | 1059 }); |
1060 } | 1060 } |
(...skipping 14 matching lines...) Expand all Loading... |
1075 | 1075 |
1076 static void testReadAsTextSync() { | 1076 static void testReadAsTextSync() { |
1077 var name = getFilename("tests/vm/data/fixed_length_file"); | 1077 var name = getFilename("tests/vm/data/fixed_length_file"); |
1078 var text = new File(name).readAsStringSync(); | 1078 var text = new File(name).readAsStringSync(); |
1079 Expect.isTrue(text.endsWith("42 bytes.")); | 1079 Expect.isTrue(text.endsWith("42 bytes.")); |
1080 Expect.equals(42, text.length); | 1080 Expect.equals(42, text.length); |
1081 name = getDataFilename("tests/standalone/io/read_as_text.dat"); | 1081 name = getDataFilename("tests/standalone/io/read_as_text.dat"); |
1082 text = new File(name).readAsStringSync(); | 1082 text = new File(name).readAsStringSync(); |
1083 Expect.equals(6, text.length); | 1083 Expect.equals(6, text.length); |
1084 var expected = [955, 120, 46, 32, 120, 10]; | 1084 var expected = [955, 120, 46, 32, 120, 10]; |
1085 Expect.listEquals(expected, text.charCodes); | 1085 Expect.listEquals(expected, text.codeUnits); |
1086 Expect.throws(() { new File(name).readAsStringSync(Encoding.ASCII); }); | 1086 Expect.throws(() { new File(name).readAsStringSync(Encoding.ASCII); }); |
1087 text = new File(name).readAsStringSync(Encoding.ISO_8859_1); | 1087 text = new File(name).readAsStringSync(Encoding.ISO_8859_1); |
1088 expected = [206, 187, 120, 46, 32, 120, 10]; | 1088 expected = [206, 187, 120, 46, 32, 120, 10]; |
1089 Expect.equals(7, text.length); | 1089 Expect.equals(7, text.length); |
1090 Expect.listEquals(expected, text.charCodes); | 1090 Expect.listEquals(expected, text.codeUnits); |
1091 } | 1091 } |
1092 | 1092 |
1093 static void testReadAsTextSyncEmptyFile() { | 1093 static void testReadAsTextSyncEmptyFile() { |
1094 var name = getFilename("tests/vm/data/empty_file"); | 1094 var name = getFilename("tests/vm/data/empty_file"); |
1095 var text = new File(name).readAsStringSync(); | 1095 var text = new File(name).readAsStringSync(); |
1096 Expect.equals(0, text.length); | 1096 Expect.equals(0, text.length); |
1097 } | 1097 } |
1098 | 1098 |
1099 static void testReadAsLines() { | 1099 static void testReadAsLines() { |
1100 var port = new ReceivePort(); | 1100 var port = new ReceivePort(); |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1321 testDirectorySync(); | 1321 testDirectorySync(); |
1322 testWriteStringUtf8(); | 1322 testWriteStringUtf8(); |
1323 testWriteStringUtf8Sync(); | 1323 testWriteStringUtf8Sync(); |
1324 }); | 1324 }); |
1325 } | 1325 } |
1326 } | 1326 } |
1327 | 1327 |
1328 main() { | 1328 main() { |
1329 FileTest.testMain(); | 1329 FileTest.testMain(); |
1330 } | 1330 } |
OLD | NEW |