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
use crate::core::{Join, Matcher};
use std::fmt;
pub struct BeSome;
pub fn be_some() -> BeSome {
BeSome
}
impl BeSome {
pub fn value<E>(self, v: E) -> BeSomeValue<E> {
BeSomeValue { value: v }
}
}
impl<A> Matcher<Option<A>, ()> for BeSome
where
A: fmt::Debug,
{
fn failure_message(&self, join: Join, actual: &Option<A>) -> String {
format!("expected {} be Some, got <{:?}>", join, actual)
}
fn matches(&self, actual: &Option<A>) -> bool {
actual.is_some()
}
}
pub struct BeSomeValue<E> {
value: E,
}
impl<A, E> Matcher<Option<A>, E> for BeSomeValue<E>
where
A: PartialEq<E> + fmt::Debug,
E: fmt::Debug,
{
fn failure_message(&self, join: Join, actual: &Option<A>) -> String {
format!(
"expected {} be equal to <Some({:?})>, got <{:?}>",
join, self.value, actual
)
}
fn matches(&self, actual: &Option<A>) -> bool {
if let Some(a) = actual.as_ref() {
a == &self.value
} else {
false
}
}
}
pub struct BeNone;
pub fn be_none() -> BeNone {
BeNone
}
impl<A> Matcher<Option<A>, ()> for BeNone
where
A: fmt::Debug,
{
fn failure_message(&self, join: Join, actual: &Option<A>) -> String {
format!("expected {} be None, got <{:?}>", join, actual)
}
fn matches(&self, actual: &Option<A>) -> bool {
actual.is_none()
}
}
#[cfg(test)]
mod tests {
use super::{be_none, be_some};
use crate::core::expect;
#[test]
fn be_some_failure_message() {
expect(None::<u8>)
.to(be_some())
.assert_eq_message("expected to be Some, got <None>");
}
#[test]
fn be_some_value_failure_message_1() {
expect(None::<u8>)
.to(be_some().value(1))
.assert_eq_message("expected to be equal to <Some(1)>, got <None>");
}
#[test]
fn be_some_value_failure_message_2() {
expect(Some(2))
.to(be_some().value(1))
.assert_eq_message("expected to be equal to <Some(1)>, got <Some(2)>");
}
#[test]
fn be_none_failure_message() {
expect(Some(2))
.to(be_none())
.assert_eq_message("expected to be None, got <Some(2)>");
}
}