Crate pretty_assertions_sorted_fork

Source
Expand description

§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_fork::{assert_eq, assert_eq_all_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!

Macros§

assert_eq
Asserts that two expressions are equal to each other (using PartialEq).
assert_eq_all_sorted
This is a wrapper with similar functionality to assert_eq, however, the Debug representation is sorted to provide deterministic output.
assert_eq_sorted
This is a wrapper with similar functionality to assert_eq, however, the Debug representation is sorted to provide deterministic output.
assert_ne
Asserts that two expressions are not equal to each other (using PartialEq).

Structs§

AllSortedDebug
New-type wrapper around an object that sorts the fmt::Debug output when displayed for deterministic output.
Comparison
A comparison of two values.
SortedDebug
New-type wrapper around an object that sorts the fmt::Debug output when displayed for deterministic output.