| 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 '../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(_, message) { |
| 35 initMetatest(message); |
| 36 |
| 37 setUpTimeout(); |
| 38 |
| 39 expectTestPasses("expect() with matching values passes", () { |
| 40 var stream = createStream(); |
| 41 stream.expect(1); |
| 42 stream.expect(2); |
| 43 stream.expect(3); |
| 44 stream.expect(4); |
| 45 stream.expect(5); |
| 46 }); |
| 47 |
| 48 expectTestFails("expect() with a non-matching value fails", () { |
| 49 var stream = createStream(); |
| 50 stream.expect(1); |
| 51 stream.expect(2); |
| 52 stream.expect(100); |
| 53 }, (errors) { |
| 54 expect(errors, hasLength(1)); |
| 55 expect(errors.first.error.message, equals( |
| 56 "Expected: <100>\n" |
| 57 " Emitted: * 1\n" |
| 58 " * 2\n" |
| 59 " * 3")); |
| 60 }); |
| 61 |
| 62 expectTestFails("expect() with too few values fails", () { |
| 63 var stream = createStream(); |
| 64 stream.expect(1); |
| 65 stream.expect(2); |
| 66 stream.expect(3); |
| 67 stream.expect(4); |
| 68 stream.expect(5); |
| 69 stream.expect(6); |
| 70 }, (errors) { |
| 71 expect(errors, hasLength(1)); |
| 72 expect(errors.first.error.message, equals( |
| 73 "Expected: <6>\n" |
| 74 " Emitted: * 1\n" |
| 75 " * 2\n" |
| 76 " * 3\n" |
| 77 " * 4\n" |
| 78 " * 5\n" |
| 79 " Which: unexpected end of stream")); |
| 80 }); |
| 81 |
| 82 expectTestPasses("expect() with matching matcher passes", () { |
| 83 var stream = createStream(); |
| 84 stream.expect(greaterThan(0)); |
| 85 stream.expect(greaterThan(1)); |
| 86 stream.expect(greaterThan(2)); |
| 87 stream.expect(greaterThan(3)); |
| 88 stream.expect(greaterThan(4)); |
| 89 }); |
| 90 |
| 91 expectTestFails("expect() with a non-matching matcher fails", () { |
| 92 var stream = createStream(); |
| 93 stream.expect(greaterThan(0)); |
| 94 stream.expect(greaterThan(1)); |
| 95 stream.expect(greaterThan(100)); |
| 96 }, (errors) { |
| 97 expect(errors, hasLength(1)); |
| 98 expect(errors.first.error.message, equals( |
| 99 "Expected: a value greater than <100>\n" |
| 100 " Emitted: * 1\n" |
| 101 " * 2\n" |
| 102 " * 3\n" |
| 103 " Which: is not a value greater than <100>")); |
| 104 }); |
| 105 |
| 106 expectTestPasses("nextValues() with matching values succeeds", () { |
| 107 createStream().expect(nextValues(3, unorderedEquals([3, 2, 1]))); |
| 108 }); |
| 109 |
| 110 expectTestFails("nextValues() without enough values fails", () { |
| 111 createStream().expect(nextValues(6, unorderedEquals([3, 2, 1]))); |
| 112 }, (errors) { |
| 113 expect(errors, hasLength(1)); |
| 114 expect(errors.first.error.message, equals( |
| 115 "Expected: 6 values that equals [3, 2, 1] unordered\n" |
| 116 " Emitted: * 1\n" |
| 117 " * 2\n" |
| 118 " * 3\n" |
| 119 " * 4\n" |
| 120 " * 5\n" |
| 121 " Which: unexpected end of stream")); |
| 122 }); |
| 123 |
| 124 expectTestFails("nextValues() with non-matching values fails", () { |
| 125 createStream().expect(nextValues(3, unorderedEquals([2, 3, 4]))); |
| 126 }, (errors) { |
| 127 expect(errors, hasLength(1)); |
| 128 expect(errors.first.error.message, equals( |
| 129 "Expected: 3 values that equals [2, 3, 4] unordered\n" |
| 130 " Emitted: * 1\n" |
| 131 " * 2\n" |
| 132 " * 3\n" |
| 133 " Which: has no match for element <4> at index 2")); |
| 134 }); |
| 135 |
| 136 expectTestPasses("inOrder() with several values matches and consumes those " |
| 137 "values", () { |
| 138 var stream = createStream(); |
| 139 stream.expect(inOrder([1, 2, 3, 4])); |
| 140 stream.expect(5); |
| 141 }); |
| 142 |
| 143 expectTestPasses("inOrder() with several stream matchers matches them", () { |
| 144 createStream().expect(inOrder([ |
| 145 consumeThrough(3), |
| 146 nextValues(2, unorderedEquals([5, 4])) |
| 147 ])); |
| 148 }); |
| 149 |
| 150 expectTestPasses("inOrder() with no values succeeds and consumes nothing", |
| 151 () { |
| 152 var stream = createStream(); |
| 153 stream.expect(inOrder([])); |
| 154 stream.expect(1); |
| 155 }); |
| 156 |
| 157 expectTestFails("inOrder() fails if a sub-matcher fails", () { |
| 158 createStream().expect(inOrder([ |
| 159 nextValues(3, unorderedEquals([2, 3, 4])), |
| 160 consumeThrough(5) |
| 161 ])); |
| 162 }, (errors) { |
| 163 expect(errors, hasLength(1)); |
| 164 expect(errors.first.error.message, equals( |
| 165 "Expected: * 3 values that equals [2, 3, 4] unordered\n" |
| 166 " | * values followed by <5>\n" |
| 167 " Emitted: * 1\n" |
| 168 " * 2\n" |
| 169 " * 3\n" |
| 170 " Which: matcher #1 failed:\n" |
| 171 " | has no match for element <4> at index 2")); |
| 172 }); |
| 173 |
| 174 expectTestFails("inOrder() with one value has a simpler description", () { |
| 175 createStream().expect(inOrder([100])); |
| 176 }, (errors) { |
| 177 expect(errors, hasLength(1)); |
| 178 expect(errors.first.error.message, equals( |
| 179 "Expected: <100>\n" |
| 180 " Emitted: * 1")); |
| 181 }); |
| 182 |
| 183 expectTestPasses("consumeThrough() consumes values through the given matcher", |
| 184 () { |
| 185 var stream = createStream(); |
| 186 stream.expect(consumeThrough(greaterThan(2))); |
| 187 stream.expect(4); |
| 188 }); |
| 189 |
| 190 expectTestPasses("consumeThrough() will stop if the first value matches", () { |
| 191 var stream = createStream(); |
| 192 stream.expect(consumeThrough(1)); |
| 193 stream.expect(2); |
| 194 }); |
| 195 |
| 196 expectTestFails("consumeThrough() will fail if the stream ends before the " |
| 197 "value is reached", () { |
| 198 createStream().expect(consumeThrough(100)); |
| 199 }, (errors) { |
| 200 expect(errors, hasLength(1)); |
| 201 expect(errors.first.error.message, equals( |
| 202 "Expected: values followed by <100>\n" |
| 203 " Emitted: * 1\n" |
| 204 " * 2\n" |
| 205 " * 3\n" |
| 206 " * 4\n" |
| 207 " * 5\n" |
| 208 " Which: unexpected end of stream")); |
| 209 }); |
| 210 |
| 211 expectTestPasses("either() will match if the first branch matches", () { |
| 212 createStream().expect(either(1, 100)); |
| 213 }); |
| 214 |
| 215 expectTestPasses("either() will match if the second branch matches", () { |
| 216 createStream().expect(either(100, 1)); |
| 217 }); |
| 218 |
| 219 expectTestPasses("either() will consume the maximal number of values if both " |
| 220 "branches match", () { |
| 221 // First branch consumes more. |
| 222 var stream = createStream(); |
| 223 stream.expect(either(inOrder([1, 2, 3]), 1)); |
| 224 stream.expect(4); |
| 225 |
| 226 // Second branch consumes more. |
| 227 stream = createStream(); |
| 228 stream.expect(either(1, inOrder([1, 2, 3]))); |
| 229 stream.expect(4); |
| 230 }); |
| 231 |
| 232 expectTestFails("either() will fail if neither branch matches", () { |
| 233 createStream().expect(either( |
| 234 inOrder([3, 2, 1]), |
| 235 nextValues(4, unorderedEquals([5, 4, 3, 2])))); |
| 236 }, (errors) { |
| 237 expect(errors, hasLength(1)); |
| 238 expect(errors.first.error.message, equals( |
| 239 "Expected: either\n" |
| 240 " | * <3>\n" |
| 241 " | * <2>\n" |
| 242 " | * <1>\n" |
| 243 " | or\n" |
| 244 " | 4 values that equals [5, 4, 3, 2] unordered\n" |
| 245 " Emitted: * 1\n" |
| 246 " * 2\n" |
| 247 " * 3\n" |
| 248 " * 4\n" |
| 249 " Which: both\n" |
| 250 " | matcher #1 failed\n" |
| 251 " | and\n" |
| 252 " | has no match for element <5> at index 0")); |
| 253 }); |
| 254 |
| 255 expectTestPasses("allow() consumes the matcher if it matches", () { |
| 256 var stream = createStream(); |
| 257 stream.expect(allow(inOrder([1, 2, 3]))); |
| 258 stream.expect(4); |
| 259 }); |
| 260 |
| 261 expectTestPasses("allow() consumes nothing if the matcher doesn't match", () { |
| 262 var stream = createStream(); |
| 263 stream.expect(allow(inOrder([1, 2, 100]))); |
| 264 stream.expect(1); |
| 265 }); |
| 266 |
| 267 expectTestPasses("isDone succeeds at the end of the stream", () { |
| 268 var stream = createStream(); |
| 269 stream.expect(consumeThrough(5)); |
| 270 stream.expect(isDone); |
| 271 }); |
| 272 |
| 273 expectTestFails("isDone fails before the end of the stream", () { |
| 274 var stream = createStream(); |
| 275 stream.expect(consumeThrough(4)); |
| 276 stream.expect(isDone); |
| 277 }, (errors) { |
| 278 expect(errors, hasLength(1)); |
| 279 expect(errors.first.error.message, equals( |
| 280 "Expected: is done\n" |
| 281 " Emitted: * 1\n" |
| 282 " * 2\n" |
| 283 " * 3\n" |
| 284 " * 4\n" |
| 285 " * 5\n" |
| 286 " Which: stream wasn't finished")); |
| 287 }); |
| 288 } |
| OLD | NEW |