use crate::std_facade::fmt;
#[cfg(feature = "std")]
use std::string::ToString;
use crate::test_runner::Reason;
#[derive(Debug, Clone)]
pub enum TestCaseError {
Reject(Reason),
Fail(Reason),
}
pub type TestCaseResult = Result<(), TestCaseError>;
impl TestCaseError {
pub fn reject(reason: impl Into<Reason>) -> Self {
TestCaseError::Reject(reason.into())
}
pub fn fail(reason: impl Into<Reason>) -> Self {
TestCaseError::Fail(reason.into())
}
}
impl fmt::Display for TestCaseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
TestCaseError::Reject(ref whence) =>
write!(f, "Input rejected at {}", whence),
TestCaseError::Fail(ref why) =>
write!(f, "Case failed: {}", why),
}
}
}
#[cfg(feature = "std")]
impl<E : ::std::error::Error> From<E> for TestCaseError {
fn from(cause: E) -> Self {
TestCaseError::fail(cause.to_string())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TestError<T> {
Abort(Reason),
Fail(Reason, T),
}
impl<T : fmt::Debug> fmt::Display for TestError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
TestError::Abort(ref why) =>
write!(f, "Test aborted: {}", why),
TestError::Fail(ref why, ref what) =>
write!(f, "Test failed: {}; minimal failing input: {:?}",
why, what),
}
}
}
#[cfg(feature = "std")]
impl<T : fmt::Debug> ::std::error::Error for TestError<T> {
fn description(&self) -> &str {
match *self {
TestError::Abort(..) => "Abort",
TestError::Fail(..) => "Fail",
}
}
}