assertables/assert_command/
assert_command_stderr_le_x.rs

1//! Assert a command stderr string is less than or equal to an expression.
2//!
3//! Pseudocode:<br>
4//! (command ⇒ stderr) = (expr into string)
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//! use std::process::Command;
11//!
12//! let mut command = Command::new("bin/printf-stderr");
13//! command.args(["%s", "alfa"]);
14//! let bytes = vec![b'z', b'z'];
15//! assert_command_stderr_le_x!(command, bytes);
16//! ```
17//!
18//! # Module macros
19//!
20//! * [`assert_command_stderr_le_x`](macro@crate::assert_command_stderr_le_x)
21//! * [`assert_command_stderr_le_x_as_result`](macro@crate::assert_command_stderr_le_x_as_result)
22//! * [`debug_assert_command_stderr_le_x`](macro@crate::debug_assert_command_stderr_le_x)
23
24/// Assert a command stderr string is less than or equal to an expression.
25///
26/// Pseudocode:<br>
27/// (command ⇒ stderr) = (expr into string)
28///
29/// * If true, return Result `Ok(stderr)`.
30///
31/// * Otherwise, return Result `Err(message)`.
32///
33/// This macro is useful for runtime checks, such as checking parameters,
34/// or sanitizing inputs, or handling different results in different ways.
35///
36/// # Module macros
37///
38/// * [`assert_command_stderr_le_x`](macro@crate::assert_command_stderr_le_x)
39/// * [`assert_command_stderr_le_x_as_result`](macro@crate::assert_command_stderr_le_x_as_result)
40/// * [`debug_assert_command_stderr_le_x`](macro@crate::debug_assert_command_stderr_le_x)
41///
42#[macro_export]
43macro_rules! assert_command_stderr_le_x_as_result {
44    ($a_command:expr, $b_expr:expr $(,)?) => {
45        match ($a_command.output(), &$b_expr) {
46            (Ok(a_output), b_expr) => {
47                let a = a_output.stderr;
48                if a.le(b_expr) {
49                    Ok(a)
50                } else {
51                    Err(format!(
52                        concat!(
53                            "assertion failed: `assert_command_stderr_le_x!(command, expr)`\n",
54                            "https://docs.rs/assertables/9.8.3/assertables/macro.assert_command_stderr_le_x.html\n",
55                            " command label: `{}`,\n",
56                            " command debug: `{:?}`,\n",
57                            " command value: `{:?}`,\n",
58                            "    expr label: `{}`,\n",
59                            "    expr debug: `{:?}`,\n",
60                            "    expr value: `{:?}`"
61                        ),
62                        stringify!($a_command),
63                        $a_command,
64                        a,
65                        stringify!($b_expr),
66                        $b_expr,
67                        b_expr
68                    ))
69                }
70            }
71            (a, b) => Err(format!(
72                concat!(
73                    "assertion failed: `assert_command_stderr_le_x!(command, expr)`\n",
74                    "https://docs.rs/assertables/9.8.3/assertables/macro.assert_command_stderr_le_x.html\n",
75                    " command label: `{}`,\n",
76                    " command debug: `{:?}`,\n",
77                    " command value: `{:?}`,\n",
78                    "    expr label: `{}`,\n",
79                    "    expr debug: `{:?}`,\n",
80                    "    expr value: `{:?}`"
81                ),
82                stringify!($a_command),
83                $a_command,
84                a,
85                stringify!($b_expr),
86                $b_expr,
87                b
88            )),
89        }
90    };
91}
92
93#[cfg(test)]
94mod test_assert_command_stderr_le_x_as_result {
95    use std::process::Command;
96    use std::sync::Once;
97
98    #[test]
99    fn lt() {
100        let mut a = Command::new("bin/printf-stderr");
101        a.args(["%s", "alfa"]);
102        let b = vec![b'z', b'z'];
103        for _ in 0..1 {
104            let actual = assert_command_stderr_le_x_as_result!(a, b);
105            assert_eq!(actual.unwrap(), vec![b'a', b'l', b'f', b'a']);
106        }
107    }
108
109    #[test]
110    fn lt_once() {
111        static A: Once = Once::new();
112        fn a() -> Command {
113            if A.is_completed() {
114                panic!("A.is_completed()")
115            } else {
116                A.call_once(|| {})
117            }
118            let mut a = Command::new("bin/printf-stderr");
119            a.args(["%s", "alfa"]);
120            a
121        }
122
123        static B: Once = Once::new();
124        fn b() -> Vec<u8> {
125            if B.is_completed() {
126                panic!("B.is_completed()")
127            } else {
128                B.call_once(|| {})
129            }
130            vec![b'z', b'z']
131        }
132
133        assert_eq!(A.is_completed(), false);
134        assert_eq!(B.is_completed(), false);
135        let result = assert_command_stderr_le_x_as_result!(a(), b());
136        assert!(result.is_ok());
137        assert_eq!(A.is_completed(), true);
138        assert_eq!(B.is_completed(), true);
139    }
140
141    #[test]
142    fn eq() {
143        let mut a = Command::new("bin/printf-stderr");
144        a.args(["%s", "alfa"]);
145        let b = vec![b'a', b'l', b'f', b'a'];
146        for _ in 0..1 {
147            let actual = assert_command_stderr_le_x_as_result!(a, b);
148            assert_eq!(actual.unwrap(), vec![b'a', b'l', b'f', b'a']);
149        }
150    }
151
152    #[test]
153    fn eq_once() {
154        static A: Once = Once::new();
155        fn a() -> Command {
156            if A.is_completed() {
157                panic!("A.is_completed()")
158            } else {
159                A.call_once(|| {})
160            }
161            let mut a = Command::new("bin/printf-stderr");
162            a.args(["%s", "alfa"]);
163            a
164        }
165
166        static B: Once = Once::new();
167        fn b() -> Vec<u8> {
168            if B.is_completed() {
169                panic!("B.is_completed()")
170            } else {
171                B.call_once(|| {})
172            }
173            vec![b'a', b'l', b'f', b'a']
174        }
175
176        assert_eq!(A.is_completed(), false);
177        assert_eq!(B.is_completed(), false);
178        let result = assert_command_stderr_le_x_as_result!(a(), b());
179        assert!(result.is_ok());
180        assert_eq!(A.is_completed(), true);
181        assert_eq!(B.is_completed(), true);
182    }
183
184    #[test]
185    fn gt() {
186        let mut a = Command::new("bin/printf-stderr");
187        a.args(["%s", "alfa"]);
188        let b = vec![b'a', b'a'];
189        let actual = assert_command_stderr_le_x_as_result!(a, b);
190        let message = concat!(
191            "assertion failed: `assert_command_stderr_le_x!(command, expr)`\n",
192            "https://docs.rs/assertables/9.8.3/assertables/macro.assert_command_stderr_le_x.html\n",
193            " command label: `a`,\n",
194            " command debug: `\"bin/printf-stderr\" \"%s\" \"alfa\"`,\n",
195            " command value: `[97, 108, 102, 97]`,\n",
196            "    expr label: `b`,\n",
197            "    expr debug: `[97, 97]`,\n",
198            "    expr value: `[97, 97]`"
199        );
200        assert_eq!(actual.unwrap_err(), message);
201    }
202}
203
204/// Assert a command stderr string is less than or equal to an expression.
205///
206/// Pseudocode:<br>
207/// (command ⇒ stderr) = (expr into string)
208///
209/// * If true, return `()`.
210///
211/// * Otherwise, call [`panic!`] with a message and the values of the
212///   expressions with their debug representations.
213///
214/// # Examples
215///
216/// ```rust
217/// use assertables::*;
218/// # use std::panic;
219/// use std::process::Command;
220///
221/// # fn main() {
222/// let mut command = Command::new("bin/printf-stderr");
223/// command.args(["%s", "alfa"]);
224/// let bytes = vec![b'z', b'z'];
225/// assert_command_stderr_le_x!(command, bytes);
226///
227/// # let result = panic::catch_unwind(|| {
228/// // This will panic
229/// let mut command = Command::new("bin/printf-stderr");
230/// command.args(["%s", "alfa"]);
231/// let bytes = vec![b'a', b'a'];
232/// assert_command_stderr_le_x!(command, bytes);
233/// # });
234/// // assertion failed: `assert_command_stderr_le_x!(command, expr)`
235/// // https://docs.rs/assertables/…/assertables/macro.assert_command_stderr_le_x.html
236/// //  command label: `command`,
237/// //  command debug: `\"bin/printf-stderr\" \"%s\" \"alfa\"`,
238/// //  command value: `[97, 108, 102, 97]`,
239/// //     expr label: `bytes`,
240/// //     expr debug: `[97, 97]`,
241/// //     expr value: `[97, 97]`
242/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
243/// # let message = concat!(
244/// #     "assertion failed: `assert_command_stderr_le_x!(command, expr)`\n",
245/// #     "https://docs.rs/assertables/9.8.3/assertables/macro.assert_command_stderr_le_x.html\n",
246/// #     " command label: `command`,\n",
247/// #     " command debug: `\"bin/printf-stderr\" \"%s\" \"alfa\"`,\n",
248/// #     " command value: `[97, 108, 102, 97]`,\n",
249/// #     "    expr label: `bytes`,\n",
250/// #     "    expr debug: `[97, 97]`,\n",
251/// #     "    expr value: `[97, 97]`"
252/// # );
253/// # assert_eq!(actual, message);
254/// # }
255/// ```
256///
257/// # Module macros
258///
259/// * [`assert_command_stderr_le_x`](macro@crate::assert_command_stderr_le_x)
260/// * [`assert_command_stderr_le_x_as_result`](macro@crate::assert_command_stderr_le_x_as_result)
261/// * [`debug_assert_command_stderr_le_x`](macro@crate::debug_assert_command_stderr_le_x)
262///
263#[macro_export]
264macro_rules! assert_command_stderr_le_x {
265    ($a_command:expr, $b_expr:expr $(,)?) => {
266        match $crate::assert_command_stderr_le_x_as_result!($a_command, $b_expr) {
267            Ok(x) => x,
268            Err(err) => panic!("{}", err),
269        }
270    };
271    ($a_command:expr, $b_expr:expr, $($message:tt)+) => {
272        match $crate::assert_command_stderr_le_x_as_result!($a_command, $b_expr) {
273            Ok(x) => x,
274            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
275        }
276    };
277}
278
279#[cfg(test)]
280mod test_assert_command_stderr_le_x {
281    use std::panic;
282    use std::process::Command;
283
284    #[test]
285    fn lt() {
286        let mut a = Command::new("bin/printf-stderr");
287        a.args(["%s", "alfa"]);
288        let b = vec![b'z', b'z'];
289        for _ in 0..1 {
290            let actual = assert_command_stderr_le_x!(a, b);
291            assert_eq!(actual, vec![b'a', b'l', b'f', b'a']);
292        }
293    }
294
295    #[test]
296    fn eq() {
297        let mut a = Command::new("bin/printf-stderr");
298        a.args(["%s", "alfa"]);
299        let b = vec![b'a', b'l', b'f', b'a'];
300        for _ in 0..1 {
301            let actual = assert_command_stderr_le_x!(a, b);
302            assert_eq!(actual, vec![b'a', b'l', b'f', b'a']);
303        }
304    }
305
306    #[test]
307    fn gt() {
308        let result = panic::catch_unwind(|| {
309            let mut a = Command::new("bin/printf-stderr");
310            a.args(["%s", "alfa"]);
311            let b = vec![b'a', b'a'];
312            let _actual = assert_command_stderr_le_x!(a, b);
313        });
314        let message = concat!(
315            "assertion failed: `assert_command_stderr_le_x!(command, expr)`\n",
316            "https://docs.rs/assertables/9.8.3/assertables/macro.assert_command_stderr_le_x.html\n",
317            " command label: `a`,\n",
318            " command debug: `\"bin/printf-stderr\" \"%s\" \"alfa\"`,\n",
319            " command value: `[97, 108, 102, 97]`,\n",
320            "    expr label: `b`,\n",
321            "    expr debug: `[97, 97]`,\n",
322            "    expr value: `[97, 97]`"
323        );
324        assert_eq!(
325            result
326                .unwrap_err()
327                .downcast::<String>()
328                .unwrap()
329                .to_string(),
330            message
331        );
332    }
333}
334
335/// Assert a command stderr string is less than or equal to an expression.
336///
337/// Pseudocode:<br>
338/// (command ⇒ stderr) = (expr into string)
339///
340/// This macro provides the same statements as [`assert_command_stderr_le_x`](macro.assert_command_stderr_le_x.html),
341/// except this macro's statements are only enabled in non-optimized
342/// builds by default. An optimized build will not execute this macro's
343/// statements unless `-C debug-assertions` is passed to the compiler.
344///
345/// This macro is useful for checks that are too expensive to be present
346/// in a release build but may be helpful during development.
347///
348/// The result of expanding this macro is always type checked.
349///
350/// An unchecked assertion allows a program in an inconsistent state to
351/// keep running, which might have unexpected consequences but does not
352/// introduce unsafety as long as this only happens in safe code. The
353/// performance cost of assertions, however, is not measurable in general.
354/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
355/// after thorough profiling, and more importantly, only in safe code!
356///
357/// This macro is intended to work in a similar way to
358/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
359///
360/// # Module macros
361///
362/// * [`assert_command_stderr_le_x`](macro@crate::assert_command_stderr_le_x)
363/// * [`assert_command_stderr_le_x`](macro@crate::assert_command_stderr_le_x)
364/// * [`debug_assert_command_stderr_le_x`](macro@crate::debug_assert_command_stderr_le_x)
365///
366#[macro_export]
367macro_rules! debug_assert_command_stderr_le_x {
368    ($($arg:tt)*) => {
369        if $crate::cfg!(debug_assertions) {
370            $crate::assert_command_stderr_le_x!($($arg)*);
371        }
372    };
373}