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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! Error source chain matchers.
use core::fmt::Debug;
use crate::error::MatchError;
use crate::expectation::Expectation;
impl<T: std::error::Error + Debug> Expectation<T> {
/// Asserts the error has a source (i.e. [`std::error::Error::source`] returns `Some`).
///
/// # Errors
///
/// Returns [`MatchError`] if the error has no source.
///
/// ```text
/// expect!(err)
/// actual: MyError("oops")
/// expected: to have a source error
/// ```
///
/// # Examples
///
/// ```
/// use behave::Expectation;
/// use std::fmt;
/// use std::io;
///
/// #[derive(Debug)]
/// struct Wrapper(io::Error);
///
/// impl fmt::Display for Wrapper {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "wrapper: {}", self.0)
/// }
/// }
///
/// impl std::error::Error for Wrapper {
/// fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
/// Some(&self.0)
/// }
/// }
///
/// let err = Wrapper(io::Error::other("inner"));
/// let result = Expectation::new(err, "err").to_have_source();
/// assert!(result.is_ok());
/// ```
pub fn to_have_source(&self) -> Result<(), MatchError> {
let is_match = self.value().source().is_some();
self.check(is_match, "to have a source error")
}
/// Asserts the error's source message contains the given substring.
///
/// First checks that [`std::error::Error::source`] returns `Some`, then checks
/// that the source's [`Display`](std::fmt::Display) output contains
/// the substring.
///
/// # Errors
///
/// Returns [`MatchError`] if the error has no source or the source
/// message does not contain the substring.
///
/// # Examples
///
/// ```
/// use behave::Expectation;
/// use std::fmt;
/// use std::io;
///
/// #[derive(Debug)]
/// struct Wrapper(io::Error);
///
/// impl fmt::Display for Wrapper {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "wrapper: {}", self.0)
/// }
/// }
///
/// impl std::error::Error for Wrapper {
/// fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
/// Some(&self.0)
/// }
/// }
///
/// let err = Wrapper(io::Error::other("connection timeout"));
/// let result = Expectation::new(err, "err")
/// .to_have_source_containing("timeout");
/// assert!(result.is_ok());
/// ```
pub fn to_have_source_containing(&self, substring: &str) -> Result<(), MatchError> {
let is_match = self
.value()
.source()
.is_some_and(|src| src.to_string().contains(substring));
self.check(
is_match,
format!("to have source error containing {substring:?}"),
)
}
}
#[cfg(test)]
mod tests {
use crate::Expectation;
use std::fmt;
use std::io;
#[derive(Debug)]
struct Wrapper(io::Error);
impl fmt::Display for Wrapper {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "wrapper: {}", self.0)
}
}
impl std::error::Error for Wrapper {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.0)
}
}
#[derive(Debug)]
struct NoSource;
impl fmt::Display for NoSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "no source")
}
}
impl std::error::Error for NoSource {}
// --- to_have_source ---
#[test]
fn to_have_source_pass() {
let err = Wrapper(io::Error::other("inner"));
assert!(Expectation::new(err, "x").to_have_source().is_ok());
}
#[test]
fn to_have_source_fail() {
assert!(Expectation::new(NoSource, "x").to_have_source().is_err());
}
#[test]
fn to_have_source_negated() {
assert!(Expectation::new(NoSource, "x")
.negate()
.to_have_source()
.is_ok());
}
// --- to_have_source_containing ---
#[test]
fn to_have_source_containing_pass() {
let err = Wrapper(io::Error::other("connection timeout"));
assert!(Expectation::new(err, "x")
.to_have_source_containing("timeout")
.is_ok());
}
#[test]
fn to_have_source_containing_fail_no_source() {
assert!(Expectation::new(NoSource, "x")
.to_have_source_containing("anything")
.is_err());
}
#[test]
fn to_have_source_containing_fail_wrong_msg() {
let err = Wrapper(io::Error::other("permission denied"));
assert!(Expectation::new(err, "x")
.to_have_source_containing("timeout")
.is_err());
}
#[test]
fn to_have_source_containing_negated() {
let err = Wrapper(io::Error::other("permission denied"));
assert!(Expectation::new(err, "x")
.negate()
.to_have_source_containing("timeout")
.is_ok());
}
}