#[macro_export]
macro_rules! assert_count_gt_as_result {
($a:expr, $b:expr $(,)?) => {
match (&$a, &$b) {
(a, b) => {
let a_count = a.clone().count();
let b_count = b.clone().count();
if a_count > b_count {
Ok((a_count, b_count))
} else {
Err(format!(
concat!(
"assertion failed: `assert_count_gt!(a, b)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_count_gt.html\n",
" a label: `{}`,\n",
" a debug: `{:?}`,\n",
" a.count(): `{:?}`,\n",
" b label: `{}`,\n",
" b debug: `{:?}`\n",
" b.count(): `{:?}`",
),
stringify!($a),
a,
a_count,
stringify!($b),
b,
b_count
))
}
}
}
};
}
#[cfg(test)]
mod test_assert_count_gt_as_result {
use std::sync::Once;
#[test]
fn gt() {
let a = "xx".chars();
let b = "x".chars();
for _ in 0..1 {
let actual = assert_count_gt_as_result!(a, b);
assert_eq!(actual.unwrap(), (2, 1));
}
}
#[test]
fn gt_once() {
static A: Once = Once::new();
fn a() -> std::str::Chars<'static> {
if A.is_completed() {
panic!("A.is_completed()")
} else {
A.call_once(|| {})
}
"xx".chars()
}
static B: Once = Once::new();
fn b() -> std::str::Chars<'static> {
if B.is_completed() {
panic!("B.is_completed()")
} else {
B.call_once(|| {})
}
"x".chars()
}
assert_eq!(A.is_completed(), false);
assert_eq!(B.is_completed(), false);
let result = assert_count_gt_as_result!(a(), b());
assert!(result.is_ok());
assert_eq!(A.is_completed(), true);
assert_eq!(B.is_completed(), true);
}
#[test]
fn eq() {
let a = "x".chars();
let b = "x".chars();
let actual = assert_count_gt_as_result!(a, b);
let message = concat!(
"assertion failed: `assert_count_gt!(a, b)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_count_gt.html\n",
" a label: `a`,\n",
" a debug: `Chars(['x'])`,\n",
" a.count(): `1`,\n",
" b label: `b`,\n",
" b debug: `Chars(['x'])`\n",
" b.count(): `1`"
);
assert_eq!(actual.unwrap_err(), message);
}
#[test]
fn lt() {
let a = "x".chars();
let b = "xx".chars();
let actual = assert_count_gt_as_result!(a, b);
let message = concat!(
"assertion failed: `assert_count_gt!(a, b)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_count_gt.html\n",
" a label: `a`,\n",
" a debug: `Chars(['x'])`,\n",
" a.count(): `1`,\n",
" b label: `b`,\n",
" b debug: `Chars(['x', 'x'])`\n",
" b.count(): `2`"
);
assert_eq!(actual.unwrap_err(), message);
}
}
#[macro_export]
macro_rules! assert_count_gt {
($a:expr, $b:expr $(,)?) => {
match $crate::assert_count_gt_as_result!($a, $b) {
Ok(x) => x,
Err(err) => panic!("{}", err),
}
};
($a:expr, $b:expr, $($message:tt)+) => {
match $crate::assert_count_gt_as_result!($a, $b) {
Ok(x) => x,
Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
}
};
}
#[cfg(test)]
mod test_assert_count_gt {
use std::panic;
#[test]
fn gt() {
let a = "xx".chars();
let b = "x".chars();
for _ in 0..1 {
let actual = assert_count_gt!(a, b);
assert_eq!(actual, (2, 1));
}
}
#[test]
fn eq() {
let result = panic::catch_unwind(|| {
let a = "x".chars();
let b = "x".chars();
let _actual = assert_count_gt!(a, b);
});
let message = concat!(
"assertion failed: `assert_count_gt!(a, b)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_count_gt.html\n",
" a label: `a`,\n",
" a debug: `Chars(['x'])`,\n",
" a.count(): `1`,\n",
" b label: `b`,\n",
" b debug: `Chars(['x'])`\n",
" b.count(): `1`"
);
assert_eq!(
result
.unwrap_err()
.downcast::<String>()
.unwrap()
.to_string(),
message
);
}
#[test]
fn lt() {
let result = panic::catch_unwind(|| {
let a = "x".chars();
let b = "xx".chars();
let _actual = assert_count_gt!(a, b);
});
let message = concat!(
"assertion failed: `assert_count_gt!(a, b)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_count_gt.html\n",
" a label: `a`,\n",
" a debug: `Chars(['x'])`,\n",
" a.count(): `1`,\n",
" b label: `b`,\n",
" b debug: `Chars(['x', 'x'])`\n",
" b.count(): `2`"
);
assert_eq!(
result
.unwrap_err()
.downcast::<String>()
.unwrap()
.to_string(),
message
);
}
}
#[macro_export]
macro_rules! debug_assert_count_gt {
($($arg:tt)*) => {
if cfg!(debug_assertions) {
$crate::assert_count_gt!($($arg)*);
}
};
}
#[cfg(test)]
mod test_debug_assert_count_gt {
use std::panic;
#[test]
fn gt() {
let a = "xx".chars();
let b = "x".chars();
for _ in 0..1 {
let _actual = debug_assert_count_gt!(a, b);
}
}
#[test]
fn eq() {
let result = panic::catch_unwind(|| {
let a = "x".chars();
let b = "x".chars();
let _actual = debug_assert_count_gt!(a, b);
});
let message = concat!(
"assertion failed: `assert_count_gt!(a, b)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_count_gt.html\n",
" a label: `a`,\n",
" a debug: `Chars(['x'])`,\n",
" a.count(): `1`,\n",
" b label: `b`,\n",
" b debug: `Chars(['x'])`\n",
" b.count(): `1`"
);
assert_eq!(
result
.unwrap_err()
.downcast::<String>()
.unwrap()
.to_string(),
message
);
}
#[test]
fn lt() {
let result = panic::catch_unwind(|| {
let a = "x".chars();
let b = "xx".chars();
let _actual = debug_assert_count_gt!(a, b);
});
let message = concat!(
"assertion failed: `assert_count_gt!(a, b)`\n",
"https://docs.rs/assertables/9.9.0/assertables/macro.assert_count_gt.html\n",
" a label: `a`,\n",
" a debug: `Chars(['x'])`,\n",
" a.count(): `1`,\n",
" b label: `b`,\n",
" b debug: `Chars(['x', 'x'])`\n",
" b.count(): `2`"
);
assert_eq!(
result
.unwrap_err()
.downcast::<String>()
.unwrap()
.to_string(),
message
);
}
}