assertables/assert_command/
assert_command_stderr_gt.rs

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