aboutsummaryrefslogtreecommitdiff
path: root/src/test_macros.rs
diff options
context:
space:
mode:
authorGus Power <gus@infinitesidequests.com>2025-05-20 17:39:48 +0100
committerGus Power <gus@infinitesidequests.com>2025-05-20 17:39:48 +0100
commit05157f8d5ba321a8886505a086db9f9f26d44ffe (patch)
tree7642dbd6a7d0fb57f80ddc6de029119c5db3abe6 /src/test_macros.rs
parent5c5e7c59f9ae8932eb87d00ec7e4fea389faffde (diff)
added some test macros to reduce noise in ipservice tests
Diffstat (limited to 'src/test_macros.rs')
-rw-r--r--src/test_macros.rs73
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);
+ });
+ };
+}