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