1//! Additional combinators for testing streams.
2
3use futures_core::stream::Stream;
4
5pub use crate::assert_unmoved::AssertUnmoved;
6pub use crate::interleave_pending::InterleavePending;
7
8/// Additional combinators for testing streams.
9pub trait StreamTestExt: Stream {
10 /// Asserts that the given is not moved after being polled.
11 ///
12 /// A check for movement is performed each time the stream is polled
13 /// and when `Drop` is called.
14 ///
15 /// Aside from keeping track of the location at which the stream was first
16 /// polled and providing assertions, this stream adds no runtime behavior
17 /// and simply delegates to the child stream.
18 fn assert_unmoved(self) -> AssertUnmoved<Self>
19 where
20 Self: Sized,
21 {
22 AssertUnmoved::new(self)
23 }
24
25 /// Introduces an extra [`Poll::Pending`](futures_core::task::Poll::Pending)
26 /// in between each item of the stream.
27 ///
28 /// # Examples
29 ///
30 /// ```
31 /// use futures::task::Poll;
32 /// use futures::stream::{self, Stream};
33 /// use futures_test::task::noop_context;
34 /// use futures_test::stream::StreamTestExt;
35 /// use futures::pin_mut;
36 ///
37 /// let stream = stream::iter(vec![1, 2]).interleave_pending();
38 /// pin_mut!(stream);
39 ///
40 /// let mut cx = noop_context();
41 ///
42 /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Pending);
43 /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Ready(Some(1)));
44 /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Pending);
45 /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Ready(Some(2)));
46 /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Pending);
47 /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Ready(None));
48 /// ```
49 fn interleave_pending(self) -> InterleavePending<Self>
50 where
51 Self: Sized,
52 {
53 InterleavePending::new(self)
54 }
55}
56
57impl<St> StreamTestExt for St where St: Stream {}
58