assertables/assert_set/mod.rs
1//! Assert for comparing set collections.
2//!
3//! These macros help with comparison of set parameters, such as two arrays or
4//! two vectors. where the item order does not matter, and the item count does
5//! not matter. These macros convert their inputs into HashSet iterators.
6//! See tutorial below.
7//!
8//! For eq & ne:
9//!
10//! * [`assert_set_eq!(collection1, collection2)`](macro@crate::assert_set_eq) ≈ set a = set b
11//!
12//! * [`assert_set_ne!(collection1, collection2)`](macro@crate::assert_set_ne) ≈ set a ≠ set b
13//!
14//! For subset & superset:
15//!
16//! * [`assert_set_subset!(collection1, collection2)`](macro@crate::assert_set_subset) ≈ set a ⊆ set b
17//!
18//! * [`assert_set_superset!(collection1, collection2)`](macro@crate::assert_set_superset) ≈ set a ⊇ set b
19//!
20//! For joint & disjoint:
21//!
22//! * [`assert_set_joint!(collection1, collection2)`](macro@crate::assert_set_joint) ≈ set a ∩ set b ≠ ∅
23//!
24//! * [`assert_set_disjoint!(collection1, collection2)`](macro@crate::assert_set_disjoint) ≈ set a ∩ set b = ∅
25//!
26//! # Example
27//!
28//! ```rust
29//! use assertables::*;
30//!
31//! let a = [1, 2];
32//! let b = [2, 1];
33//! assert_set_eq!(&a, &b);
34//! ```
35//!
36//! ## Tutorial
37//!
38//! A **set** means a collection of elements, without any ordering, without duplicate elements.
39//!
40//! A set is sometimes written by using mathematical notation, which looks like this:
41//!
42//! ```text
43//! set = {1, 2, 3}
44//! ```
45//!
46//! The definition of a set includes never having duplicate elements:
47//!
48//! ```text
49//! set = {1, 2, 3, 2} // error because the element 2 is a duplicate
50//! ```
51//!
52//! Sets are equal when they contain all the same elements in any order:
53//!
54//! ```text
55//! {1, 2, 3} = {1, 2, 3} (same order)
56//! {1, 2, 3} = {3, 2, 1} (different order)
57//! ```
58//!
59//! Sets are not equal when they contain any different elements:
60//!
61//! ```text
62//! {1, 2, 3} ≠ {1, 2}
63//! {1, 2, 3} ≠ {1, 2, 3, 4}
64//! ```
65//!
66//! To create a set using Rust, one way is to create an array or vector, then convert it into an iterator by using the method `into_iter`, then convert the elements into a set by using `std::collections::BTreeSet`:
67//!
68//! ```rust
69//! # use ::std::collections::BTreeSet;
70//! let array = [1, 2, 3];
71//! let set: BTreeSet<_> = array.into_iter().collect();
72//! ```
73//!
74//! To compare two arrays as sets, one way is to convert each array to a set, then use `assert_eq!` to compare the sets:
75//!
76//! ```rust
77//! # use ::std::collections::BTreeSet;
78//! let array1 = [1, 2, 3];
79//! let array2 = [3, 2, 1];
80//! let set1: BTreeSet<_> = array1.into_iter().collect();
81//! let set2: BTreeSet<_> = array2.into_iter().collect();
82//! assert_eq!(set1, set2);
83//! ```
84//!
85//! The `assertables` crate provides macros that do the conversion for you:
86//!
87//! ```rust
88//! # use ::std::collections::BTreeSet;
89//! # use assertables::*;
90//! let array1 = [1, 2, 3];
91//! let array2 = [3, 2, 1];
92//! assert_set_eq!(array1, array2);
93//! ```
94
95/// Assert set implementation preparation.
96#[macro_export]
97macro_rules! assert_set_impl_prep {
98 ($impl_into_iter:expr $(,)?) => {
99 match ($impl_into_iter) {
100 impl_into_iter => impl_into_iter.into_iter().collect(),
101 }
102 };
103}
104
105// Comparisons
106pub mod assert_set_eq;
107pub mod assert_set_ne;
108
109// Overlaps
110pub mod assert_set_disjoint;
111pub mod assert_set_joint;
112
113// Containers
114pub mod assert_set_subset;
115pub mod assert_set_superset;