Chromium Code Reviews| OLD | NEW |
|---|---|
| (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', ' line 4 ', 'l5']; | |
| 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 var stringSink = | |
| 45 new StringConversionSink.withCallback((result) => lines = result); | |
| 46 var sink = new LineSplitter().startChunkedConversion(stringSink); | |
| 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 sink.addSlice(str, index, end, false); | |
| 54 index += chunkSize; | |
| 55 } | |
| 56 | |
| 57 sink.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 // Note: codeUnits is fine. Text is ASCII. | |
| 108 controller.add("Line".codeUnits); | |
| 109 controller.close(); | |
| 110 Expect.isTrue(done, 'should be done by now'); | |
| 111 } | |
| 112 | |
| 113 void testReadLine2() { | |
| 114 var controller = new StreamController(sync: true); | |
| 115 | |
| 116 var stream = controller.stream | |
| 117 .transform(new Utf8Decoder()) | |
| 118 .transform(new LineSplitter()); | |
| 119 | |
| 120 var done = false; | |
| 121 | |
| 122 var stage = 0; | |
| 123 var subStage = 0; | |
| 124 stream.listen((line) { | |
| 125 if (stage == 0) { | |
| 126 if (subStage == 0) { | |
| 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 | |
| 174 // Note: codeUnits is fine. Text is ASCII. | |
| 175 controller.add("Line1\nLine2\r\nLine3\rLi".codeUnits); | |
| 176 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
| |
| 177 controller.add("\n\n\r\n\r\n\r\r".codeUnits); | |
| 178 controller.close(); | |
| 179 Expect.isTrue(done, 'should be done here...'); | |
| 180 } | |
| OLD | NEW |