| 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 |  | 
| 5 import "dart:io"; |  | 
| 6 import "dart:isolate"; |  | 
| 7 |  | 
| 8 void testUtf8() { |  | 
| 9   List<int> data = [0x01, |  | 
| 10                     0x7f, |  | 
| 11                     0xc2, 0x80, |  | 
| 12                     0xdf, 0xbf, |  | 
| 13                     0xe0, 0xa0, 0x80, |  | 
| 14                     0xef, 0xbf, 0xbf, |  | 
| 15                     0xf0, 0x9d, 0x84, 0x9e]; |  | 
| 16   ListInputStream s = new ListInputStream(); |  | 
| 17   s.write(data); |  | 
| 18   s.markEndOfStream(); |  | 
| 19   StringInputStream stream = new StringInputStream(s); |  | 
| 20   void stringData() { |  | 
| 21     String s = stream.read(); |  | 
| 22     Expect.equals(8, s.length); |  | 
| 23     Expect.equals(new String.fromCharCodes([0x01]), s[0]); |  | 
| 24     Expect.equals(new String.fromCharCodes([0x7f]), s[1]); |  | 
| 25     Expect.equals(new String.fromCharCodes([0x80]), s[2]); |  | 
| 26     Expect.equals(new String.fromCharCodes([0x7ff]), s[3]); |  | 
| 27     Expect.equals(new String.fromCharCodes([0x800]), s[4]); |  | 
| 28     Expect.equals(new String.fromCharCodes([0xffff]), s[5]); |  | 
| 29     Expect.equals(new String.fromCharCodes([0xffff]), s[5]); |  | 
| 30 |  | 
| 31     // Surrogate pair for U+1D11E. |  | 
| 32     Expect.equals(new String.fromCharCodes([0xd834, 0xdd1e]), |  | 
| 33                   s.substring(6, 8)); |  | 
| 34   } |  | 
| 35   stream.onData = stringData; |  | 
| 36 } |  | 
| 37 |  | 
| 38 void testLatin1() { |  | 
| 39   List<int> data = [0x01, |  | 
| 40                     0x7f, |  | 
| 41                     0x44, 0x61, 0x72, 0x74, |  | 
| 42                     0x80, |  | 
| 43                     0xff]; |  | 
| 44   ListInputStream s = new ListInputStream(); |  | 
| 45   s.write(data); |  | 
| 46   s.markEndOfStream(); |  | 
| 47   StringInputStream stream = new StringInputStream(s, Encoding.ISO_8859_1); |  | 
| 48   void stringData() { |  | 
| 49     String s = stream.read(); |  | 
| 50     Expect.equals(8, s.length); |  | 
| 51     Expect.equals(new String.fromCharCodes([0x01]), s[0]); |  | 
| 52     Expect.equals(new String.fromCharCodes([0x7f]), s[1]); |  | 
| 53     Expect.equals("Dart", s.substring(2, 6)); |  | 
| 54     Expect.equals(new String.fromCharCodes([0x80]), s[6]); |  | 
| 55     Expect.equals(new String.fromCharCodes([0xff]), s[7]); |  | 
| 56   } |  | 
| 57   stream.onData = stringData; |  | 
| 58 } |  | 
| 59 |  | 
| 60 void testAscii() { |  | 
| 61   List<int> data = [0x01, |  | 
| 62                     0x44, 0x61, 0x72, 0x74, |  | 
| 63                     0x7f]; |  | 
| 64   ListInputStream s = new ListInputStream(); |  | 
| 65   s.write(data); |  | 
| 66   s.markEndOfStream(); |  | 
| 67   StringInputStream stream = |  | 
| 68       new StringInputStream(s, Encoding.ASCII); |  | 
| 69   void stringData() { |  | 
| 70     String s = stream.read(); |  | 
| 71     Expect.equals(6, s.length); |  | 
| 72     Expect.equals(new String.fromCharCodes([0x01]), s[0]); |  | 
| 73     Expect.equals("Dart", s.substring(1, 5)); |  | 
| 74     Expect.equals(new String.fromCharCodes([0x7f]), s[5]); |  | 
| 75   } |  | 
| 76   stream.onData = stringData; |  | 
| 77 } |  | 
| 78 |  | 
| 79 void testReadLine1() { |  | 
| 80   ListInputStream s = new ListInputStream(); |  | 
| 81   StringInputStream stream = new StringInputStream(s); |  | 
| 82   var stage = 0; |  | 
| 83 |  | 
| 84   void stringData() { |  | 
| 85     var line; |  | 
| 86     if (stage == 0) { |  | 
| 87       line = stream.readLine(); |  | 
| 88       Expect.equals(null, line); |  | 
| 89       stage++; |  | 
| 90       s.markEndOfStream(); |  | 
| 91     } else if (stage == 1) { |  | 
| 92       line = stream.readLine(); |  | 
| 93       Expect.equals("Line", line); |  | 
| 94       line = stream.readLine(); |  | 
| 95       Expect.equals(null, line); |  | 
| 96       stage++; |  | 
| 97     } |  | 
| 98   } |  | 
| 99 |  | 
| 100   void streamClosed() { |  | 
| 101     Expect.equals(true, stream.closed); |  | 
| 102     Expect.equals(2, stage); |  | 
| 103   } |  | 
| 104 |  | 
| 105   stream.onData = stringData; |  | 
| 106   stream.onClosed = streamClosed; |  | 
| 107   s.write("Line".charCodes); |  | 
| 108 } |  | 
| 109 |  | 
| 110 void testReadLine2() { |  | 
| 111   ListInputStream s = new ListInputStream(); |  | 
| 112   StringInputStream stream = new StringInputStream(s); |  | 
| 113   var stage = 0; |  | 
| 114 |  | 
| 115   void stringData() { |  | 
| 116     var line; |  | 
| 117     if (stage == 0) { |  | 
| 118       Expect.equals(21, stream.available()); |  | 
| 119       line = stream.readLine(); |  | 
| 120       Expect.equals("Line1", line); |  | 
| 121       Expect.equals(15, stream.available()); |  | 
| 122       line = stream.readLine(); |  | 
| 123       Expect.equals("Line2", line); |  | 
| 124       Expect.equals(8, stream.available()); |  | 
| 125       line = stream.readLine(); |  | 
| 126       Expect.equals("Line3", line); |  | 
| 127       line = stream.readLine(); |  | 
| 128       Expect.equals(2, stream.available()); |  | 
| 129       Expect.equals(null, line); |  | 
| 130       stage++; |  | 
| 131       s.write("ne4\n".charCodes); |  | 
| 132     } else if (stage == 1) { |  | 
| 133       Expect.equals(6, stream.available()); |  | 
| 134       line = stream.readLine(); |  | 
| 135       Expect.equals("Line4", line); |  | 
| 136       Expect.equals(0, stream.available()); |  | 
| 137       line = stream.readLine(); |  | 
| 138       Expect.equals(null, line); |  | 
| 139       stage++; |  | 
| 140       s.write("\n\n\r\n\r\n\r\r".charCodes); |  | 
| 141     } else if (stage == 2) { |  | 
| 142       // Expect 5 empty lines. As long as the stream is not closed the |  | 
| 143       // final \r cannot be interpreted as a end of line. |  | 
| 144       Expect.equals(8, stream.available()); |  | 
| 145       for (int i = 0; i < 5; i++) { |  | 
| 146         line = stream.readLine(); |  | 
| 147         Expect.equals("", line); |  | 
| 148       } |  | 
| 149       Expect.equals(1, stream.available()); |  | 
| 150       line = stream.readLine(); |  | 
| 151       Expect.equals(null, line); |  | 
| 152       stage++; |  | 
| 153       s.markEndOfStream(); |  | 
| 154     } else if (stage == 3) { |  | 
| 155       // The final \r can now be interpreted as an end of line. |  | 
| 156       Expect.equals(1, stream.available()); |  | 
| 157       line = stream.readLine(); |  | 
| 158       Expect.equals("", line); |  | 
| 159       line = stream.readLine(); |  | 
| 160       Expect.equals(null, line); |  | 
| 161       stage++; |  | 
| 162     } |  | 
| 163   } |  | 
| 164 |  | 
| 165   void streamClosed() { |  | 
| 166     Expect.equals(4, stage); |  | 
| 167     Expect.equals(true, stream.closed); |  | 
| 168   } |  | 
| 169 |  | 
| 170   stream.onLine = stringData; |  | 
| 171   stream.onClosed = streamClosed; |  | 
| 172   s.write("Line1\nLine2\r\nLine3\rLi".charCodes); |  | 
| 173 } |  | 
| 174 |  | 
| 175 void testReadChunks() { |  | 
| 176   ListInputStream s = new ListInputStream(); |  | 
| 177   StringInputStream stream = new StringInputStream(s); |  | 
| 178 |  | 
| 179   void stringData() { |  | 
| 180     var data; |  | 
| 181     Expect.equals(8, stream.available()); |  | 
| 182     data = stream.read(1); |  | 
| 183     Expect.equals("A", data); |  | 
| 184     Expect.equals(7, stream.available()); |  | 
| 185     data = stream.read(2); |  | 
| 186     Expect.equals("BC", data); |  | 
| 187     Expect.equals(5, stream.available()); |  | 
| 188     data = stream.read(3); |  | 
| 189     Expect.equals("D12", data); |  | 
| 190     Expect.equals(2, stream.available()); |  | 
| 191     data = stream.read(); |  | 
| 192     Expect.equals("34", data); |  | 
| 193     Expect.equals(0, stream.available()); |  | 
| 194     data = stream.read(1); |  | 
| 195     Expect.equals(null, data); |  | 
| 196     Expect.equals(0, stream.available()); |  | 
| 197     data = stream.read(2); |  | 
| 198     Expect.equals(null, data); |  | 
| 199     Expect.equals(0, stream.available()); |  | 
| 200     data = stream.read(3); |  | 
| 201     Expect.equals(null, data); |  | 
| 202     Expect.equals(0, stream.available()); |  | 
| 203     data = stream.read(); |  | 
| 204     Expect.equals(null, data); |  | 
| 205     Expect.equals(0, stream.available()); |  | 
| 206   } |  | 
| 207 |  | 
| 208   s.write("ABCD1234".charCodes); |  | 
| 209   stream.onData = stringData; |  | 
| 210 } |  | 
| 211 |  | 
| 212 void testReadMixed() { |  | 
| 213   ListInputStream s = new ListInputStream(); |  | 
| 214   StringInputStream stream = new StringInputStream(s); |  | 
| 215   var stage = 0; |  | 
| 216 |  | 
| 217   void stringData() { |  | 
| 218     var data; |  | 
| 219     if (stage == 0) { |  | 
| 220       Expect.equals(11, stream.available()); |  | 
| 221       data = stream.read(2); |  | 
| 222       Expect.equals("A\r", data); |  | 
| 223       Expect.equals(9, stream.available()); |  | 
| 224       data = stream.readLine(); |  | 
| 225       Expect.equals("", data); |  | 
| 226       Expect.equals(8, stream.available()); |  | 
| 227       data = stream.read(4); |  | 
| 228       Expect.equals("BCD\n", data); |  | 
| 229       Expect.equals(4, stream.available()); |  | 
| 230       data = stream.readLine(); |  | 
| 231       Expect.equals(null, data); |  | 
| 232       data = stream.read(4); |  | 
| 233       Expect.equals("1234", data); |  | 
| 234       s.write("A\r\nBCD\n1234".charCodes); |  | 
| 235       stage++; |  | 
| 236     } else if (stage == 1) { |  | 
| 237       Expect.equals(11, stream.available()); |  | 
| 238       data = stream.read(1); |  | 
| 239       Expect.equals("A", data); |  | 
| 240       Expect.equals(10, stream.available()); |  | 
| 241       data = stream.read(1); |  | 
| 242       Expect.equals("\r", data); |  | 
| 243       Expect.equals(9, stream.available()); |  | 
| 244       data = stream.read(1); |  | 
| 245       Expect.equals("\n", data); |  | 
| 246       Expect.equals(8, stream.available()); |  | 
| 247       data = stream.readLine(); |  | 
| 248       Expect.equals("BCD", data); |  | 
| 249       data = stream.readLine(); |  | 
| 250       Expect.equals(null, data); |  | 
| 251       data = stream.read(4); |  | 
| 252       Expect.equals("1234", data); |  | 
| 253       s.write("A\r\nBCD\n1234".charCodes); |  | 
| 254       stage++; |  | 
| 255     } else if (stage == 2) { |  | 
| 256       Expect.equals(11, stream.available()); |  | 
| 257       data = stream.read(7); |  | 
| 258       Expect.equals("A\r\nBCD\n", data); |  | 
| 259       Expect.equals(4, stream.available()); |  | 
| 260       data = stream.readLine(); |  | 
| 261       Expect.equals(null, data); |  | 
| 262       data = stream.read(); |  | 
| 263       Expect.equals("1234", data); |  | 
| 264       stage++; |  | 
| 265       s.markEndOfStream(); |  | 
| 266     } |  | 
| 267   } |  | 
| 268 |  | 
| 269   void streamClosed() { |  | 
| 270     Expect.equals(3, stage); |  | 
| 271     Expect.equals(true, stream.closed); |  | 
| 272   } |  | 
| 273 |  | 
| 274   s.write("A\r\nBCD\n1234".charCodes); |  | 
| 275   stream.onData = stringData; |  | 
| 276   stream.onClosed = streamClosed; |  | 
| 277 } |  | 
| 278 |  | 
| 279 class TestException implements Exception { |  | 
| 280   TestException(); |  | 
| 281 } |  | 
| 282 |  | 
| 283 class ErrorInputStream implements InputStream { |  | 
| 284   ErrorInputStream(); |  | 
| 285   List<int> read([int len]) => null; |  | 
| 286   int readInto(List<int> buffer, [int offset, int len]) => 0; |  | 
| 287   int available() => 0; |  | 
| 288   void pipe(OutputStream output, {bool close: true}){ } |  | 
| 289   void close() { } |  | 
| 290   bool get closed => true; |  | 
| 291   void set onData(void callback()) { } |  | 
| 292   void set onClosed(void callback()) { } |  | 
| 293   void set onError(void callback(Exception e)) { |  | 
| 294     callback(new TestException()); |  | 
| 295   } |  | 
| 296 } |  | 
| 297 |  | 
| 298 testErrorHandler() { |  | 
| 299   var errors = 0; |  | 
| 300   var stream = new StringInputStream(new ErrorInputStream()); |  | 
| 301   stream.onError = (e) { |  | 
| 302     errors++; |  | 
| 303     Expect.isTrue(e is TestException); |  | 
| 304   }; |  | 
| 305   Expect.equals(1, errors); |  | 
| 306 } |  | 
| 307 |  | 
| 308 testEncodingErrorWithHandler() { |  | 
| 309   var port = new ReceivePort(); |  | 
| 310   var errors = 0; |  | 
| 311   var expected = [206, 187, 120, 46, 32, 120, 10]; |  | 
| 312   ListInputStream input = new ListInputStream(); |  | 
| 313   input.write(expected); |  | 
| 314   var stringStream = new StringInputStream(input, Encoding.ASCII); |  | 
| 315   stringStream.onData = () { |  | 
| 316     Expect.fail("We should not get any data"); |  | 
| 317   }; |  | 
| 318   stringStream.onError = (e) { |  | 
| 319     port.close(); |  | 
| 320     Expect.isTrue(e is Exception); |  | 
| 321   }; |  | 
| 322 } |  | 
| 323 |  | 
| 324 |  | 
| 325 main() { |  | 
| 326   testUtf8(); |  | 
| 327   testLatin1(); |  | 
| 328   testAscii(); |  | 
| 329   testReadLine1(); |  | 
| 330   testReadLine2(); |  | 
| 331   testReadChunks(); |  | 
| 332   testReadMixed(); |  | 
| 333   testErrorHandler(); |  | 
| 334   testEncodingErrorWithHandler(); |  | 
| 335 } |  | 
| OLD | NEW | 
|---|