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

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

Issue 1240623002: Add split function to LineSplitter class in dart:convert. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Addto changelog Created 5 years, 5 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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 library line_splitter_test; 5 library line_splitter_test;
6 import "package:expect/expect.dart"; 6 import "package:expect/expect.dart";
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 import 'dart:math' as MATH; 9 import 'dart:math' as MATH;
10 10
11 11
12 void main() { 12 void main() {
13 testSimpleConvert(); 13 testSimpleConvert();
14 testSplit();
15 testSplitWithOffsets();
14 testManyLines(); 16 testManyLines();
15 testReadLine1(); 17 testReadLine1();
16 testReadLine2(); 18 testReadLine2();
19 testChunkedConversion();
17 } 20 }
18 21
19 void testManyLines() { 22 void testManyLines() {
20 const breaks = const ['\n', '\r\n']; 23 const breaks = const ['\n', '\r\n'];
21 int breakIndex = 0; 24 int breakIndex = 0;
22 25
23 var inputs = const ['line1', 'line2', 'long line 3', ' line 4 ', 'l5']; 26 var inputs = const ['line1', 'line2', 'long line 3', ' line 4 ', 'l5'];
24 27
25 28
26 var buffer = inputs.fold(new StringBuffer(), (buff, e) { 29 var buffer = inputs.fold(new StringBuffer(), (buff, e) {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 132
130 // Note: codeUnits is fine. Text is ASCII. 133 // Note: codeUnits is fine. Text is ASCII.
131 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits); 134 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits);
132 controller.add("ne4\n".codeUnits); 135 controller.add("ne4\n".codeUnits);
133 controller.add("\n\n\r\n\r\n\r\r".codeUnits); 136 controller.add("\n\n\r\n\r\n\r\r".codeUnits);
134 controller.add("Line5\r".codeUnits); 137 controller.add("Line5\r".codeUnits);
135 controller.add("\nLine6\n".codeUnits); 138 controller.add("\nLine6\n".codeUnits);
136 controller.close(); 139 controller.close();
137 Expect.equals(expectedLines.length, index); 140 Expect.equals(expectedLines.length, index);
138 } 141 }
142
143
144 void testSplit() {
145 var test = """line1
146 line2
147 line3""";
148
149
150 var result = LineSplitter.split(test).toList();
151
152 Expect.listEquals(['line1', 'line2', 'line3'], result);
153
154 test = "Line1\nLine2\r\nLine3\rLi"
155 "ne4\n"
156 "\n\n\r\n\r\n\r\r";
157
158 result = LineSplitter.split(test).toList();
159
160 Expect.listEquals(
161 ['Line1', 'Line2', 'Line3', 'Line4', '', '', '', '', '', ''],
162 result);
163 }
164
165 void testSplitWithOffsets() {
166 var test = """line1
167 line2
168 line3""";
169
170 var result = LineSplitter.split(test, 4).toList();
171 Expect.listEquals(['1', 'line2', 'line3'], result);
172
173 result = LineSplitter.split(test, 5).toList();
174 Expect.listEquals(['', 'line2', 'line3'], result);
175
176 result = LineSplitter.split(test, 6).toList();
177 Expect.listEquals(['line2', 'line3'], result);
178
179 result = LineSplitter.split(test, 0, 8).toList();
180 Expect.listEquals(['line1', 'li'], result);
181
182 result = LineSplitter.split(test, 6, 11).toList();
183 Expect.listEquals(['line2'], result);
184
185 test = "Line1\nLine2\r\nLine3\rLi"
186 "ne4\n"
187 "\n\n\r\n\r\n\r\r";
188
189 result = LineSplitter.split(test).toList();
190
191 Expect.listEquals(
192 ['Line1', 'Line2', 'Line3', 'Line4', '', '', '', '', '', ''],
193 result);
194
195 test = "a\n\nb\r\nc\n\rd\r\re\r\n\nf\r\n";
196 result = LineSplitter.split(test).toList();
197 Expect.listEquals(["a", "", "b", "c", "", "d", "", "e", "", "f"], result);
198 }
199
200 void testChunkedConversion() {
201 // Test any split of this complex string.
202 var test = "a\n\nb\r\nc\n\rd\r\re\r\n\nf\rg\nh\r\n";
203 var result = ["a", "", "b","c", "", "d", "", "e", "", "f", "g", "h"];
204 for (int i = 0; i < test.length; i++) {
205 var output = [];
206 var splitter = new LineSplitter();
207 var outSink = new ChunkedConversionSink.withCallback(output.addAll);
208 var sink = splitter.startChunkedConversion(outSink);
209 sink.addSlice(test, 0, i, false);
210 sink.addSlice(test, i, test.length, false);
211 sink.close();
212 Expect.listEquals(result, output);
213 }
214
215 // Test the string split into three parts in any way.
216 for (int i = 0; i < test.length; i++) {
217 for (int j = i; j < test.length; j++) {
218 var output = [];
219 var splitter = new LineSplitter();
220 var outSink = new ChunkedConversionSink.withCallback(output.addAll);
221 var sink = splitter.startChunkedConversion(outSink);
222 sink.addSlice(test, 0, i, false);
223 sink.addSlice(test, i, j, false);
224 sink.addSlice(test, j, test.length, true);
225 Expect.listEquals(result, output);
226 }
227 }
228 }
OLDNEW
« sdk/lib/convert/line_splitter.dart ('K') | « sdk/lib/convert/line_splitter.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698