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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::fmt;

#[derive(Clone)]
pub enum TestResult {
    Pass,
    Fail,
    Error,
    Skipped,
}

impl fmt::Display for TestResult {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let rep = match *self {
            TestResult::Pass => ".",
            TestResult::Fail => "F",
            TestResult::Error => "E",
            TestResult::Skipped => "S",
        };
        write!(f, "{}", rep)
    }
}

pub struct TestTimings {
    pub time_s: f64,
    pub runs_per_s: f64,
}

impl fmt::Display for TestTimings {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
               "Finished in {:3}s, {} runs/s.",
               self.time_s,
               self.runs_per_s)
    }
}

pub struct TestStats {
    pub runs: usize,
    pub failures: usize,
    pub errors: usize,
    pub skips: usize,
}

impl TestStats {
    pub fn new() -> TestStats {
        TestStats {
            runs: 0,
            failures: 0,
            errors: 0,
            skips: 0,
        }
    }

    pub fn create(result: &TestResult) -> TestStats {
        TestStats {
            runs: 1,
            failures: match *result {
                TestResult::Fail => 1,
                _ => 0,
            },
            errors: match *result {
                TestResult::Error => 1,
                _ => 0,
            },
            skips: match *result {
                TestResult::Skipped => 1,
                _ => 0,
            },
        }
    }

    pub fn combine(a: TestStats, b: TestStats) -> TestStats {
        TestStats {
            runs: a.runs + b.runs,
            failures: a.failures + b.failures,
            errors: a.errors + b.errors,
            skips: a.skips + b.skips,
        }
    }
}

impl fmt::Display for TestStats {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
               "{} runs, {} failures, {} errors, {} skips.",
               self.runs,
               self.failures,
               self.errors,
               self.skips)
    }
}