aboutsummaryrefslogtreecommitdiff
path: root/src/test_macros.rs
blob: e1998cd9a226796d555ce13725d14e787bf82332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#[macro_export]
macro_rules! test {
    (fn $name:ident() $body:block) => {
        #[test]
        fn $name() -> Result<(), Box<dyn std::error::Error>> {
            (|| -> Result<(), Box<dyn std::error::Error>> {
                $body;
                Ok(())
            })()
        }
    };
}

#[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_config_file_not_found {
    ($result:expr, $expected_path:expr) => {
        assert_error!($result, AppError::ConfigFileNotFound(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);
        });
    };
}