assertables/assert_iter/
assert_iter_ne.rs

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