| 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 scheduled_test.stream_matcher_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:scheduled_test/scheduled_stream.dart'; | |
| 10 import 'package:scheduled_test/scheduled_test.dart'; | |
| 11 import 'package:scheduled_test/src/utils.dart'; | |
| 12 | |
| 13 import 'package:metatest/metatest.dart'; | |
| 14 import '../utils.dart'; | |
| 15 | |
| 16 /// Returns a [ScheduledStream] that asynchronously emits the numbers 1 through | |
| 17 /// 5 and then closes. | |
| 18 ScheduledStream createStream() { | |
| 19 var controller = new StreamController(); | |
| 20 var stream = new ScheduledStream(controller.stream); | |
| 21 | |
| 22 var future = pumpEventQueue(); | |
| 23 for (var i = 1; i <= 5; i++) { | |
| 24 future = future.then((_) { | |
| 25 controller.add(i); | |
| 26 return pumpEventQueue(); | |
| 27 }); | |
| 28 } | |
| 29 future.then((_) => controller.close()); | |
| 30 | |
| 31 return stream; | |
| 32 } | |
| 33 | |
| 34 void main() => initTests(_test); | |
| 35 | |
| 36 void _test(message) { | |
| 37 initMetatest(message); | |
| 38 | |
| 39 setUpTimeout(); | |
| 40 | |
| 41 expectTestPasses("expect() with matching values passes", () { | |
| 42 var stream = createStream(); | |
| 43 stream.expect(1); | |
| 44 stream.expect(2); | |
| 45 stream.expect(3); | |
| 46 stream.expect(4); | |
| 47 stream.expect(5); | |
| 48 }); | |
| 49 | |
| 50 expectTestFails("expect() with a non-matching value fails", () { | |
| 51 var stream = createStream(); | |
| 52 stream.expect(1); | |
| 53 stream.expect(2); | |
| 54 stream.expect(100); | |
| 55 }, (errors) { | |
| 56 expect(errors, hasLength(1)); | |
| 57 expect(errors.first.error.message, equals( | |
| 58 "Expected: <100>\n" | |
| 59 " Emitted: * 1\n" | |
| 60 " * 2\n" | |
| 61 " * 3")); | |
| 62 }); | |
| 63 | |
| 64 expectTestFails("expect() with too few values fails", () { | |
| 65 var stream = createStream(); | |
| 66 stream.expect(1); | |
| 67 stream.expect(2); | |
| 68 stream.expect(3); | |
| 69 stream.expect(4); | |
| 70 stream.expect(5); | |
| 71 stream.expect(6); | |
| 72 }, (errors) { | |
| 73 expect(errors, hasLength(1)); | |
| 74 expect(errors.first.error.message, equals( | |
| 75 "Expected: <6>\n" | |
| 76 " Emitted: * 1\n" | |
| 77 " * 2\n" | |
| 78 " * 3\n" | |
| 79 " * 4\n" | |
| 80 " * 5\n" | |
| 81 " Which: unexpected end of stream")); | |
| 82 }); | |
| 83 | |
| 84 expectTestPasses("expect() with matching matcher passes", () { | |
| 85 var stream = createStream(); | |
| 86 stream.expect(greaterThan(0)); | |
| 87 stream.expect(greaterThan(1)); | |
| 88 stream.expect(greaterThan(2)); | |
| 89 stream.expect(greaterThan(3)); | |
| 90 stream.expect(greaterThan(4)); | |
| 91 }); | |
| 92 | |
| 93 expectTestFails("expect() with a non-matching matcher fails", () { | |
| 94 var stream = createStream(); | |
| 95 stream.expect(greaterThan(0)); | |
| 96 stream.expect(greaterThan(1)); | |
| 97 stream.expect(greaterThan(100)); | |
| 98 }, (errors) { | |
| 99 expect(errors, hasLength(1)); | |
| 100 expect(errors.first.error.message, equals( | |
| 101 "Expected: a value greater than <100>\n" | |
| 102 " Emitted: * 1\n" | |
| 103 " * 2\n" | |
| 104 " * 3\n" | |
| 105 " Which: is not a value greater than <100>")); | |
| 106 }); | |
| 107 | |
| 108 expectTestPasses("nextValues() with matching values succeeds", () { | |
| 109 createStream().expect(nextValues(3, unorderedEquals([3, 2, 1]))); | |
| 110 }); | |
| 111 | |
| 112 expectTestFails("nextValues() without enough values fails", () { | |
| 113 createStream().expect(nextValues(6, unorderedEquals([3, 2, 1]))); | |
| 114 }, (errors) { | |
| 115 expect(errors, hasLength(1)); | |
| 116 expect(errors.first.error.message, equals( | |
| 117 "Expected: 6 values that equals [3, 2, 1] unordered\n" | |
| 118 " Emitted: * 1\n" | |
| 119 " * 2\n" | |
| 120 " * 3\n" | |
| 121 " * 4\n" | |
| 122 " * 5\n" | |
| 123 " Which: unexpected end of stream")); | |
| 124 }); | |
| 125 | |
| 126 expectTestFails("nextValues() with non-matching values fails", () { | |
| 127 createStream().expect(nextValues(3, unorderedEquals([2, 3, 4]))); | |
| 128 }, (errors) { | |
| 129 expect(errors, hasLength(1)); | |
| 130 expect(errors.first.error.message, equals( | |
| 131 "Expected: 3 values that equals [2, 3, 4] unordered\n" | |
| 132 " Emitted: * 1\n" | |
| 133 " * 2\n" | |
| 134 " * 3\n" | |
| 135 " Which: has no match for <4> at index 2")); | |
| 136 }); | |
| 137 | |
| 138 expectTestPasses("inOrder() with several values matches and consumes those " | |
| 139 "values", () { | |
| 140 var stream = createStream(); | |
| 141 stream.expect(inOrder([1, 2, 3, 4])); | |
| 142 stream.expect(5); | |
| 143 }); | |
| 144 | |
| 145 expectTestPasses("inOrder() with several stream matchers matches them", () { | |
| 146 createStream().expect(inOrder([ | |
| 147 consumeThrough(3), | |
| 148 nextValues(2, unorderedEquals([5, 4])) | |
| 149 ])); | |
| 150 }); | |
| 151 | |
| 152 expectTestPasses("inOrder() with no values succeeds and consumes nothing", | |
| 153 () { | |
| 154 var stream = createStream(); | |
| 155 stream.expect(inOrder([])); | |
| 156 stream.expect(1); | |
| 157 }); | |
| 158 | |
| 159 expectTestFails("inOrder() fails if a sub-matcher fails", () { | |
| 160 createStream().expect(inOrder([ | |
| 161 nextValues(3, unorderedEquals([2, 3, 4])), | |
| 162 consumeThrough(5) | |
| 163 ])); | |
| 164 }, (errors) { | |
| 165 expect(errors, hasLength(1)); | |
| 166 expect(errors.first.error.message, equals( | |
| 167 "Expected: * 3 values that equals [2, 3, 4] unordered\n" | |
| 168 " | * values followed by <5>\n" | |
| 169 " Emitted: * 1\n" | |
| 170 " * 2\n" | |
| 171 " * 3\n" | |
| 172 " Which: matcher #1 failed:\n" | |
| 173 " | has no match for <4> at index 2")); | |
| 174 }); | |
| 175 | |
| 176 expectTestFails("inOrder() with one value has a simpler description", () { | |
| 177 createStream().expect(inOrder([100])); | |
| 178 }, (errors) { | |
| 179 expect(errors, hasLength(1)); | |
| 180 expect(errors.first.error.message, equals( | |
| 181 "Expected: <100>\n" | |
| 182 " Emitted: * 1")); | |
| 183 }); | |
| 184 | |
| 185 expectTestPasses("consumeThrough() consumes values through the given matcher", | |
| 186 () { | |
| 187 var stream = createStream(); | |
| 188 stream.expect(consumeThrough(inOrder([2, 3]))); | |
| 189 stream.expect(4); | |
| 190 }); | |
| 191 | |
| 192 expectTestPasses("consumeThrough() will stop if the first value matches", () { | |
| 193 var stream = createStream(); | |
| 194 stream.expect(consumeThrough(inOrder([1, 2]))); | |
| 195 stream.expect(3); | |
| 196 }); | |
| 197 | |
| 198 expectTestFails("consumeThrough() will fail if the stream ends before the " | |
| 199 "value is reached", () { | |
| 200 createStream().expect(consumeThrough(inOrder([5, 6]))); | |
| 201 }, (errors) { | |
| 202 expect(errors, hasLength(1)); | |
| 203 expect(errors.first.error.message, equals( | |
| 204 "Expected: values followed by:\n" | |
| 205 " | * <5>\n" | |
| 206 " | * <6>\n" | |
| 207 " Emitted: * 1\n" | |
| 208 " * 2\n" | |
| 209 " * 3\n" | |
| 210 " * 4\n" | |
| 211 " * 5\n" | |
| 212 " Which: unexpected end of stream")); | |
| 213 }); | |
| 214 | |
| 215 expectTestPasses("consumeWhile() consumes values while the given matcher " | |
| 216 "matches", () { | |
| 217 var stream = createStream(); | |
| 218 stream.expect(consumeWhile(lessThan(4))); | |
| 219 stream.expect(4); | |
| 220 }); | |
| 221 | |
| 222 expectTestPasses("consumeWhile() consumes values in chunks", () { | |
| 223 var stream = createStream(); | |
| 224 stream.expect(consumeWhile(inOrder([anything, anything]))); | |
| 225 stream.expect(5); | |
| 226 }); | |
| 227 | |
| 228 expectTestPasses("consumeWhile() will stop if the first value doesn't match", | |
| 229 () { | |
| 230 var stream = createStream(); | |
| 231 stream.expect(consumeWhile(inOrder([2, 3]))); | |
| 232 stream.expect(1); | |
| 233 }); | |
| 234 | |
| 235 expectTestPasses("either() will match if the first branch matches", () { | |
| 236 createStream().expect(either(1, 100)); | |
| 237 }); | |
| 238 | |
| 239 expectTestPasses("either() will match if the second branch matches", () { | |
| 240 createStream().expect(either(100, 1)); | |
| 241 }); | |
| 242 | |
| 243 expectTestPasses("either() will consume the maximal number of values if both " | |
| 244 "branches match", () { | |
| 245 // First branch consumes more. | |
| 246 var stream = createStream(); | |
| 247 stream.expect(either(inOrder([1, 2, 3]), 1)); | |
| 248 stream.expect(4); | |
| 249 | |
| 250 // Second branch consumes more. | |
| 251 stream = createStream(); | |
| 252 stream.expect(either(1, inOrder([1, 2, 3]))); | |
| 253 stream.expect(4); | |
| 254 }); | |
| 255 | |
| 256 expectTestFails("either() will fail if neither branch matches", () { | |
| 257 createStream().expect(either( | |
| 258 inOrder([3, 2, 1]), | |
| 259 nextValues(4, unorderedEquals([5, 4, 3, 2])))); | |
| 260 }, (errors) { | |
| 261 expect(errors, hasLength(1)); | |
| 262 expect(errors.first.error.message, equals( | |
| 263 "Expected: either\n" | |
| 264 " | * <3>\n" | |
| 265 " | * <2>\n" | |
| 266 " | * <1>\n" | |
| 267 " | or\n" | |
| 268 " | 4 values that equals [5, 4, 3, 2] unordered\n" | |
| 269 " Emitted: * 1\n" | |
| 270 " * 2\n" | |
| 271 " * 3\n" | |
| 272 " * 4\n" | |
| 273 " Which: both\n" | |
| 274 " | matcher #1 failed\n" | |
| 275 " | and\n" | |
| 276 " | has no match for <5> at index 0")); | |
| 277 }); | |
| 278 | |
| 279 expectTestPasses("allow() consumes the matcher if it matches", () { | |
| 280 var stream = createStream(); | |
| 281 stream.expect(allow(inOrder([1, 2, 3]))); | |
| 282 stream.expect(4); | |
| 283 }); | |
| 284 | |
| 285 expectTestPasses("allow() consumes nothing if the matcher doesn't match", () { | |
| 286 var stream = createStream(); | |
| 287 stream.expect(allow(inOrder([1, 2, 100]))); | |
| 288 stream.expect(1); | |
| 289 }); | |
| 290 | |
| 291 expectTestPasses("never() consumes everything if the matcher never matches", | |
| 292 () { | |
| 293 var stream = createStream(); | |
| 294 stream.expect(never(inOrder([2, 1]))); | |
| 295 }); | |
| 296 | |
| 297 expectTestFails("never() fails if the matcher matches", () { | |
| 298 var stream = createStream(); | |
| 299 stream.expect(never(inOrder([2, 3]))); | |
| 300 }, (errors) { | |
| 301 expect(errors, hasLength(1)); | |
| 302 expect(errors.first.error.message, equals( | |
| 303 "Expected: never\n" | |
| 304 " | * <2>\n" | |
| 305 " | * <3>\n" | |
| 306 " Emitted: * 1\n" | |
| 307 " * 2\n" | |
| 308 " * 3\n" | |
| 309 " Which: matched\n" | |
| 310 " | * <2>\n" | |
| 311 " | * <3>")); | |
| 312 }); | |
| 313 | |
| 314 expectTestPasses("isDone succeeds at the end of the stream", () { | |
| 315 var stream = createStream(); | |
| 316 stream.expect(consumeThrough(5)); | |
| 317 stream.expect(isDone); | |
| 318 }); | |
| 319 | |
| 320 expectTestFails("isDone fails before the end of the stream", () { | |
| 321 var stream = createStream(); | |
| 322 stream.expect(consumeThrough(4)); | |
| 323 stream.expect(isDone); | |
| 324 }, (errors) { | |
| 325 expect(errors, hasLength(1)); | |
| 326 expect(errors.first.error.message, equals( | |
| 327 "Expected: is done\n" | |
| 328 " Emitted: * 1\n" | |
| 329 " * 2\n" | |
| 330 " * 3\n" | |
| 331 " * 4\n" | |
| 332 " * 5\n" | |
| 333 " Which: stream wasn't finished")); | |
| 334 }); | |
| 335 } | |
| OLD | NEW |