assertables/assert_bag/assert_bag_eq.rs
1//! Assert a bag is equal to another.
2//!
3//! Pseudocode:<br>
4//! (a_collection ⇒ a_bag) = (b_collection ⇒ b_bag)
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a = [1, 1];
12//! let b = [1, 1];
13//! assert_bag_eq!(a, b);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_bag_eq`](macro@crate::assert_bag_eq)
19//! * [`assert_bag_eq_as_result`](macro@crate::assert_bag_eq_as_result)
20//! * [`debug_assert_bag_eq`](macro@crate::debug_assert_bag_eq)
21
22/// Assert a bag is equal to another.
23///
24/// Pseudocode:<br>
25/// (a_collection ⇒ a_bag) = (b_collection ⇒ b_bag)
26///
27/// * If true, return Result `Ok((a, 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_bag_eq!`](macro@crate::assert_bag_eq)
37/// * [`assert_bag_eq_as_result`](macro@crate::assert_bag_eq_as_result)
38/// * [`debug_assert_bag_eq`](macro@crate::debug_assert_bag_eq)
39///
40#[macro_export]
41macro_rules! assert_bag_eq_as_result {
42 ($a_collection:expr, $b_collection:expr $(,)?) => {
43 match (&$a_collection, &$b_collection) {
44 (a_collection, b_collection) => {
45 let a = assert_bag_impl_prep!(a_collection);
46 let b = assert_bag_impl_prep!(b_collection);
47 if a == b {
48 Ok((a, b))
49 } else {
50 Err(format!(
51 concat!(
52 "assertion failed: `assert_bag_eq!(a_collection, b_collection)`\n",
53 "https://docs.rs/assertables/9.8.1/assertables/macro.assert_bag_eq.html\n",
54 " a label: `{}`,\n",
55 " a debug: `{:?}`,\n",
56 " b label: `{}`,\n",
57 " b debug: `{:?}`,\n",
58 " a bag: `{:?}`,\n",
59 " b bag: `{:?}`"
60 ),
61 stringify!($a_collection),
62 a_collection,
63 stringify!($b_collection),
64 b_collection,
65 a,
66 b
67 ))
68 }
69 }
70 }
71 };
72}
73
74#[cfg(test)]
75mod test_assert_bag_eq_as_result {
76 use std::collections::BTreeMap;
77 // use std::sync::Once;
78
79 #[test]
80 fn eq() {
81 let a = [1, 1];
82 let b = [1, 1];
83 for _ in 0..1 {
84 let actual = assert_bag_eq_as_result!(a, b);
85 assert_eq!(
86 actual.unwrap(),
87 (BTreeMap::from([(&1, 2)]), BTreeMap::from([(&1, 2)]))
88 );
89 }
90 }
91
92 //TODO
93 // #[test]
94 // fn eq_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, 1]
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 // [1, 1]
113 // }
114
115 // assert_eq!(A.is_completed(), false);
116 // assert_eq!(B.is_completed(), false);
117 // let result = assert_bag_eq_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 ne() {
125 let a = [1, 1];
126 let b = [1, 1, 1];
127 let actual = assert_bag_eq_as_result!(a, b);
128 let message = concat!(
129 "assertion failed: `assert_bag_eq!(a_collection, b_collection)`\n",
130 "https://docs.rs/assertables/9.8.1/assertables/macro.assert_bag_eq.html\n",
131 " a label: `a`,\n",
132 " a debug: `[1, 1]`,\n",
133 " b label: `b`,\n",
134 " b debug: `[1, 1, 1]`,\n",
135 " a bag: `{1: 2}`,\n",
136 " b bag: `{1: 3}`"
137 );
138 assert_eq!(actual.unwrap_err(), message);
139 }
140}
141
142/// Assert a bag is equal to another.
143///
144/// Pseudocode:<br>
145/// (a_collection ⇒ a_bag) = (b_collection ⇒ b_bag)
146///
147/// * If true, return `(a, b)`.
148///
149/// * Otherwise, call [`panic!`] in order to print 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, 1];
160/// let b = [1, 1];
161/// assert_bag_eq!(a, b);
162///
163/// # let result = panic::catch_unwind(|| {
164/// // This will panic
165/// let a = [1, 1];
166/// let b = [1, 1, 1];
167/// assert_bag_eq!(a, b);
168/// # });
169/// // assertion failed: `assert_bag_eq!(a_collection, b_collection)`
170/// // https://docs.rs/assertables/…/assertables/macro.assert_bag_eq.html
171/// // a label: `a`,
172/// // a debug: `[1, 1]`,
173/// // b label: `b`,
174/// // b debug: `[1, 1, 1]`,
175/// // a bag: `{1: 2}`,
176/// // b bag: `{1: 3}`
177/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
178/// # let message = concat!(
179/// # "assertion failed: `assert_bag_eq!(a_collection, b_collection)`\n",
180/// # "https://docs.rs/assertables/9.8.1/assertables/macro.assert_bag_eq.html\n",
181/// # " a label: `a`,\n",
182/// # " a debug: `[1, 1]`,\n",
183/// # " b label: `b`,\n",
184/// # " b debug: `[1, 1, 1]`,\n",
185/// # " a bag: `{1: 2}`,\n",
186/// # " b bag: `{1: 3}`"
187/// # );
188/// # assert_eq!(actual, message);
189/// # }
190/// ```
191///
192/// This implementation uses [`::std::collections::BTreeMap`](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html) to count items and sort them.
193///
194/// # Module macros
195///
196/// * [`assert_bag_eq`](macro@crate::assert_bag_eq)
197/// * [`assert_bag_eq_as_result`](macro@crate::assert_bag_eq_as_result)
198/// * [`debug_assert_bag_eq`](macro@crate::debug_assert_bag_eq)
199///
200#[macro_export]
201macro_rules! assert_bag_eq {
202 ($a_collection:expr, $b_collection:expr $(,)?) => {
203 match $crate::assert_bag_eq_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_bag_eq_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_bag_eq {
218 use std::collections::BTreeMap;
219 use std::panic;
220
221 #[test]
222 fn eq() {
223 let a = [1, 1];
224 let b = [1, 1];
225 for _ in 0..1 {
226 let actual = assert_bag_eq!(a, b);
227 assert_eq!(
228 actual,
229 (BTreeMap::from([(&1, 2)]), BTreeMap::from([(&1, 2)]))
230 );
231 }
232 }
233
234 #[test]
235 fn ne() {
236 let result = panic::catch_unwind(|| {
237 let a = [1, 1];
238 let b = [1, 1, 1];
239 let _actual = assert_bag_eq!(a, b);
240 });
241 let message = concat!(
242 "assertion failed: `assert_bag_eq!(a_collection, b_collection)`\n",
243 "https://docs.rs/assertables/9.8.1/assertables/macro.assert_bag_eq.html\n",
244 " a label: `a`,\n",
245 " a debug: `[1, 1]`,\n",
246 " b label: `b`,\n",
247 " b debug: `[1, 1, 1]`,\n",
248 " a bag: `{1: 2}`,\n",
249 " b bag: `{1: 3}`"
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 bag is equal to another.
263///
264/// Pseudocode:<br>
265/// (a_collection ⇒ a_bag) = (b_collection ⇒ b_bag)
266///
267/// This macro provides the same statements as [`assert_bag_eq`](macro.assert_bag_eq.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_bag_eq`](macro@crate::assert_bag_eq)
290/// * [`assert_bag_eq_as_result`](macro@crate::assert_bag_eq_as_result)
291/// * [`debug_assert_bag_eq`](macro@crate::debug_assert_bag_eq)
292///
293#[macro_export]
294macro_rules! debug_assert_bag_eq {
295 ($($arg:tt)*) => {
296 if $crate::cfg!(debug_assertions) {
297 $crate::assert_bag_eq!($($arg)*);
298 }
299 };
300}