assertables/assert_command/
assert_command_stderr_eq_x.rs

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