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