Macro k9::assert_err_matches_regex
source · [−]macro_rules! assert_err_matches_regex {
($err:expr, $regex:expr) => { ... };
($err:expr, $regex:expr, $context:expr) => { ... };
}
Expand description
Asserts that the passed Result
argument is an Err
and
and the debug string of that error matches provided regex.
Regular expressions are compiled using regex
crate.
use k9::assert_err_matches_regex;
// Borrowed from Rust by Example: https://doc.rust-lang.org/stable/rust-by-example/std/result.html
fn divide(x: f64, y: f64) -> Result<f64, &'static str> {
if y == 0.0 {
// This operation would `fail`, instead let's return the reason of
// the failure wrapped in `Err`
Err("Cannot divide by 0.")
} else {
// This operation is valid, return the result wrapped in `Ok`
Ok(x / y)
}
}
let division_error = divide(4.0, 0.0);
assert_err_matches_regex!(division_error, "Cannot");