cnf_lib/test/
assert.rs

1// Copyright (C) 2023 Andreas Hartmann <hartan@7x.de>
2// GNU General Public License v3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5//! Various asserts on [`Query`](crate::provider::Query) types.
6
7/// Assert that a query turned up an error.
8#[macro_export]
9macro_rules! is_err {
10    ($query:ident) => {
11        assert!(
12            $query.results.is_err(),
13            "expected query to contain error: {:#?}",
14            $query
15        );
16    };
17}
18pub use is_err;
19
20pub mod err {
21    //! Assertions for specific errors in tests.
22    //!
23    //! The errors are expected in the `results` member of a [`Query`](crate::provider::Query).
24
25    /// Assert on [`crate::provider::Error::NotFound`].
26    #[macro_export]
27    macro_rules! not_found {
28        ($query:ident) => {
29            assert!(
30                matches!($query.results, Err(ProviderError::NotFound(_))),
31                "query didn't hold expected error 'NotFound': {:#?}",
32                $query
33            );
34        };
35    }
36    pub use not_found;
37
38    /// Assert on [`crate::provider::Error::Requirements`].
39    #[macro_export]
40    macro_rules! requirements {
41        ($query:ident) => {
42            assert!(
43                matches!($query.results, Err(ProviderError::Requirements(_))),
44                "query didn't hold expected error 'Requirements': {:#?}",
45                $query
46            );
47        };
48    }
49    pub use requirements;
50
51    /// Assert on [`crate::provider::Error::Execution`].
52    #[macro_export]
53    macro_rules! execution {
54        ($query:ident) => {
55            assert!(
56                matches!($query.results, Err(ProviderError::Execution(_))),
57                "query didn't hold expected error 'Execution': {:#?}",
58                $query
59            );
60        };
61    }
62    pub use execution;
63
64    /// Assert on [`crate::provider::Error::ApplicationError`].
65    #[macro_export]
66    macro_rules! application {
67        ($query:ident) => {
68            assert!(
69                matches!($query.results, Err(ProviderError::ApplicationError(_))),
70                "query didn't hold expected error 'ApplicationError': {:#?}",
71                $query
72            );
73        };
74    }
75    pub use application;
76}