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
use super::*;
use std::fmt::Debug;
#[extend::ext(name=SweetVec)]
pub impl<T: Debug> Vec<T> {
/// Performs an assertion ensuring at least one item in the vec passes the predicate.
///
/// ## Example
///
/// ```
/// # use sweet::prelude::*;
/// vec![false, true].xpect_any(|v| *v == true);
/// ```
///
/// ## Panics
///
/// Panics if no items pass.
fn xpect_any(&self, func: impl Fn(&T) -> bool) -> &Self {
if !self.iter().any(|item| func(item)) {
assert_ext::panic_expected_received_display_debug(
"Any item to pass predicate",
self,
);
}
self
}
/// Performs an assertion ensuring the `Vec` is empty.
///
/// ## Example
///
/// ```
/// # use sweet::prelude::*;
/// Vec::<u32>::new().xpect_empty();
/// ```
///
/// ## Panics
///
/// Panics if the `Vec` is not empty.
fn xpect_empty(&self) -> &Self {
if !self.is_empty() {
assert_ext::panic_expected_received_display_debug(
"To be empty",
self,
);
}
self
}
}
#[cfg(test)]
mod test {
use crate::prelude::*;
#[test]
#[should_panic]
fn xpect_any_panics() { vec![false, false].xpect_any(|v| *v == true); }
#[test]
fn xpect_any() { vec![false, true].xpect_any(|v| *v == true); }
#[test]
fn xpect_empty() { Vec::<()>::default().xpect_empty(); }
}