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
131
132
133
134
135
136
137
138
139
140
141
use std::fmt::Debug;
use super::super::*;
macro_rules! matchresult_from_comparison {
( $actual: ident $comparison: tt $expected: ident, $name: expr ) => {{
let builder = MatchResultBuilder::for_($name);
if $actual $comparison &$expected {
builder.matched()
} else {
builder.failed_comparison($actual, &$expected)
}
}}
}
pub fn assertion_always_succeeds<'a,T:'a>() -> Box<Matcher<'a,T> + 'a> {
Box::new(|_s: &T| MatchResultBuilder::for_("succeeds_always").matched())
}
pub fn assertion_always_fails<'a,T:'a>() -> Box<Matcher<'a,T> + 'a> {
Box::new(|_s: &T| {
MatchResultBuilder::for_("fails_always").failed_because("This matcher fails always")
})
}
pub fn is<'a, T:'a, M>(matcher: M) -> M
where M: Matcher<'a,T> {
matcher
}
pub fn not<'a, T: 'a>(matcher: Box<Matcher<'a,T> + 'a>) -> Box<Matcher<'a,T> + 'a> {
Box::new(move |actual: &'a T| {
match matcher.check(actual) {
MatchResult::Matched { name } =>
MatchResultBuilder::for_(&format!("not({})", name))
.failed_because(&format!("{} is satisfied", name)),
MatchResult::Failed { name, .. } =>
MatchResultBuilder::for_(&format!("not({})", name)).matched()
}
})
}
pub fn equal_to<'a, T>(expected: T) -> Box<Matcher<'a,T> + 'a>
where T: PartialEq + Debug + 'a {
Box::new(move |actual: &T| matchresult_from_comparison!(actual == expected, "equal"))
}
pub fn eq<'a, T: PartialEq + Debug + 'a>(expected: T) -> Box<Matcher<'a,T> + 'a> { equal_to(expected) }
pub fn less_than<'a, T>(expected: T) -> Box<Matcher<'a,T> + 'a>
where T: PartialOrd + Debug + 'a {
Box::new(move |actual: &T| matchresult_from_comparison!(actual < expected, "less_than"))
}
pub fn lt<'a, T: PartialOrd + Debug + 'a>(expected: T) -> Box<Matcher<'a,T> + 'a> { less_than(expected) }
pub fn greater_than<'a, T>(expected: T) -> Box<Matcher<'a,T> + 'a>
where T: PartialOrd + Debug + 'a {
Box::new(move |actual: &T| matchresult_from_comparison!(actual > expected, "greater_than"))
}
pub fn gt<'a, T: PartialOrd + Debug + 'a>(expected: T) -> Box<Matcher<'a,T> + 'a> { greater_than(expected) }
pub fn less_than_or_equal<'a, T>(expected: T) -> Box<Matcher<'a,T> + 'a>
where T: PartialOrd + Debug + 'a {
Box::new(move |actual: &T| matchresult_from_comparison!(actual <= expected, "less_than_or_equal"))
}
pub fn leq<'a, T: PartialOrd + Debug + 'a>(expected: T) -> Box<Matcher<'a,T> + 'a> { less_than_or_equal(expected) }
pub fn greater_than_or_equal<'a, T>(expected: T) -> Box<Matcher<'a,T> + 'a>
where T: PartialOrd + Debug + 'a {
Box::new(move |actual: &T| matchresult_from_comparison!(actual >= expected, "greater_than_or_equal"))
}
pub fn geq<'a, T: PartialOrd + Debug + 'a>(expected: T) -> Box<Matcher<'a,T> + 'a> { greater_than_or_equal(expected) }
pub fn close_to<'a, T>(expected: T, eps: T) -> Box<Matcher<'a,T> + 'a>
where T: Copy + PartialOrd + std::ops::Add<Output=T> + std::ops::Sub<Output=T> + Debug + 'a {
Box::new(move |actual: &T| {
let builder = MatchResultBuilder::for_("close_to");
if &(expected - eps) <= actual && actual <= &(expected + eps) {
builder.matched()
} else {
builder.failed_because(&format!("{:?} should be between {:?} and {:?}",
actual, expected - eps, expected + eps)
)
}
})
}
pub fn same_object<'a, T>(expected: &'a T) -> Box<Matcher<'a,T> + 'a>
where T: Debug + 'a {
Box::new(move |actual: &T| {
let builder = MatchResultBuilder::for_("same_object");
if (actual as *const _) == (expected as *const _) {
builder.matched()
} else {
builder.failed_comparison(&actual, &expected)
}
})
}