all_asserts/
lib.rs

1//! # `all_asserts`
2//!
3//! The `all_asserts` crate provides multiple assert macros which aren't there in the
4//! standard library. These macros provide extremely useful outputs and can be used for
5//! writing better tests.
6
7/// Asserts that the left hand side expression is greater
8/// than the right hand side expression
9///
10/// # Examples
11/// ```
12/// use all_asserts::assert_gt;
13/// let a = 100; let b = 100;
14/// #[cfg(should_panic)]
15/// assert_gt!(a, b);
16/// ```
17///
18#[macro_export]
19macro_rules! assert_gt {
20    ($left:expr, $right:expr $(,)?) => ({
21        match (&$left, &$right) {
22            (left_val, right_val) => {
23                if !(*left_val > *right_val) {
24                    ::std::panic!(r#"assertion failed: `(left > right) but here (left <= right)`
25  left: `{:?}`,
26 right: `{:?}`"#, &*left_val, &*right_val)
27                }
28            }
29        }
30    });
31    ($left:expr, $right:expr, $($arg:tt)+) => ({
32        match (&($left), &($right)) {
33            (left_val, right_val) => {
34                if !(*left_val > *right_val) {
35                    ::std::panic!(r#"assertion failed: `(left > right) but here (left <= right)`
36  left: `{:?}`,
37 right: `{:?}`: {}"#, &*left_val, &*right_val,
38                           ::std::format_args!($($arg)+))
39                    }
40                }
41            }
42        });
43    }
44
45/// This is a debug-only variant of the [`assert_gt`] macro
46///
47/// [`assert_gt`]: ./macro.assert_gt.html
48#[macro_export]
49macro_rules! debug_assert_gt {
50    ($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { $crate::assert_gt!($($arg)*); })
51}
52/// Asserts that the left hand side expression is greater
53/// than or equal to the right hand side expression
54///
55/// # Examples
56/// ```
57/// # #[macro_use] use all_asserts;
58/// let a = 200; let b = 100;
59/// #[cfg(should_panic)]
60/// assert_ge!(a, b);
61/// ```
62///
63#[macro_export]
64macro_rules! assert_ge {
65    ($left:expr, $right:expr $(,)?) => ({
66        match (&$left, &$right) {
67            (left_val, right_val) => {
68                if !(*left_val >= *right_val) {
69                    ::std::panic!(r#"assertion failed: `(left >= right) but here (left < right)`
70  left: `{:?}`,
71 right: `{:?}`"#, &*left_val, &*right_val)
72                }
73            }
74        }
75    });
76    ($left:expr, $right:expr, $($arg:tt)+) => ({
77        match (&($left), &($right)) {
78            (left_val, right_val) => {
79                if !(*left_val > *right_val) {
80                    ::std::panic!(r#"assertion failed: `(left >= right) but here (left < right)`
81  left: `{:?}`,
82 right: `{:?}`: {}"#, &*left_val, &*right_val,
83                           ::std::format_args!($($arg)+))
84                }
85            }
86        }
87    });
88}
89
90/// This is a debug-only variant of the [`assert_ge`] macro
91///
92/// [`assert_ge`]: ./macro.assert_ge.html
93#[macro_export]
94macro_rules! debug_assert_ge {
95    ($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { $crate::assert_ge!($($arg)*); })
96}
97
98/// Asserts that the left hand side expression is lesser
99/// than the right hand side expression
100///
101/// # Examples
102/// ```
103/// # #[macro_use] use all_asserts;
104/// let a = 100; let b = 100;
105/// #[cfg(should_panic)]
106/// assert_lt!(a, b); // This would panic
107/// ```
108///
109#[macro_export]
110macro_rules! assert_lt {
111    ($left:expr, $right:expr $(,)?) => ({
112        match (&$left, &$right) {
113            (left_val, right_val) => {
114                if !(*left_val < *right_val) {
115                    ::std::panic!(r#"assertion failed: `(left < right) but here (left >= right)`
116  left: `{:?}`,
117 right: `{:?}`"#, &*left_val, &*right_val)
118                }
119            }
120        }
121    });
122    ($left:expr, $right:expr, $($arg:tt)+) => ({
123        match (&($left), &($right)) {
124            (left_val, right_val) => {
125                if !(*left_val < *right_val) {
126                    ::std::panic!(r#"assertion failed: `(left < right) but here (left >= right)`
127  left: `{:?}`,
128 right: `{:?}`: {}"#, &*left_val, &*right_val,
129                           ::std::format_args!($($arg)+))
130                }
131            }
132        }
133    });
134}
135
136/// This is a debug-only variant of the [`assert_lt`] macro
137///
138/// [`assert_lt`]: ./macro.assert_lt.html
139#[macro_export]
140macro_rules! debug_assert_lt {
141    ($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { $crate::assert_lt!($($arg)*); })
142}
143
144/// Asserts that the left hand side expression is lesser than
145/// or equal to the right hand side expression
146///
147/// # Examples
148/// ```
149/// # #[macro_use] use all_asserts;
150/// let a = 200; let b = 100;
151/// #[cfg(should_panic)]
152/// assert_le!(a, b); // This would panic
153/// ```
154///
155#[macro_export]
156macro_rules! assert_le {
157    ($left:expr, $right:expr $(,)?) => ({
158        match (&$left, &$right) {
159            (left_val, right_val) => {
160                if !(*left_val <= *right_val) {
161                    ::std::panic!(r#"assertion failed: `(left <= right) but here (left > right)`
162  left: `{:?}`,
163 right: `{:?}`"#, &*left_val, &*right_val)
164                }
165            }
166        }
167    });
168    ($left:expr, $right:expr, $($arg:tt)+) => ({
169        match (&($left), &($right)) {
170            (left_val, right_val) => {
171                if !(*left_val <= *right_val) {
172                    ::std::panic!(r#"assertion failed: `(left <= right) but here (left > right)`
173  left: `{:?}`,
174 right: `{:?}`: {}"#, &*left_val, &*right_val,
175                           ::std::format_args!($($arg)+))
176                }
177            }
178        }
179    });
180}
181
182/// This is a debug-only variant of the [`assert_le`] macro
183///
184/// [`assert_le`]: ./macro.assert_le.html
185#[macro_export]
186macro_rules! debug_assert_le {
187    ($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { $crate::assert_le!($($arg)*); })
188}
189
190/// Asserts that the right hand side expression is
191/// within the range on the left hand side
192///
193/// # Examples
194/// ```
195/// # #[macro_use] use all_asserts::assert_range;
196/// assert_range!((100..200), 101);
197/// ```
198///
199#[macro_export]
200macro_rules! assert_range {
201    ($left:expr, $right:expr) => ({
202        match (&$left, &$right) {
203            (left_val, right_val) => {
204                if !(::std::ops::RangeBounds::contains(left_val, right_val)) {
205                    ::std::panic!(
206                        r#"assertion failed: `{} is not in range of {:?}` - it should have been in this range"#,
207                        right_val,
208                        left_val
209                    )
210                }
211            }
212        }
213    });
214    ($left:expr, $right:expr, $($arg:tt)+) => ({
215        match (&($left), &($right)) {
216            (left_val, right_val) => {
217                if !(::std::ops::RangeBounds::contains(left_val, right_val)) {
218                    ::std::panic!(
219                        r#"assertion failed: `{} is not in range of {:?}` - it should have been in this range: {}"#,
220                        right_val,
221                        left_val,
222                        ::std::format_args!($($arg)+)
223                    )
224                }
225            }
226        }
227    });
228}
229
230/// This is a debug-only variant of the [`assert_range`] macro
231///
232/// [`assert_range`]: ./macro.assert_range.html
233#[macro_export]
234macro_rules! debug_assert_range {
235    ($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { $crate::assert_range!($($arg)*); })
236}
237
238/// Asserts that the right hand side expression is not
239/// within the range on the left hand side
240///
241/// # Examples
242/// ```
243/// # #[macro_use] use all_asserts::assert_nrange;
244/// assert_nrange!((100..200), 1000);
245/// ```
246///
247#[macro_export]
248macro_rules! assert_nrange {
249    ($left:expr, $right:expr $(,)?) => ({
250        match (&$left, &$right) {
251            (left_val, right_val) => {
252                if (::std::ops::RangeBounds::contains(left_val, right_val)) {
253                    ::std::panic!(
254                        r#"assertion failed: `{} is in range of {:?}` - it should not have been in this range"#,
255                        right_val,
256                        left_val
257                    )
258                }
259            }
260        }
261    });
262    ($left:expr, $right:expr, $($arg:tt)+) => ({
263        match (&($left), &($right)) {
264            (left_val, right_val) => {
265                if (::std::ops::RangeBounds::contains(left_val, right_val)) {
266                    ::std::panic!(
267                        r#"assertion failed: `{} is in range of {:?}` - it should not have been in this range: {}"#,
268                        right_val,
269                        left_val,
270                        ::std::format_args!($($arg)+)
271                    )
272                }
273            }
274        }
275    });
276}
277
278/// This is a debug-only variant of the [`assert_nrange`] macro
279///
280/// [`assert_nrange`]: ./macro.assert_nrange.html
281#[macro_export]
282macro_rules! debug_assert_nrange {
283    ($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { $crate::assert_nrange!($($arg)*); })
284}
285
286/// Asserts that the right hand side expression is
287/// within the range on the left hand side
288///
289/// # Examples
290/// ```
291/// # #[macro_use] use all_asserts::assert_near;
292/// assert_near!(100.0, 100.9, 1.0);
293/// assert_near!(100.9, 100.0, 1.0);
294/// ```
295///
296#[macro_export]
297macro_rules! assert_near {
298    ($left:expr, $right:expr, $epsilon:expr $(,)?) => ({
299        match (&$left, &$right, &$epsilon) {
300            (left_val, right_val, epsilon_val) => {
301                if !((*left_val <= (*right_val + *epsilon_val)) && (*left_val >= (*right_val - *epsilon_val))) {
302                    ::std::panic!(
303                        r#"assertion failed: `{} is not within epsilon {} of {}`"#,
304                        right_val,
305                        epsilon_val,
306                        left_val
307                    )
308                }
309            }
310        }
311    });
312    ($left:expr, $right:expr, $epsilon:expr, $($arg:tt)+ $(,)?) => ({
313        match (&($left), &($right), &($epsilon)) {
314            (left_val, right_val, epsilon_val) => {
315                if !((*left_val <= (*right_val + *epsilon_val)) && (*left_val >= (*right_val - *epsilon_val))) {
316                    ::std::panic!(
317                        r#"assertion failed: `{} is not within epsilon {} of {}`"#,
318                        right_val,
319                        epsilon_val,
320                        left_val,
321                        ::std::format_args!($($arg)+)
322                    )
323                }
324            }
325        }
326    });
327}
328
329/// This is a debug-only variant of the [`assert_near`] macro
330///
331/// [`assert_near`]: ./macro.assert_near.html
332#[macro_export]
333macro_rules! debug_assert_near {
334    ($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { $crate::assert_near!($($arg)*); })
335}
336
337/// Asserts that the expression is true
338///
339/// # Examples
340/// ```
341/// use all_asserts::assert_true;
342/// let a = false;
343/// #[cfg(should_panic)]
344/// assert_true!(a);
345/// let a = true;
346/// assert_true!(a);
347/// ```
348///
349#[macro_export]
350macro_rules! assert_true {
351    ($cond:expr $(,)?) => ({
352        ::std::assert!($cond)
353    });
354    ($cond:expr, $($arg:tt)+) => ({
355        ::std::assert!($cond, $($arg)+)
356    });
357}
358
359/// This is a debug-only variant of the [`assert_true`] macro
360///
361/// [`assert_true`]: ./macro.assert_true.html
362#[macro_export]
363macro_rules! debug_assert_true {
364    ($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { $crate::assert_true!($($arg)*); })
365}
366
367/// Asserts that the expression is false
368///
369/// # Examples
370/// ```
371/// use all_asserts::assert_false;
372/// let a = true;
373/// #[cfg(should_panic)]
374/// assert_false!(a);
375/// let a = false;
376/// assert_false!(a);
377/// ```
378///
379#[macro_export]
380macro_rules! assert_false {
381    ($cond:expr $(,)?) => ({
382        ::std::assert!(!$cond)
383    });
384    ($cond:expr, $($arg:tt)+) => ({
385        ::std::assert!(!$cond, $($arg)+)
386    });
387}
388
389/// This is a debug-only variant of the [`assert_false`] macro
390///
391/// [`assert_false`]: ./macro.assert_false.html
392#[macro_export]
393macro_rules! debug_assert_false {
394    ($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { $crate::assert_false($($arg)*); })
395}