| 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:async"; |
| 6 import "dart:io"; |
| 7 import "dart:isolate"; |
| 8 import "dart:utf"; |
| 9 |
| 10 void testUtf8() { |
| 11 List<int> data = [0x01, |
| 12 0x7f, |
| 13 0xc2, 0x80, |
| 14 0xdf, 0xbf, |
| 15 0xe0, 0xa0, 0x80, |
| 16 0xef, 0xbf, 0xbf, |
| 17 0xf0, 0x9d, 0x84, 0x9e, |
| 18 0x100, -0x1, -0xFF]; |
| 19 var controller = new StreamController(); |
| 20 controller.add(data); |
| 21 controller.close(); |
| 22 var stringStream = controller.stream |
| 23 .transform(new StringDecoder(Encoding.UTF_8)); |
| 24 stringStream.listen( |
| 25 (s) { |
| 26 Expect.equals(11, s.length); |
| 27 Expect.equals(new String.fromCharCodes([0x01]), s[0]); |
| 28 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); |
| 29 Expect.equals(new String.fromCharCodes([0x80]), s[2]); |
| 30 Expect.equals(new String.fromCharCodes([0x7ff]), s[3]); |
| 31 Expect.equals(new String.fromCharCodes([0x800]), s[4]); |
| 32 Expect.equals(new String.fromCharCodes([0xffff]), s[5]); |
| 33 Expect.equals(new String.fromCharCodes([0xffff]), s[5]); |
| 34 |
| 35 // Surrogate pair for U+1D11E. |
| 36 Expect.equals(new String.fromCharCodes([0xd834, 0xdd1e]), |
| 37 s.substring(6, 8)); |
| 38 |
| 39 Expect.equals(new String.fromCharCodes( |
| 40 [UNICODE_REPLACEMENT_CHARACTER_CODEPOINT, |
| 41 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT, |
| 42 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), |
| 43 s.substring(8, 11)); |
| 44 }); |
| 45 } |
| 46 |
| 47 void testLatin1() { |
| 48 List<int> data = [0x01, |
| 49 0x7f, |
| 50 0x44, 0x61, 0x72, 0x74, |
| 51 0x80, |
| 52 0xff, |
| 53 0x100, -0x1, -0xff]; |
| 54 var controller = new StreamController(); |
| 55 controller.add(data); |
| 56 controller.close(); |
| 57 var stream = controller.stream |
| 58 .transform(new StringDecoder(Encoding.ISO_8859_1)); |
| 59 stream.listen((s) { |
| 60 Expect.equals(11, s.length); |
| 61 Expect.equals(new String.fromCharCodes([0x01]), s[0]); |
| 62 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); |
| 63 Expect.equals("Dart", s.substring(2, 6)); |
| 64 Expect.equals(new String.fromCharCodes([0x80]), s[6]); |
| 65 Expect.equals(new String.fromCharCodes([0xff]), s[7]); |
| 66 Expect.equals('???', s.substring(8, 11)); |
| 67 }); |
| 68 } |
| 69 |
| 70 void testAscii() { |
| 71 List<int> data = [0x01, |
| 72 0x44, 0x61, 0x72, 0x74, |
| 73 0x7f, |
| 74 0xf4, 0x100, -0x1, -0xff]; |
| 75 var controller = new StreamController(); |
| 76 controller.add(data); |
| 77 controller.close(); |
| 78 var stream = controller.stream |
| 79 .transform(new StringDecoder(Encoding.ASCII)); |
| 80 stream.listen((s) { |
| 81 Expect.equals(10, s.length); |
| 82 Expect.equals(new String.fromCharCodes([0x01]), s[0]); |
| 83 Expect.equals("Dart", s.substring(1, 5)); |
| 84 Expect.equals(new String.fromCharCodes([0x7f]), s[5]); |
| 85 Expect.equals('????', s.substring(6, 10)); |
| 86 }); |
| 87 } |
| 88 |
| 89 void testReadLine1() { |
| 90 var controller = new StreamController(); |
| 91 var stream = controller.stream |
| 92 .transform(new StringDecoder()) |
| 93 .transform(new LineTransformer()); |
| 94 |
| 95 var stage = 0; |
| 96 |
| 97 void stringData(line) { |
| 98 var line; |
| 99 if (stage == 0) { |
| 100 Expect.equals(null, line); |
| 101 stage++; |
| 102 controller.close(); |
| 103 } else if (stage == 1) { |
| 104 Expect.equals("Line", line); |
| 105 stage++; |
| 106 } |
| 107 } |
| 108 |
| 109 void streamClosed() { |
| 110 Expect.equals(2, stage); |
| 111 } |
| 112 |
| 113 stream.listen( |
| 114 stringData, |
| 115 onDone: streamClosed); |
| 116 |
| 117 controller.add("Line".charCodes); |
| 118 } |
| 119 |
| 120 void testReadLine2() { |
| 121 var controller = new StreamController(); |
| 122 |
| 123 var stream = controller.stream |
| 124 .transform(new StringDecoder()) |
| 125 .transform(new LineTransformer()); |
| 126 |
| 127 var stage = 0; |
| 128 var subStage = 0; |
| 129 stream.listen((line) { |
| 130 if (stage == 0) { |
| 131 if (subStage == 0) { |
| 132 Expect.equals("Line1", line); |
| 133 subStage++; |
| 134 } else if (subStage == 1) { |
| 135 Expect.equals("Line2", line); |
| 136 subStage++; |
| 137 } else if (subStage == 2) { |
| 138 Expect.equals("Line3", line); |
| 139 subStage = 0; |
| 140 stage++; |
| 141 controller.add("ne4\n".charCodes); |
| 142 } else { |
| 143 Expect.fail("Stage 0 failed"); |
| 144 } |
| 145 } else if (stage == 1) { |
| 146 if (subStage == 0) { |
| 147 Expect.equals("Line4", line); |
| 148 subStage = 0; |
| 149 stage++; |
| 150 controller.add("\n\n\r\n\r\n\r\r".charCodes); |
| 151 } else { |
| 152 Expect.fail("Stage 1 failed"); |
| 153 } |
| 154 } else if (stage == 2) { |
| 155 if (subStage < 4) { |
| 156 // Expect 5 empty lines. As long as the stream is not closed the |
| 157 // final \r cannot be interpreted as a end of line. |
| 158 Expect.equals("", line); |
| 159 subStage++; |
| 160 } else if (subStage == 4) { |
| 161 Expect.equals("", line); |
| 162 subStage = 0; |
| 163 stage++; |
| 164 controller.close(); |
| 165 } else { |
| 166 Expect.fail("Stage 2 failed"); |
| 167 } |
| 168 } else if (stage == 3) { |
| 169 if (subStage == 0) { |
| 170 Expect.equals("", line); |
| 171 stage++; |
| 172 } else { |
| 173 Expect.fail("Stage 3 failed"); |
| 174 } |
| 175 } |
| 176 }, onDone: () { |
| 177 Expect.equals(4, stage); |
| 178 Expect.equals(0, subStage); |
| 179 }); |
| 180 |
| 181 controller.add("Line1\nLine2\r\nLine3\rLi".charCodes); |
| 182 } |
| 183 |
| 184 class TestException implements Exception { |
| 185 TestException(); |
| 186 } |
| 187 |
| 188 testErrorHandler() { |
| 189 var controller = new StreamController(); |
| 190 var errors = 0; |
| 191 var stream = controller.stream |
| 192 .transform(new StringDecoder()) |
| 193 .transform(new LineTransformer()); |
| 194 stream.listen( |
| 195 (_) {}, |
| 196 onDone: () { |
| 197 Expect.equals(1, errors); |
| 198 }, |
| 199 onError: (e) { |
| 200 errors++; |
| 201 Expect.isTrue(e.error is TestException); |
| 202 }); |
| 203 controller.signalError(new TestException()); |
| 204 controller.close(); |
| 205 } |
| 206 |
| 207 testLatin1EncoderError() { |
| 208 List<int> data = [0x01, |
| 209 0x7f, |
| 210 0x44, 0x61, 0x72, 0x74, |
| 211 0x80, |
| 212 0xff, |
| 213 0x100]; |
| 214 var controller = new StreamController(); |
| 215 controller.add(new String.fromCharCodes(data)); |
| 216 controller.close(); |
| 217 var stream = controller.stream |
| 218 .transform(new StringEncoder(Encoding.ISO_8859_1)); |
| 219 stream.listen( |
| 220 (s) { |
| 221 Expect.fail("data not expected"); |
| 222 }, |
| 223 onError: (error) { |
| 224 print(error); |
| 225 Expect.isTrue(error.error is FormatException); |
| 226 }); |
| 227 |
| 228 } |
| 229 |
| 230 main() { |
| 231 testUtf8(); |
| 232 testLatin1(); |
| 233 testAscii(); |
| 234 testReadLine1(); |
| 235 testReadLine2(); |
| 236 testErrorHandler(); |
| 237 testLatin1EncoderError(); |
| 238 } |
| OLD | NEW |