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