Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, 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 // Testing file input stream, VM-only, standalone test. | 4 // Testing file input stream, VM-only, standalone test. |
| 5 | 5 |
| 6 #import("dart:io"); | 6 #import("dart:io"); |
| 7 | 7 |
| 8 // Helper method to be able to run the test from the runtime | 8 // Helper method to be able to run the test from the runtime |
| 9 // directory, or the top directory. | 9 // directory, or the top directory. |
| 10 String getFilename(String path) => | 10 String getFilename(String path) => |
| 11 new File(path).existsSync() ? path : '../' + path; | 11 new File(path).existsSync() ? path : '../' + path; |
| 12 | 12 |
| 13 void testStringInputStreamSync() { | 13 void testStringInputStreamSync() { |
| 14 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 14 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
| 15 // File contains "Hello Dart\nwassup!\n" | 15 // File contains "Hello Dart\nwassup!\n" |
| 16 File file = new File(fileName); | 16 File file = new File(fileName); |
| 17 StringInputStream x = new StringInputStream(file.openInputStream()); | 17 StringInputStream x = new StringInputStream(file.openInputStreamSync()); |
| 18 x.lineHandler = () { | 18 x.lineHandler = () { |
| 19 // The file input stream is known (for now) to have read the whole | 19 // The file input stream is known (for now) to have read the whole |
| 20 // file when the data handler is called. | 20 // file when the data handler is called. |
| 21 String line = x.readLine(); | 21 String line = x.readLine(); |
| 22 Expect.equals("Hello Dart", line); | 22 Expect.equals("Hello Dart", line); |
| 23 line = x.readLine(); | 23 line = x.readLine(); |
| 24 Expect.equals("wassup!", line); | 24 Expect.equals("wassup!", line); |
| 25 }; | 25 }; |
| 26 } | 26 } |
| 27 | 27 |
| 28 void testInputStreamAsync() { | 28 void testInputStreamAsync() { |
| 29 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 29 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
| 30 // File contains "Hello Dart\nwassup!\n" | 30 // File contains "Hello Dart\nwassup!\n" |
| 31 var expected = "Hello Dart\nwassup!\n".charCodes(); | 31 var expected = "Hello Dart\nwassup!\n".charCodes(); |
| 32 InputStream x = (new File(fileName)).openInputStream(); | 32 InputStream x = (new File(fileName)).openInputStreamSync(); |
| 33 var byteCount = 0; | 33 var byteCount = 0; |
| 34 x.dataHandler = () { | 34 x.dataHandler = () { |
| 35 Expect.equals(expected[byteCount], x.read(1)[0]); | 35 Expect.equals(expected[byteCount], x.read(1)[0]); |
| 36 byteCount++; | 36 byteCount++; |
| 37 }; | 37 }; |
| 38 x.closeHandler = () { | 38 x.closeHandler = () { |
| 39 Expect.equals(expected.length, byteCount); | 39 Expect.equals(expected.length, byteCount); |
| 40 }; | 40 }; |
| 41 } | 41 } |
| 42 | 42 |
| 43 | 43 |
| 44 void testStringInputStreamAsync(String name, int length) { | 44 void testStringInputStreamAsync(String name, int length) { |
| 45 String fileName = getFilename("tests/standalone/src/$name"); | 45 String fileName = getFilename("tests/standalone/src/$name"); |
| 46 // File contains 10 lines. | 46 // File contains 10 lines. |
| 47 File file = new File(fileName); | 47 File file = new File(fileName); |
| 48 Expect.equals(length, file.openSync().lengthSync()); | 48 Expect.equals(length, file.openSync().lengthSync()); |
| 49 StringInputStream x = new StringInputStream(file.openInputStream()); | 49 StringInputStream x = new StringInputStream(file.openInputStreamSync()); |
| 50 int lineCount = 0; | 50 int lineCount = 0; |
| 51 x.lineHandler = () { | 51 x.lineHandler = () { |
| 52 var line = x.readLine(); | 52 var line = x.readLine(); |
| 53 lineCount++; | 53 lineCount++; |
| 54 Expect.isTrue(lineCount <= 10); | 54 Expect.isTrue(lineCount <= 10); |
| 55 if (line[0] != "#") { | 55 if (line[0] != "#") { |
| 56 Expect.equals("Line $lineCount", line); | 56 Expect.equals("Line $lineCount", line); |
| 57 } | 57 } |
| 58 }; | 58 }; |
| 59 x.closeHandler = () { | 59 x.closeHandler = () { |
| 60 Expect.equals(10, lineCount); | 60 Expect.equals(10, lineCount); |
| 61 }; | 61 }; |
| 62 } | 62 } |
| 63 | 63 |
| 64 | 64 |
| 65 void testChunkedInputStream() { | 65 void testChunkedInputStream() { |
| 66 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 66 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
| 67 // File contains 19 bytes ("Hello Dart\nwassup!") | 67 // File contains 19 bytes ("Hello Dart\nwassup!") |
| 68 File file = new File(fileName); | 68 File file = new File(fileName); |
| 69 ChunkedInputStream x = new ChunkedInputStream(file.openInputStream()); | 69 ChunkedInputStream x = new ChunkedInputStream(file.openInputStreamSync()); |
| 70 x.chunkSize = 9; | 70 x.chunkSize = 9; |
| 71 List<int> chunk = x.read(); | 71 List<int> chunk = x.read(); |
| 72 Expect.equals(9, chunk.length); | 72 Expect.equals(9, chunk.length); |
| 73 x.chunkSize = 5; | 73 x.chunkSize = 5; |
| 74 chunk = x.read(); | 74 chunk = x.read(); |
| 75 Expect.equals(5, chunk.length); | 75 Expect.equals(5, chunk.length); |
| 76 chunk = x.read(); | 76 chunk = x.read(); |
| 77 Expect.equals(5, chunk.length); | 77 Expect.equals(5, chunk.length); |
| 78 chunk = x.read(); | 78 chunk = x.read(); |
| 79 Expect.equals(null, chunk); | 79 Expect.equals(null, chunk); |
| 80 } | 80 } |
| 81 | 81 |
| 82 | 82 |
| 83 void testOpenInputStreamAsync() { | |
| 84 // Create a port for waiting on the final result of this test. | |
| 85 ReceivePort done = new ReceivePort(); | |
| 86 done.receive((message, replyTo) { | |
| 87 done.close(); | |
| 88 }); | |
| 89 | |
| 90 // Test using the asynchronous way of opening an input stream. | |
| 91 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | |
| 92 File file = new File(fileName); | |
| 93 file.exists(); | |
| 94 file.existsHandler = (exists) { | |
| 95 if (exists) { | |
| 96 file.openInputStream(); | |
| 97 } else { | |
| 98 Expect.fail("Test file not found"); | |
| 99 } | |
| 100 }; | |
| 101 file.inputStreamHandler = (InputStream stream) { | |
| 102 done.toSendPort().send("Got an InputStream"); | |
| 103 }; | |
| 104 file.errorHandler = (String error) { | |
| 105 Expect.fail("Error $error"); | |
| 106 }; | |
| 107 } | |
| 108 | |
| 109 | |
| 83 main() { | 110 main() { |
| 84 testStringInputStreamSync(); | 111 testStringInputStreamSync(); |
| 85 testInputStreamAsync(); | 112 testInputStreamAsync(); |
| 86 // Check the length of these files as both are text files where one | 113 // Check the length of these files as both are text files where one |
| 87 // is without a terminating line separator which can easily be added | 114 // is without a terminating line separator which can easily be added |
| 88 // back if accidentally opened in a text editor. | 115 // back if accidentally opened in a text editor. |
| 89 testStringInputStreamAsync("readline_test1.dat", 111); | 116 //testStringInputStreamAsync("readline_test1.dat", 111); |
|
Mads Ager (google)
2012/02/22 13:20:20
Accidental edit?
Søren Gjesse
2012/02/22 14:03:18
Yes.
| |
| 90 testStringInputStreamAsync("readline_test2.dat", 114); | 117 testStringInputStreamAsync("readline_test2.dat", 114); |
| 91 testChunkedInputStream(); | 118 testChunkedInputStream(); |
| 119 testOpenInputStreamAsync(); | |
| 92 } | 120 } |
| OLD | NEW |