testutils/
assert_empty.rs

1/// Asserts that expression is empty using `is_empty`.
2///
3/// ## Uses
4///
5/// Assertions are always checked in both debug and release builds, and cannot be disabled.
6/// See [`debug_assert_empty!`] for assertions that are not enabled in release builds by default.
7///
8/// ## Custom messages
9///
10/// This macro has a second form, where a custom panic message can be provided
11/// with or without arguments for formatting. See [`std::fmt`] for syntax for this form.
12///
13/// ## Examples
14///
15/// ```rust
16/// use testutils::assert_empty;
17/// # fn main() {
18/// let vec: Vec<usize> = vec![];
19///
20/// assert_empty!(vec);
21///
22/// // With custom messages
23/// assert_empty!(vec, "Expecting {:?} to be empty", vec);
24/// # }
25/// ```
26///
27/// A non-empty value will cause a panic:
28///
29/// ```rust,should_panic
30/// use testutils::assert_empty;
31/// # fn main() {
32/// let vec: Vec<usize> = vec![1];
33///
34/// assert_empty!(vec); // Will panic
35/// //
36/// # }
37/// ```
38///
39/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
40/// [`debug_assert_empty!`]: ./macro.debug_assert_empty.html
41#[macro_export]
42macro_rules! assert_empty {
43	($expression:expr) => {
44		assert!($expression.is_empty(), "assertion failed, expected {:?} to be empty", $expression)
45	};
46	($expression:expr, $($arg:tt)+) => {
47		assert!(
48			$expression.is_empty(),
49			"assertion failed, expected {:?} to be empty: {}",
50			$expression,
51			format_args!($($arg)+)
52		)
53	};
54}
55
56/// Asserts that expression is empty using `is_empty`.
57///
58/// Like [`assert_empty!`], this macro also has a second version,
59/// where a custom panic message can be provided.
60///
61/// ## Uses
62///
63/// See [`debug_assert!`] documentation for possible use cases.
64/// The same applies to this macro.
65///
66/// [`debug_assert!`]: https://doc.rust-lang.org/std/macro.debug_assert.html
67/// [`assert_empty!`]: ./macro.assert_empty.html
68#[macro_export]
69macro_rules! debug_assert_empty {
70	($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_empty!($($arg)*); })
71}