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
//! Display and Debug string matchers.
use core::fmt::{Debug, Display};
use crate::error::MatchError;
use crate::expectation::Expectation;
impl<T: Display + Debug> Expectation<T> {
/// Asserts the value's [`Display`] output equals the expected string.
///
/// # Errors
///
/// Returns [`MatchError`] if the display output does not match.
///
/// ```text
/// expect!(status)
/// actual: Active
/// expected: to display as "Inactive"
/// ```
///
/// # Examples
///
/// ```
/// use behave::Expectation;
///
/// let result = Expectation::new(42, "42").to_display_as("42");
/// assert!(result.is_ok());
/// ```
pub fn to_display_as(&self, expected: &str) -> Result<(), MatchError> {
let display = format!("{}", self.value());
let is_match = display == expected;
self.check(is_match, format!("to display as {expected:?}"))
}
/// Asserts the value's [`Display`] output contains the given substring.
///
/// # Errors
///
/// Returns [`MatchError`] if the display output does not contain the substring.
///
/// # Examples
///
/// ```
/// use behave::Expectation;
///
/// let result = Expectation::new(42, "42").to_display_containing("4");
/// assert!(result.is_ok());
/// ```
pub fn to_display_containing(&self, substring: &str) -> Result<(), MatchError> {
let display = format!("{}", self.value());
let is_match = display.contains(substring);
self.check(
is_match,
format!("to have Display output containing {substring:?}"),
)
}
}
impl<T: Debug> Expectation<T> {
/// Asserts the value's [`Debug`] output contains the given substring.
///
/// # Errors
///
/// Returns [`MatchError`] if the debug output does not contain the substring.
///
/// # Examples
///
/// ```
/// use behave::Expectation;
///
/// let result = Expectation::new(vec![1, 2, 3], "v")
/// .to_debug_containing("[1, 2, 3]");
/// assert!(result.is_ok());
/// ```
pub fn to_debug_containing(&self, substring: &str) -> Result<(), MatchError> {
let debug = format!("{:?}", self.value());
let is_match = debug.contains(substring);
self.check(
is_match,
format!("to have Debug output containing {substring:?}"),
)
}
}
#[cfg(test)]
mod tests {
use crate::Expectation;
// --- to_display_as ---
#[test]
fn to_display_as_pass() {
assert!(Expectation::new(42, "x").to_display_as("42").is_ok());
}
#[test]
fn to_display_as_fail() {
assert!(Expectation::new(42, "x").to_display_as("99").is_err());
}
#[test]
fn to_display_as_negated() {
assert!(Expectation::new(42, "x")
.negate()
.to_display_as("99")
.is_ok());
}
// --- to_display_containing ---
#[test]
fn to_display_containing_pass() {
assert!(Expectation::new(42, "x").to_display_containing("4").is_ok());
}
#[test]
fn to_display_containing_fail() {
assert!(Expectation::new(42, "x")
.to_display_containing("9")
.is_err());
}
#[test]
fn to_display_containing_negated() {
assert!(Expectation::new(42, "x")
.negate()
.to_display_containing("9")
.is_ok());
}
// --- to_debug_containing ---
#[test]
fn to_debug_containing_pass() {
assert!(Expectation::new(vec![1, 2, 3], "v")
.to_debug_containing("[1, 2, 3]")
.is_ok());
}
#[test]
fn to_debug_containing_fail() {
assert!(Expectation::new(vec![1, 2, 3], "v")
.to_debug_containing("[9]")
.is_err());
}
#[test]
fn to_debug_containing_negated() {
assert!(Expectation::new(vec![1, 2, 3], "v")
.negate()
.to_debug_containing("[9]")
.is_ok());
}
}