| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import "dart:async"; | 6 import "dart:async"; |
| 7 import "dart:io"; | 7 import "dart:io"; |
| 8 import "dart:isolate"; | 8 import "dart:isolate"; |
| 9 import "dart:utf"; | 9 import "dart:utf"; |
| 10 | 10 |
| 11 void main() { |
| 12 testUtf8(); |
| 13 testLatin1(); |
| 14 testAscii(); |
| 15 testReadLine1(); |
| 16 testReadLine2(); |
| 17 testErrorHandler(); |
| 18 testLatin1EncoderError(); |
| 19 } |
| 20 |
| 11 void testUtf8() { | 21 void testUtf8() { |
| 12 List<int> data = [0x01, | 22 List<int> data = [0x01, |
| 13 0x7f, | 23 0x7f, |
| 14 0xc2, 0x80, | 24 0xc2, 0x80, |
| 15 0xdf, 0xbf, | 25 0xdf, 0xbf, |
| 16 0xe0, 0xa0, 0x80, | 26 0xe0, 0xa0, 0x80, |
| 17 0xef, 0xbf, 0xbf, | 27 0xef, 0xbf, 0xbf, |
| 18 0xf0, 0x9d, 0x84, 0x9e, | 28 0xf0, 0x9d, 0x84, 0x9e, |
| 19 0x100, -0x1, -0xFF]; | 29 0x100, -0x1, -0xFF]; |
| 20 var controller = new StreamController(sync: true); | 30 var controller = new StreamController(sync: true); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 | 99 |
| 90 void testReadLine1() { | 100 void testReadLine1() { |
| 91 var controller = new StreamController(sync: true); | 101 var controller = new StreamController(sync: true); |
| 92 var stream = controller.stream | 102 var stream = controller.stream |
| 93 .transform(new StringDecoder()) | 103 .transform(new StringDecoder()) |
| 94 .transform(new LineTransformer()); | 104 .transform(new LineTransformer()); |
| 95 | 105 |
| 96 var stage = 0; | 106 var stage = 0; |
| 97 | 107 |
| 98 void stringData(line) { | 108 void stringData(line) { |
| 99 var line; | 109 Expect.equals(stage, 0); |
| 100 if (stage == 0) { | 110 Expect.equals("Line", line); |
| 101 Expect.equals(null, line); | 111 stage++; |
| 102 stage++; | |
| 103 controller.close(); | |
| 104 } else if (stage == 1) { | |
| 105 Expect.equals("Line", line); | |
| 106 stage++; | |
| 107 } | |
| 108 } | 112 } |
| 109 | 113 |
| 110 void streamClosed() { | 114 void streamClosed() { |
| 111 Expect.equals(2, stage); | 115 Expect.equals(1, stage); |
| 112 } | 116 } |
| 113 | 117 |
| 114 stream.listen( | 118 stream.listen( |
| 115 stringData, | 119 stringData, |
| 116 onDone: streamClosed); | 120 onDone: streamClosed); |
| 117 | 121 |
| 122 // Note: codeUnits is fine. Text is ASCII. |
| 118 controller.add("Line".codeUnits); | 123 controller.add("Line".codeUnits); |
| 124 controller.close(); |
| 125 Expect.equals(1, stage); |
| 119 } | 126 } |
| 120 | 127 |
| 121 void testReadLine2() { | 128 void testReadLine2() { |
| 122 var controller = new StreamController(sync: true); | 129 var controller = new StreamController(sync: true); |
| 123 | 130 |
| 124 var stream = controller.stream | 131 var stream = controller.stream |
| 125 .transform(new StringDecoder()) | 132 .transform(new StringDecoder()) |
| 126 .transform(new LineTransformer()); | 133 .transform(new LineTransformer()); |
| 127 | 134 |
| 128 var stage = 0; | 135 var expectedLines = ['Line1', 'Line2','Line3', 'Line4', |
| 129 var subStage = 0; | 136 '', '', '', '', '', '', |
| 137 'Line5', 'Line6']; |
| 138 |
| 139 var index = 0; |
| 140 |
| 130 stream.listen((line) { | 141 stream.listen((line) { |
| 131 if (stage == 0) { | 142 Expect.equals(expectedLines[index++], line); |
| 132 if (subStage == 0) { | 143 }); |
| 133 Expect.equals("Line1", line); | |
| 134 subStage++; | |
| 135 } else if (subStage == 1) { | |
| 136 Expect.equals("Line2", line); | |
| 137 subStage++; | |
| 138 } else if (subStage == 2) { | |
| 139 Expect.equals("Line3", line); | |
| 140 subStage = 0; | |
| 141 stage++; | |
| 142 controller.add("ne4\n".codeUnits); | |
| 143 } else { | |
| 144 Expect.fail("Stage 0 failed"); | |
| 145 } | |
| 146 } else if (stage == 1) { | |
| 147 if (subStage == 0) { | |
| 148 Expect.equals("Line4", line); | |
| 149 subStage = 0; | |
| 150 stage++; | |
| 151 controller.add("\n\n\r\n\r\n\r\r".codeUnits); | |
| 152 } else { | |
| 153 Expect.fail("Stage 1 failed"); | |
| 154 } | |
| 155 } else if (stage == 2) { | |
| 156 if (subStage < 4) { | |
| 157 // Expect 5 empty lines. As long as the stream is not closed the | |
| 158 // final \r cannot be interpreted as a end of line. | |
| 159 Expect.equals("", line); | |
| 160 subStage++; | |
| 161 } else if (subStage == 4) { | |
| 162 Expect.equals("", line); | |
| 163 subStage = 0; | |
| 164 stage++; | |
| 165 controller.close(); | |
| 166 } else { | |
| 167 Expect.fail("Stage 2 failed"); | |
| 168 } | |
| 169 } else if (stage == 3) { | |
| 170 if (subStage == 0) { | |
| 171 Expect.equals("", line); | |
| 172 stage++; | |
| 173 } else { | |
| 174 Expect.fail("Stage 3 failed"); | |
| 175 } | |
| 176 } | |
| 177 }, onDone: () { | |
| 178 Expect.equals(4, stage); | |
| 179 Expect.equals(0, subStage); | |
| 180 }); | |
| 181 | 144 |
| 145 // Note: codeUnits is fine. Text is ASCII. |
| 182 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits); | 146 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits); |
| 147 controller.add("ne4\n".codeUnits); |
| 148 controller.add("\n\n\r\n\r\n\r\r".codeUnits); |
| 149 controller.add("Line5\r".codeUnits); |
| 150 controller.add("\nLine6\n".codeUnits); |
| 151 controller.close(); |
| 152 Expect.equals(expectedLines.length, index); |
| 183 } | 153 } |
| 184 | 154 |
| 185 class TestException implements Exception { | 155 class TestException implements Exception { |
| 186 TestException(); | 156 TestException(); |
| 187 } | 157 } |
| 188 | 158 |
| 189 testErrorHandler() { | 159 void testErrorHandler() { |
| 190 var controller = new StreamController(sync: true); | 160 var controller = new StreamController(sync: true); |
| 191 var errors = 0; | 161 var errors = 0; |
| 192 var stream = controller.stream | 162 var stream = controller.stream |
| 193 .transform(new StringDecoder()) | 163 .transform(new StringDecoder()) |
| 194 .transform(new LineTransformer()); | 164 .transform(new LineTransformer()); |
| 195 stream.listen( | 165 stream.listen( |
| 196 (_) {}, | 166 (_) {}, |
| 197 onDone: () { | 167 onDone: () { |
| 198 Expect.equals(1, errors); | 168 Expect.equals(1, errors); |
| 199 }, | 169 }, |
| 200 onError: (error) { | 170 onError: (error) { |
| 201 errors++; | 171 errors++; |
| 202 Expect.isTrue(error is TestException); | 172 Expect.isTrue(error is TestException); |
| 203 }); | 173 }); |
| 204 controller.addError(new TestException()); | 174 controller.addError(new TestException()); |
| 205 controller.close(); | 175 controller.close(); |
| 206 } | 176 } |
| 207 | 177 |
| 208 testLatin1EncoderError() { | 178 void testLatin1EncoderError() { |
| 209 List<int> data = [0x01, | 179 List<int> data = [0x01, |
| 210 0x7f, | 180 0x7f, |
| 211 0x44, 0x61, 0x72, 0x74, | 181 0x44, 0x61, 0x72, 0x74, |
| 212 0x80, | 182 0x80, |
| 213 0xff, | 183 0xff, |
| 214 0x100]; | 184 0x100]; |
| 215 var controller = new StreamController(sync: true); | 185 var controller = new StreamController(sync: true); |
| 216 controller.add(new String.fromCharCodes(data)); | 186 controller.add(new String.fromCharCodes(data)); |
| 217 controller.close(); | 187 controller.close(); |
| 218 var stream = controller.stream | 188 var stream = controller.stream |
| 219 .transform(new StringEncoder(Encoding.ISO_8859_1)); | 189 .transform(new StringEncoder(Encoding.ISO_8859_1)); |
| 220 stream.listen( | 190 stream.listen( |
| 221 (s) { | 191 (s) { |
| 222 Expect.fail("data not expected"); | 192 Expect.fail("data not expected"); |
| 223 }, | 193 }, |
| 224 onError: (error) { | 194 onError: (error) { |
| 225 Expect.isTrue(error is FormatException); | 195 Expect.isTrue(error is FormatException); |
| 226 }); | 196 }); |
| 227 | 197 |
| 228 } | 198 } |
| 229 | |
| 230 main() { | |
| 231 testUtf8(); | |
| 232 testLatin1(); | |
| 233 testAscii(); | |
| 234 testReadLine1(); | |
| 235 testReadLine2(); | |
| 236 testErrorHandler(); | |
| 237 testLatin1EncoderError(); | |
| 238 } | |
| OLD | NEW |