macro_rules! assert_command_stdout_matches {
($a_command:expr, $b_matcher:expr $(,)?) => { ... };
($a_command:expr, $b_matcher:expr, $($message:tt)+) => { ... };
}Expand description
Assert a command stdout string is a match to a regex.
-
If true, return
(). -
Otherwise, call
panic!with a message and the values of the expressions with their debug representations.
Examples
use std::process::Command;
use regex::Regex;
// Return Ok
let mut command = Command::new("printf");
command.args(["%s", "hello"]);
let matcher = Regex::new(r"el").unwrap();
assert_command_stdout_matches!(command, matcher);
//-> ()
// Panic with error message
let result = panic::catch_unwind(|| {
let mut command = Command::new("printf");
command.args(["%s", "hello"]);
let matcher = Regex::new(r"xyz").unwrap();
assert_command_stdout_matches!(command, matcher);
//-> panic!
});
assert!(result.is_err());
let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
let expect = concat!(
"assertion failed: `assert_command_stdout_matches!(left_command, right_matcher)`\n",
" left_command label: `command`,\n",
" left_command debug: `\"printf\" \"%s\" \"hello\"`,\n",
" right_matcher label: `matcher`,\n",
" right_matcher debug: `xyz`,\n",
" left: `\"hello\"`,\n",
" right: `xyz`"
);
assert_eq!(actual, expect);
// Panic with error message
let result = panic::catch_unwind(|| {
let mut command = Command::new("printf");
command.args(["%s", "hello"]);
let matcher = Regex::new(r"xyz").unwrap();
assert_command_stdout_matches!(command, matcher, "message");
//-> panic!
});
assert!(result.is_err());
let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
let expect = "message";
assert_eq!(actual, expect);