OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, 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 class ListInputStream implements InputStream { |
| 6 List<int> read() { |
| 7 var result = _data; |
| 8 _data = null; |
| 9 return result; |
| 10 } |
| 11 |
| 12 void set dataHandler(void callback()) { |
| 13 _dataHandler = callback; |
| 14 } |
| 15 |
| 16 void set closeHandler(void callback()) { |
| 17 _closeHandler = callback; |
| 18 } |
| 19 |
| 20 void set errorHandler(void callback(int error)) { |
| 21 } |
| 22 |
| 23 void write(List<int> data) { |
| 24 Expect.equals(null, _data); |
| 25 _data = data; |
| 26 if (_dataHandler != null) { |
| 27 // Data handler is not called through the event loop here. |
| 28 _dataHandler(); |
| 29 } |
| 30 } |
| 31 |
| 32 void close() { |
| 33 // Close handler is not called through the event loop here. |
| 34 if (_closeHandler != null) _closeHandler(); |
| 35 } |
| 36 |
| 37 List<int> _data; |
| 38 var _dataHandler; |
| 39 var _closeHandler; |
| 40 } |
| 41 |
| 42 void testUtf8() { |
| 43 List<int> data = [0x01, |
| 44 0x7f, |
| 45 0xc2, 0x80, |
| 46 0xdf, 0xbf, |
| 47 0xe0, 0xa0, 0x80, |
| 48 0xef, 0xbf, 0xbf]; |
| 49 InputStream s = new ListInputStream(); |
| 50 StringInputStream stream = new StringInputStream(s); |
| 51 void stringData() { |
| 52 String s = stream.read(); |
| 53 Expect.equals(6, s.length); |
| 54 Expect.equals(new String.fromCharCodes([0x01]), s[0]); |
| 55 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); |
| 56 Expect.equals(new String.fromCharCodes([0x80]), s[2]); |
| 57 Expect.equals(new String.fromCharCodes([0x7ff]), s[3]); |
| 58 Expect.equals(new String.fromCharCodes([0x800]), s[4]); |
| 59 Expect.equals(new String.fromCharCodes([0xffff]), s[5]); |
| 60 } |
| 61 stream.dataHandler = stringData; |
| 62 s.write(data); |
| 63 } |
| 64 |
| 65 void testLatin1() { |
| 66 List<int> data = [0x01, |
| 67 0x7f, |
| 68 0x44, 0x61, 0x72, 0x74, |
| 69 0x80, |
| 70 0xff]; |
| 71 InputStream s = new ListInputStream(); |
| 72 StringInputStream stream = new StringInputStream(s, "ISO-8859-1"); |
| 73 void stringData() { |
| 74 String s = stream.read(); |
| 75 Expect.equals(8, s.length); |
| 76 Expect.equals(new String.fromCharCodes([0x01]), s[0]); |
| 77 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); |
| 78 Expect.equals("Dart", s.substring(2, 6)); |
| 79 Expect.equals(new String.fromCharCodes([0x80]), s[6]); |
| 80 Expect.equals(new String.fromCharCodes([0xff]), s[7]); |
| 81 } |
| 82 stream.dataHandler = stringData; |
| 83 s.write(data); |
| 84 } |
| 85 |
| 86 void testAscii() { |
| 87 List<int> data = [0x01, |
| 88 0x44, 0x61, 0x72, 0x74, |
| 89 0x7f]; |
| 90 InputStream s = new ListInputStream(); |
| 91 StringInputStream stream = new StringInputStream(s, "ASCII"); |
| 92 void stringData() { |
| 93 String s = stream.read(); |
| 94 Expect.equals(6, s.length); |
| 95 Expect.equals(new String.fromCharCodes([0x01]), s[0]); |
| 96 Expect.equals("Dart", s.substring(1, 5)); |
| 97 Expect.equals(new String.fromCharCodes([0x7f]), s[5]); |
| 98 } |
| 99 stream.dataHandler = stringData; |
| 100 s.write(data); |
| 101 } |
| 102 |
| 103 void testReadLine1() { |
| 104 InputStream 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 line = stream.readLine(); |
| 112 Expect.equals(null, line); |
| 113 stage++; |
| 114 s.close(); |
| 115 } else if (stage == 1) { |
| 116 line = stream.readLine(); |
| 117 Expect.equals("Line", line); |
| 118 line = stream.readLine(); |
| 119 Expect.equals(null, line); |
| 120 Expect.equals(true, stream.closed); |
| 121 stage++; |
| 122 } |
| 123 } |
| 124 stream.dataHandler = stringData; |
| 125 s.write("Line".charCodes()); |
| 126 Expect.equals(2, stage); |
| 127 } |
| 128 |
| 129 void testReadLine2() { |
| 130 InputStream s = new ListInputStream(); |
| 131 StringInputStream stream = new StringInputStream(s); |
| 132 var stage = 0; |
| 133 |
| 134 void stringData() { |
| 135 var line; |
| 136 if (stage == 0) { |
| 137 line = stream.readLine(); |
| 138 Expect.equals("Line1", line); |
| 139 line = stream.readLine(); |
| 140 Expect.equals("Line2", line); |
| 141 line = stream.readLine(); |
| 142 Expect.equals("Line3", line); |
| 143 line = stream.readLine(); |
| 144 Expect.equals(null, line); |
| 145 stage++; |
| 146 s.write("ne4\n".charCodes()); |
| 147 } else if (stage == 1) { |
| 148 line = stream.readLine(); |
| 149 Expect.equals("Line4", line); |
| 150 line = stream.readLine(); |
| 151 Expect.equals(null, line); |
| 152 stage++; |
| 153 s.write("\n\n\r\n\r\n\r\r".charCodes()); |
| 154 } else if (stage == 2) { |
| 155 // Expect 5 empty lines. As long as the stream is not closed the |
| 156 // final \r cannot be interpreted as a end of line. |
| 157 for (int i = 0; i < 5; i++) { |
| 158 line = stream.readLine(); |
| 159 Expect.equals("", line); |
| 160 } |
| 161 line = stream.readLine(); |
| 162 Expect.equals(null, line); |
| 163 stage++; |
| 164 s.close(); |
| 165 } else if (stage == 3) { |
| 166 // The final \r can not be interpreted as a end of line. |
| 167 line = stream.readLine(); |
| 168 Expect.equals("", line); |
| 169 line = stream.readLine(); |
| 170 Expect.equals(null, line); |
| 171 Expect.equals(true, stream.closed); |
| 172 stage++; |
| 173 } |
| 174 } |
| 175 stream.dataHandler = stringData; |
| 176 s.write("Line1\nLine2\r\nLine3\rLi".charCodes()); |
| 177 Expect.equals(4, stage); |
| 178 } |
| 179 |
| 180 main() { |
| 181 testUtf8(); |
| 182 testLatin1(); |
| 183 testAscii(); |
| 184 testReadLine1(); |
| 185 testReadLine2(); |
| 186 } |
OLD | NEW |