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