assertables/assert_command/
assert_command_stderr_ne.rs

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