| OLD | NEW |
| 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 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 Expect.isTrue(done, 'should be done by now'); | 110 Expect.isTrue(done, 'should be done by now'); |
| 111 } | 111 } |
| 112 | 112 |
| 113 void testReadLine2() { | 113 void testReadLine2() { |
| 114 var controller = new StreamController(sync: true); | 114 var controller = new StreamController(sync: true); |
| 115 | 115 |
| 116 var stream = controller.stream | 116 var stream = controller.stream |
| 117 .transform(new Utf8Decoder()) | 117 .transform(new Utf8Decoder()) |
| 118 .transform(new LineSplitter()); | 118 .transform(new LineSplitter()); |
| 119 | 119 |
| 120 var done = false; | 120 var expectedLines = ['Line1', 'Line2','Line3', 'Line4', |
| 121 '', '', '', '', '', '', |
| 122 'Line5', 'Line6']; |
| 121 | 123 |
| 122 var stage = 0; | 124 var index = 0; |
| 123 var subStage = 0; | 125 |
| 124 stream.listen((line) { | 126 stream.listen((line) { |
| 125 if (stage == 0) { | 127 Expect.equals(expectedLines[index++], line); |
| 126 if (subStage == 0) { | 128 }); |
| 127 Expect.equals("Line1", line); | |
| 128 subStage++; | |
| 129 } else if (subStage == 1) { | |
| 130 Expect.equals("Line2", line); | |
| 131 subStage++; | |
| 132 } else if (subStage == 2) { | |
| 133 Expect.equals("Line3", line); | |
| 134 subStage = 0; | |
| 135 stage++; | |
| 136 } else { | |
| 137 Expect.fail("Stage 0 failed"); | |
| 138 } | |
| 139 } else if (stage == 1) { | |
| 140 if (subStage == 0) { | |
| 141 Expect.equals("Line4", line); | |
| 142 subStage = 0; | |
| 143 stage++; | |
| 144 } else { | |
| 145 Expect.fail("Stage 1 failed"); | |
| 146 } | |
| 147 } else if (stage == 2) { | |
| 148 if (subStage < 4) { | |
| 149 // Expect 5 empty lines. As long as the stream is not closed the | |
| 150 // final \r cannot be interpreted as a end of line. | |
| 151 Expect.equals("", line); | |
| 152 subStage++; | |
| 153 } else if (subStage == 4) { | |
| 154 Expect.equals("", line); | |
| 155 subStage = 0; | |
| 156 stage++; | |
| 157 } else { | |
| 158 Expect.fail("Stage 2 failed"); | |
| 159 } | |
| 160 } else if (stage == 3) { | |
| 161 if (subStage == 0) { | |
| 162 Expect.equals("", line); | |
| 163 stage++; | |
| 164 } else { | |
| 165 Expect.fail("Stage 3 failed"); | |
| 166 } | |
| 167 } | |
| 168 }, onDone: () { | |
| 169 Expect.equals(4, stage); | |
| 170 Expect.equals(0, subStage); | |
| 171 done = true; | |
| 172 }); | |
| 173 | 129 |
| 174 // Note: codeUnits is fine. Text is ASCII. | 130 // Note: codeUnits is fine. Text is ASCII. |
| 175 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits); | 131 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits); |
| 176 controller.add("ne4\n".codeUnits); | 132 controller.add("ne4\n".codeUnits); |
| 177 controller.add("\n\n\r\n\r\n\r\r".codeUnits); | 133 controller.add("\n\n\r\n\r\n\r\r".codeUnits); |
| 134 controller.add("Line5\r".codeUnits); |
| 135 controller.add("\nLine6\n".codeUnits); |
| 178 controller.close(); | 136 controller.close(); |
| 179 Expect.isTrue(done, 'should be done here...'); | 137 Expect.equals(expectedLines.length, index); |
| 180 } | 138 } |
| OLD | NEW |