#[macro_export]
macro_rules! assert_is_empty_as_result {
($a:expr $(,)?) => {
match (&$a) {
a => {
if a.is_empty() {
Ok(())
} else {
Err(format!(
concat!(
"assertion failed: `assert_is_empty!(a)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_is_empty.html\n",
" label: `{}`,\n",
" debug: `{:?}`",
),
stringify!($a),
a,
))
}
}
}
};
}
#[cfg(test)]
mod test_assert_is_empty_as_result {
use std::sync::Once;
#[test]
fn success() {
let a = "";
for _ in 0..1 {
let actual = assert_is_empty_as_result!(a);
assert_eq!(actual.unwrap(), ());
}
}
#[test]
fn success_once() {
static A: Once = Once::new();
fn a() -> &'static str {
if A.is_completed() {
panic!("A.is_completed()")
} else {
A.call_once(|| {})
}
""
}
assert_eq!(A.is_completed(), false);
let result = assert_is_empty_as_result!(a());
assert!(result.is_ok());
assert_eq!(A.is_completed(), true);
}
#[test]
fn failure() {
let a = "alfa";
let actual = assert_is_empty_as_result!(a);
let message = concat!(
"assertion failed: `assert_is_empty!(a)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_is_empty.html\n",
" label: `a`,\n",
" debug: `\"alfa\"`"
);
assert_eq!(actual.unwrap_err(), message);
}
}
#[macro_export]
macro_rules! assert_is_empty {
($a:expr $(,)?) => {
match $crate::assert_is_empty_as_result!($a) {
Ok(()) => (),
Err(err) => panic!("{}", err),
}
};
($a:expr, $($message:tt)+) => {
match $crate::assert_is_empty_as_result!($a) {
Ok(()) => (),
Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
}
};
}
#[cfg(test)]
mod test_assert_is_empty {
use std::panic;
#[test]
fn success() {
let a = "";
for _ in 0..1 {
let actual = assert_is_empty!(a);
assert_eq!(actual, ());
}
}
#[test]
fn failure() {
let a = "alfa";
let result = panic::catch_unwind(|| {
let _actual = assert_is_empty!(a);
});
let message = concat!(
"assertion failed: `assert_is_empty!(a)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_is_empty.html\n",
" label: `a`,\n",
" debug: `\"alfa\"`"
);
assert_eq!(
result
.unwrap_err()
.downcast::<String>()
.unwrap()
.to_string(),
message
);
}
}
#[macro_export]
macro_rules! debug_assert_is_empty {
($($arg:tt)*) => {
if cfg!(debug_assertions) {
$crate::assert_is_empty!($($arg)*);
}
};
}
#[cfg(test)]
mod test_debug_assert_is_empty {
use std::panic;
#[test]
fn success() {
let a = "";
for _ in 0..1 {
let _actual = debug_assert_is_empty!(a);
}
}
#[test]
fn failure() {
let a = "alfa";
let result = panic::catch_unwind(|| {
let _actual = debug_assert_is_empty!(a);
});
let message = concat!(
"assertion failed: `assert_is_empty!(a)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_is_empty.html\n",
" label: `a`,\n",
" debug: `\"alfa\"`"
);
assert_eq!(
result
.unwrap_err()
.downcast::<String>()
.unwrap()
.to_string(),
message
);
}
}