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