futures_test/assert.rs
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
use futures_core::stream::Stream;
#[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
///
/// ```
/// use futures::stream;
/// use futures_test::future::FutureTestExt;
/// use futures_test::{
/// assert_stream_pending, assert_stream_next, assert_stream_done,
/// };
/// use futures::pin_mut;
///
/// let 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::__private::assert::assert_is_unpin_stream(stream);
let stream = $crate::__private::Pin::new(stream);
let mut cx = $crate::task::noop_context();
let poll = $crate::__private::stream::Stream::poll_next(stream, &mut 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
///
/// ```
/// use futures::stream;
/// use futures_test::future::FutureTestExt;
/// use futures_test::{
/// assert_stream_pending, assert_stream_next, assert_stream_done,
/// };
/// use futures::pin_mut;
///
/// let 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::__private::assert::assert_is_unpin_stream(stream);
let stream = $crate::__private::Pin::new(stream);
let mut cx = $crate::task::noop_context();
match $crate::__private::stream::Stream::poll_next(stream, &mut cx) {
$crate::__private::task::Poll::Ready($crate::__private::Some(x)) => {
assert_eq!(x, $item);
}
$crate::__private::task::Poll::Ready($crate::__private::None) => {
panic!(
"assertion failed: expected stream to provide item but stream is at its end"
);
}
$crate::__private::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
///
/// ```
/// use futures::stream;
/// use futures_test::future::FutureTestExt;
/// use futures_test::{
/// assert_stream_pending, assert_stream_next, assert_stream_done,
/// };
/// use futures::pin_mut;
///
/// let 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::__private::assert::assert_is_unpin_stream(stream);
let stream = $crate::__private::Pin::new(stream);
let mut cx = $crate::task::noop_context();
match $crate::__private::stream::Stream::poll_next(stream, &mut cx) {
$crate::__private::task::Poll::Ready($crate::__private::Some(_)) => {
panic!("assertion failed: expected stream to be done but had more elements");
}
$crate::__private::task::Poll::Ready($crate::__private::None) => {}
$crate::__private::task::Poll::Pending => {
panic!("assertion failed: expected stream to be done but was pending");
}
}
}};
}