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

Unified 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: todos 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 side-by-side diff with in-line comments
Download patch
« sdk/lib/convert/line_splitter.dart ('K') | « sdk/lib/io/string_transformer.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/convert/line_splitter_test.dart
diff --git a/tests/lib/convert/line_splitter_test.dart b/tests/lib/convert/line_splitter_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..acdc633e480b85e51f0025d345a7939fe7b3615e
--- /dev/null
+++ b/tests/lib/convert/line_splitter_test.dart
@@ -0,0 +1,180 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library line_splitter_test;
+import "package:expect/expect.dart";
+import 'dart:async';
+import 'dart:convert';
+import 'dart:math' as MATH;
+
+
+void main() {
+ testSimpleConvert();
+ testManyLines();
+ testReadLine1();
+ testReadLine2();
+}
+
+void testManyLines() {
+ const breaks = const ['\n', '\r\n'];
+ int breakIndex = 0;
+
+ var inputs = const ['line1', 'line2', 'long line 3', ' line 4 ', 'l5'];
+
+
+ var buffer = inputs.fold(new StringBuffer(), (buff, e) {
+ buff.write(e);
+ buff.write(breaks[breakIndex]);
+
+ breakIndex++;
+ breakIndex = breakIndex % breaks.length;
+
+ return buff;
+ });
+
+
+ var foo = _getLinesSliced(buffer.toString());
+ Expect.equals(inputs.join(), foo);
+}
+
+
+String _getLinesSliced(String str) {
+ String lines;
+ var stringSink =
+ new StringConversionSink.withCallback((result) => lines = result);
+ var sink = new LineSplitter().startChunkedConversion(stringSink);
+
+ const chunkSize = 3;
+ var index = 0;
+ while(index < str.length) {
+ var end = MATH.min(str.length, index + chunkSize);
+
+ sink.addSlice(str, index, end, false);
+ index += chunkSize;
+ }
+
+ sink.close();
+ return lines;
+}
+
+void testSimpleConvert() {
+ var test = """line1
+line2
+line3""";
+
+
+ var decoder = new LineSplitter();
+ var result = decoder.convert(test);
+
+ Expect.listEquals(['line1', 'line2', 'line3'], result);
+
+ test = "Line1\nLine2\r\nLine3\rLi"
+ "ne4\n"
+ "\n\n\r\n\r\n\r\r";
+
+ result = decoder.convert(test);
+
+ Expect.listEquals(
+ ['Line1', 'Line2', 'Line3', 'Line4', '', '', '', '', '', ''],
+ result);
+}
+
+void testReadLine1() {
+ var controller = new StreamController(sync: true);
+ var stream = controller.stream
+ .transform(new Utf8Decoder())
+ .transform(new LineSplitter());
+
+ var stage = 0;
+ var done = false;
+
+ void stringData(line) {
+ Expect.equals(stage, 0);
+ Expect.equals("Line", line);
+ stage++;
+ }
+
+ void streamClosed() {
+ Expect.equals(1, stage);
+ done = true;
+ }
+
+ stream.listen(
+ stringData,
+ onDone: streamClosed);
+
+ // Note: codeUnits is fine. Text is ASCII.
+ controller.add("Line".codeUnits);
+ controller.close();
+ Expect.isTrue(done, 'should be done by now');
+}
+
+void testReadLine2() {
+ var controller = new StreamController(sync: true);
+
+ var stream = controller.stream
+ .transform(new Utf8Decoder())
+ .transform(new LineSplitter());
+
+ var done = false;
+
+ var stage = 0;
+ var subStage = 0;
+ stream.listen((line) {
+ if (stage == 0) {
+ if (subStage == 0) {
+ Expect.equals("Line1", line);
+ subStage++;
+ } else if (subStage == 1) {
+ Expect.equals("Line2", line);
+ subStage++;
+ } else if (subStage == 2) {
+ Expect.equals("Line3", line);
+ subStage = 0;
+ stage++;
+ } else {
+ Expect.fail("Stage 0 failed");
+ }
+ } else if (stage == 1) {
+ if (subStage == 0) {
+ Expect.equals("Line4", line);
+ subStage = 0;
+ stage++;
+ } else {
+ Expect.fail("Stage 1 failed");
+ }
+ } else if (stage == 2) {
+ if (subStage < 4) {
+ // Expect 5 empty lines. As long as the stream is not closed the
+ // final \r cannot be interpreted as a end of line.
+ Expect.equals("", line);
+ subStage++;
+ } else if (subStage == 4) {
+ Expect.equals("", line);
+ subStage = 0;
+ stage++;
+ } else {
+ Expect.fail("Stage 2 failed");
+ }
+ } else if (stage == 3) {
+ if (subStage == 0) {
+ Expect.equals("", line);
+ stage++;
+ } else {
+ Expect.fail("Stage 3 failed");
+ }
+ }
+ }, onDone: () {
+ Expect.equals(4, stage);
+ Expect.equals(0, subStage);
+ done = true;
+ });
+
+ // Note: codeUnits is fine. Text is ASCII.
+ controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits);
+ controller.add("ne4\n".codeUnits);
Søren Gjesse 2013/08/12 12:57:27 Looks as if input break between \r and \n is not t
+ controller.add("\n\n\r\n\r\n\r\r".codeUnits);
+ controller.close();
+ Expect.isTrue(done, 'should be done here...');
+}
« sdk/lib/convert/line_splitter.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