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 testUtf8() { | 11 void testUtf8() { |
12 List<int> data = [0x01, | 12 List<int> data = [0x01, |
13 0x7f, | 13 0x7f, |
14 0xc2, 0x80, | 14 0xc2, 0x80, |
15 0xdf, 0xbf, | 15 0xdf, 0xbf, |
16 0xe0, 0xa0, 0x80, | 16 0xe0, 0xa0, 0x80, |
17 0xef, 0xbf, 0xbf, | 17 0xef, 0xbf, 0xbf, |
18 0xf0, 0x9d, 0x84, 0x9e, | 18 0xf0, 0x9d, 0x84, 0x9e, |
19 0x100, -0x1, -0xFF]; | 19 0x100, -0x1, -0xFF]; |
20 var controller = new StreamController(); | 20 var controller = new StreamController(sync: true); |
21 controller.add(data); | 21 controller.add(data); |
22 controller.close(); | 22 controller.close(); |
23 var stringStream = controller.stream | 23 var stringStream = controller.stream |
24 .transform(new StringDecoder(Encoding.UTF_8)); | 24 .transform(new StringDecoder(Encoding.UTF_8)); |
25 stringStream.listen( | 25 stringStream.listen( |
26 (s) { | 26 (s) { |
27 Expect.equals(11, s.length); | 27 Expect.equals(11, s.length); |
28 Expect.equals(new String.fromCharCodes([0x01]), s[0]); | 28 Expect.equals(new String.fromCharCodes([0x01]), s[0]); |
29 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); | 29 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); |
30 Expect.equals(new String.fromCharCodes([0x80]), s[2]); | 30 Expect.equals(new String.fromCharCodes([0x80]), s[2]); |
(...skipping 14 matching lines...) Expand all Loading... |
45 }); | 45 }); |
46 } | 46 } |
47 | 47 |
48 void testLatin1() { | 48 void testLatin1() { |
49 List<int> data = [0x01, | 49 List<int> data = [0x01, |
50 0x7f, | 50 0x7f, |
51 0x44, 0x61, 0x72, 0x74, | 51 0x44, 0x61, 0x72, 0x74, |
52 0x80, | 52 0x80, |
53 0xff, | 53 0xff, |
54 0x100, -0x1, -0xff]; | 54 0x100, -0x1, -0xff]; |
55 var controller = new StreamController(); | 55 var controller = new StreamController(sync: true); |
56 controller.add(data); | 56 controller.add(data); |
57 controller.close(); | 57 controller.close(); |
58 var stream = controller.stream | 58 var stream = controller.stream |
59 .transform(new StringDecoder(Encoding.ISO_8859_1)); | 59 .transform(new StringDecoder(Encoding.ISO_8859_1)); |
60 stream.listen((s) { | 60 stream.listen((s) { |
61 Expect.equals(11, s.length); | 61 Expect.equals(11, s.length); |
62 Expect.equals(new String.fromCharCodes([0x01]), s[0]); | 62 Expect.equals(new String.fromCharCodes([0x01]), s[0]); |
63 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); | 63 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); |
64 Expect.equals("Dart", s.substring(2, 6)); | 64 Expect.equals("Dart", s.substring(2, 6)); |
65 Expect.equals(new String.fromCharCodes([0x80]), s[6]); | 65 Expect.equals(new String.fromCharCodes([0x80]), s[6]); |
66 Expect.equals(new String.fromCharCodes([0xff]), s[7]); | 66 Expect.equals(new String.fromCharCodes([0xff]), s[7]); |
67 Expect.equals('???', s.substring(8, 11)); | 67 Expect.equals('???', s.substring(8, 11)); |
68 }); | 68 }); |
69 } | 69 } |
70 | 70 |
71 void testAscii() { | 71 void testAscii() { |
72 List<int> data = [0x01, | 72 List<int> data = [0x01, |
73 0x44, 0x61, 0x72, 0x74, | 73 0x44, 0x61, 0x72, 0x74, |
74 0x7f, | 74 0x7f, |
75 0xf4, 0x100, -0x1, -0xff]; | 75 0xf4, 0x100, -0x1, -0xff]; |
76 var controller = new StreamController(); | 76 var controller = new StreamController(sync: true); |
77 controller.add(data); | 77 controller.add(data); |
78 controller.close(); | 78 controller.close(); |
79 var stream = controller.stream | 79 var stream = controller.stream |
80 .transform(new StringDecoder(Encoding.ASCII)); | 80 .transform(new StringDecoder(Encoding.ASCII)); |
81 stream.listen((s) { | 81 stream.listen((s) { |
82 Expect.equals(10, s.length); | 82 Expect.equals(10, s.length); |
83 Expect.equals(new String.fromCharCodes([0x01]), s[0]); | 83 Expect.equals(new String.fromCharCodes([0x01]), s[0]); |
84 Expect.equals("Dart", s.substring(1, 5)); | 84 Expect.equals("Dart", s.substring(1, 5)); |
85 Expect.equals(new String.fromCharCodes([0x7f]), s[5]); | 85 Expect.equals(new String.fromCharCodes([0x7f]), s[5]); |
86 Expect.equals('????', s.substring(6, 10)); | 86 Expect.equals('????', s.substring(6, 10)); |
87 }); | 87 }); |
88 } | 88 } |
89 | 89 |
90 void testReadLine1() { | 90 void testReadLine1() { |
91 var controller = new StreamController(); | 91 var controller = new StreamController(sync: true); |
92 var stream = controller.stream | 92 var stream = controller.stream |
93 .transform(new StringDecoder()) | 93 .transform(new StringDecoder()) |
94 .transform(new LineTransformer()); | 94 .transform(new LineTransformer()); |
95 | 95 |
96 var stage = 0; | 96 var stage = 0; |
97 | 97 |
98 void stringData(line) { | 98 void stringData(line) { |
99 var line; | 99 var line; |
100 if (stage == 0) { | 100 if (stage == 0) { |
101 Expect.equals(null, line); | 101 Expect.equals(null, line); |
(...skipping 10 matching lines...) Expand all Loading... |
112 } | 112 } |
113 | 113 |
114 stream.listen( | 114 stream.listen( |
115 stringData, | 115 stringData, |
116 onDone: streamClosed); | 116 onDone: streamClosed); |
117 | 117 |
118 controller.add("Line".codeUnits); | 118 controller.add("Line".codeUnits); |
119 } | 119 } |
120 | 120 |
121 void testReadLine2() { | 121 void testReadLine2() { |
122 var controller = new StreamController(); | 122 var controller = new StreamController(sync: true); |
123 | 123 |
124 var stream = controller.stream | 124 var stream = controller.stream |
125 .transform(new StringDecoder()) | 125 .transform(new StringDecoder()) |
126 .transform(new LineTransformer()); | 126 .transform(new LineTransformer()); |
127 | 127 |
128 var stage = 0; | 128 var stage = 0; |
129 var subStage = 0; | 129 var subStage = 0; |
130 stream.listen((line) { | 130 stream.listen((line) { |
131 if (stage == 0) { | 131 if (stage == 0) { |
132 if (subStage == 0) { | 132 if (subStage == 0) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 }); | 180 }); |
181 | 181 |
182 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits); | 182 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits); |
183 } | 183 } |
184 | 184 |
185 class TestException implements Exception { | 185 class TestException implements Exception { |
186 TestException(); | 186 TestException(); |
187 } | 187 } |
188 | 188 |
189 testErrorHandler() { | 189 testErrorHandler() { |
190 var controller = new StreamController(); | 190 var controller = new StreamController(sync: true); |
191 var errors = 0; | 191 var errors = 0; |
192 var stream = controller.stream | 192 var stream = controller.stream |
193 .transform(new StringDecoder()) | 193 .transform(new StringDecoder()) |
194 .transform(new LineTransformer()); | 194 .transform(new LineTransformer()); |
195 stream.listen( | 195 stream.listen( |
196 (_) {}, | 196 (_) {}, |
197 onDone: () { | 197 onDone: () { |
198 Expect.equals(1, errors); | 198 Expect.equals(1, errors); |
199 }, | 199 }, |
200 onError: (error) { | 200 onError: (error) { |
201 errors++; | 201 errors++; |
202 Expect.isTrue(error is TestException); | 202 Expect.isTrue(error is TestException); |
203 }); | 203 }); |
204 controller.addError(new TestException()); | 204 controller.addError(new TestException()); |
205 controller.close(); | 205 controller.close(); |
206 } | 206 } |
207 | 207 |
208 testLatin1EncoderError() { | 208 testLatin1EncoderError() { |
209 List<int> data = [0x01, | 209 List<int> data = [0x01, |
210 0x7f, | 210 0x7f, |
211 0x44, 0x61, 0x72, 0x74, | 211 0x44, 0x61, 0x72, 0x74, |
212 0x80, | 212 0x80, |
213 0xff, | 213 0xff, |
214 0x100]; | 214 0x100]; |
215 var controller = new StreamController(); | 215 var controller = new StreamController(sync: true); |
216 controller.add(new String.fromCharCodes(data)); | 216 controller.add(new String.fromCharCodes(data)); |
217 controller.close(); | 217 controller.close(); |
218 var stream = controller.stream | 218 var stream = controller.stream |
219 .transform(new StringEncoder(Encoding.ISO_8859_1)); | 219 .transform(new StringEncoder(Encoding.ISO_8859_1)); |
220 stream.listen( | 220 stream.listen( |
221 (s) { | 221 (s) { |
222 Expect.fail("data not expected"); | 222 Expect.fail("data not expected"); |
223 }, | 223 }, |
224 onError: (error) { | 224 onError: (error) { |
225 Expect.isTrue(error is FormatException); | 225 Expect.isTrue(error is FormatException); |
226 }); | 226 }); |
227 | 227 |
228 } | 228 } |
229 | 229 |
230 main() { | 230 main() { |
231 testUtf8(); | 231 testUtf8(); |
232 testLatin1(); | 232 testLatin1(); |
233 testAscii(); | 233 testAscii(); |
234 testReadLine1(); | 234 testReadLine1(); |
235 testReadLine2(); | 235 testReadLine2(); |
236 testErrorHandler(); | 236 testErrorHandler(); |
237 testLatin1EncoderError(); | 237 testLatin1EncoderError(); |
238 } | 238 } |
OLD | NEW |