assertables/assert_count/
assert_count_gt.rs

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