assertables/assert_is_empty/assert_not_empty.rs
1//! Assert an expression (such as a string or array) is not empty.
2//!
3//! Pseudocode:<br>
4//! ¬ a.is_empty()
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a = "alfa";
12//! assert_not_empty!(a);
13//! ```
14//!
15//! # Module macros
16//!
17//! * [`assert_not_empty`](macro@crate::assert_not_empty)
18//! * [`assert_not_empty_as_result`](macro@crate::assert_not_empty_as_result)
19//! * [`debug_assert_not_empty`](macro@crate::debug_assert_not_empty)
20
21/// Assert an expression (such as a string or array) is not empty.
22///
23/// Pseudocode:<br>
24/// ¬ a.is_empty()
25///
26/// * If true, return Result `Ok(())`.
27///
28/// * Otherwise, return Result `Err(message)`.
29///
30/// This macro is useful for runtime checks, such as checking parameters,
31/// or sanitizing inputs, or handling different results in different ways.
32///
33/// # Module macros
34///
35/// * [`assert_not_empty`](macro@crate::assert_not_empty)
36/// * [`assert_not_empty_as_result`](macro@crate::assert_not_empty_as_result)
37/// * [`debug_assert_not_empty`](macro@crate::debug_assert_not_empty)
38///
39#[macro_export]
40macro_rules! assert_not_empty_as_result {
41 ($a:expr $(,)?) => {{
42 match (&$a) {
43 a => {
44 if !(a.is_empty()) {
45 Ok(())
46 } else {
47 Err(
48 format!(
49 concat!(
50 "assertion failed: `assert_not_empty!(a)`\n",
51 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_not_empty.html\n",
52 " label: `{}`,\n",
53 " debug: `{:?}`"
54 ),
55 stringify!($a),
56 a,
57 )
58 )
59 }
60 }
61 }
62 }};
63}
64
65#[cfg(test)]
66mod test_assert_not_empty_as_result {
67
68 #[test]
69 fn success() {
70 let a = "alfa";
71 let actual = assert_not_empty_as_result!(a);
72 assert_eq!(actual.unwrap(), ());
73 }
74
75 #[test]
76 fn failure() {
77 let a = "";
78 let actual = assert_not_empty_as_result!(a);
79 let message = concat!(
80 "assertion failed: `assert_not_empty!(a)`\n",
81 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_not_empty.html\n",
82 " label: `a`,\n",
83 " debug: `\"\"`",
84 );
85 assert_eq!(actual.unwrap_err(), message);
86 }
87}
88
89/// Assert an expression (such as a string or array) is not empty.
90///
91/// Pseudocode:<br>
92/// ¬ a.is_empty()
93///
94/// * If true, return `()`.
95///
96/// * Otherwise, call [`panic!`] with a message and the values of the
97/// expressions with their debug representations.
98///
99/// # Examples
100///
101/// ```rust
102/// use assertables::*;
103/// # use std::panic;
104///
105/// # fn main() {
106/// let a = "alfa";
107/// assert_not_empty!(a);
108///
109/// # let result = panic::catch_unwind(|| {
110/// // This will panic
111/// let a = "";
112/// assert_not_empty!(a);
113/// # });
114/// // assertion failed: `assert_not_empty!(a)`
115/// // https://docs.rs/assertables/9.5.5/assertables/macro.assert_not_empty.html
116/// // label: `a`,
117/// // debug: `\"\"`
118/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
119/// # let message = concat!(
120/// # "assertion failed: `assert_not_empty!(a)`\n",
121/// # "https://docs.rs/assertables/9.5.5/assertables/macro.assert_not_empty.html\n",
122/// # " label: `a`,\n",
123/// # " debug: `\"\"`"
124/// # );
125/// # assert_eq!(actual, message);
126/// # }
127/// ```
128///
129/// # Module macros
130///
131/// * [`assert_not_empty`](macro@crate::assert_not_empty)
132/// * [`assert_not_empty_as_result`](macro@crate::assert_not_empty_as_result)
133/// * [`debug_assert_not_empty`](macro@crate::debug_assert_not_empty)
134///
135#[macro_export]
136macro_rules! assert_not_empty {
137 ($a:expr $(,)?) => {{
138 match $crate::assert_not_empty_as_result!($a) {
139 Ok(()) => (),
140 Err(err) => panic!("{}", err),
141 }
142 }};
143 ($a:expr, $($message:tt)+) => {{
144 match $crate::assert_not_empty_as_result!($a) {
145 Ok(()) => (),
146 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
147 }
148 }};
149}
150
151#[cfg(test)]
152mod test_assert_not_empty {
153 use std::panic;
154
155 #[test]
156 fn success() {
157 let a = "alfa";
158 let actual = assert_not_empty!(a);
159 assert_eq!(actual, ());
160 }
161
162 #[test]
163 fn failure() {
164 let a = "";
165 let result = panic::catch_unwind(|| {
166 let _actual = assert_not_empty!(a);
167 });
168 let message = concat!(
169 "assertion failed: `assert_not_empty!(a)`\n",
170 "https://docs.rs/assertables/9.5.5/assertables/macro.assert_not_empty.html\n",
171 " label: `a`,\n",
172 " debug: `\"\"`",
173 );
174 assert_eq!(
175 result
176 .unwrap_err()
177 .downcast::<String>()
178 .unwrap()
179 .to_string(),
180 message
181 );
182 }
183}
184
185/// Assert an expression (such as a string or array) is not empty.
186///
187/// Pseudocode:<br>
188/// ¬ a.is_empty()
189///
190/// This macro provides the same statements as [`assert_not_empty`](macro.assert_not_empty.html),
191/// except this macro's statements are only enabled in non-optimized
192/// builds by default. An optimized build will not execute this macro's
193/// statements unless `-C debug-assertions` is passed to the compiler.
194///
195/// This macro is useful for checks that are too expensive to be present
196/// in a release build but may be helpful during development.
197///
198/// The result of expanding this macro is always type checked.
199///
200/// An unchecked assertion allows a program in an inconsistent state to
201/// keep running, which might have unexpected consequences but does not
202/// introduce unsafety as long as this only happens in safe code. The
203/// performance cost of assertions, however, is not measurable in general.
204/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
205/// after thorough profiling, and more importantly, only in safe code!
206///
207/// This macro is intended to work in a similar way to
208/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
209///
210/// # Module macros
211///
212/// * [`assert_not_empty`](macro@crate::assert_not_empty)
213/// * [`assert_not_empty`](macro@crate::assert_not_empty)
214/// * [`debug_assert_not_empty`](macro@crate::debug_assert_not_empty)
215///
216#[macro_export]
217macro_rules! debug_assert_not_empty {
218 ($($arg:tt)*) => {
219 if $crate::cfg!(debug_assertions) {
220 $crate::assert_not_empty!($($arg)*);
221 }
222 };
223}