#assertions #testing #assert #diff

no-std assert_unordered

A direct replacement for assert_eq for unordered collections

10 releases

0.3.5 Apr 17, 2022
0.3.4 Apr 17, 2022
0.2.1 Apr 16, 2022
0.1.1 Apr 16, 2022

#1169 in Rust patterns

Download history 7870/week @ 2024-07-21 7983/week @ 2024-07-28 9195/week @ 2024-08-04 8880/week @ 2024-08-11 9578/week @ 2024-08-18 8139/week @ 2024-08-25 7429/week @ 2024-09-01 9907/week @ 2024-09-08 8951/week @ 2024-09-15 9456/week @ 2024-09-22 10434/week @ 2024-09-29 10615/week @ 2024-10-06 9576/week @ 2024-10-13 9415/week @ 2024-10-20 7986/week @ 2024-10-27 8210/week @ 2024-11-03

35,822 downloads per month
Used in 68 crates (7 directly)

MIT/Apache

37KB
275 lines

assert_unordered

Crate Docs

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

Usage

NOTE: no-default-features can be used to disable color output (and enable no-std support)

[dev-dependencies]
assert_unordered = "0.3"

Which Macro?

TLDR; - favor assert_eq_unordered_sort unless the trait requirements can't be met

  • assert_eq_unordered
    • Requires only Debug and PartialEq on the elements
    • Collection level equality check, and if unequal, falls back to item by item compare (O(n^2))
  • assert_eq_unordered_sort
    • Requires 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))

Example

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:

example_error

License

This project is licensed optionally under either:

Dependencies