Skip to main content

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.6/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.6/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.6/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.6/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            let expect = (vec![b'a', b'l', b'f', b'a'], vec![b'a', b'l', b'f', b'a']);
322            assert_eq!(actual, expect);
323        }
324    }
325
326    #[test]
327    fn lt() {
328        let result = panic::catch_unwind(|| {
329            let mut a = Command::new("bin/printf-stderr");
330            a.args(["%s", "alfa"]);
331            let mut b = Command::new("bin/printf-stderr");
332            b.args(["%s", "zz"]);
333            let _actual = assert_command_stderr_ge!(a, b);
334        });
335        let message = concat!(
336            "assertion failed: `assert_command_stderr_ge!(a_command, b_command)`\n",
337            "https://docs.rs/assertables/9.8.6/assertables/macro.assert_command_stderr_ge.html\n",
338            " a label: `a`,\n",
339            " a debug: `\"bin/printf-stderr\" \"%s\" \"alfa\"`,\n",
340            " a value: `[97, 108, 102, 97]`,\n",
341            " b label: `b`,\n",
342            " b debug: `\"bin/printf-stderr\" \"%s\" \"zz\"`,\n",
343            " b value: `[122, 122]`"
344        );
345        assert_eq!(
346            result
347                .unwrap_err()
348                .downcast::<String>()
349                .unwrap()
350                .to_string(),
351            message
352        );
353    }
354}
355
356/// Assert a command stderr string is greater than or equal to another.
357///
358/// This macro provides the same statements as [`assert_command_stderr_ge {`](macro.assert_command_stderr_ge {.html),
359/// except this macro's statements are only enabled in non-optimized
360/// builds by default. An optimized build will not execute this macro's
361/// statements unless `-C debug-assertions` is passed to the compiler.
362///
363/// This macro is useful for checks that are too expensive to be present
364/// in a release build but may be helpful during development.
365///
366/// The result of expanding this macro is always type checked.
367///
368/// An unchecked assertion allows a program in an inconsistent state to
369/// keep running, which might have unexpected consequences but does not
370/// introduce unsafety as long as this only happens in safe code. The
371/// performance cost of assertions, however, is not measurable in general.
372/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
373/// after thorough profiling, and more importantly, only in safe code!
374///
375/// This macro is intended to work in a similar way to
376/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
377///
378/// # Module macros
379///
380/// * [`assert_command_stderr_ge {`](macro@crate::assert_command_stderr_ge {)
381/// * [`assert_command_stderr_ge {`](macro@crate::assert_command_stderr_ge {)
382/// * [`debug_assert_command_stderr_ge {`](macro@crate::debug_assert_command_stderr_ge {)
383///
384#[macro_export]
385macro_rules! debug_assert_command_stderr_ge {
386    ($($arg:tt)*) => {
387        if cfg!(debug_assertions) {
388            $crate::assert_command_stderr_ge!($($arg)*);
389        }
390    };
391}
392
393#[cfg(test)]
394mod test_debug_assert_command_stderr_ge {
395    use std::panic;
396    use std::process::Command;
397
398    #[test]
399    fn gt() {
400        let mut a = Command::new("bin/printf-stderr");
401        a.args(["%s", "alfa"]);
402        let mut b = Command::new("bin/printf-stderr");
403        b.args(["%s", "aa"]);
404        for _ in 0..1 {
405            let _actual = debug_assert_command_stderr_ge!(a, b);
406            // assert_eq!(actual, (vec![b'a', b'l', b'f', b'a'], vec![b'a', b'a']));
407        }
408    }
409
410    #[test]
411    fn eq() {
412        let mut a = Command::new("bin/printf-stderr");
413        a.args(["%s", "alfa"]);
414        let mut b = Command::new("bin/printf-stderr");
415        b.args(["%s", "alfa"]);
416        for _ in 0..1 {
417            let _actual = debug_assert_command_stderr_ge!(a, b);
418            let _expect = (vec![b'a', b'l', b'f', b'a'], vec![b'a', b'l', b'f', b'a']);
419            // assert_eq!(actual, expect);
420        }
421    }
422
423    #[test]
424    fn lt() {
425        let result = panic::catch_unwind(|| {
426            let mut a = Command::new("bin/printf-stderr");
427            a.args(["%s", "alfa"]);
428            let mut b = Command::new("bin/printf-stderr");
429            b.args(["%s", "zz"]);
430            let _actual = debug_assert_command_stderr_ge!(a, b);
431        });
432        let message = concat!(
433            "assertion failed: `assert_command_stderr_ge!(a_command, b_command)`\n",
434            "https://docs.rs/assertables/9.8.6/assertables/macro.assert_command_stderr_ge.html\n",
435            " a label: `a`,\n",
436            " a debug: `\"bin/printf-stderr\" \"%s\" \"alfa\"`,\n",
437            " a value: `[97, 108, 102, 97]`,\n",
438            " b label: `b`,\n",
439            " b debug: `\"bin/printf-stderr\" \"%s\" \"zz\"`,\n",
440            " b value: `[122, 122]`"
441        );
442        assert_eq!(
443            result
444                .unwrap_err()
445                .downcast::<String>()
446                .unwrap()
447                .to_string(),
448            message
449        );
450    }
451}