| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 FileInputStream, VM-only, standalone test. | 4 // Testing FileInputStream, VM-only, standalone test. |
| 5 | 5 |
| 6 // Helper method to be able to run the test from the runtime | 6 // Helper method to be able to run the test from the runtime |
| 7 // directory, or the top directory. | 7 // directory, or the top directory. |
| 8 String getFilename(String path) => | 8 String getFilename(String path) => |
| 9 new File(path).existsSync() ? path : '../' + path; | 9 new File(path).existsSync() ? path : '../' + path; |
| 10 | 10 |
| 11 void testStringInputStreamSync() { | 11 void testStringInputStreamSync() { |
| 12 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 12 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
| 13 // File contains "Hello Dart\nwassup!\n" | 13 // File contains "Hello Dart\nwassup!\n" |
| 14 File file = new File(fileName); | 14 File file = new File(fileName); |
| 15 StringInputStream x = new StringInputStream(file.openInputStream()); | 15 StringInputStream x = new StringInputStream(file.openInputStream()); |
| 16 String line = x.readLine(); | 16 x.lineHandler = () { |
| 17 Expect.equals("Hello Dart", line); | 17 // The file input stream is known (for now) to have read the whole |
| 18 line = x.readLine(); | 18 // file when the data handler is called. |
| 19 Expect.equals("wassup!", line); | 19 String line = x.readLine(); |
| 20 Expect.equals("Hello Dart", line); |
| 21 line = x.readLine(); |
| 22 Expect.equals("wassup!", line); |
| 23 }; |
| 20 } | 24 } |
| 21 | 25 |
| 22 void testInputStreamAsync() { | 26 void testInputStreamAsync() { |
| 23 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 27 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
| 24 // File contains "Hello Dart\nwassup!\n" | 28 // File contains "Hello Dart\nwassup!\n" |
| 25 var expected = "Hello Dart\nwassup!\n".charCodes(); | 29 var expected = "Hello Dart\nwassup!\n".charCodes(); |
| 26 InputStream x = (new File(fileName)).openInputStream(); | 30 InputStream x = (new File(fileName)).openInputStream(); |
| 27 var byteCount = 0; | 31 var byteCount = 0; |
| 28 x.dataHandler = () { | 32 x.dataHandler = () { |
| 29 Expect.equals(expected[byteCount], x.read(1)[0]); | 33 Expect.equals(expected[byteCount], x.read(1)[0]); |
| 30 byteCount++; | 34 byteCount++; |
| 31 }; | 35 }; |
| 32 x.closeHandler = () { | 36 x.closeHandler = () { |
| 33 Expect.equals(expected.length, byteCount); | 37 Expect.equals(expected.length, byteCount); |
| 34 }; | 38 }; |
| 35 } | 39 } |
| 36 | 40 |
| 37 | 41 |
| 38 void testStringInputStreamAsync1() { | 42 void testStringInputStreamAsync(String name, int length) { |
| 39 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 43 String fileName = getFilename("tests/standalone/src/$name"); |
| 40 // File contains "Hello Dart\nwassup!\n" | 44 // File contains 10 lines. |
| 41 File file = new File(fileName); | 45 File file = new File(fileName); |
| 42 StringInputStream x = new StringInputStream(file.openInputStream()); | 46 Expect.equals(length, file.openSync().lengthSync()); |
| 43 var result = ""; | |
| 44 x.dataHandler = () { | |
| 45 result += x.read(); | |
| 46 }; | |
| 47 x.closeHandler = () { | |
| 48 Expect.equals("Hello Dart\nwassup!\n", result); | |
| 49 }; | |
| 50 } | |
| 51 | |
| 52 | |
| 53 void testStringInputStreamAsync2() { | |
| 54 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | |
| 55 // File contains "Hello Dart\nwassup!\n" | |
| 56 File file = new File(fileName); | |
| 57 StringInputStream x = new StringInputStream(file.openInputStream()); | 47 StringInputStream x = new StringInputStream(file.openInputStream()); |
| 58 int lineCount = 0; | 48 int lineCount = 0; |
| 59 x.lineHandler = () { | 49 x.lineHandler = () { |
| 60 var line = x.readLine(); | 50 var line = x.readLine(); |
| 61 Expect.isTrue(lineCount == 0 || lineCount == 1); | |
| 62 if (lineCount == 0) Expect.equals("Hello Dart", line); | |
| 63 if (lineCount == 1) Expect.equals("wassup!", line); | |
| 64 lineCount++; | 51 lineCount++; |
| 52 Expect.isTrue(lineCount <= 10); |
| 53 if (line[0] != "#") { |
| 54 Expect.equals("Line $lineCount", line); |
| 55 } |
| 65 }; | 56 }; |
| 66 x.closeHandler = () { | 57 x.closeHandler = () { |
| 67 Expect.equals(2, lineCount); | 58 Expect.equals(10, lineCount); |
| 68 }; | 59 }; |
| 69 } | 60 } |
| 70 | 61 |
| 71 | 62 |
| 72 void testChunkedInputStream() { | 63 void testChunkedInputStream() { |
| 73 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 64 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
| 74 // File contains 19 bytes ("Hello Dart\nwassup!") | 65 // File contains 19 bytes ("Hello Dart\nwassup!") |
| 75 File file = new File(fileName); | 66 File file = new File(fileName); |
| 76 ChunkedInputStream x = new ChunkedInputStream(file.openInputStream()); | 67 ChunkedInputStream x = new ChunkedInputStream(file.openInputStream()); |
| 77 x.chunkSize = 9; | 68 x.chunkSize = 9; |
| 78 List<int> chunk = x.read(); | 69 List<int> chunk = x.read(); |
| 79 Expect.equals(9, chunk.length); | 70 Expect.equals(9, chunk.length); |
| 80 x.chunkSize = 5; | 71 x.chunkSize = 5; |
| 81 chunk = x.read(); | 72 chunk = x.read(); |
| 82 Expect.equals(5, chunk.length); | 73 Expect.equals(5, chunk.length); |
| 83 chunk = x.read(); | 74 chunk = x.read(); |
| 84 Expect.equals(5, chunk.length); | 75 Expect.equals(5, chunk.length); |
| 85 chunk = x.read(); | 76 chunk = x.read(); |
| 86 Expect.equals(null, chunk); | 77 Expect.equals(null, chunk); |
| 87 } | 78 } |
| 88 | 79 |
| 89 | 80 |
| 90 main() { | 81 main() { |
| 91 testStringInputStreamSync(); | 82 testStringInputStreamSync(); |
| 92 testInputStreamAsync(); | 83 testInputStreamAsync(); |
| 93 testStringInputStreamAsync1(); | 84 // Check the length of these files as both are text files where one |
| 94 testStringInputStreamAsync2(); | 85 // is without a terminating line separator which can easily be added |
| 86 // back if accidentally opened in a text editor. |
| 87 testStringInputStreamAsync("readline_test1.dat", 111); |
| 88 testStringInputStreamAsync("readline_test2.dat", 114); |
| 95 testChunkedInputStream(); | 89 testChunkedInputStream(); |
| 96 } | 90 } |
| OLD | NEW |