assertables/assert_command/
assert_command_stderr_lt.rs

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