#[macro_export]
macro_rules! assert_command_stdout_string_is_match_as_result {
($command:expr, $matcher:expr $(,)?) => {
match ($command.output(), &$matcher) {
(Ok(a), matcher) => {
let a = String::from_utf8(a.stdout).unwrap();
if matcher.is_match(&a) {
Ok(a)
} else {
Err(
format!(
concat!(
"assertion failed: `assert_command_stdout_string_is_match!(command, matcher)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_command_stdout_string_is_match.html\n",
" command label: `{}`,\n",
" command debug: `{:?}`,\n",
" command value: `{:?}`,\n",
" matcher label: `{}`,\n",
" matcher debug: `{:?}`,\n",
" matcher value: `{:?}`"
),
stringify!($command),
$command,
a,
stringify!($matcher),
$matcher,
matcher
)
)
}
},
(a, matcher) => {
Err(
format!(
concat!(
"assertion failed: `assert_command_stdout_string_is_match!(command, matcher)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_command_stdout_string_is_match.html\n",
" command label: `{}`,\n",
" command debug: `{:?}`,\n",
" command value: `{:?}`,\n",
" matcher label: `{}`,\n",
" matcher debug: `{:?}`,\n",
" matcher value: `{:?}`"
),
stringify!($command),
$command,
a,
stringify!($matcher),
$matcher,
matcher
)
)
}
}
};
}
#[cfg(test)]
mod test_assert_command_stdout_string_is_match_as_result {
use regex::Regex;
use std::process::Command;
use std::sync::Once;
#[test]
fn success() {
let mut a = Command::new("bin/printf-stdout");
a.args(["%s", "alfa"]);
let b = Regex::new(r"lf").expect("regex");
for _ in 0..1 {
let actual = assert_command_stdout_string_is_match_as_result!(a, b);
assert_eq!(actual.unwrap(), "alfa");
}
}
#[test]
fn success_once() {
static A: Once = Once::new();
fn a() -> Command {
if A.is_completed() {
panic!("A.is_completed()")
} else {
A.call_once(|| {})
}
let mut a = Command::new("bin/printf-stdout");
a.args(["%s", "alfa"]);
a
}
static B: Once = Once::new();
fn b() -> Regex {
if B.is_completed() {
panic!("B.is_completed()")
} else {
B.call_once(|| {})
}
Regex::new(r"lf").expect("regex")
}
assert_eq!(A.is_completed(), false);
assert_eq!(B.is_completed(), false);
let result = assert_command_stdout_string_is_match_as_result!(a(), b());
assert!(result.is_ok());
assert_eq!(A.is_completed(), true);
assert_eq!(B.is_completed(), true);
}
#[test]
fn failure() {
let mut a = Command::new("bin/printf-stdout");
a.args(["%s", "alfa"]);
let b = Regex::new(r"zz").expect("regex");
let actual = assert_command_stdout_string_is_match_as_result!(a, b);
let message = concat!(
"assertion failed: `assert_command_stdout_string_is_match!(command, matcher)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_command_stdout_string_is_match.html\n",
" command label: `a`,\n",
" command debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
" command value: `\"alfa\"`,\n",
" matcher label: `b`,\n",
" matcher debug: `Regex(\"zz\")`,\n",
" matcher value: `Regex(\"zz\")`"
);
assert_eq!(actual.unwrap_err(), message);
}
}
#[macro_export]
macro_rules! assert_command_stdout_string_is_match {
($command:expr, $matcher:expr $(,)?) => {
match $crate::assert_command_stdout_string_is_match_as_result!($command, $matcher) {
Ok(x) => x,
Err(err) => panic!("{}", err),
}
};
($command:expr, $matcher:expr, $($message:tt)+) => {
match $crate::assert_command_stdout_string_is_match_as_result!($command, $matcher) {
Ok(x) => x,
Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
}
};
}
#[cfg(test)]
mod test_assert_command_stdout_string_is_match {
use regex::Regex;
use std::panic;
use std::process::Command;
#[test]
fn success() {
let mut a = Command::new("bin/printf-stdout");
a.args(["%s", "alfa"]);
let b = Regex::new(r"lf").expect("regex");
for _ in 0..1 {
let actual = assert_command_stdout_string_is_match!(a, b);
assert_eq!(actual, "alfa");
}
}
#[test]
fn failure() {
let result = panic::catch_unwind(|| {
let mut a = Command::new("bin/printf-stdout");
a.args(["%s", "alfa"]);
let b = Regex::new(r"zz").expect("regex");
let _actual = assert_command_stdout_string_is_match!(a, b);
});
let message = concat!(
"assertion failed: `assert_command_stdout_string_is_match!(command, matcher)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_command_stdout_string_is_match.html\n",
" command label: `a`,\n",
" command debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
" command value: `\"alfa\"`,\n",
" matcher label: `b`,\n",
" matcher debug: `Regex(\"zz\")`,\n",
" matcher value: `Regex(\"zz\")`"
);
assert_eq!(
result
.unwrap_err()
.downcast::<String>()
.unwrap()
.to_string(),
message
);
}
}
#[macro_export]
macro_rules! debug_assert_command_stdout_string_is_match {
($($arg:tt)*) => {
if cfg!(debug_assertions) {
$crate::assert_command_stdout_string_is_match!($($arg)*);
}
};
}
#[cfg(test)]
mod test_debug_assert_command_stdout_string_is_match {
use regex::Regex;
use std::panic;
use std::process::Command;
#[test]
fn success() {
let mut a = Command::new("bin/printf-stdout");
a.args(["%s", "alfa"]);
let b = Regex::new(r"lf").expect("regex");
for _ in 0..1 {
let _actual = debug_assert_command_stdout_string_is_match!(a, b);
}
}
#[test]
fn failure() {
let result = panic::catch_unwind(|| {
let mut a = Command::new("bin/printf-stdout");
a.args(["%s", "alfa"]);
let b = Regex::new(r"zz").expect("regex");
let _actual = debug_assert_command_stdout_string_is_match!(a, b);
});
let message = concat!(
"assertion failed: `assert_command_stdout_string_is_match!(command, matcher)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_command_stdout_string_is_match.html\n",
" command label: `a`,\n",
" command debug: `\"bin/printf-stdout\" \"%s\" \"alfa\"`,\n",
" command value: `\"alfa\"`,\n",
" matcher label: `b`,\n",
" matcher debug: `Regex(\"zz\")`,\n",
" matcher value: `Regex(\"zz\")`"
);
assert_eq!(
result
.unwrap_err()
.downcast::<String>()
.unwrap()
.to_string(),
message
);
}
}