#testing-debugging #sorting #diff #pretty #color #assert #hash-map

dev pretty_assertions_sorted

Wrapper around the rust-pretty-assertions crate that allows for the ability to sort the Debug output

5 stable releases

1.2.3 Jun 16, 2023
1.2.2 Jun 15, 2023
1.2.1 Sep 7, 2022
1.2.0 May 19, 2022
1.1.2 Dec 8, 2021

#190 in Development tools

Download history 2455/week @ 2024-07-22 2983/week @ 2024-07-29 3613/week @ 2024-08-05 3628/week @ 2024-08-12 3402/week @ 2024-08-19 4000/week @ 2024-08-26 3864/week @ 2024-09-02 4078/week @ 2024-09-09 3660/week @ 2024-09-16 3673/week @ 2024-09-23 3516/week @ 2024-09-30 2988/week @ 2024-10-07 3604/week @ 2024-10-14 3780/week @ 2024-10-21 3441/week @ 2024-10-28 3498/week @ 2024-11-04

14,593 downloads per month
Used in 10 crates

MIT/Apache

160KB
386 lines

Pretty Assertions (Sorted)

This crate wraps the pretty_assertions crate, which highlights differences in a test failure via a colorful diff.

However, the diff is based on the Debug output of the objects. For objects that have non-deterministic output, eg. two HashMaps with close to the same contents, the diff will be polluted and obscured with with false-positive differences like here:

standard assertion

This is much easier to understand when the diff is sorted:

sorted assertion

This is a pretty trivial example, you could solve this instead by converting the HashMap to a BTreeMap in your tests. But it's not always feasible to replace the types with ordered versions, especially for HashMaps that are deeply nested in types outside of your control.

To use the sorted version, import like this:

use pretty_assertions_sorted::{assert_eq, assert_eq_sorted};

assert_eq is provided as a re-export of pretty_assertions::assert_eq and should be used if you don't want the Debug output to be sorted, or if the Debug output can't be sorted (not supported types, eg. f64::NEG_INFINITY, or custom Debug output).

Tip

Specify it as [dev-dependencies] and it will only be used for compiling tests, examples, and benchmarks. This way the compile time of cargo build won't be affected!

License: MIT/Apache-2.0

Dependencies

~2MB
~32K SLoC