use std::fmt;
use crate::core::{Matcher, NegFormat};
use crate::matchers::result::BeOkMatcher;
use super::ExpectationFormat;
fn result_format<T, E>() -> ExpectationFormat<Result<T, E>> {
ExpectationFormat::new("to be Ok(_)", "to be Err(_)")
}
pub fn be_ok<'a, T, E>() -> Matcher<'a, Result<T, E>, T, E>
where
T: fmt::Debug + 'a,
E: fmt::Debug + 'a,
{
Matcher::transform(BeOkMatcher::new(), result_format())
}
pub fn be_err<'a, T, E>() -> Matcher<'a, Result<T, E>, E, T>
where
T: fmt::Debug + 'a,
E: fmt::Debug + 'a,
{
Matcher::transform_neg(BeOkMatcher::new(), NegFormat(result_format()))
}
#[cfg(test)]
mod tests {
use super::{be_err, be_ok};
use crate::expect;
fn ok() -> Result<(), ()> {
Ok(())
}
fn err() -> Result<(), ()> {
Err(())
}
#[test]
fn succeeds_when_ok() {
expect!(ok()).to(be_ok());
}
#[test]
fn succeeds_when_not_ok() {
expect!(err()).to_not(be_ok());
}
#[test]
#[should_panic]
fn fails_when_ok() {
expect!(ok()).to_not(be_ok());
}
#[test]
#[should_panic]
fn fails_when_not_ok() {
expect!(err()).to(be_ok());
}
#[test]
fn succeeds_when_err() {
expect!(err()).to(be_err());
}
#[test]
fn succeeds_when_not_err() {
expect!(ok()).to_not(be_err());
}
#[test]
#[should_panic]
fn fails_when_err() {
expect!(err()).to_not(be_err());
}
#[test]
#[should_panic]
fn fails_when_not_err() {
expect!(ok()).to(be_err());
}
}