assertables/assert_len/assert_len_lt.rs
1//! Assert a length is less than another.
2//!
3//! Pseudocode:<br>
4//! a.len() < b.len()
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a = "x";
12//! let b = "xx";
13//! assert_len_lt!(a, b);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_len_lt`](macro@crate::assert_len_lt)
19//! * [`assert_len_lt_as_result`](macro@crate::assert_len_lt_as_result)
20//! * [`debug_assert_len_lt`](macro@crate::debug_assert_len_lt)
21
22/// Assert a length is less than another.
23///
24/// Pseudocode:<br>
25/// a.len() < b.len()
26///
27/// * If true, return Result `Ok((a.len(), b.len()))`.
28///
29/// * Otherwise, return Result `Err(message)`.
30///
31/// This macro is useful for runtime checks, such as checking parameters,
32/// or sanitizing inputs, or handling different results in different ways.
33///
34/// # Module macros
35///
36/// * [`assert_len_lt`](macro@crate::assert_len_lt)
37/// * [`assert_len_lt_as_result`](macro@crate::assert_len_lt_as_result)
38/// * [`debug_assert_len_lt`](macro@crate::debug_assert_len_lt)
39///
40#[macro_export]
41macro_rules! assert_len_lt_as_result {
42 ($a:expr, $b:expr $(,)?) => {
43 match ($a, $b) {
44 (a, b) => {
45 let a_len = a.len();
46 let b_len = b.len();
47 if a_len < b_len {
48 Ok((a_len, b_len))
49 } else {
50 Err(format!(
51 concat!(
52 "assertion failed: `assert_len_lt!(a, b)`\n",
53 "https://docs.rs/assertables/",
54 env!("CARGO_PKG_VERSION"),
55 "/assertables/macro.assert_len_lt.html\n",
56 " a label: `{}`,\n",
57 " a debug: `{:?}`,\n",
58 " a.len(): `{:?}`,\n",
59 " b label: `{}`,\n",
60 " b debug: `{:?}`\n",
61 " b.len(): `{:?}`",
62 ),
63 stringify!($a),
64 a,
65 a_len,
66 stringify!($b),
67 b,
68 b_len
69 ))
70 }
71 }
72 }
73 };
74}
75
76#[cfg(test)]
77mod test_assert_len_lt_as_result {
78 use std::sync::Once;
79
80 #[test]
81 fn lt() {
82 let a = "x";
83 let b = "xx";
84 for _ in 0..1 {
85 let actual = assert_len_lt_as_result!(a, b);
86 assert_eq!(actual.unwrap(), (1, 2));
87 }
88 }
89
90 #[test]
91 fn lt_once() {
92 static A: Once = Once::new();
93 fn a() -> &'static str {
94 if A.is_completed() {
95 panic!("A.is_completed()")
96 } else {
97 A.call_once(|| {})
98 }
99 "x"
100 }
101
102 static B: Once = Once::new();
103 fn b() -> &'static str {
104 if B.is_completed() {
105 panic!("B.is_completed()")
106 } else {
107 B.call_once(|| {})
108 }
109 "xx"
110 }
111
112 assert_eq!(A.is_completed(), false);
113 assert_eq!(B.is_completed(), false);
114 let result = assert_len_lt_as_result!(a(), b());
115 assert!(result.is_ok());
116 assert_eq!(A.is_completed(), true);
117 assert_eq!(B.is_completed(), true);
118 }
119
120 #[test]
121 fn eq() {
122 let a = "x";
123 let b = "x";
124 let actual = assert_len_lt_as_result!(a, b);
125 let message = concat!(
126 "assertion failed: `assert_len_lt!(a, b)`\n",
127 "https://docs.rs/assertables/",
128 env!("CARGO_PKG_VERSION"),
129 "/assertables/macro.assert_len_lt.html\n",
130 " a label: `a`,\n",
131 " a debug: `\"x\"`,\n",
132 " a.len(): `1`,\n",
133 " b label: `b`,\n",
134 " b debug: `\"x\"`\n",
135 " b.len(): `1`"
136 );
137 assert_eq!(actual.unwrap_err(), message);
138 }
139
140 #[test]
141 fn gt() {
142 let a = "xx";
143 let b = "x";
144 let actual = assert_len_lt_as_result!(a, b);
145 let message = concat!(
146 "assertion failed: `assert_len_lt!(a, b)`\n",
147 "https://docs.rs/assertables/",
148 env!("CARGO_PKG_VERSION"),
149 "/assertables/macro.assert_len_lt.html\n",
150 " a label: `a`,\n",
151 " a debug: `\"xx\"`,\n",
152 " a.len(): `2`,\n",
153 " b label: `b`,\n",
154 " b debug: `\"x\"`\n",
155 " b.len(): `1`"
156 );
157 assert_eq!(actual.unwrap_err(), message);
158 }
159}
160
161/// Assert a length is less than another.
162///
163/// Pseudocode:<br>
164/// a.len() < b.len()
165///
166/// * If true, return `(a.len(), b.len())`.
167///
168/// * Otherwise, call [`panic!`] with a message and the values of the
169/// expressions with their debug representations.
170///
171/// # Examples
172///
173/// ```rust
174/// use assertables::*;
175/// # use std::panic;
176///
177/// # fn main() {
178/// let a = "x";
179/// let b = "xx";
180/// assert_len_lt!(a, b);
181///
182/// # let result = panic::catch_unwind(|| {
183/// // This will panic
184/// let a = "xx";
185/// let b = "x";
186/// assert_len_lt!(a, b);
187/// # });
188/// // assertion failed: `assert_len_lt!(a, b)`
189/// // https://docs.rs/assertables/9.7.0/assertables/macro.assert_len_lt.html
190/// // a label: `a`,
191/// // a debug: `\"xx\"`,
192/// // a.len(): `2`",
193/// // b label: `b`,
194/// // b debug: `\"x\"`,
195/// // b.len(): `1`"
196/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
197/// # let message = concat!(
198/// # "assertion failed: `assert_len_lt!(a, b)`\n",
199/// # "https://docs.rs/assertables/", env!("CARGO_PKG_VERSION"), "/assertables/macro.assert_len_lt.html\n",
200/// # " a label: `a`,\n",
201/// # " a debug: `\"xx\"`,\n",
202/// # " a.len(): `2`,\n",
203/// # " b label: `b`,\n",
204/// # " b debug: `\"x\"`\n",
205/// # " b.len(): `1`",
206/// # );
207/// # assert_eq!(actual, message);
208/// # }
209/// ```
210///
211/// # Module macros
212///
213/// * [`assert_len_lt`](macro@crate::assert_len_lt)
214/// * [`assert_len_lt_as_result`](macro@crate::assert_len_lt_as_result)
215/// * [`debug_assert_len_lt`](macro@crate::debug_assert_len_lt)
216///
217#[macro_export]
218macro_rules! assert_len_lt {
219 ($a:expr, $b:expr $(,)?) => {
220 match $crate::assert_len_lt_as_result!($a, $b) {
221 Ok(x) => x,
222 Err(err) => panic!("{}", err),
223 }
224 };
225 ($a:expr, $b:expr, $($message:tt)+) => {
226 match $crate::assert_len_lt_as_result!($a, $b) {
227 Ok(x) => x,
228 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
229 }
230 };
231}
232
233#[cfg(test)]
234mod test_assert_len_lt {
235 use std::panic;
236
237 #[test]
238 fn lt() {
239 let a = "x";
240 let b = "xx";
241 for _ in 0..1 {
242 let actual = assert_len_lt!(a, b);
243 assert_eq!(actual, (1, 2));
244 }
245 }
246
247 #[test]
248 fn eq() {
249 let a = "x";
250 let b = "x";
251 let result = panic::catch_unwind(|| {
252 let _actual = assert_len_lt!(a, b);
253 });
254 let message = concat!(
255 "assertion failed: `assert_len_lt!(a, b)`\n",
256 "https://docs.rs/assertables/",
257 env!("CARGO_PKG_VERSION"),
258 "/assertables/macro.assert_len_lt.html\n",
259 " a label: `a`,\n",
260 " a debug: `\"x\"`,\n",
261 " a.len(): `1`,\n",
262 " b label: `b`,\n",
263 " b debug: `\"x\"`\n",
264 " b.len(): `1`"
265 );
266 assert_eq!(
267 result
268 .unwrap_err()
269 .downcast::<String>()
270 .unwrap()
271 .to_string(),
272 message
273 );
274 }
275
276 #[test]
277 fn gt() {
278 let a = "xx";
279 let b = "x";
280 let result = panic::catch_unwind(|| {
281 let _actual = assert_len_lt!(a, b);
282 });
283 let message = concat!(
284 "assertion failed: `assert_len_lt!(a, b)`\n",
285 "https://docs.rs/assertables/",
286 env!("CARGO_PKG_VERSION"),
287 "/assertables/macro.assert_len_lt.html\n",
288 " a label: `a`,\n",
289 " a debug: `\"xx\"`,\n",
290 " a.len(): `2`,\n",
291 " b label: `b`,\n",
292 " b debug: `\"x\"`\n",
293 " b.len(): `1`"
294 );
295 assert_eq!(
296 result
297 .unwrap_err()
298 .downcast::<String>()
299 .unwrap()
300 .to_string(),
301 message
302 );
303 }
304}
305
306/// Assert a length is less than another.
307///
308/// Pseudocode:<br>
309/// a.len() < b.len()
310///
311/// This macro provides the same statements as [`assert_len_lt`](macro.assert_len_lt.html),
312/// except this macro's statements are only enabled in non-optimized
313/// builds by default. An optimized build will not execute this macro's
314/// statements unless `-C debug-assertions` is passed to the compiler.
315///
316/// This macro is useful for checks that are too expensive to be present
317/// in a release build but may be helpful during development.
318///
319/// The result of expanding this macro is always type checked.
320///
321/// An unchecked assertion allows a program in an inconsistent state to
322/// keep running, which might have unexpected consequences but does not
323/// introduce unsafety as long as this only happens in safe code. The
324/// performance cost of assertions, however, is not measurable in general.
325/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
326/// after thorough profiling, and more importantly, only in safe code!
327///
328/// This macro is intended to work in a similar way to
329/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
330///
331/// # Module macros
332///
333/// * [`assert_len_lt`](macro@crate::assert_len_lt)
334/// * [`assert_len_lt`](macro@crate::assert_len_lt)
335/// * [`debug_assert_len_lt`](macro@crate::debug_assert_len_lt)
336///
337#[macro_export]
338macro_rules! debug_assert_len_lt {
339 ($($arg:tt)*) => {
340 if $crate::cfg!(debug_assertions) {
341 $crate::assert_len_lt!($($arg)*);
342 }
343 };
344}