From 101234e3007d8bef73f03ad562b5a3b51fbf754d Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 27 Apr 2022 13:47:46 +0530 Subject: [PATCH] chore: convert check_status into macro to satisfy borrow checker --- src/meta.rs | 2 +- src/tests.rs | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/meta.rs b/src/meta.rs index 1bccc90..7b66cc4 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -70,6 +70,6 @@ mod tests { let app = get_app!(ctx).await; let resp = get_request!(app, V1_API_ROUTES.meta.build_details); - assert!(tests::check_status(resp, StatusCode::OK).await); + check_status!(resp, StatusCode::OK); } } diff --git a/src/tests.rs b/src/tests.rs index 5883c7c..c9209e4 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -130,13 +130,22 @@ macro_rules! get_app { /// Utility function to check for status of a test response, attempt response payload serialization /// and print payload if response status doesn't match expected status -pub async fn check_status(resp: ServiceResponse, expected: StatusCode) -> bool { - let status = resp.status(); - if status != expected { - eprintln!("[error] Expected status code: {expected} received: {status}"); - let response: serde_json::Value = actix_web::test::read_body_json(resp).await; - eprintln!("[error] Body:\n{:#?}", response); - } - - status == expected +#[macro_export] +macro_rules! check_status { + ($resp:expr, $expected:expr) => { + let status = $resp.status(); + if status != $expected { + eprintln!( + "[error] Expected status code: {} received: {status}", + $expected + ); + let response: serde_json::Value = actix_web::test::read_body_json($resp).await; + eprintln!("[error] Body:\n{:#?}", response); + assert_eq!(status, $expected); + panic!() + } + { + assert_eq!(status, $expected); + } + }; }