-
Notifications
You must be signed in to change notification settings - Fork 61
/
report.rs
295 lines (233 loc) · 7.09 KB
/
report.rs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use snafu::{prelude::*, CleanedErrorText, IntoError, Report};
use std::process::ExitCode;
macro_rules! assert_contains {
(needle: $needle:expr, haystack: $haystack:expr) => {
assert!(
$haystack.contains($needle),
"Expected {:?} to include {:?}",
$haystack,
$needle,
)
};
}
macro_rules! assert_not_contains {
(needle: $needle:expr, haystack: $haystack:expr) => {
assert!(
!$haystack.contains($needle),
"Expected {:?} to not include {:?}",
$haystack,
$needle,
)
};
}
#[test]
fn includes_the_error_display_text() {
#[derive(Debug, Snafu)]
#[snafu(display("This is my Display text!"))]
struct Error;
let r = Report::from_error(Error);
let msg = r.to_string();
let expected = "This is my Display text!";
assert_contains!(needle: expected, haystack: msg);
}
#[test]
fn includes_the_source_display_text() {
#[derive(Debug, Snafu)]
#[snafu(display("This is my inner Display"))]
struct InnerError;
#[derive(Debug, Snafu)]
#[snafu(display("This is my outer Display"))]
struct OuterError {
source: InnerError,
}
let e = OuterSnafu.into_error(InnerError);
let r = Report::from_error(e);
let msg = r.to_string();
let expected = "This is my inner Display";
assert_contains!(needle: expected, haystack: msg);
}
#[test]
fn reduces_duplication_of_the_source_display_text() {
// Including the source in the Display message is discouraged but
// quite common.
#[derive(Debug, Snafu)]
#[snafu(display("Level 0"))]
struct Level0Error;
#[derive(Debug, Snafu)]
#[snafu(display("Level 1: {source}"))]
struct Level1Error {
source: Level0Error,
}
#[derive(Debug, Snafu)]
#[snafu(display("Level 2: {source}"))]
struct Level2Error {
source: Level1Error,
}
let e = Level2Snafu.into_error(Level1Snafu.into_error(Level0Error));
let raw_msg = e.to_string();
let expected = "Level 2: Level 1";
assert_contains!(needle: expected, haystack: raw_msg);
let r = Report::from_error(e);
let msg = r.to_string();
assert_not_contains!(needle: expected, haystack: msg);
}
#[test]
fn removes_complete_duplication_in_the_source_display_text() {
// Including **only** the source in the Display message is also
// discouraged but occurs.
#[derive(Debug, Snafu)]
#[snafu(display("Level 0"))]
struct Level0Error;
#[derive(Debug, Snafu)]
#[snafu(display("{source}"))]
struct Level1Error {
source: Level0Error,
}
#[derive(Debug, Snafu)]
#[snafu(display("{source}"))]
struct Level2Error {
source: Level1Error,
}
let e = Level2Snafu.into_error(Level1Snafu.into_error(Level0Error));
let raw_msg = e.to_string();
assert_contains!(needle: "Level 0", haystack: raw_msg);
let r = Report::from_error(e);
let msg = r.to_string();
assert_not_contains!(needle: "Caused by", haystack: msg);
}
#[test]
fn debug_and_display_are_the_same() {
#[derive(Debug, Snafu)]
#[snafu(display("This is my inner Display"))]
struct InnerError;
#[derive(Debug, Snafu)]
#[snafu(display("This is my outer Display"))]
struct OuterError {
source: InnerError,
}
let e = OuterSnafu.into_error(InnerError);
let r = Report::from_error(e);
let display = format!("{r}",);
let debug = format!("{r:?}");
assert_eq!(display, debug);
}
/// `Report as Termination` prints-out the "Error:" prefix. Ensure that `Report as Display` does
/// not also add such a prefix, to avoid printing-out "Error: Error: ...".
#[test]
fn display_not_prefixed() {
#[derive(Debug, Snafu)]
#[snafu(display("This is my Display text!"))]
struct Error;
let r = Report::from_error(Error);
let msg = r.to_string();
let msg = msg.trim_start();
assert!(!msg.starts_with("Err"));
assert!(!msg.starts_with("err"));
}
#[test]
fn procedural_macro_works_with_result_return_type() {
#[derive(Debug, Snafu)]
struct Error;
#[snafu::report]
fn mainlike_result() -> Result<(), Error> {
Ok(())
}
let _: Report<Error> = mainlike_result();
}
#[test]
fn procedural_macro_works_with_tough_inference() {
#[derive(Debug, Snafu)]
struct InnerError;
#[derive(Debug, Snafu)]
struct OuterError {
source: InnerError,
}
fn inner() -> Result<(), InnerError> {
InnerSnafu.fail()
}
#[snafu::report]
fn mainlike_result() -> Result<(), OuterError> {
loop {
inner().context(OuterSnafu)?;
}
}
let _: Report<_> = mainlike_result();
}
#[test]
fn termination_returns_failure_code() {
use std::process::Termination;
#[derive(Debug, Snafu)]
struct Error;
let r = Report::from_error(Error);
let code: ExitCode = r.report();
assert!(
nasty_hack_exit_code_eq(code, ExitCode::FAILURE),
"Wanted {:?} but got {:?}",
ExitCode::FAILURE,
code,
);
}
fn nasty_hack_exit_code_eq(left: ExitCode, right: ExitCode) -> bool {
use std::mem;
#[cfg(target_os = "windows")]
type ExitCodeSize = u32;
#[cfg(not(target_os = "windows"))]
type ExitCodeSize = u8;
let (left, right): (ExitCodeSize, ExitCodeSize) = unsafe {
assert_eq!(mem::size_of::<ExitCodeSize>(), mem::size_of::<ExitCode>());
(mem::transmute(left), mem::transmute(right))
};
left == right
}
#[derive(Debug, Snafu)]
struct TestFunctionError;
#[test]
#[snafu::report]
fn procedural_macro_works_with_test_functions() -> Result<(), TestFunctionError> {
Ok(())
}
#[track_caller]
fn assert_cleaning_step(iter: &mut CleanedErrorText, text: &str, removed_text: &str) {
let (error, actual_text, actual_cleaned) =
iter.next().expect("Iterator unexpectedly exhausted");
let actual_original_text = error.to_string();
let original_text = [text, removed_text].concat();
let cleaned = !removed_text.is_empty();
assert_eq!(original_text, actual_original_text);
assert_eq!(text, actual_text);
assert_eq!(cleaned, actual_cleaned);
}
#[test]
fn cleaning_a_leaf_error_changes_nothing() {
#[derive(Debug, Snafu)]
#[snafu(display("But I am only C"))]
struct C;
let c = C;
let mut iter = CleanedErrorText::new(&c);
assert_cleaning_step(&mut iter, "But I am only C", "");
assert!(iter.next().is_none());
}
#[test]
fn cleaning_nested_errors_removes_duplication() {
#[derive(Debug, Snafu)]
#[snafu(display("This is A: {source}"))]
struct A {
source: B,
}
#[derive(Debug, Snafu)]
#[snafu(display("And this is B: {source}"))]
struct B {
source: C,
}
#[derive(Debug, Snafu)]
#[snafu(display("But I am only C"))]
struct C;
let a = A {
source: B { source: C },
};
let mut iter = CleanedErrorText::new(&a);
assert_cleaning_step(&mut iter, "This is A", ": And this is B: But I am only C");
assert_cleaning_step(&mut iter, "And this is B", ": But I am only C");
assert_cleaning_step(&mut iter, "But I am only C", "");
assert!(iter.next().is_none());
}