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