assertables/
assert_le.rs

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