assertables/assert_command/
assert_command_stderr_ge.rs

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