assertables/assert_status/
assert_status_success_false.rs

1//! Assert a status is a failure.
2//!
3//! Pseudocode:<br>
4//! a ⇒ status ⇒ success = false
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//! use std::process::Command;
11//!
12//! let mut a = Command::new("bin/exit-with-arg"); a.arg("1");
13//! assert_status_success_false!(a);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_status_success_false`](macro@crate::assert_status_success_false)
19//! * [`assert_status_success_false_as_result`](macro@crate::assert_status_success_false_as_result)
20//! * [`debug_assert_status_success_false`](macro@crate::debug_assert_status_success_false)
21
22/// Assert a status is a failure.
23///
24/// Pseudocode:<br>
25/// a ⇒ status ⇒ success = false
26///
27/// * If true, return Result `Ok(a ⇒ status ⇒ code ⇒ value)`.
28///
29/// * Otherwise, return Result `Err(message)`.
30///
31/// This macro is useful for runtime checks, such as checking parameters,
32/// or sanitizing inputs, or handling different results in different ways.
33///
34/// # Module macros
35///
36/// * [`assert_status_success_false`](macro@crate::assert_status_success_false)
37/// * [`assert_status_success_false_as_result`](macro@crate::assert_status_success_false_as_result)
38/// * [`debug_assert_status_success_false`](macro@crate::debug_assert_status_success_false)
39///
40#[macro_export]
41macro_rules! assert_status_success_false_as_result {
42    ($a:expr $(,)?) => {{
43        match ($a.status()) {
44            Ok(a1) => {
45                if !a1.success()  {
46                    Ok(true)
47                } else {
48                    Err(
49                        format!(
50                            concat!(
51                                "assertion failed: `assert_status_success_false!(a)`\n",
52                                "https://docs.rs/assertables/9.5.3/assertables/macro.assert_status_success_false.html\n",
53                                " a label: `{}`,\n",
54                                " a debug: `{:?}`",
55                            ),
56                            stringify!($a),
57                            $a,
58                        )
59                    )
60                }
61            },
62            a_status => {
63                Err(
64                    format!(
65                        concat!(
66                            "assertion failed: `assert_status_success_false!(a)`\n",
67                            "https://docs.rs/assertables/9.5.3/assertables/macro.assert_status_success_false.html\n",
68                            "  a label: `{}`,\n",
69                            "  a debug: `{:?}`,\n",
70                            " a status: `{:?}`",
71                        ),
72                        stringify!($a),
73                        $a,
74                        a_status
75                    )
76                )
77            }
78        }
79    }};
80}
81
82#[cfg(test)]
83mod test_assert_status_success_false_as_result {
84    use std::process::Command;
85
86    #[test]
87    fn success() {
88        let mut a = Command::new("bin/exit-with-arg");
89        a.arg("1");
90        let actual = assert_status_success_false_as_result!(a);
91        assert_eq!(actual.unwrap(), true);
92    }
93
94    #[test]
95    fn failure() {
96        let mut a = Command::new("bin/exit-with-arg");
97        a.arg("0");
98        let actual = assert_status_success_false_as_result!(a);
99        let message = concat!(
100            "assertion failed: `assert_status_success_false!(a)`\n",
101            "https://docs.rs/assertables/9.5.3/assertables/macro.assert_status_success_false.html\n",
102            " a label: `a`,\n",
103            " a debug: `\"bin/exit-with-arg\" \"0\"`",
104        );
105        assert_eq!(actual.unwrap_err(), message);
106    }
107}
108
109/// Assert a status is a failure.
110///
111/// Pseudocode:<br>
112/// a ⇒ status ⇒ success = false
113///
114/// * If true, return `a ⇒ status ⇒ code ⇒ value``.
115///
116/// * Otherwise, call [`panic!`] with a message and the values of the
117///   expressions with their debug representations.
118///
119/// # Examples
120///
121/// ```rust
122/// use assertables::*;
123/// use std::process::Command;
124/// # use std::panic;
125///
126/// # fn main() {
127/// let mut a = Command::new("bin/exit-with-arg"); a.arg("1");
128/// assert_status_success_false!(a);
129///
130/// # let result = panic::catch_unwind(|| {
131/// // This will panic
132/// let mut a = Command::new("bin/exit-with-arg"); a.arg("0");
133/// assert_status_success_false!(a);
134/// # });
135/// // assertion failed: `assert_status_success_false!(a)`
136/// // https://docs.rs/assertables/9.5.3/assertables/macro.assert_status_success_false.html
137/// //  a label: `a`,
138/// //  a debug: `\"bin/exit-with-arg\" \"0\"`
139/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
140/// # let message = concat!(
141/// #     "assertion failed: `assert_status_success_false!(a)`\n",
142/// #     "https://docs.rs/assertables/9.5.3/assertables/macro.assert_status_success_false.html\n",
143/// #     " a label: `a`,\n",
144/// #     " a debug: `\"bin/exit-with-arg\" \"0\"`"
145/// # );
146/// # assert_eq!(actual, message);
147/// # }
148/// ```
149///
150/// # Module macros
151///
152/// * [`assert_status_success_false`](macro@crate::assert_status_success_false)
153/// * [`assert_status_success_false_as_result`](macro@crate::assert_status_success_false_as_result)
154/// * [`debug_assert_status_success_false`](macro@crate::debug_assert_status_success_false)
155///
156#[macro_export]
157macro_rules! assert_status_success_false {
158    ($a:expr $(,)?) => {{
159        match $crate::assert_status_success_false_as_result!($a) {
160            Ok(x) => x,
161            Err(err) => panic!("{}", err),
162        }
163    }};
164    ($a:expr, $($message:tt)+) => {{
165        match $crate::assert_status_success_false_as_result!($a) {
166            Ok(x) => x,
167            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
168        }
169    }};
170}
171
172#[cfg(test)]
173mod test_assert_status_success_false {
174    use std::panic;
175    use std::process::Command;
176
177    #[test]
178    fn success() {
179        let mut a = Command::new("bin/exit-with-arg");
180        a.arg("1");
181        let actual = assert_status_success_false!(a);
182        assert_eq!(actual, true);
183    }
184
185    #[test]
186    fn failure() {
187        let result = panic::catch_unwind(|| {
188            let mut a = Command::new("bin/exit-with-arg");
189            a.arg("0");
190            let _actual = assert_status_success_false!(a);
191        });
192        let message = concat!(
193            "assertion failed: `assert_status_success_false!(a)`\n",
194            "https://docs.rs/assertables/9.5.3/assertables/macro.assert_status_success_false.html\n",
195            " a label: `a`,\n",
196            " a debug: `\"bin/exit-with-arg\" \"0\"`",
197        );
198        assert_eq!(
199            result
200                .unwrap_err()
201                .downcast::<String>()
202                .unwrap()
203                .to_string(),
204            message
205        );
206    }
207}
208
209/// Assert a status is a failure.
210///
211/// Pseudocode:<br>
212/// a ⇒ status ⇒ success = false
213///
214/// This macro provides the same statements as [`assert_status_success_false`](macro.assert_status_success_false.html),
215/// except this macro's statements are only enabled in non-optimized
216/// builds by default. An optimized build will not execute this macro's
217/// statements unless `-C debug-assertions` is passed to the compiler.
218///
219/// This macro is useful for checks that are too expensive to be present
220/// in a release build but may be helpful during development.
221///
222/// The result of expanding this macro is always type checked.
223///
224/// An unchecked assertion allows a "bin/exit-with-arg" in an inconsistent state to
225/// keep running, which might have unexpected consequences but does not
226/// introduce unsafety as long as this only happens in safe code. The
227/// performance cost of assertions, however, is not measurable in general.
228/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
229/// after thorough profiling, and more importantly, only in safe code!
230///
231/// This macro is intended to work in a similar way to
232/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
233///
234/// # Module macros
235///
236/// * [`assert_status_success_false`](macro@crate::assert_status_success_false)
237/// * [`assert_status_success_false`](macro@crate::assert_status_success_false)
238/// * [`debug_assert_status_success_false`](macro@crate::debug_assert_status_success_false)
239///
240#[macro_export]
241macro_rules! debug_assert_status_success_false {
242    ($($arg:tt)*) => {
243        if $crate::cfg!(debug_assertions) {
244            $crate::assert_status_success_false!($($arg)*);
245        }
246    };
247}