2019-04-23 23:17:57 -04:00
|
|
|
//! A collection of useful macros for testing futures and tokio based code
|
|
|
|
|
|
2019-06-24 12:34:30 -07:00
|
|
|
/// Assert a `Poll` is ready, returning the value.
|
2019-04-23 23:17:57 -04:00
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! assert_ready {
|
|
|
|
|
($e:expr) => {{
|
2019-06-24 12:34:30 -07:00
|
|
|
use core::task::Poll::*;
|
2019-04-23 23:17:57 -04:00
|
|
|
match $e {
|
2019-06-24 12:34:30 -07:00
|
|
|
Ready(v) => v,
|
|
|
|
|
Pending => panic!("pending"),
|
2019-04-23 23:17:57 -04:00
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
($e:expr, $($msg:tt),+) => {{
|
2019-06-24 12:34:30 -07:00
|
|
|
use core::task::Poll::*;
|
2019-04-23 23:17:57 -04:00
|
|
|
match $e {
|
2019-06-24 12:34:30 -07:00
|
|
|
Ready(v) => v,
|
|
|
|
|
Pending => {
|
2019-04-23 23:17:57 -04:00
|
|
|
let msg = format_args!($($msg),+);
|
2019-06-24 12:34:30 -07:00
|
|
|
panic!("pending; {}", msg)
|
2019-04-23 23:17:57 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 12:34:30 -07:00
|
|
|
/// Assert a `Poll<Result<...>>` is ready and `Ok`, returning the value.
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! assert_ready_ok {
|
|
|
|
|
($e:expr) => {{
|
|
|
|
|
use tokio_test::{assert_ready, assert_ok};
|
|
|
|
|
let val = assert_ready!($e);
|
|
|
|
|
assert_ok!(val)
|
|
|
|
|
}};
|
|
|
|
|
($e:expr, $($msg:tt),+) => {{
|
|
|
|
|
use tokio_test::{assert_ready, assert_ok};
|
|
|
|
|
let val = assert_ready!($e, $($msg),*);
|
|
|
|
|
assert_ok!(val, $($msg),*)
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Assert a `Poll<Result<...>>` is ready and `Err`, returning the error.
|
2019-04-23 23:17:57 -04:00
|
|
|
#[macro_export]
|
2019-06-24 12:34:30 -07:00
|
|
|
macro_rules! assert_ready_err {
|
2019-04-23 23:17:57 -04:00
|
|
|
($e:expr) => {{
|
2019-06-24 12:34:30 -07:00
|
|
|
use tokio_test::{assert_ready, assert_err};
|
|
|
|
|
let val = assert_ready!($e);
|
|
|
|
|
assert_err!(val)
|
|
|
|
|
}};
|
|
|
|
|
($e:expr, $($msg:tt),+) => {{
|
|
|
|
|
use tokio_test::{assert_ready, assert_err};
|
|
|
|
|
let val = assert_ready!($e, $($msg),*);
|
|
|
|
|
assert_err!(val, $($msg),*)
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Asset a `Poll` is pending.
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! assert_pending {
|
|
|
|
|
($e:expr) => {{
|
|
|
|
|
use core::task::Poll::*;
|
2019-04-23 23:17:57 -04:00
|
|
|
match $e {
|
2019-06-24 12:34:30 -07:00
|
|
|
Pending => {}
|
|
|
|
|
Ready(v) => panic!("ready; value = {:?}", v),
|
2019-04-23 23:17:57 -04:00
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
($e:expr, $($msg:tt),+) => {{
|
2019-06-24 12:34:30 -07:00
|
|
|
use core::task::Poll::*;
|
2019-04-23 23:17:57 -04:00
|
|
|
match $e {
|
2019-06-24 12:34:30 -07:00
|
|
|
Pending => {}
|
|
|
|
|
Ready(v) => {
|
2019-04-23 23:17:57 -04:00
|
|
|
let msg = format_args!($($msg),+);
|
|
|
|
|
panic!("ready; value = {:?}; {}", v, msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Assert if a poll is ready and check for equality on the value
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! assert_ready_eq {
|
|
|
|
|
($e:expr, $expect:expr) => {
|
2019-06-30 08:48:53 -07:00
|
|
|
let val = $crate::assert_ready!($e);
|
|
|
|
|
assert_eq!(val, $expect)
|
2019-04-23 23:17:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
($e:expr, $expect:expr, $($msg:tt),+) => {
|
2019-06-30 08:48:53 -07:00
|
|
|
let val = $crate::assert_ready!($e);
|
|
|
|
|
assert_eq!(val, $expect, $($msg),*)
|
2019-04-23 23:17:57 -04:00
|
|
|
};
|
|
|
|
|
}
|