use core::{SourceLocation, Matcher, Join, TestResult};
pub fn expect<A>(value: A) -> ActualValue<A> {
ActualValue::new(value)
}
#[derive(Debug)]
pub struct ActualValue<A> {
value: A,
location: Option<SourceLocation>,
}
impl<A> ActualValue<A> {
pub fn new(value: A) -> Self {
ActualValue {
value: value,
location: None,
}
}
pub fn location(mut self, l: SourceLocation) -> Self {
self.location = Some(l);
self
}
pub fn to<M, E>(self, matcher: M) -> TestResult
where M: Matcher<A, E>
{
self.matching(matcher, Join::To)
}
pub fn to_not<M, E>(self, matcher: M) -> TestResult
where M: Matcher<A, E>
{
self.matching(matcher, Join::ToNot)
}
pub fn not_to<M, E>(self, matcher: M) -> TestResult
where M: Matcher<A, E>
{
self.matching(matcher, Join::NotTo)
}
fn matching<M, E>(self, matcher: M, join: Join) -> TestResult
where M: Matcher<A, E>
{
let success = if join.is_assertion() {
matcher.matches(&self.value)
} else {
!matcher.matches(&self.value)
};
if success {
TestResult::new_success()
} else {
TestResult::new_failure(matcher.failure_message(join, &self.value), self.location)
}
}
}