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 #import("dart:io"); | 5 #import("dart:io"); |
6 | 6 |
7 void testUtf8() { | 7 void testUtf8() { |
8 List<int> data = [0x01, | 8 List<int> data = [0x01, |
9 0x7f, | 9 0x7f, |
10 0xc2, 0x80, | 10 0xc2, 0x80, |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 void streamClosed() { | 93 void streamClosed() { |
94 Expect.equals(true, stream.closed); | 94 Expect.equals(true, stream.closed); |
95 Expect.equals(2, stage); | 95 Expect.equals(2, stage); |
96 } | 96 } |
97 | 97 |
98 stream.onData = stringData; | 98 stream.onData = stringData; |
99 stream.onClosed = streamClosed; | 99 stream.onClosed = streamClosed; |
100 s.write("Line".charCodes()); | 100 s.write("Line".charCodes); |
101 } | 101 } |
102 | 102 |
103 void testReadLine2() { | 103 void testReadLine2() { |
104 ListInputStream s = new ListInputStream(); | 104 ListInputStream s = new ListInputStream(); |
105 StringInputStream stream = new StringInputStream(s); | 105 StringInputStream stream = new StringInputStream(s); |
106 var stage = 0; | 106 var stage = 0; |
107 | 107 |
108 void stringData() { | 108 void stringData() { |
109 var line; | 109 var line; |
110 if (stage == 0) { | 110 if (stage == 0) { |
111 Expect.equals(21, stream.available()); | 111 Expect.equals(21, stream.available()); |
112 line = stream.readLine(); | 112 line = stream.readLine(); |
113 Expect.equals("Line1", line); | 113 Expect.equals("Line1", line); |
114 Expect.equals(15, stream.available()); | 114 Expect.equals(15, stream.available()); |
115 line = stream.readLine(); | 115 line = stream.readLine(); |
116 Expect.equals("Line2", line); | 116 Expect.equals("Line2", line); |
117 Expect.equals(8, stream.available()); | 117 Expect.equals(8, stream.available()); |
118 line = stream.readLine(); | 118 line = stream.readLine(); |
119 Expect.equals("Line3", line); | 119 Expect.equals("Line3", line); |
120 line = stream.readLine(); | 120 line = stream.readLine(); |
121 Expect.equals(2, stream.available()); | 121 Expect.equals(2, stream.available()); |
122 Expect.equals(null, line); | 122 Expect.equals(null, line); |
123 stage++; | 123 stage++; |
124 s.write("ne4\n".charCodes()); | 124 s.write("ne4\n".charCodes); |
125 } else if (stage == 1) { | 125 } else if (stage == 1) { |
126 Expect.equals(6, stream.available()); | 126 Expect.equals(6, stream.available()); |
127 line = stream.readLine(); | 127 line = stream.readLine(); |
128 Expect.equals("Line4", line); | 128 Expect.equals("Line4", line); |
129 Expect.equals(0, stream.available()); | 129 Expect.equals(0, stream.available()); |
130 line = stream.readLine(); | 130 line = stream.readLine(); |
131 Expect.equals(null, line); | 131 Expect.equals(null, line); |
132 stage++; | 132 stage++; |
133 s.write("\n\n\r\n\r\n\r\r".charCodes()); | 133 s.write("\n\n\r\n\r\n\r\r".charCodes); |
134 } else if (stage == 2) { | 134 } else if (stage == 2) { |
135 // Expect 5 empty lines. As long as the stream is not closed the | 135 // Expect 5 empty lines. As long as the stream is not closed the |
136 // final \r cannot be interpreted as a end of line. | 136 // final \r cannot be interpreted as a end of line. |
137 Expect.equals(8, stream.available()); | 137 Expect.equals(8, stream.available()); |
138 for (int i = 0; i < 5; i++) { | 138 for (int i = 0; i < 5; i++) { |
139 line = stream.readLine(); | 139 line = stream.readLine(); |
140 Expect.equals("", line); | 140 Expect.equals("", line); |
141 } | 141 } |
142 Expect.equals(1, stream.available()); | 142 Expect.equals(1, stream.available()); |
143 line = stream.readLine(); | 143 line = stream.readLine(); |
(...skipping 11 matching lines...) Expand all Loading... |
155 } | 155 } |
156 } | 156 } |
157 | 157 |
158 void streamClosed() { | 158 void streamClosed() { |
159 Expect.equals(4, stage); | 159 Expect.equals(4, stage); |
160 Expect.equals(true, stream.closed); | 160 Expect.equals(true, stream.closed); |
161 } | 161 } |
162 | 162 |
163 stream.onLine = stringData; | 163 stream.onLine = stringData; |
164 stream.onClosed = streamClosed; | 164 stream.onClosed = streamClosed; |
165 s.write("Line1\nLine2\r\nLine3\rLi".charCodes()); | 165 s.write("Line1\nLine2\r\nLine3\rLi".charCodes); |
166 } | 166 } |
167 | 167 |
168 void testReadChunks() { | 168 void testReadChunks() { |
169 ListInputStream s = new ListInputStream(); | 169 ListInputStream s = new ListInputStream(); |
170 StringInputStream stream = new StringInputStream(s); | 170 StringInputStream stream = new StringInputStream(s); |
171 | 171 |
172 void stringData() { | 172 void stringData() { |
173 var data; | 173 var data; |
174 Expect.equals(8, stream.available()); | 174 Expect.equals(8, stream.available()); |
175 data = stream.read(1); | 175 data = stream.read(1); |
(...skipping 15 matching lines...) Expand all Loading... |
191 Expect.equals(null, data); | 191 Expect.equals(null, data); |
192 Expect.equals(0, stream.available()); | 192 Expect.equals(0, stream.available()); |
193 data = stream.read(3); | 193 data = stream.read(3); |
194 Expect.equals(null, data); | 194 Expect.equals(null, data); |
195 Expect.equals(0, stream.available()); | 195 Expect.equals(0, stream.available()); |
196 data = stream.read(); | 196 data = stream.read(); |
197 Expect.equals(null, data); | 197 Expect.equals(null, data); |
198 Expect.equals(0, stream.available()); | 198 Expect.equals(0, stream.available()); |
199 } | 199 } |
200 | 200 |
201 s.write("ABCD1234".charCodes()); | 201 s.write("ABCD1234".charCodes); |
202 stream.onData = stringData; | 202 stream.onData = stringData; |
203 } | 203 } |
204 | 204 |
205 void testReadMixed() { | 205 void testReadMixed() { |
206 ListInputStream s = new ListInputStream(); | 206 ListInputStream s = new ListInputStream(); |
207 StringInputStream stream = new StringInputStream(s); | 207 StringInputStream stream = new StringInputStream(s); |
208 var stage = 0; | 208 var stage = 0; |
209 | 209 |
210 void stringData() { | 210 void stringData() { |
211 var data; | 211 var data; |
212 if (stage == 0) { | 212 if (stage == 0) { |
213 Expect.equals(11, stream.available()); | 213 Expect.equals(11, stream.available()); |
214 data = stream.read(2); | 214 data = stream.read(2); |
215 Expect.equals("A\r", data); | 215 Expect.equals("A\r", data); |
216 Expect.equals(9, stream.available()); | 216 Expect.equals(9, stream.available()); |
217 data = stream.readLine(); | 217 data = stream.readLine(); |
218 Expect.equals("", data); | 218 Expect.equals("", data); |
219 Expect.equals(8, stream.available()); | 219 Expect.equals(8, stream.available()); |
220 data = stream.read(4); | 220 data = stream.read(4); |
221 Expect.equals("BCD\n", data); | 221 Expect.equals("BCD\n", data); |
222 Expect.equals(4, stream.available()); | 222 Expect.equals(4, stream.available()); |
223 data = stream.readLine(); | 223 data = stream.readLine(); |
224 Expect.equals(null, data); | 224 Expect.equals(null, data); |
225 data = stream.read(4); | 225 data = stream.read(4); |
226 Expect.equals("1234", data); | 226 Expect.equals("1234", data); |
227 s.write("A\r\nBCD\n1234".charCodes()); | 227 s.write("A\r\nBCD\n1234".charCodes); |
228 stage++; | 228 stage++; |
229 } else if (stage == 1) { | 229 } else if (stage == 1) { |
230 Expect.equals(11, stream.available()); | 230 Expect.equals(11, stream.available()); |
231 data = stream.read(1); | 231 data = stream.read(1); |
232 Expect.equals("A", data); | 232 Expect.equals("A", data); |
233 Expect.equals(10, stream.available()); | 233 Expect.equals(10, stream.available()); |
234 data = stream.read(1); | 234 data = stream.read(1); |
235 Expect.equals("\r", data); | 235 Expect.equals("\r", data); |
236 Expect.equals(9, stream.available()); | 236 Expect.equals(9, stream.available()); |
237 data = stream.read(1); | 237 data = stream.read(1); |
238 Expect.equals("\n", data); | 238 Expect.equals("\n", data); |
239 Expect.equals(8, stream.available()); | 239 Expect.equals(8, stream.available()); |
240 data = stream.readLine(); | 240 data = stream.readLine(); |
241 Expect.equals("BCD", data); | 241 Expect.equals("BCD", data); |
242 data = stream.readLine(); | 242 data = stream.readLine(); |
243 Expect.equals(null, data); | 243 Expect.equals(null, data); |
244 data = stream.read(4); | 244 data = stream.read(4); |
245 Expect.equals("1234", data); | 245 Expect.equals("1234", data); |
246 s.write("A\r\nBCD\n1234".charCodes()); | 246 s.write("A\r\nBCD\n1234".charCodes); |
247 stage++; | 247 stage++; |
248 } else if (stage == 2) { | 248 } else if (stage == 2) { |
249 Expect.equals(11, stream.available()); | 249 Expect.equals(11, stream.available()); |
250 data = stream.read(7); | 250 data = stream.read(7); |
251 Expect.equals("A\r\nBCD\n", data); | 251 Expect.equals("A\r\nBCD\n", data); |
252 Expect.equals(4, stream.available()); | 252 Expect.equals(4, stream.available()); |
253 data = stream.readLine(); | 253 data = stream.readLine(); |
254 Expect.equals(null, data); | 254 Expect.equals(null, data); |
255 data = stream.read(); | 255 data = stream.read(); |
256 Expect.equals("1234", data); | 256 Expect.equals("1234", data); |
257 stage++; | 257 stage++; |
258 s.markEndOfStream(); | 258 s.markEndOfStream(); |
259 } | 259 } |
260 } | 260 } |
261 | 261 |
262 void streamClosed() { | 262 void streamClosed() { |
263 Expect.equals(3, stage); | 263 Expect.equals(3, stage); |
264 Expect.equals(true, stream.closed); | 264 Expect.equals(true, stream.closed); |
265 } | 265 } |
266 | 266 |
267 s.write("A\r\nBCD\n1234".charCodes()); | 267 s.write("A\r\nBCD\n1234".charCodes); |
268 stream.onData = stringData; | 268 stream.onData = stringData; |
269 stream.onClosed = streamClosed; | 269 stream.onClosed = streamClosed; |
270 } | 270 } |
271 | 271 |
272 class TestException implements Exception { | 272 class TestException implements Exception { |
273 TestException(); | 273 TestException(); |
274 } | 274 } |
275 | 275 |
276 class ErrorInputStream implements InputStream { | 276 class ErrorInputStream implements InputStream { |
277 ErrorInputStream(); | 277 ErrorInputStream(); |
(...skipping 23 matching lines...) Expand all Loading... |
301 main() { | 301 main() { |
302 testUtf8(); | 302 testUtf8(); |
303 testLatin1(); | 303 testLatin1(); |
304 testAscii(); | 304 testAscii(); |
305 testReadLine1(); | 305 testReadLine1(); |
306 testReadLine2(); | 306 testReadLine2(); |
307 testReadChunks(); | 307 testReadChunks(); |
308 testReadMixed(); | 308 testReadMixed(); |
309 testErrorHandler(); | 309 testErrorHandler(); |
310 } | 310 } |
OLD | NEW |