| 1 | use futures_core::stream::Stream; |
| 2 | |
| 3 | #[doc (hidden)] |
| 4 | pub fn assert_is_unpin_stream<S: Stream + Unpin>(_: &mut S) {} |
| 5 | |
| 6 | /// Assert that the next poll to the provided stream will return |
| 7 | /// [`Poll::Pending`](futures_core::task::Poll::Pending). |
| 8 | /// |
| 9 | /// # Examples |
| 10 | /// |
| 11 | /// ``` |
| 12 | /// use futures::stream; |
| 13 | /// use futures_test::future::FutureTestExt; |
| 14 | /// use futures_test::{ |
| 15 | /// assert_stream_pending, assert_stream_next, assert_stream_done, |
| 16 | /// }; |
| 17 | /// use futures::pin_mut; |
| 18 | /// |
| 19 | /// let stream = stream::once((async { 5 }).pending_once()); |
| 20 | /// pin_mut!(stream); |
| 21 | /// |
| 22 | /// assert_stream_pending!(stream); |
| 23 | /// assert_stream_next!(stream, 5); |
| 24 | /// assert_stream_done!(stream); |
| 25 | /// ``` |
| 26 | #[macro_export ] |
| 27 | macro_rules! assert_stream_pending { |
| 28 | ($stream:expr) => {{ |
| 29 | let mut stream = &mut $stream; |
| 30 | $crate::__private::assert::assert_is_unpin_stream(stream); |
| 31 | let stream = $crate::__private::Pin::new(stream); |
| 32 | let mut cx = $crate::task::noop_context(); |
| 33 | let poll = $crate::__private::stream::Stream::poll_next(stream, &mut cx); |
| 34 | if poll.is_ready() { |
| 35 | panic!("assertion failed: stream is not pending" ); |
| 36 | } |
| 37 | }}; |
| 38 | } |
| 39 | |
| 40 | /// Assert that the next poll to the provided stream will return |
| 41 | /// [`Poll::Ready`](futures_core::task::Poll::Ready) with the provided item. |
| 42 | /// |
| 43 | /// # Examples |
| 44 | /// |
| 45 | /// ``` |
| 46 | /// use futures::stream; |
| 47 | /// use futures_test::future::FutureTestExt; |
| 48 | /// use futures_test::{ |
| 49 | /// assert_stream_pending, assert_stream_next, assert_stream_done, |
| 50 | /// }; |
| 51 | /// use futures::pin_mut; |
| 52 | /// |
| 53 | /// let stream = stream::once((async { 5 }).pending_once()); |
| 54 | /// pin_mut!(stream); |
| 55 | /// |
| 56 | /// assert_stream_pending!(stream); |
| 57 | /// assert_stream_next!(stream, 5); |
| 58 | /// assert_stream_done!(stream); |
| 59 | /// ``` |
| 60 | #[macro_export ] |
| 61 | macro_rules! assert_stream_next { |
| 62 | ($stream:expr, $item:expr) => {{ |
| 63 | let mut stream = &mut $stream; |
| 64 | $crate::__private::assert::assert_is_unpin_stream(stream); |
| 65 | let stream = $crate::__private::Pin::new(stream); |
| 66 | let mut cx = $crate::task::noop_context(); |
| 67 | match $crate::__private::stream::Stream::poll_next(stream, &mut cx) { |
| 68 | $crate::__private::task::Poll::Ready($crate::__private::Some(x)) => { |
| 69 | assert_eq!(x, $item); |
| 70 | } |
| 71 | $crate::__private::task::Poll::Ready($crate::__private::None) => { |
| 72 | panic!( |
| 73 | "assertion failed: expected stream to provide item but stream is at its end" |
| 74 | ); |
| 75 | } |
| 76 | $crate::__private::task::Poll::Pending => { |
| 77 | panic!("assertion failed: expected stream to provide item but stream wasn't ready" ); |
| 78 | } |
| 79 | } |
| 80 | }}; |
| 81 | } |
| 82 | |
| 83 | /// Assert that the next poll to the provided stream will return an empty |
| 84 | /// [`Poll::Ready`](futures_core::task::Poll::Ready) signalling the |
| 85 | /// completion of the stream. |
| 86 | /// |
| 87 | /// # Examples |
| 88 | /// |
| 89 | /// ``` |
| 90 | /// use futures::stream; |
| 91 | /// use futures_test::future::FutureTestExt; |
| 92 | /// use futures_test::{ |
| 93 | /// assert_stream_pending, assert_stream_next, assert_stream_done, |
| 94 | /// }; |
| 95 | /// use futures::pin_mut; |
| 96 | /// |
| 97 | /// let stream = stream::once((async { 5 }).pending_once()); |
| 98 | /// pin_mut!(stream); |
| 99 | /// |
| 100 | /// assert_stream_pending!(stream); |
| 101 | /// assert_stream_next!(stream, 5); |
| 102 | /// assert_stream_done!(stream); |
| 103 | /// ``` |
| 104 | #[macro_export ] |
| 105 | macro_rules! assert_stream_done { |
| 106 | ($stream:expr) => {{ |
| 107 | let mut stream = &mut $stream; |
| 108 | $crate::__private::assert::assert_is_unpin_stream(stream); |
| 109 | let stream = $crate::__private::Pin::new(stream); |
| 110 | let mut cx = $crate::task::noop_context(); |
| 111 | match $crate::__private::stream::Stream::poll_next(stream, &mut cx) { |
| 112 | $crate::__private::task::Poll::Ready($crate::__private::Some(_)) => { |
| 113 | panic!("assertion failed: expected stream to be done but had more elements" ); |
| 114 | } |
| 115 | $crate::__private::task::Poll::Ready($crate::__private::None) => {} |
| 116 | $crate::__private::task::Poll::Pending => { |
| 117 | panic!("assertion failed: expected stream to be done but was pending" ); |
| 118 | } |
| 119 | } |
| 120 | }}; |
| 121 | } |
| 122 | |