-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
102 lines (91 loc) · 3.24 KB
/
Copy pathmod.rs
File metadata and controls
102 lines (91 loc) · 3.24 KB
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
use crate::assertions::AssertErrorHasSource;
use crate::colored::{mark_missing, mark_missing_string, mark_unexpected, mark_unexpected_string};
use crate::expectations::{
ErrorHasSource, ErrorHasSourceMessage, error_has_source, error_has_source_message, not,
};
use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Invertible, Spec};
use crate::std::error::Error;
use crate::std::format;
use crate::std::string::{String, ToString};
impl<'a, S, R> AssertErrorHasSource for Spec<'a, S, R>
where
S: Error,
R: FailingStrategy,
{
type SourceMessage = Spec<'a, Option<String>, R>;
fn has_no_source(self) -> Self {
self.expecting(not(error_has_source()))
}
fn has_source(self) -> Self {
self.expecting(error_has_source())
}
fn has_source_message(self, expected_source_message: impl Into<String>) -> Self::SourceMessage {
let expected_source_message = expected_source_message.into();
self.expecting(error_has_source_message(expected_source_message))
.mapping(|err| err.source().map(ToString::to_string))
}
}
impl<S> Expectation<S> for ErrorHasSource
where
S: Error,
{
fn test(&mut self, subject: &S) -> bool {
subject.source().is_some()
}
fn message(
&self,
expression: &Expression<'_>,
actual: &S,
inverted: bool,
format: &DiffFormat,
) -> String {
let (a, expected) = if inverted {
("no", "<error with no source>")
} else {
("a", "<error with some source>")
};
let marked_actual = mark_unexpected(actual, format);
let marked_expected = mark_missing_string(expected, format);
format!(
"expected {expression} to have {a} source\n but was: {marked_actual}\n expected: {marked_expected}"
)
}
}
impl Invertible for ErrorHasSource {}
impl<S> Expectation<S> for ErrorHasSourceMessage
where
S: Error,
{
fn test(&mut self, subject: &S) -> bool {
subject
.source()
.is_some_and(|msg| msg.to_string() == self.expected_source_message)
}
fn message(
&self,
expression: &Expression<'_>,
actual: &S,
inverted: bool,
format: &DiffFormat,
) -> String {
let not = if inverted { "not " } else { "" };
let expected = &self.expected_source_message;
if let Some(actual_source) = actual.source() {
let marked_actual = mark_unexpected_string(&actual_source.to_string(), format);
let marked_expected = mark_missing_string(expected, format);
format!(
"expected {expression} to have a source message {not}equal to \"{expected}\"\n but was: \"{marked_actual}\"\n expected: \"{marked_expected}\""
)
} else {
let mut marked_actual = mark_unexpected(actual, format);
marked_actual.push_str(" - which has no source");
let marked_expected = mark_missing(expected, format);
format!(
"expected {expression} to have a source message {not}equal to \"{expected}\"\n but was: {marked_actual}\n expected: {not}{marked_expected}"
)
}
}
}
impl Invertible for ErrorHasSourceMessage {}
#[cfg(test)]
mod tests;