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

Side by Side Diff: tests/lib/convert/line_splitter_test.dart

Issue 22070003: convert: new LineSplitter, (io: Deprecate LineTransformer) (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 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) 2013, 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 library line_splitter_test;
6 import "package:expect/expect.dart";
7 import 'dart:async';
8 import 'dart:convert';
9 import 'dart:math' as MATH;
10
11
12 void main() {
13 testSimpleConvert();
14 testManyLines();
15 testReadLine1();
16 testReadLine2();
17 }
18
19 void testManyLines() {
20 const breaks = const ['\n', '\r\n'];
21 int breakIndex = 0;
22
23 var inputs = const ['line1', 'line2', 'long line 3', ' this is line 4 ', 'l5'] ;
floitsch 2013/08/07 12:58:26 80chars.
kevmoo-old 2013/08/07 17:13:41 Done.
24
25
26 var buffer = inputs.fold(new StringBuffer(), (buff, e) {
27 buff.write(e);
28 buff.write(breaks[breakIndex]);
29
30 breakIndex++;
31 breakIndex = breakIndex % breaks.length;
32
33 return buff;
34 });
35
36
37 var foo = _getLinesSliced(buffer.toString());
38 Expect.equals(inputs.join(), foo);
39 }
40
41
42 String _getLinesSliced(String str) {
43 String lines;
44 ChunkedConversionSink byteSink =
floitsch 2013/08/07 12:58:26 Rename byteSink to stringSink.
kevmoo-old 2013/08/07 17:13:41 Done.
45 new StringConversionSink.withCallback((result) => lines = result);
46 var stringConversionSink = new LineSplitter().startChunkedConversion(byteSink) ;
floitsch 2013/08/07 12:58:26 80chars.
kevmoo-old 2013/08/07 17:13:41 Done.
47
48 const chunkSize = 3;
49 var index = 0;
50 while(index < str.length) {
51 var end = MATH.min(str.length, index + chunkSize);
52
53 stringConversionSink.addSlice(str, index, end, false);
54 index += chunkSize;
55 }
56
57 stringConversionSink.close();
58 return lines;
59 }
60
61 void testSimpleConvert() {
62 var test = """line1
63 line2
64 line3""";
65
66
67 var decoder = new LineSplitter();
68 var result = decoder.convert(test);
69
70 Expect.listEquals(['line1', 'line2', 'line3'], result);
71
72 test = "Line1\nLine2\r\nLine3\rLi"
73 "ne4\n"
74 "\n\n\r\n\r\n\r\r";
75
76 result = decoder.convert(test);
77
78 Expect.listEquals(
79 ['Line1', 'Line2', 'Line3', 'Line4', '', '', '', '', '', ''],
80 result);
81 }
82
83 void testReadLine1() {
84 var controller = new StreamController(sync: true);
85 var stream = controller.stream
86 .transform(new Utf8Decoder())
87 .transform(new LineSplitter());
88
89 var stage = 0;
90 var done = false;
91
92 void stringData(line) {
93 Expect.equals(stage, 0);
94 Expect.equals("Line", line);
95 stage++;
96 }
97
98 void streamClosed() {
99 Expect.equals(1, stage);
100 done = true;
101 }
102
103 stream.listen(
104 stringData,
105 onDone: streamClosed);
106
107 controller.add("Line".codeUnits);
floitsch 2013/08/07 12:58:26 Add comment that UTF8 is ok, because the string is
kevmoo-old 2013/08/07 17:13:41 Done.
108 controller.close();
109 Expect.isTrue(done, 'should be done by now');
110 }
111
112 void testReadLine2() {
113 var controller = new StreamController(sync: true);
114
115 var stream = controller.stream
116 .transform(new Utf8Decoder())
117 .transform(new LineSplitter());
118
119 var done = false;
120
121 var stage = 0;
122 var subStage = 0;
123 stream.listen((line) {
124 if (stage == 0) {
125 if (subStage == 0) {
126 Expect.equals("Line1", line);
127 subStage++;
128 } else if (subStage == 1) {
129 Expect.equals("Line2", line);
130 subStage++;
131 } else if (subStage == 2) {
132 Expect.equals("Line3", line);
133 subStage = 0;
134 stage++;
135 } else {
136 Expect.fail("Stage 0 failed");
137 }
138 } else if (stage == 1) {
139 if (subStage == 0) {
140 Expect.equals("Line4", line);
141 subStage = 0;
142 stage++;
143 } else {
144 Expect.fail("Stage 1 failed");
145 }
146 } else if (stage == 2) {
147 if (subStage < 4) {
148 // Expect 5 empty lines. As long as the stream is not closed the
149 // final \r cannot be interpreted as a end of line.
150 Expect.equals("", line);
151 subStage++;
152 } else if (subStage == 4) {
153 Expect.equals("", line);
154 subStage = 0;
155 stage++;
156 } else {
157 Expect.fail("Stage 2 failed");
158 }
159 } else if (stage == 3) {
160 if (subStage == 0) {
161 Expect.equals("", line);
162 stage++;
163 } else {
164 Expect.fail("Stage 3 failed");
165 }
166 }
167 }, onDone: () {
168 Expect.equals(4, stage);
169 Expect.equals(0, subStage);
170 done = true;
171 });
172
173 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits);
floitsch 2013/08/07 12:58:26 Add comment that "codeUnits" is ok, because the st
kevmoo-old 2013/08/07 17:13:41 Done.
174 controller.add("ne4\n".codeUnits);
175 controller.add("\n\n\r\n\r\n\r\r".codeUnits);
176 controller.close();
177 Expect.isTrue(done, 'should be done here...');
178 }
OLDNEW
« sdk/lib/io/string_transformer.dart ('K') | « sdk/lib/io/string_transformer.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698