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