From 05157f8d5ba321a8886505a086db9f9f26d44ffe Mon Sep 17 00:00:00 2001 From: Gus Power Date: Tue, 20 May 2025 17:39:48 +0100 Subject: added some test macros to reduce noise in ipservice tests --- src/test_macros.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/test_macros.rs (limited to 'src/test_macros.rs') diff --git a/src/test_macros.rs b/src/test_macros.rs new file mode 100644 index 0000000..9f3ff2b --- /dev/null +++ b/src/test_macros.rs @@ -0,0 +1,73 @@ +#[macro_export] +macro_rules! assert_error { + ($result:expr, $pattern:pat => $body:block) => { + match $result { + Ok(_) => panic!("Expected an error, but got Ok result"), + Err(e) => match e { + $pattern => $body, + other => panic!("Expected {}, but got {:?}", stringify!($pattern), other), + }, + } + }; +} + +#[macro_export] +macro_rules! assert_file_not_found { + ($result:expr, $expected_path:expr) => { + assert_error!($result, AppError::FileNotFound(path) => { + assert_eq!(path, $expected_path, + "Expected file not found for path {:?}, but got path {:?}", + $expected_path, path); + }); + }; +} + +#[macro_export] +macro_rules! assert_io_error { + ($result:expr) => { + assert_error!($result, AppError::IoError(_) => {}); + }; + ($result:expr, $kind:pat) => { + assert_error!($result, AppError::IoError(err) => { + assert!(matches!(err.kind(), $kind), + "Expected IO error of kind {}, but got {:?}", + stringify!($kind), err.kind()); + }); + }; +} + +#[macro_export] +macro_rules! assert_config_parse_error { + ($result:expr, $expected_path:expr) => { + assert_error!($result, AppError::ConfigParseError { path, source: _ } => { + assert_eq!(path, $expected_path, + "Expected config parse error for path {:?}, but got path {:?}", + $expected_path, path); + }); + }; +} + +#[macro_export] +macro_rules! assert_request_failed { + ($result:expr, $expected_url:expr) => { + assert_error!($result, AppError::RequestFailed { url, source: _ } => { + assert_eq!(url, $expected_url, + "Expected request failed for URL {}, but got URL {}", + $expected_url, url); + }); + }; +} + +#[macro_export] +macro_rules! assert_invalid_response { + ($result:expr, $expected_url:expr, $reason_prefix:expr) => { + assert_error!($result, AppError::InvalidResponse{ url, reason } => { + assert_eq!(url, $expected_url, + "Expected invalid response for URL {}, but got URL {}", + $expected_url, url); + assert!(reason.starts_with($reason_prefix), + "Expected reason to start with '{}', but got '{}'", + $reason_prefix, reason); + }); + }; +} -- cgit v1.2.3