diff options
Diffstat (limited to 'src/test_macros.rs')
| -rw-r--r-- | src/test_macros.rs | 73 |
1 files changed, 73 insertions, 0 deletions
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); + }); + }; +} |
