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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::actual::Actual;
use crate::mode::Panic;
use crate::prelude::ResultExtractAssertions;
use crate::tracking::AssertionTracking;
use crate::{AssertThat, PanicValue};
use alloc::{boxed::Box, string::String};
use core::any::Any;
use core::fmt::{Debug, Write};
use core::panic::UnwindSafe;
use futures::FutureExt;
use indoc::writedoc;
/// Data-extracting assertions for `FnOnce` values.
/// Only available in Panic mode, as these transform the assertion subject type.
#[cfg_attr(feature = "fluent", assertr_derive::fluent_aliases)]
pub trait FnOnceAssertions<'t, R> {
#[cfg(feature = "std")]
#[cfg_attr(feature = "fluent", fluent_alias("panic"))]
fn panics(self) -> AssertThat<'t, PanicValue, Panic>;
#[cfg(feature = "std")]
#[cfg_attr(feature = "fluent", fluent_alias("not_panic"))]
fn does_not_panic(self) -> AssertThat<'t, R, Panic>
where
R: Debug;
}
impl<'t, R, F: FnOnce() -> R> FnOnceAssertions<'t, R> for AssertThat<'t, F, Panic> {
#[track_caller]
#[cfg(feature = "std")]
fn panics(self) -> AssertThat<'t, PanicValue, Panic> {
self.track_assertion();
let this: AssertThat<Result<(), Box<dyn Any + Send + 'static>>, Panic> =
self.map(|it| match it {
Actual::Borrowed(_) => panic!("panics() can only be called on an owned FnOnce!"),
Actual::Owned(f) => {
// First, call the closure, receiving its output.
let res = std::panic::catch_unwind(core::panic::AssertUnwindSafe(f));
// Then, we drop the output,
// while catching any panics resulting from the `Drop` implementation.
let res = std::panic::catch_unwind(core::panic::AssertUnwindSafe(move || {
res.map(|value| drop(value))
}));
Actual::Owned(res.flatten())
}
});
if this.actual().is_ok() {
this.fail(|w: &mut String| {
writedoc! {w, r"
Expected: Function to panic when called.
Actual: No panic occurred!
"}
});
}
this.is_err()
.with_detail_message("Function did not panic as expected!")
.map(|it| {
let boxed_any = it.unwrap_owned();
PanicValue(boxed_any).into()
})
}
#[track_caller]
#[cfg(feature = "std")]
fn does_not_panic(self) -> AssertThat<'t, R, Panic>
where
R: Debug,
{
self.track_assertion();
let this: AssertThat<Result<R, Box<dyn Any + Send + 'static>>, Panic> =
self.map(|it| match it {
Actual::Borrowed(_) => {
panic!("does_not_panic() can only be called on an owned FnOnce!")
}
Actual::Owned(f) => {
// Only capture the closures output while catching panics.
// We do not expect ANY panics, so just put the output in the returned
// assertion context. Should a `Drop` impl of R lead to a panic,
// the asserting code will see that panic.
// We cannot test for drop panics in a more deliberate way hare,
// e.g. by actually trying to drop the value, because we want the
// user to be a ble to issue further assertions on value of `R`.
let res = std::panic::catch_unwind(core::panic::AssertUnwindSafe(f));
Actual::Owned(res)
}
});
if this.actual().is_err() {
this.fail(|w: &mut String| {
writedoc! {w, r"
Expected: Function to not panic when called.
Actual: Function panicked unexpectedly!
"}
});
}
this.is_ok()
.with_detail_message("Function panicked unexpectedly!")
.map(|it| it.unwrap_owned().into())
}
}
/// Data-extracting assertions for async `FnOnce` values.
/// Only available in Panic mode.
pub trait AsyncFnOnceAssertions<'t, R> {
#[cfg(feature = "std")]
fn panics_async(self) -> impl Future<Output = AssertThat<'t, PanicValue, Panic>>;
#[cfg(feature = "std")]
fn does_not_panic_async(self) -> impl Future<Output = AssertThat<'t, R, Panic>>
where
R: Debug + 't;
}
impl<'t, Fut, R, F> AsyncFnOnceAssertions<'t, R> for AssertThat<'t, F, Panic>
where
F: FnOnce() -> Fut + 't,
Fut: Future<Output = R> + UnwindSafe,
{
// #[track_caller] // This is implied in the default async desugaring.
#[cfg(feature = "std")]
async fn panics_async(self) -> AssertThat<'t, PanicValue, Panic> {
self.track_assertion();
// Execute the user function
let this: AssertThat<Result<(), Box<dyn Any + Send>>, Panic> = self
.map_async(|it| {
let f = match it {
Actual::Borrowed(_) => {
panic!("panics_async() can only be called on an owned FnOnce!")
}
Actual::Owned(f) => f,
};
async move {
// First, we await the future, receiving its output.
let res = FutureExt::catch_unwind(f()).await;
// Then, we drop the output,
// while catching any panics resulting from the `Drop` implementation.
let res = std::panic::catch_unwind(core::panic::AssertUnwindSafe(move || {
res.map(|value| drop(value))
}));
res.flatten()
}
})
.await;
if this.actual().is_ok() {
this.fail(|w: &mut String| {
writedoc! {w, r"
Expected: Function to panic when called.
Actual: No panic occurred!
"}
});
}
this.is_err()
.with_detail_message("Function did not panic as expected!")
.map(|it| {
let boxed_any: Box<dyn Any + Send> = it.unwrap_owned();
Actual::Owned(PanicValue(boxed_any))
})
}
// #[track_caller] // This is implied in the default async desugaring.
#[cfg(feature = "std")]
async fn does_not_panic_async(self) -> AssertThat<'t, R, Panic>
where
R: Debug + 't,
{
self.track_assertion();
let this: AssertThat<Result<R, Box<dyn Any + Send + 'static>>, Panic> = self
.map_async(|it| {
let f = match it {
Actual::Borrowed(_) => {
panic!("does_not_panic_async() can only be called on an owned FnOnce!")
}
Actual::Owned(f) => f,
};
async move {
// Only await the futures output while catching panics.
// We do not expect ANY panics, so just put the output in the returned
// assertion context. Should a `Drop` impl of R lead to a panic,
// the asserting code will see that panic.
// We cannot test for drop panics in a more deliberate way hare,
// e.g. by actually trying to drop the value, because we want the
// user to be a ble to issue further assertions on value of `R`.
FutureExt::catch_unwind(f()).await
}
})
.await;
if this.actual().is_err() {
this.fail(|w: &mut String| {
writedoc! {w, r"
Expected: Function to not panic when called.
Actual: Function panicked unexpectedly!
"}
});
}
this.is_ok()
.with_detail_message("Function panicked unexpectedly!")
.map(|it| Actual::Owned(it.unwrap_owned()))
}
}
#[cfg(test)]
mod tests {
mod fn_once {
mod panics {
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_panic_occurs() {
assert_that!(|| unimplemented!())
.panics()
.has_type::<&str>()
.is_equal_to("not implemented");
}
#[test]
fn panics_when_no_panic_occurs() {
assert_that_panic_by(|| assert_that!(|| 42).with_location(false).panics())
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Expected: Function to panic when called.
Actual: No panic occurred!
-------- assertr --------
"#});
}
}
mod does_not_panic {
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_no_panic_occurs() {
assert_that!(|| 42).does_not_panic();
}
#[test]
fn fails_when_panic_occurs() {
assert_that_panic_by(|| {
assert_that!(|| unimplemented!())
.with_location(false)
.does_not_panic()
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Expected: Function to not panic when called.
Actual: Function panicked unexpectedly!
-------- assertr --------
"#});
}
}
}
mod async_fn_once {
mod panics {
use crate::assert_that_panic_by_async;
use crate::prelude::*;
use indoc::formatdoc;
#[tokio::test]
async fn succeeds_when_panic_occurs() {
assert_that!(async || unimplemented!())
.panics_async()
.await
.has_type::<&str>()
.is_equal_to("not implemented");
}
#[tokio::test]
async fn panics_when_no_panic_occurs() {
assert_that_panic_by_async(async || {
assert_that!(async || 42)
.with_location(false)
.panics_async()
.await
})
.await
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Expected: Function to panic when called.
Actual: No panic occurred!
-------- assertr --------
"#});
}
}
mod does_not_panic {
use crate::assert_that_panic_by_async;
use crate::prelude::*;
use indoc::formatdoc;
#[tokio::test]
async fn succeeds_when_no_panic_occurs() {
assert_that!(|| 42).does_not_panic();
}
#[tokio::test]
async fn fails_when_panic_occurs() {
assert_that_panic_by_async(async || {
assert_that!(async || unimplemented!())
.with_location(false)
.does_not_panic_async()
.await
})
.await
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Expected: Function to not panic when called.
Actual: Function panicked unexpectedly!
-------- assertr --------
"#});
}
}
}
}