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
use super::*;
impl<T> Asserter<Option<T>>
where
T: PartialEq + std::fmt::Display,
{
pub fn is_some(&self) {
match &self.value {
None => {
panic!("Expected '{}' to be Some(_), but found None", &self.name)
}
Some(_) => {}
}
}
pub fn is_some_with_value(&self, value: T) {
match &self.value {
Some(val) => {
if *val != value {
panic!(
"Expected '{}' to be Some({}), but found Some({}).",
&self.name, value, val
);
}
}
None => panic!(
"Expected '{}' to be Some({}), but found None.",
&self.name, value
),
}
}
pub fn is_none(&self) {
match &self.value {
None => {}
Some(val) => panic!(
"Expected '{}' to be None, but found Some({}).",
&self.name, val
),
}
}
}
#[cfg(test)]
mod test_option_asserter {
use super::*;
#[test]
fn test_is_some_without_value() {
let option = Option::Some(3);
assert_that!(option).is_some();
let option = Option::<i32>::None;
assert_that_code!(|| assert_that!(option).is_some())
.panics()
.with_message("Expected 'option' to be Some(_), but found None");
}
#[test]
fn test_is_some_with_some() {
let option = Option::Some(3);
assert_that!(option).is_some_with_value(3);
assert_that_code!(|| assert_that!(option).is_some_with_value(4))
.panics()
.with_message("Expected 'option' to be Some(4), but found Some(3).");
}
#[test]
fn test_is_some_with_none() {
let option = Option::None;
assert_that_code!(|| assert_that!(option).is_some_with_value(4))
.panics()
.with_message("Expected 'option' to be Some(4), but found None.");
}
#[test]
fn test_is_none_with_none() {
let option = Option::<String>::None;
assert_that!(option).is_none();
}
#[test]
fn test_is_none_with_some() {
let option = Option::Some(3);
assert_that_code!(|| assert_that!(option).is_none())
.panics()
.with_message("Expected 'option' to be None, but found Some(3).");
}
}