tower_test/macros.rs
1/// Asserts that the mock handle receives a new request equal to the given
2/// value.
3///
4/// On success, the [`SendResponse`] handle for the matched request is returned,
5/// allowing the caller to respond to the request. On failure, the macro panics.
6///
7/// # Examples
8///
9/// ```rust
10/// use tower_service::Service;
11/// use tower_test::{mock, assert_request_eq};
12/// use tokio_test::assert_ready;
13///
14/// # async fn test() {
15/// let (mut service, mut handle) = mock::spawn();
16///
17/// assert_ready!(service.poll_ready());
18///
19/// let response = service.call("hello");
20///
21/// assert_request_eq!(handle, "hello").send_response("world");
22///
23/// assert_eq!(response.await.unwrap(), "world");
24/// # }
25/// ```
26/// [`SendResponse`]: crate::mock::SendResponse
27#[macro_export]
28macro_rules! assert_request_eq {
29 ($mock_handle:expr, $expect:expr) => {
30 assert_request_eq!($mock_handle, $expect,)
31 };
32 ($mock_handle:expr, $expect:expr, $($arg:tt)*) => {{
33 let (actual, send_response) = match $mock_handle.next_request().await {
34 Some(r) => r,
35 None => panic!("expected a request but none was received."),
36 };
37
38 assert_eq!(actual, $expect, $($arg)*);
39 send_response
40 }};
41}