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