Crates.io | assert_unordered |
lib.rs | assert_unordered |
version | 0.3.5 |
source | src |
created_at | 2022-04-16 13:26:54.175398 |
updated_at | 2022-04-17 21:13:19.549468 |
description | A direct replacement for `assert_eq` for unordered collections |
homepage | |
repository | https://github.com/nu11ptr/assert_unordered |
max_upload_size | |
id | 568965 |
size | 58,591 |
A direct replacement for assert_eq
for unordered collections
This macro is useful for any situation where the ordering of the collection doesn't matter, even
if they are always in the same order. This is because the stdlib assert_eq
shows the entire
collection for both left and right and leaves it up to the user to visually scan for differences.
In contrast, this crate only works with collections (types that implement IntoIterator
) and
therefore can show only the differences (see below for an example of what the output looks like).
NOTE: As of 0.3.2, the output by default is in color similar to pretty_assertions
NOTE: no-default-features
can be used to disable color output (and enable no-std
support)
[dev-dependencies]
assert_unordered = "0.3"
TLDR; - favor assert_eq_unordered_sort
unless the trait requirements can't be met
Debug
and PartialEq
on the elementsRequires Debug
, Eq
and Ord
on the elements
Collection level equality check, and if unequal, sorts and then compares again, and if still unequal, falls back to item by item compare (O(n^2))
use assert_unordered::assert_eq_unordered;
#[derive(Debug, PartialEq)]
struct MyType(i32);
fn main() {
let expected = vec![MyType(1), MyType(2), MyType(4), MyType(5)];
let actual = vec![MyType(2), MyType(0), MyType(4)];
assert_eq_unordered!(expected, actual);
}
Output:
This project is licensed optionally under either: