assertables/assert_command/
assert_command_stderr_ge_x.rs

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