assertables/assert_set/
assert_set_ne.rs

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