Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: tests/standalone/io/string_transformer_test.dart

Issue 22872012: Remove Encoding-enum from dart:io and add interface in dart:convert. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix typo. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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:convert";
7 import "dart:io";
8 import "dart:isolate";
9
10 import "package:expect/expect.dart";
11
12 const UNICODE_REPLACEMENT_CHARACTER_CODEPOINT = 0xFFFD;
13
14 void main() {
15 testUtf8();
16 testLatin1();
17 testAscii();
18 testReadLine1();
19 testReadLine2();
20 testErrorHandler();
21 testLatin1EncoderError();
22 }
23
24 void testUtf8() {
25 List<int> data = [0x01,
26 0x7f,
27 0xc2, 0x80,
28 0xdf, 0xbf,
29 0xe0, 0xa0, 0x80,
30 0xef, 0xbf, 0xbf,
31 0xf0, 0x9d, 0x84, 0x9e,
32 0x100, -0x1, -0xFF];
33 var controller = new StreamController(sync: true);
34 controller.add(data);
35 controller.close();
36 var stringStream = controller.stream
37 .transform(new StringDecoder(Encoding.UTF_8));
38 stringStream.listen(
39 (s) {
40 Expect.equals(11, s.length);
41 Expect.equals(new String.fromCharCodes([0x01]), s[0]);
42 Expect.equals(new String.fromCharCodes([0x7f]), s[1]);
43 Expect.equals(new String.fromCharCodes([0x80]), s[2]);
44 Expect.equals(new String.fromCharCodes([0x7ff]), s[3]);
45 Expect.equals(new String.fromCharCodes([0x800]), s[4]);
46 Expect.equals(new String.fromCharCodes([0xffff]), s[5]);
47 Expect.equals(new String.fromCharCodes([0xffff]), s[5]);
48
49 // Surrogate pair for U+1D11E.
50 Expect.equals(new String.fromCharCodes([0xd834, 0xdd1e]),
51 s.substring(6, 8));
52
53 Expect.equals(new String.fromCharCodes(
54 [UNICODE_REPLACEMENT_CHARACTER_CODEPOINT,
55 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT,
56 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]),
57 s.substring(8, 11));
58 });
59 }
60
61 void testLatin1() {
62 List<int> data = [0x01,
63 0x7f,
64 0x44, 0x61, 0x72, 0x74,
65 0x80,
66 0xff,
67 0x100, -0x1, -0xff];
68 var controller = new StreamController(sync: true);
69 controller.add(data);
70 controller.close();
71 var stream = controller.stream
72 .transform(new StringDecoder(Encoding.ISO_8859_1));
73 stream.listen((s) {
74 Expect.equals(11, s.length);
75 Expect.equals(new String.fromCharCodes([0x01]), s[0]);
76 Expect.equals(new String.fromCharCodes([0x7f]), s[1]);
77 Expect.equals("Dart", s.substring(2, 6));
78 Expect.equals(new String.fromCharCodes([0x80]), s[6]);
79 Expect.equals(new String.fromCharCodes([0xff]), s[7]);
80 Expect.equals('???', s.substring(8, 11));
81 });
82 }
83
84 void testAscii() {
85 List<int> data = [0x01,
86 0x44, 0x61, 0x72, 0x74,
87 0x7f,
88 0xf4, 0x100, -0x1, -0xff];
89 var controller = new StreamController(sync: true);
90 controller.add(data);
91 controller.close();
92 var stream = controller.stream
93 .transform(new StringDecoder(Encoding.ASCII));
94 stream.listen((s) {
95 Expect.equals(10, s.length);
96 Expect.equals(new String.fromCharCodes([0x01]), s[0]);
97 Expect.equals("Dart", s.substring(1, 5));
98 Expect.equals(new String.fromCharCodes([0x7f]), s[5]);
99 Expect.equals('????', s.substring(6, 10));
100 });
101 }
102
103 void testReadLine1() {
104 var controller = new StreamController(sync: true);
105 var stream = controller.stream
106 .transform(new StringDecoder())
107 .transform(new LineSplitter());
108
109 var stage = 0;
110
111 void stringData(line) {
112 Expect.equals(stage, 0);
113 Expect.equals("Line", line);
114 stage++;
115 }
116
117 void streamClosed() {
118 Expect.equals(1, stage);
119 }
120
121 stream.listen(
122 stringData,
123 onDone: streamClosed);
124
125 // Note: codeUnits is fine. Text is ASCII.
126 controller.add("Line".codeUnits);
127 controller.close();
128 Expect.equals(1, stage);
129 }
130
131 void testReadLine2() {
132 var controller = new StreamController(sync: true);
133
134 var stream = controller.stream
135 .transform(new StringDecoder())
136 .transform(new LineSplitter());
137
138 var expectedLines = ['Line1', 'Line2','Line3', 'Line4',
139 '', '', '', '', '', '',
140 'Line5', 'Line6'];
141
142 var index = 0;
143
144 stream.listen((line) {
145 Expect.equals(expectedLines[index++], line);
146 });
147
148 // Note: codeUnits is fine. Text is ASCII.
149 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits);
150 controller.add("ne4\n".codeUnits);
151 controller.add("\n\n\r\n\r\n\r\r".codeUnits);
152 controller.add("Line5\r".codeUnits);
153 controller.add("\nLine6\n".codeUnits);
154 controller.close();
155 Expect.equals(expectedLines.length, index);
156 }
157
158 class TestException implements Exception {
159 TestException();
160 }
161
162 void testErrorHandler() {
163 var controller = new StreamController(sync: true);
164 var errors = 0;
165 var stream = controller.stream
166 .transform(new StringDecoder())
167 .transform(new LineSplitter());
168 stream.listen(
169 (_) {},
170 onDone: () {
171 Expect.equals(1, errors);
172 },
173 onError: (error) {
174 errors++;
175 Expect.isTrue(error is TestException);
176 });
177 controller.addError(new TestException());
178 controller.close();
179 }
180
181 void testLatin1EncoderError() {
182 List<int> data = [0x01,
183 0x7f,
184 0x44, 0x61, 0x72, 0x74,
185 0x80,
186 0xff,
187 0x100];
188 var controller = new StreamController(sync: true);
189 controller.add(new String.fromCharCodes(data));
190 controller.close();
191 var stream = controller.stream
192 .transform(new StringEncoder(Encoding.ISO_8859_1));
193 stream.listen(
194 (s) {
195 Expect.fail("data not expected");
196 },
197 onError: (error) {
198 Expect.isTrue(error is FormatException);
199 });
200
201 }
OLDNEW
« no previous file with comments | « tests/standalone/io/string_decoder_test.dart ('k') | tests/standalone/io/web_socket_protocol_processor_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698