assertables/assert_any.rs
1//! Assert every element of the iterator matches a predicate.
2//!
3//! Pseudocode:<br>
4//! collection into iter ∀ predicate
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a = [1, 2, 3];
12//! assert_any!(a.into_iter(), |x: i8| x > 0);
13//! ```
14//!
15//! This implementation uses [`::std::iter::Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html).
16//!
17//! # Module macros
18//!
19//! * [`assert_any`](macro@crate::assert_any)
20//! * [`assert_any_as_result`](macro@crate::assert_any_as_result)
21//! * [`debug_assert_any`](macro@crate::debug_assert_any)
22
23/// Assert every element of the iterator matches a predicate.
24///
25/// Pseudocode:<br>
26/// collection into iter ∀ predicate
27///
28/// * If true, return Result `Ok(())`.
29///
30/// * Otherwise, return Result `Err(message)`.
31///
32/// This macro is useful for runtime checks, such as checking parameters,
33/// or sanitizing inputs, or handling different results in different ways.
34///
35/// This implementation uses [`::std::iter::Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html).
36///
37/// # Module macros
38///
39/// * [`assert_any`](macro@crate::assert_any)
40/// * [`assert_any_as_result`](macro@crate::assert_any_as_result)
41/// * [`debug_assert_any`](macro@crate::debug_assert_any)
42///
43#[macro_export]
44macro_rules! assert_any_as_result {
45 ($collection:expr, $predicate:expr $(,)?) => {{
46 match (&$collection, &$predicate) {
47 (collection, _predicate) => {
48 if $collection.any($predicate) {
49 Ok(())
50 } else {
51 Err(format!(
52 concat!(
53 "assertion failed: `assert_any!(collection, predicate)`\n",
54 "https://docs.rs/assertables/9.5.1/assertables/macro.assert_any.html\n",
55 " collection label: `{}`,\n",
56 " collection debug: `{:?}`,\n",
57 " predicate: `{}`",
58 ),
59 stringify!($collection),
60 collection,
61 stringify!($predicate)
62 ))
63 }
64 }
65 }
66 }};
67}
68
69#[cfg(test)]
70mod test_assert_any_as_result {
71
72 #[test]
73 fn success() {
74 let a = [1, 2, 3];
75 let actual = assert_any_as_result!(a.into_iter(), |x: i8| x > 0);
76 assert_eq!(actual.unwrap(), ());
77 }
78
79 #[test]
80 fn failure() {
81 let a = [1, 2, 3];
82 let actual = assert_any_as_result!(a.into_iter(), |x: i8| x > 3);
83 let message = concat!(
84 "assertion failed: `assert_any!(collection, predicate)`\n",
85 "https://docs.rs/assertables/9.5.1/assertables/macro.assert_any.html\n",
86 " collection label: `a.into_iter()`,\n",
87 " collection debug: `IntoIter([1, 2, 3])`,\n",
88 " predicate: `|x: i8| x > 3`"
89 );
90 assert_eq!(actual.unwrap_err(), message);
91 }
92}
93
94/// Assert every element of the iterator matches a predicate.
95///
96/// Pseudocode:<br>
97/// collection into iter ∀ predicate
98///
99/// * If true, return `()`.
100///
101/// * Otherwise, call [`panic!`] with a message and the values of the
102/// expressions with their debug representations.
103///
104/// # Examples
105///
106/// ```rust
107/// use assertables::*;
108/// # use std::panic;
109///
110/// # fn main() {
111/// let a = [1, 2, 3];
112/// assert_any!(a.into_iter(), |x: i8| x > 0);
113///
114/// # let result = panic::catch_unwind(|| {
115/// // This will panic
116/// let a = [1, 2, 3];
117/// assert_any!(a.into_iter(), |x: i8| x > 3);
118/// # });
119/// // assertion failed: `assert_any!(collection, predicate)`
120/// // https://docs.rs/assertables/9.5.1/assertables/macro.assert_any.html
121/// // collection label: `a.into_iter()`,
122/// // collection debug: `IntoIter([1, 2, 3])`,
123/// // predicate: `|x: i8| x > 3`
124/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
125/// # let message = concat!(
126/// # "assertion failed: `assert_any!(collection, predicate)`\n",
127/// # "https://docs.rs/assertables/9.5.1/assertables/macro.assert_any.html\n",
128/// # " collection label: `a.into_iter()`,\n",
129/// # " collection debug: `IntoIter([1, 2, 3])`,\n",
130/// # " predicate: `|x: i8| x > 3`",
131/// # );
132/// # assert_eq!(actual, message);
133/// # }
134/// ```
135///
136/// This implementation uses [`::std::iter::Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html).
137///
138/// # Module macros
139///
140/// * [`assert_any`](macro@crate::assert_any)
141/// * [`assert_any_as_result`](macro@crate::assert_any_as_result)
142/// * [`debug_assert_any`](macro@crate::debug_assert_any)
143///
144#[macro_export]
145macro_rules! assert_any {
146 ($collection:expr, $predicate:expr $(,)?) => {{
147 match $crate::assert_any_as_result!($collection, $predicate) {
148 Ok(()) => (),
149 Err(err) => panic!("{}", err),
150 }
151 }};
152 ($collection:expr, $predicate:expr, $($message:tt)+) => {{
153 match $crate::assert_any_as_result!($collection, $predicate) {
154 Ok(()) => (),
155 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
156 }
157 }};
158}
159
160#[cfg(test)]
161mod test_assert_any {
162 use std::panic;
163
164 #[test]
165 fn success() {
166 let a = [1, 2, 3];
167 let actual = assert_any!(a.into_iter(), |x: i8| x > 0);
168 assert_eq!(actual, ());
169 }
170
171 #[test]
172 fn failure() {
173 let a = [1, 2, 3];
174 let result = panic::catch_unwind(|| {
175 let _actual = assert_any!(a.into_iter(), |x: i8| x > 3);
176 });
177 let message = concat!(
178 "assertion failed: `assert_any!(collection, predicate)`\n",
179 "https://docs.rs/assertables/9.5.1/assertables/macro.assert_any.html\n",
180 " collection label: `a.into_iter()`,\n",
181 " collection debug: `IntoIter([1, 2, 3])`,\n",
182 " predicate: `|x: i8| x > 3`"
183 );
184 assert_eq!(
185 result
186 .unwrap_err()
187 .downcast::<String>()
188 .unwrap()
189 .to_string(),
190 message
191 );
192 }
193}
194
195/// Assert every element of the iterator matches a predicate.
196///
197/// Pseudocode:<br>
198/// collection into iter ∀ predicate
199///
200/// This macro provides the same statements as [`assert_any`](macro.assert_any.html),
201/// except this macro's statements are only enabled in non-optimized
202/// builds by default. An optimized build will not execute this macro's
203/// statements unless `-C debug-assertions` is passed to the compiler.
204///
205/// This macro is useful for checks that are too expensive to be present
206/// in a release build but may be helpful during development.
207///
208/// The result of expanding this macro is always type checked.
209///
210/// An unchecked assertion allows a program in an inconsistent state to
211/// keep running, which might have unexpected consequences but does not
212/// introduce unsafety as long as this only happens in safe code. The
213/// performance cost of assertions, however, is not measurable in general.
214/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
215/// after thorough profiling, and more importantly, only in safe code!
216///
217/// This macro is intended to work in a similar way to
218/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
219///
220/// # Module macros
221///
222/// * [`assert_any`](macro@crate::assert_any)
223/// * [`assert_any`](macro@crate::assert_any)
224/// * [`debug_assert_any`](macro@crate::debug_assert_any)
225///
226#[macro_export]
227macro_rules! debug_assert_any {
228 ($($arg:tt)*) => {
229 if $crate::cfg!(debug_assertions) {
230 $crate::assert_any!($($arg)*);
231 }
232 };
233}