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 scheduled_test.stream_matcher; | 5 library scheduled_test.stream_matcher; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import '../scheduled_stream.dart'; | 10 import '../scheduled_stream.dart'; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 new _EitherMatcher(streamMatcher1, streamMatcher2); | 75 new _EitherMatcher(streamMatcher1, streamMatcher2); |
76 | 76 |
77 /// A matcher that consumes [streamMatcher] if it matches, or nothing otherwise. | 77 /// A matcher that consumes [streamMatcher] if it matches, or nothing otherwise. |
78 /// | 78 /// |
79 /// This matcher will always match a stream. It exists to consume values that | 79 /// This matcher will always match a stream. It exists to consume values that |
80 /// may or may not be emitted by a stream. | 80 /// may or may not be emitted by a stream. |
81 /// | 81 /// |
82 /// [streamMatcher] can be a [StreamMatcher], a [Matcher], or an [Object]. | 82 /// [streamMatcher] can be a [StreamMatcher], a [Matcher], or an [Object]. |
83 StreamMatcher allow(streamMatcher) => new _AllowMatcher(streamMatcher); | 83 StreamMatcher allow(streamMatcher) => new _AllowMatcher(streamMatcher); |
84 | 84 |
| 85 /// A matcher that asserts that a stream never emits values matching |
| 86 /// [streamMatcher]. |
| 87 /// |
| 88 /// This will consume the remainder of a stream. |
| 89 /// |
| 90 /// [streamMatcher] can be a [StreamMatcher], a [Matcher], or an [Object]. |
| 91 StreamMatcher never(streamMatcher) => new _NeverMatcher(streamMatcher); |
| 92 |
85 /// A matcher that matches a stream that emits no more values. | 93 /// A matcher that matches a stream that emits no more values. |
86 StreamMatcher get isDone => new _IsDoneMatcher(); | 94 StreamMatcher get isDone => new _IsDoneMatcher(); |
87 | 95 |
88 /// See [nextValue]. | 96 /// See [nextValue]. |
89 class _NextValueMatcher implements StreamMatcher { | 97 class _NextValueMatcher implements StreamMatcher { |
90 final Matcher _matcher; | 98 final Matcher _matcher; |
91 | 99 |
92 _NextValueMatcher(matcher) | 100 _NextValueMatcher(matcher) |
93 : _matcher = wrapMatcher(matcher); | 101 : _matcher = wrapMatcher(matcher); |
94 | 102 |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 }); | 280 }); |
273 } | 281 } |
274 | 282 |
275 String toString() { | 283 String toString() { |
276 return new StringDescription('allow\n') | 284 return new StringDescription('allow\n') |
277 .add(prefixLines(_matcher.toString())) | 285 .add(prefixLines(_matcher.toString())) |
278 .toString(); | 286 .toString(); |
279 } | 287 } |
280 } | 288 } |
281 | 289 |
| 290 /// See [never]. |
| 291 class _NeverMatcher implements StreamMatcher { |
| 292 final StreamMatcher _matcher; |
| 293 |
| 294 _NeverMatcher(streamMatcher) |
| 295 : _matcher = new StreamMatcher.wrap(streamMatcher); |
| 296 |
| 297 Future<Description> tryMatch(ScheduledStream stream) { |
| 298 consumeNext() { |
| 299 return stream.hasNext.then((hasNext) { |
| 300 if (!hasNext) return new Future.value(); |
| 301 |
| 302 var fork = stream.fork(); |
| 303 return _matcher.tryMatch(fork).whenComplete(fork.close) |
| 304 .then((description) { |
| 305 if (description != null) { |
| 306 return stream.next().then((_) => consumeNext()); |
| 307 } |
| 308 |
| 309 return new StringDescription("matched\n") |
| 310 .add(prefixLines(_matcher.toString(), prefix: ' ')); |
| 311 }); |
| 312 }); |
| 313 } |
| 314 |
| 315 return consumeNext(); |
| 316 } |
| 317 |
| 318 String toString() => |
| 319 'never\n${prefixLines(_matcher.toString(), prefix: ' ')}'; |
| 320 } |
| 321 |
282 /// See [isDone]. | 322 /// See [isDone]. |
283 class _IsDoneMatcher implements StreamMatcher { | 323 class _IsDoneMatcher implements StreamMatcher { |
284 _IsDoneMatcher(); | 324 _IsDoneMatcher(); |
285 | 325 |
286 Future<Description> tryMatch(ScheduledStream stream) { | 326 Future<Description> tryMatch(ScheduledStream stream) { |
287 return stream.hasNext.then((hasNext) { | 327 return stream.hasNext.then((hasNext) { |
288 if (!hasNext) return null; | 328 if (!hasNext) return null; |
289 return new StringDescription("stream wasn't finished"); | 329 return new StringDescription("stream wasn't finished"); |
290 }); | 330 }); |
291 } | 331 } |
292 | 332 |
293 String toString() => 'is done'; | 333 String toString() => 'is done'; |
294 } | 334 } |
OLD | NEW |