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