#[macro_export]
macro_rules! assert_fails {
($expr:expr, $search_str:expr $(,)?) => {{
let expr = $expr;
let search_str = $search_str;
if let Err(errors) = expr {
let mut found_error = false;
let mut all_errors_string = "".to_owned();
for error in &errors {
let error_string = error.to_string();
let _ = write!(all_errors_string, "{}\n", error_string);
if error_string.contains(search_str) {
found_error = true;
}
}
assert!(
found_error,
"The expression failed as expected, but the expected message was not found in \
any of the errors: {:?}.",
errors,
);
} else {
panic!("The expression was supposed to fail, but it succeeded.");
}
}};
}
#[macro_export]
macro_rules! assert_same {
($expr1:expr, $expr2:expr $(,)?) => {{
let expr1 = $expr1;
let expr2 = $expr2;
let mut _dummy = &expr1;
_dummy = &expr2;
assert_eq!(format!("{:?}", expr1), format!("{:?}", expr2));
}};
}
#[cfg(test)]
mod tests {
use {
crate::{assert_fails, assert_same, error::Error},
std::fmt::Write,
};
#[test]
#[should_panic(expected = "The expression was supposed to fail, but it succeeded.")]
fn assert_fails_empty() {
let success: Result<usize, Vec<Error>> = Ok(42);
assert_fails!(success, "search string");
}
#[test]
fn assert_fails_match() {
let success: Result<usize, Vec<Error>> = Err(vec![
Error {
message: "foo bar".to_owned(),
reason: None,
},
Error {
message: "foo search string bar".to_owned(),
reason: None,
},
Error {
message: "foo bar".to_owned(),
reason: None,
},
]);
assert_fails!(success, "search string");
}
#[test]
#[should_panic(
// This comma on the comment at the end of the line below is needed to satisfy the trailing
// commas check.
expected = "\
The expression failed as expected, but the expected message was not found in any of \
the errors: [\
Error { message: \"foo\", reason: None }, \
Error { message: \"bar\", reason: None }, \
Error { message: \"baz\", reason: None }\
]."
)]
fn assert_fails_mismatch() {
let success: Result<usize, Vec<Error>> = Err(vec![
Error {
message: "foo".to_owned(),
reason: None,
},
Error {
message: "bar".to_owned(),
reason: None,
},
Error {
message: "baz".to_owned(),
reason: None,
},
]);
assert_fails!(success, "search string");
}
#[test]
fn assert_same_match() {
assert_same!(42, 42);
}
#[test]
#[should_panic(expected = "42")]
fn assert_same_mismatch() {
assert_same!(42, 43);
}
}