Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(496)

Side by Side Diff: tests/standalone/src/FileInputStreamTest.dart

Issue 9487005: Move back to having File.open{Input,Output}Stream return the stream directly. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // 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 #import("dart:isolate"); 7 #import("dart:isolate");
8 8
9 // Helper method to be able to run the test from the runtime 9 // Helper method to be able to run the test from the runtime
10 // directory, or the top directory. 10 // directory, or the top directory.
11 String getFilename(String path) => 11 String getFilename(String path) =>
12 new File(path).existsSync() ? path : '../' + path; 12 new File(path).existsSync() ? path : '../' + path;
13 13
14 void testStringInputStreamSync() { 14 void testStringInputStreamSync() {
15 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); 15 String fileName = getFilename("tests/standalone/src/readuntil_test.dat");
16 // File contains "Hello Dart\nwassup!\n" 16 // File contains "Hello Dart\nwassup!\n"
17 File file = new File(fileName); 17 File file = new File(fileName);
18 StringInputStream x = new StringInputStream(file.openInputStreamSync()); 18 StringInputStream x = new StringInputStream(file.openInputStream());
19 x.lineHandler = () { 19 x.lineHandler = () {
20 // The file input stream is known (for now) to have read the whole 20 // The file input stream is known (for now) to have read the whole
21 // file when the data handler is called. 21 // file when the data handler is called.
22 String line = x.readLine(); 22 String line = x.readLine();
23 Expect.equals("Hello Dart", line); 23 Expect.equals("Hello Dart", line);
24 line = x.readLine(); 24 line = x.readLine();
25 Expect.equals("wassup!", line); 25 Expect.equals("wassup!", line);
26 }; 26 };
27 } 27 }
28 28
29 void testInputStreamAsync() { 29 void testInputStreamAsync() {
30 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); 30 String fileName = getFilename("tests/standalone/src/readuntil_test.dat");
31 // File contains "Hello Dart\nwassup!\n" 31 // File contains "Hello Dart\nwassup!\n"
32 var expected = "Hello Dart\nwassup!\n".charCodes(); 32 var expected = "Hello Dart\nwassup!\n".charCodes();
33 InputStream x = (new File(fileName)).openInputStreamSync(); 33 InputStream x = (new File(fileName)).openInputStream();
34 var byteCount = 0; 34 var byteCount = 0;
35 x.dataHandler = () { 35 x.dataHandler = () {
36 Expect.equals(expected[byteCount], x.read(1)[0]); 36 Expect.equals(expected[byteCount], x.read(1)[0]);
37 byteCount++; 37 byteCount++;
38 }; 38 };
39 x.closeHandler = () { 39 x.closeHandler = () {
40 Expect.equals(expected.length, byteCount); 40 Expect.equals(expected.length, byteCount);
41 }; 41 };
42 } 42 }
43 43
44 44
45 void testStringInputStreamAsync(String name, int length) { 45 void testStringInputStreamAsync(String name, int length) {
46 String fileName = getFilename("tests/standalone/src/$name"); 46 String fileName = getFilename("tests/standalone/src/$name");
47 // File contains 10 lines. 47 // File contains 10 lines.
48 File file = new File(fileName); 48 File file = new File(fileName);
49 Expect.equals(length, file.openSync().lengthSync()); 49 Expect.equals(length, file.openSync().lengthSync());
50 StringInputStream x = new StringInputStream(file.openInputStreamSync()); 50 StringInputStream x = new StringInputStream(file.openInputStream());
51 int lineCount = 0; 51 int lineCount = 0;
52 x.lineHandler = () { 52 x.lineHandler = () {
53 var line = x.readLine(); 53 var line = x.readLine();
54 lineCount++; 54 lineCount++;
55 Expect.isTrue(lineCount <= 10); 55 Expect.isTrue(lineCount <= 10);
56 if (line[0] != "#") { 56 if (line[0] != "#") {
57 Expect.equals("Line $lineCount", line); 57 Expect.equals("Line $lineCount", line);
58 } 58 }
59 }; 59 };
60 x.closeHandler = () { 60 x.closeHandler = () {
61 Expect.equals(10, lineCount); 61 Expect.equals(10, lineCount);
62 }; 62 };
63 } 63 }
64 64
65 65
66 void testChunkedInputStream() { 66 void testChunkedInputStream() {
67 // Force the test to timeout if it does not finish. 67 // Force the test to timeout if it does not finish.
68 ReceivePort done = new ReceivePort.singleShot(); 68 ReceivePort done = new ReceivePort.singleShot();
69 done.receive((message, replyTo) {}); 69 done.receive((message, replyTo) {});
70 70
71 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); 71 String fileName = getFilename("tests/standalone/src/readuntil_test.dat");
72 // File contains 19 bytes ("Hello Dart\nwassup!") 72 // File contains 19 bytes ("Hello Dart\nwassup!")
73 File file = new File(fileName); 73 File file = new File(fileName);
74 ChunkedInputStream x = new ChunkedInputStream(file.openInputStreamSync()); 74 ChunkedInputStream x = new ChunkedInputStream(file.openInputStream());
75 x.chunkSize = 9; 75 x.chunkSize = 9;
76 x.dataHandler = () { 76 x.dataHandler = () {
77 List<int> chunk = x.read(); 77 List<int> chunk = x.read();
78 Expect.equals(9, chunk.length); 78 Expect.equals(9, chunk.length);
79 x.chunkSize = 5; 79 x.chunkSize = 5;
80 x.dataHandler = () { 80 x.dataHandler = () {
81 chunk = x.read(); 81 chunk = x.read();
82 Expect.equals(5, chunk.length); 82 Expect.equals(5, chunk.length);
83 x.dataHandler = () { 83 x.dataHandler = () {
84 chunk = x.read(); 84 chunk = x.read();
85 Expect.equals(5, chunk.length); 85 Expect.equals(5, chunk.length);
86 chunk = x.read(); 86 chunk = x.read();
87 Expect.equals(null, chunk); 87 Expect.equals(null, chunk);
88 done.toSendPort().send(null); 88 done.toSendPort().send(null);
89 }; 89 };
90 }; 90 };
91 }; 91 };
92 } 92 }
93 93
94 94
95 void testOpenInputStreamAsync() {
96 // Create a port for waiting on the final result of this test.
97 ReceivePort done = new ReceivePort.singleShot();
98 done.receive((message, replyTo) {});
99
100 // Test using the asynchronous way of opening an input stream.
101 String fileName = getFilename("tests/standalone/src/readuntil_test.dat");
102 File file = new File(fileName);
103 file.exists();
104 file.existsHandler = (exists) {
105 if (exists) {
106 file.openInputStream();
107 } else {
108 Expect.fail("Test file not found");
109 }
110 };
111 file.inputStreamHandler = (InputStream stream) {
112 done.toSendPort().send("Got an InputStream");
113 };
114 file.errorHandler = (String error) {
115 Expect.fail("Error $error");
116 };
117 }
118
119
120 main() { 95 main() {
121 testStringInputStreamSync(); 96 testStringInputStreamSync();
122 testInputStreamAsync(); 97 testInputStreamAsync();
123 // Check the length of these files as both are text files where one 98 // Check the length of these files as both are text files where one
124 // is without a terminating line separator which can easily be added 99 // is without a terminating line separator which can easily be added
125 // back if accidentally opened in a text editor. 100 // back if accidentally opened in a text editor.
126 testStringInputStreamAsync("readline_test1.dat", 111); 101 testStringInputStreamAsync("readline_test1.dat", 111);
127 testStringInputStreamAsync("readline_test2.dat", 114); 102 testStringInputStreamAsync("readline_test2.dat", 114);
128 testChunkedInputStream(); 103 testChunkedInputStream();
129 testOpenInputStreamAsync();
130 } 104 }
OLDNEW
« no previous file with comments | « samples/actors/samples/sssp/sssp-util.dart ('k') | tests/standalone/src/FileOutputStreamTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698