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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::convert::From;
use report::Report;
#[cfg(feature = "expectest_compat")]
use expectest::core::TestResult as ExpectestResult;
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ExampleReport {
Success,
Failure(Option<String>),
Ignored,
}
impl Report for ExampleReport {
fn is_success(&self) -> bool {
self == &ExampleReport::Success
}
fn is_failure(&self) -> bool {
self != &ExampleReport::Success
}
fn get_passed(&self) -> u32 {
if &ExampleReport::Success == self {
1
} else {
0
}
}
fn get_failed(&self) -> u32 {
if let &ExampleReport::Failure(_) = self {
1
} else {
0
}
}
fn get_ignored(&self) -> u32 {
if &ExampleReport::Ignored == self {
1
} else {
0
}
}
}
impl From<()> for ExampleReport {
fn from(_other: ()) -> ExampleReport {
ExampleReport::Success
}
}
impl From<bool> for ExampleReport {
fn from(other: bool) -> ExampleReport {
if other {
ExampleReport::Success
} else {
ExampleReport::Failure(Some(
"assertion failed: `expected condition to be true`"
.to_owned(),
))
}
}
}
impl<T1, T2> From<Result<T1, T2>> for ExampleReport
where
T2: ::std::fmt::Debug,
{
fn from(other: Result<T1, T2>) -> ExampleReport {
match other {
Ok(_) => ExampleReport::Success,
Err(error) => ExampleReport::Failure(Some(format!("{:?}", error))),
}
}
}
#[cfg(feature = "expectest_compat")]
impl From<ExpectestResult> for ExampleReport {
fn from(other: ExpectestResult) -> ExampleReport {
match other {
ExpectestResult::Success => ExampleReport::Success,
ExpectestResult::Failure(failure) => {
ExampleReport::Failure(Some(format!("{:?}", failure)))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_void() {
assert!(ExampleReport::from(()).is_success());
}
#[test]
fn from_bool() {
assert!(ExampleReport::from(true).is_success());
assert!(ExampleReport::from(false).is_failure());
}
#[test]
fn from_result() {
let ok_result: Result<(), ()> = Ok(());
let err_result: Result<(), ()> = Err(());
assert!(ExampleReport::from(ok_result).is_success());
assert!(ExampleReport::from(err_result).is_failure());
}
#[cfg(feature = "expectest_compat")]
#[test]
#[should_panic]
fn from_expectest_result() {
let ok_result = ExpectestResult::new_success();
let err_result = ExpectestResult::new_failure("dummy".to_owned(), None);
assert!(ExampleReport::from(ok_result).is_success());
assert!(ExampleReport::from(err_result).is_failure());
}
}