1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
pub use ::failure::{Error, Fail};
use core::fmt::{Debug, Display};

#[derive(Debug)]
pub struct TestFailure<Input> {
    pub error: Error,
    pub input: Input,
    pub seed: Option<u64>,
}

impl<Input: Debug> Display for TestFailure<Input> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        writeln!(
            f,
            "\n======================== Test Failure ========================\n"
        )?;
        if let Some(seed) = &self.seed {
            writeln!(f, "BOLERO_RANDOM_SEED={}\n", seed)?;
        }
        writeln!(f, "Input: \n{:?}\n", self.input)?;
        writeln!(f, "Error: \n{}", self.error)?;

        if f.alternate() {
            if std::env::var("RUST_BACKTRACE")
                .ok()
                .filter(|v| v == "1")
                .is_some()
            {
                writeln!(f, "{}", self.error.backtrace())?;
            } else {
                writeln!(f, "note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.")?;
            }
        }

        writeln!(
            f,
            "\n=============================================================="
        )?;
        Ok(())
    }
}

impl<Input: 'static + Debug + Send + Sync> Fail for TestFailure<Input> {
    fn cause(&self) -> Option<&dyn Fail> {
        Some(self.error.as_fail())
    }
}