assertables/assert_count/assert_count_ne.rs
1//! Assert a count is not equal to another.
2//!
3//! Pseudocode:<br>
4//! a.count() ≠ b.count()
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a = "x".chars();
12//! let b = "xx".chars();
13//! assert_count_ne!(a, b);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_count_ne`](macro@crate::assert_count_ne)
19//! * [`assert_count_ne_as_result`](macro@crate::assert_count_ne_as_result)
20//! * [`debug_assert_count_ne`](macro@crate::debug_assert_count_ne)
21
22/// Assert a count is not equal to another.
23///
24/// Pseudocode:<br>
25/// a.count() ≠ b.count()
26///
27/// * If true, return Result `Ok((a.count(), 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_count_ne`](macro@crate::assert_count_ne)
37/// * [`assert_count_ne_as_result`](macro@crate::assert_count_ne_as_result)
38/// * [`debug_assert_count_ne`](macro@crate::debug_assert_count_ne)
39///
40#[macro_export]
41macro_rules! assert_count_ne_as_result {
42 ($a:expr, $b:expr $(,)?) => {{
43 match (&$a, &$b) {
44 (a, b) => {
45 let a_count = a.clone().count();
46 let b_count = b.clone().count();
47 if a_count != b_count {
48 Ok((a_count, b_count))
49 } else {
50 Err(
51 format!(
52 concat!(
53 "assertion failed: `assert_count_ne!(a, b)`\n",
54 "https://docs.rs/assertables/9.5.3/assertables/macro.assert_count_ne.html\n",
55 " a label: `{}`,\n",
56 " a debug: `{:?}`,\n",
57 " a.count(): `{:?}`,\n",
58 " b label: `{}`,\n",
59 " b debug: `{:?}`\n",
60 " b.count(): `{:?}`",
61 ),
62 stringify!($a),
63 a,
64 a_count,
65 stringify!($b),
66 b,
67 b_count
68 )
69 )
70 }
71 }
72 }
73 }};
74}
75
76#[cfg(test)]
77mod test_assert_count_ne_as_result {
78
79 #[test]
80 fn lt() {
81 let a = "x".chars();
82 let b = "xx".chars();
83 let actual = assert_count_ne_as_result!(a, b);
84 assert_eq!(actual.unwrap(), (1, 2));
85 }
86
87 #[test]
88 fn gt() {
89 let a = "xx".chars();
90 let b = "x".chars();
91 let actual = assert_count_ne_as_result!(a, b);
92 assert_eq!(actual.unwrap(), (2, 1));
93 }
94
95 #[test]
96 fn failure() {
97 let a = "x".chars();
98 let b = "x".chars();
99 let actual = assert_count_ne_as_result!(a, b);
100 let message = concat!(
101 "assertion failed: `assert_count_ne!(a, b)`\n",
102 "https://docs.rs/assertables/9.5.3/assertables/macro.assert_count_ne.html\n",
103 " a label: `a`,\n",
104 " a debug: `Chars(['x'])`,\n",
105 " a.count(): `1`,\n",
106 " b label: `b`,\n",
107 " b debug: `Chars(['x'])`\n",
108 " b.count(): `1`"
109 );
110 assert_eq!(actual.unwrap_err(), message);
111 }
112}
113
114/// Assert a count is not equal to another.
115///
116/// Pseudocode:<br>
117/// a.count() ≠ b.count()
118///
119/// * If true, return `(a.count(), b)`.
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".chars();
132/// let b = "xx".chars();
133/// assert_count_ne!(a, b);
134///
135/// # let result = panic::catch_unwind(|| {
136/// // This will panic
137/// let a = "x".chars();
138/// let b = "x".chars();
139/// assert_count_ne!(a, b);
140/// # });
141/// // assertion failed: `assert_count_ne!(a, b)`
142/// // https://docs.rs/assertables/9.5.3/assertables/macro.assert_count_ne.html
143/// // a label: `a`,
144/// // a debug: `Chars(['x'])`,
145/// // a.count(): `1`",
146/// // b label: `b`,
147/// // b debug: `Chars(['x'])`,
148/// // b.count(): `1`"
149/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
150/// # let message = concat!(
151/// # "assertion failed: `assert_count_ne!(a, b)`\n",
152/// # "https://docs.rs/assertables/9.5.3/assertables/macro.assert_count_ne.html\n",
153/// # " a label: `a`,\n",
154/// # " a debug: `Chars(['x'])`,\n",
155/// # " a.count(): `1`,\n",
156/// # " b label: `b`,\n",
157/// # " b debug: `Chars(['x'])`\n",
158/// # " b.count(): `1`",
159/// # );
160/// # assert_eq!(actual, message);
161/// # }
162/// ```
163///
164/// # Module macros
165///
166/// * [`assert_count_ne`](macro@crate::assert_count_ne)
167/// * [`assert_count_ne_as_result`](macro@crate::assert_count_ne_as_result)
168/// * [`debug_assert_count_ne`](macro@crate::debug_assert_count_ne)
169///
170#[macro_export]
171macro_rules! assert_count_ne {
172 ($a:expr, $b:expr $(,)?) => {{
173 match $crate::assert_count_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_count_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_count_ne {
188 use std::panic;
189
190 #[test]
191 fn lt() {
192 let a = "x".chars();
193 let b = "xx".chars();
194 let actual = assert_count_ne!(a, b);
195 assert_eq!(actual, (1, 2));
196 }
197
198 #[test]
199 fn gt() {
200 let a = "xx".chars();
201 let b = "x".chars();
202 let actual = assert_count_ne!(a, b);
203 assert_eq!(actual, (2, 1));
204 }
205
206 #[test]
207 fn failure() {
208 let result = panic::catch_unwind(|| {
209 let a = "x".chars();
210 let b = "x".chars();
211 let _actual = assert_count_ne!(a, b);
212 });
213 let message = concat!(
214 "assertion failed: `assert_count_ne!(a, b)`\n",
215 "https://docs.rs/assertables/9.5.3/assertables/macro.assert_count_ne.html\n",
216 " a label: `a`,\n",
217 " a debug: `Chars(['x'])`,\n",
218 " a.count(): `1`,\n",
219 " b label: `b`,\n",
220 " b debug: `Chars(['x'])`\n",
221 " b.count(): `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 count is not equal to another.
235///
236/// Pseudocode:<br>
237/// a.count() ≠ b.count()
238///
239/// This macro provides the same statements as [`assert_count_ne`](macro.assert_count_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_count_ne`](macro@crate::assert_count_ne)
262/// * [`assert_count_ne`](macro@crate::assert_count_ne)
263/// * [`debug_assert_count_ne`](macro@crate::debug_assert_count_ne)
264///
265#[macro_export]
266macro_rules! debug_assert_count_ne {
267 ($($arg:tt)*) => {
268 if $crate::cfg!(debug_assertions) {
269 $crate::assert_count_ne!($($arg)*);
270 }
271 };
272}