assertables/assert_command/
assert_command_stdout_string_contains.rs

1//! Assert a command stdout string contains a given containee.
2//!
3//! Pseudocode:<br>
4//! (command ⇒ stdout ⇒ string) contains (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-stdout");
13//! command.args(["%s", "alfa"]);
14//! let containee = "lf";
15//! assert_command_stdout_string_contains!(command, &containee);
16//! ```
17//!
18//! # Module macros
19//!
20//! * [`assert_command_stdout_string_contains`](macro@crate::assert_command_stdout_string_contains)
21//! * [`assert_command_stdout_string_contains_as_result`](macro@crate::assert_command_stdout_string_contains_as_result)
22//! * [`debug_assert_command_stdout_string_contains`](macro@crate::debug_assert_command_stdout_string_contains)
23
24/// Assert a command stdout string contains a given containee.
25///
26/// Pseudocode:<br>
27/// (command ⇒ stdout ⇒ string) contains (expr into string)
28///
29/// * If true, return Result `Ok(command ⇒ stdout ⇒ string)`.
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_stdout_string_contains`](macro@crate::assert_command_stdout_string_contains)
39/// * [`assert_command_stdout_string_contains_as_result`](macro@crate::assert_command_stdout_string_contains_as_result)
40/// * [`debug_assert_command_stdout_string_contains`](macro@crate::debug_assert_command_stdout_string_contains)
41///
42#[macro_export]
43macro_rules! assert_command_stdout_string_contains_as_result {
44    ($command:expr, $containee:expr $(,)?) => {
45        match ($command.output(), &$containee) {
46            (Ok(a), containee) => {
47                let a = String::from_utf8(a.stdout).unwrap();
48                if a.contains($containee) {
49                    Ok(a)
50                } else {
51                    Err(
52                        format!(
53                            concat!(
54                                "assertion failed: `assert_command_stdout_string_contains!(command, containee)`\n",
55                                "https://docs.rs/assertables/9.5.3/assertables/macro.assert_command_stdout_string_contains.html\n",
56                                "   command label: `{}`,\n",
57                                "   command debug: `{:?}`,\n",
58                                "   command value: `{:?}`,\n",
59                                " containee label: `{}`,\n",
60                                " containee debug: `{:?}`,\n",
61                                " containee value: `{:?}`"
62                            ),
63                            stringify!($command),
64                            $command,
65                            a,
66                            stringify!($containee),
67                            $containee,
68                            containee
69                        )
70                    )
71                }
72            },
73            (a, containee) => {
74                Err(
75                    format!(
76                        concat!(
77                            "assertion failed: `assert_command_stdout_string_contains!(command, containee)`\n",
78                            "https://docs.rs/assertables/9.5.3/assertables/macro.assert_command_stdout_string_contains.html\n",
79                            "   command label: `{}`,\n",
80                            "   command debug: `{:?}`,\n",
81                            "   command value: `{:?}`,\n",
82                            " containee label: `{}`,\n",
83                            " containee debug: `{:?}`,\n",
84                            " containee value: `{:?}`",
85                        ),
86                        stringify!($command),
87                        $command,
88                        a,
89                        stringify!($containee),
90                        $containee,
91                        containee
92                )
93                )
94            }
95        }
96    };
97}
98
99#[cfg(test)]
100mod test_assert_command_stdout_string_contains_as_result {
101    use std::process::Command;
102
103    #[test]
104    fn success() {
105        let mut a = Command::new("bin/printf-stdout");
106        a.args(["%s", "alfa"]);
107        let b = "lf";
108        let actual = assert_command_stdout_string_contains_as_result!(a, b);
109        assert_eq!(actual.unwrap(), "alfa");
110    }
111
112    #[test]
113    fn failure() {
114        let mut a = Command::new("bin/printf-stdout");
115        a.args(["%s", "alfa"]);
116        let b = "zz";
117        let actual = assert_command_stdout_string_contains_as_result!(a, b);
118        let message = concat!(
119            "assertion failed: `assert_command_stdout_string_contains!(command, containee)`\n",
120            "https://docs.rs/assertables/9.5.3/assertables/macro.assert_command_stdout_string_contains.html\n",
121            "   command label: `a`,\n",
122            "   command debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
123            "   command value: `\"alfa\"`,\n",
124            " containee label: `b`,\n",
125            " containee debug: `\"zz\"`,\n",
126            " containee value: `\"zz\"`",
127        );
128        assert_eq!(actual.unwrap_err(), message);
129    }
130}
131
132/// Assert a command stdout string contains a given containee.
133///
134/// Pseudocode:<br>
135/// (command ⇒ stdout ⇒ string) contains (expr into string)
136///
137/// * If true, return (command ⇒ stdout ⇒ string).
138///
139/// * Otherwise, call [`panic!`] with a message and the values of the
140///   expressions with their debug representations.
141///
142/// This uses [`::std::String`](https://doc.rust-lang.org/std/string/struct.String.html) method `contains`.
143///
144/// * The containee can be a &str, char, a slice of chars, or a function or
145/// closure that determines if a character contains.
146///
147/// # Examples
148///
149/// ```rust
150/// use assertables::*;
151/// # use std::panic;
152/// use std::process::Command;
153///
154/// # fn main() {
155/// let mut command = Command::new("bin/printf-stdout");
156/// command.args(["%s", "alfa"]);
157/// let containee = "lf";
158/// assert_command_stdout_string_contains!(command, &containee);
159///
160/// # let result = panic::catch_unwind(|| {
161/// // This will panic
162/// let mut command = Command::new("bin/printf-stdout");
163/// command.args(["%s", "alfa"]);
164/// let containee = "zz";
165/// assert_command_stdout_string_contains!(command, &containee);
166/// # });
167/// // assertion failed: `assert_command_stdout_string_contains!(command, containee)`
168/// // https://docs.rs/assertables/9.5.3/assertables/macro.assert_command_stdout_string_contains.html
169/// //    command label: `command`,
170/// //    command debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,
171/// //    command value: `\"alfa\"`,
172/// //  containee label: `&containee`,
173/// //  containee debug: `\"zz\"`,
174/// //  containee value: `\"zz\"`
175/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
176/// # let message = concat!(
177/// #     "assertion failed: `assert_command_stdout_string_contains!(command, containee)`\n",
178/// #     "https://docs.rs/assertables/9.5.3/assertables/macro.assert_command_stdout_string_contains.html\n",
179/// #     "   command label: `command`,\n",
180/// #     "   command debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
181/// #     "   command value: `\"alfa\"`,\n",
182/// #     " containee label: `&containee`,\n",
183/// #     " containee debug: `\"zz\"`,\n",
184/// #     " containee value: `\"zz\"`"
185/// # );
186/// # assert_eq!(actual, message);
187/// # }
188/// ```
189///
190/// # Module macros
191///
192/// * [`assert_command_stdout_string_contains`](macro@crate::assert_command_stdout_string_contains)
193/// * [`assert_command_stdout_string_contains_as_result`](macro@crate::assert_command_stdout_string_contains_as_result)
194/// * [`debug_assert_command_stdout_string_contains`](macro@crate::debug_assert_command_stdout_string_contains)
195///
196#[macro_export]
197macro_rules! assert_command_stdout_string_contains {
198    ($command:expr, $containee:expr $(,)?) => {
199        match $crate::assert_command_stdout_string_contains_as_result!($command, $containee) {
200            Ok(x) => x,
201            Err(err) => panic!("{}", err),
202        }
203    };
204    ($command:expr, $containee:expr, $($message:tt)+) => {
205        match $crate::assert_command_stdout_string_contains_as_result!($command, $containee) {
206            Ok(x) => x,
207            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
208        }
209    };
210}
211
212#[cfg(test)]
213mod test_assert_command_stdout_string_contains {
214    use std::panic;
215    use std::process::Command;
216
217    #[test]
218    fn success() {
219        let mut a = Command::new("bin/printf-stdout");
220        a.args(["%s", "alfa"]);
221        let b = "lf";
222        let actual = assert_command_stdout_string_contains!(a, b);
223        assert_eq!(actual, "alfa");
224    }
225
226    #[test]
227    fn failure() {
228        let result = panic::catch_unwind(|| {
229            let mut a = Command::new("bin/printf-stdout");
230            a.args(["%s", "alfa"]);
231            let b = "zz";
232            let _actual = assert_command_stdout_string_contains!(a, b);
233        });
234        let message = concat!(
235            "assertion failed: `assert_command_stdout_string_contains!(command, containee)`\n",
236            "https://docs.rs/assertables/9.5.3/assertables/macro.assert_command_stdout_string_contains.html\n",
237            "   command label: `a`,\n",
238            "   command debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
239            "   command value: `\"alfa\"`,\n",
240            " containee label: `b`,\n",
241            " containee debug: `\"zz\"`,\n",
242            " containee value: `\"zz\"`",
243        );
244        assert_eq!(
245            result
246                .unwrap_err()
247                .downcast::<String>()
248                .unwrap()
249                .to_string(),
250            message
251        );
252    }
253}
254
255/// Assert a command stdout string contains a given containee.
256///
257/// Pseudocode:<br>
258/// (command ⇒ stdout ⇒ string) contains (expr into string)
259///
260/// This macro provides the same statements as [`assert_command_stdout_string_contains`](macro.assert_command_stdout_string_contains.html),
261/// except this macro's statements are only enabled in non-optimized
262/// builds by default. An optimized build will not execute this macro's
263/// statements unless `-C debug-assertions` is passed to the compiler.
264///
265/// This macro is useful for checks that are too expensive to be present
266/// in a release build but may be helpful during development.
267///
268/// The result of expanding this macro is always type checked.
269///
270/// An unchecked assertion allows a program in an inconsistent state to
271/// keep running, which might have unexpected consequences but does not
272/// introduce unsafety as long as this only happens in safe code. The
273/// performance cost of assertions, however, is not measurable in general.
274/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
275/// after thorough profiling, and more importantly, only in safe code!
276///
277/// This macro is intended to work in a similar way to
278/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
279///
280/// # Module macros
281///
282/// * [`assert_command_stdout_string_contains`](macro@crate::assert_command_stdout_string_contains)
283/// * [`assert_command_stdout_string_contains`](macro@crate::assert_command_stdout_string_contains)
284/// * [`debug_assert_command_stdout_string_contains`](macro@crate::debug_assert_command_stdout_string_contains)
285///
286#[macro_export]
287macro_rules! debug_assert_command_stdout_string_contains {
288    ($($arg:tt)*) => {
289        if $crate::cfg!(debug_assertions) {
290            $crate::assert_command_stdout_string_contains!($($arg)*);
291        }
292    };
293}