1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use futures_core::stream::Stream;
use std::marker::Unpin;

#[doc(hidden)]
pub fn assert_is_unpin_stream<S: Stream + Unpin>(_: &mut S) {}

/// Assert that the next poll to the provided stream will return
/// [`Poll::Pending`](futures_core::task::Poll::Pending).
///
/// # Examples
///
/// ```
/// #![feature(async_await, futures_api, pin)]
/// use futures::stream;
/// use futures_test::future::FutureTestExt;
/// use futures_test::{
///     assert_stream_pending, assert_stream_next, assert_stream_done,
/// };
/// use pin_utils::pin_mut;
///
/// let mut stream = stream::once((async { 5 }).pending_once());
/// pin_mut!(stream);
///
/// assert_stream_pending!(stream);
/// assert_stream_next!(stream, 5);
/// assert_stream_done!(stream);
/// ```
#[macro_export]
macro_rules! assert_stream_pending {
    ($stream:expr) => {{
        let mut stream = &mut $stream;
        $crate::assert::assert_is_unpin_stream(stream);
        let stream = $crate::std_reexport::pin::PinMut::new(stream);
        let cx = &mut $crate::task::no_spawn_context();
        let poll = $crate::futures_core_reexport::stream::Stream::poll_next(
            stream, cx,
        );
        if poll.is_ready() {
            panic!("assertion failed: stream is not pending");
        }
    }};
}

/// Assert that the next poll to the provided stream will return
/// [`Poll::Ready`](futures_core::task::Poll::Ready) with the provided item.
///
/// # Examples
///
/// ```
/// #![feature(async_await, futures_api, pin)]
/// use futures::stream;
/// use futures_test::future::FutureTestExt;
/// use futures_test::{
///     assert_stream_pending, assert_stream_next, assert_stream_done,
/// };
/// use pin_utils::pin_mut;
///
/// let mut stream = stream::once((async { 5 }).pending_once());
/// pin_mut!(stream);
///
/// assert_stream_pending!(stream);
/// assert_stream_next!(stream, 5);
/// assert_stream_done!(stream);
/// ```
#[macro_export]
macro_rules! assert_stream_next {
    ($stream:expr, $item:expr) => {{
        let mut stream = &mut $stream;
        $crate::assert::assert_is_unpin_stream(stream);
        let stream = $crate::std_reexport::pin::PinMut::new(stream);
        let cx = &mut $crate::task::no_spawn_context();
        match $crate::futures_core_reexport::stream::Stream::poll_next(stream, cx) {
            $crate::futures_core_reexport::task::Poll::Ready(Some(x)) => {
                assert_eq!(x, $item);
            }
            $crate::futures_core_reexport::task::Poll::Ready(None) => {
                panic!("assertion failed: expected stream to provide item but stream is at its end");
            }
            $crate::futures_core_reexport::task::Poll::Pending => {
                panic!("assertion failed: expected stream to provide item but stream wasn't ready");
            }
        }
    }}
}

/// Assert that the next poll to the provided stream will return an empty
/// [`Poll::Ready`](futures_core::task::Poll::Ready) signalling the
/// completion of the stream.
///
/// # Examples
///
/// ```
/// #![feature(async_await, futures_api, pin)]
/// use futures::stream;
/// use futures_test::future::FutureTestExt;
/// use futures_test::{
///     assert_stream_pending, assert_stream_next, assert_stream_done,
/// };
/// use pin_utils::pin_mut;
///
/// let mut stream = stream::once((async { 5 }).pending_once());
/// pin_mut!(stream);
///
/// assert_stream_pending!(stream);
/// assert_stream_next!(stream, 5);
/// assert_stream_done!(stream);
/// ```
#[macro_export]
macro_rules! assert_stream_done {
    ($stream:expr) => {{
        let mut stream = &mut $stream;
        $crate::assert::assert_is_unpin_stream(stream);
        let stream = $crate::std_reexport::pin::PinMut::new(stream);
        let cx = &mut $crate::task::no_spawn_context();
        match $crate::futures_core_reexport::stream::Stream::poll_next(stream, cx) {
            $crate::futures_core_reexport::task::Poll::Ready(Some(_)) => {
                panic!("assertion failed: expected stream to be done but had more elements");
            }
            $crate::futures_core_reexport::task::Poll::Ready(None) => {}
            $crate::futures_core_reexport::task::Poll::Pending => {
                panic!("assertion failed: expected stream to be done but was pending");
            }
        }
    }}
}