| 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 | |
| 7 void testUtf8() { | |
| 8 List<int> data = [0x01, | |
| 9 0x7f, | |
| 10 0xc2, 0x80, | |
| 11 0xdf, 0xbf, | |
| 12 0xe0, 0xa0, 0x80, | |
| 13 0xef, 0xbf, 0xbf]; | |
| 14 ListInputStream s = new ListInputStream(); | |
| 15 s.write(data); | |
| 16 s.markEndOfStream(); | |
| 17 StringInputStream stream = new StringInputStream(s); | |
| 18 void stringData() { | |
| 19 String s = stream.read(); | |
| 20 Expect.equals(6, s.length); | |
| 21 Expect.equals(new String.fromCharCodes([0x01]), s[0]); | |
| 22 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); | |
| 23 Expect.equals(new String.fromCharCodes([0x80]), s[2]); | |
| 24 Expect.equals(new String.fromCharCodes([0x7ff]), s[3]); | |
| 25 Expect.equals(new String.fromCharCodes([0x800]), s[4]); | |
| 26 Expect.equals(new String.fromCharCodes([0xffff]), s[5]); | |
| 27 } | |
| 28 stream.onData = stringData; | |
| 29 } | |
| 30 | |
| 31 void testLatin1() { | |
| 32 List<int> data = [0x01, | |
| 33 0x7f, | |
| 34 0x44, 0x61, 0x72, 0x74, | |
| 35 0x80, | |
| 36 0xff]; | |
| 37 ListInputStream s = new ListInputStream(); | |
| 38 s.write(data); | |
| 39 s.markEndOfStream(); | |
| 40 StringInputStream stream = new StringInputStream(s, Encoding.ISO_8859_1); | |
| 41 void stringData() { | |
| 42 String s = stream.read(); | |
| 43 Expect.equals(8, s.length); | |
| 44 Expect.equals(new String.fromCharCodes([0x01]), s[0]); | |
| 45 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); | |
| 46 Expect.equals("Dart", s.substring(2, 6)); | |
| 47 Expect.equals(new String.fromCharCodes([0x80]), s[6]); | |
| 48 Expect.equals(new String.fromCharCodes([0xff]), s[7]); | |
| 49 } | |
| 50 stream.onData = stringData; | |
| 51 } | |
| 52 | |
| 53 void testAscii() { | |
| 54 List<int> data = [0x01, | |
| 55 0x44, 0x61, 0x72, 0x74, | |
| 56 0x7f]; | |
| 57 ListInputStream s = new ListInputStream(); | |
| 58 s.write(data); | |
| 59 s.markEndOfStream(); | |
| 60 StringInputStream stream = | |
| 61 new StringInputStream(s, Encoding.ASCII); | |
| 62 void stringData() { | |
| 63 String s = stream.read(); | |
| 64 Expect.equals(6, s.length); | |
| 65 Expect.equals(new String.fromCharCodes([0x01]), s[0]); | |
| 66 Expect.equals("Dart", s.substring(1, 5)); | |
| 67 Expect.equals(new String.fromCharCodes([0x7f]), s[5]); | |
| 68 } | |
| 69 stream.onData = stringData; | |
| 70 } | |
| 71 | |
| 72 void testReadLine1() { | |
| 73 ListInputStream s = new ListInputStream(); | |
| 74 StringInputStream stream = new StringInputStream(s); | |
| 75 var stage = 0; | |
| 76 | |
| 77 void stringData() { | |
| 78 var line; | |
| 79 if (stage == 0) { | |
| 80 line = stream.readLine(); | |
| 81 Expect.equals(null, line); | |
| 82 stage++; | |
| 83 s.markEndOfStream(); | |
| 84 } else if (stage == 1) { | |
| 85 line = stream.readLine(); | |
| 86 Expect.equals("Line", line); | |
| 87 line = stream.readLine(); | |
| 88 Expect.equals(null, line); | |
| 89 stage++; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void streamClosed() { | |
| 94 Expect.equals(true, stream.closed); | |
| 95 Expect.equals(2, stage); | |
| 96 } | |
| 97 | |
| 98 stream.onData = stringData; | |
| 99 stream.onClosed = streamClosed; | |
| 100 s.write("Line".charCodes()); | |
| 101 } | |
| 102 | |
| 103 void testReadLine2() { | |
| 104 ListInputStream s = new ListInputStream(); | |
| 105 StringInputStream stream = new StringInputStream(s); | |
| 106 var stage = 0; | |
| 107 | |
| 108 void stringData() { | |
| 109 var line; | |
| 110 if (stage == 0) { | |
| 111 Expect.equals(21, stream.available()); | |
| 112 line = stream.readLine(); | |
| 113 Expect.equals("Line1", line); | |
| 114 Expect.equals(15, stream.available()); | |
| 115 line = stream.readLine(); | |
| 116 Expect.equals("Line2", line); | |
| 117 Expect.equals(8, stream.available()); | |
| 118 line = stream.readLine(); | |
| 119 Expect.equals("Line3", line); | |
| 120 line = stream.readLine(); | |
| 121 Expect.equals(2, stream.available()); | |
| 122 Expect.equals(null, line); | |
| 123 stage++; | |
| 124 s.write("ne4\n".charCodes()); | |
| 125 } else if (stage == 1) { | |
| 126 Expect.equals(6, stream.available()); | |
| 127 line = stream.readLine(); | |
| 128 Expect.equals("Line4", line); | |
| 129 Expect.equals(0, stream.available()); | |
| 130 line = stream.readLine(); | |
| 131 Expect.equals(null, line); | |
| 132 stage++; | |
| 133 s.write("\n\n\r\n\r\n\r\r".charCodes()); | |
| 134 } else if (stage == 2) { | |
| 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. | |
| 137 Expect.equals(8, stream.available()); | |
| 138 for (int i = 0; i < 5; i++) { | |
| 139 line = stream.readLine(); | |
| 140 Expect.equals("", line); | |
| 141 } | |
| 142 Expect.equals(1, stream.available()); | |
| 143 line = stream.readLine(); | |
| 144 Expect.equals(null, line); | |
| 145 stage++; | |
| 146 s.markEndOfStream(); | |
| 147 } else if (stage == 3) { | |
| 148 // The final \r can now be interpreted as an end of line. | |
| 149 Expect.equals(1, stream.available()); | |
| 150 line = stream.readLine(); | |
| 151 Expect.equals("", line); | |
| 152 line = stream.readLine(); | |
| 153 Expect.equals(null, line); | |
| 154 stage++; | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 void streamClosed() { | |
| 159 Expect.equals(4, stage); | |
| 160 Expect.equals(true, stream.closed); | |
| 161 } | |
| 162 | |
| 163 stream.onLine = stringData; | |
| 164 stream.onClosed = streamClosed; | |
| 165 s.write("Line1\nLine2\r\nLine3\rLi".charCodes()); | |
| 166 } | |
| 167 | |
| 168 class TestException implements Exception { | |
| 169 TestException(); | |
| 170 } | |
| 171 | |
| 172 class ErrorInputStream implements InputStream { | |
| 173 ErrorInputStream(); | |
| 174 List<int> read([int len]) => null; | |
| 175 int readInto(List<int> buffer, [int offset, int len]) => 0; | |
| 176 int available() => 0; | |
| 177 void pipe(OutputStream output, [bool close]){ } | |
| 178 void close() { } | |
| 179 bool get closed() => true; | |
| 180 void set onData(void callback()) { } | |
| 181 void set onClosed(void callback()) { } | |
| 182 void set onError(void callback(Exception e)) { | |
| 183 callback(new TestException()); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 testErrorHandler() { | |
| 188 var errors = 0; | |
| 189 var stream = new StringInputStream(new ErrorInputStream()); | |
| 190 stream.onError = (e) { | |
| 191 errors++; | |
| 192 Expect.isTrue(e is TestException); | |
| 193 }; | |
| 194 Expect.equals(1, errors); | |
| 195 } | |
| 196 | |
| 197 main() { | |
| 198 testUtf8(); | |
| 199 testLatin1(); | |
| 200 testAscii(); | |
| 201 testReadLine1(); | |
| 202 testReadLine2(); | |
| 203 testErrorHandler(); | |
| 204 } | |
| OLD | NEW |