A Rust library that highlights differences between expected and actual values in test assertions.
- Colorized diff output (ANSI terminal support)
- Custom type support through traits
- Integration with common test frameworks
Add to your Cargo.toml:
assertion-diff-helper = "*"use assertion_diff_helper::prelude::*;
let expected = "hello world";
let actual = "hello word";
println!("{}", diff_strings(expected, actual));Expected: hello world
Actual: hello word
Output (simplified):
hello wor[l]d
^ expected 'l' not found in 'actual'
To highlight diffs in your own types, implement the Diffable trait:
use assertion_diff_helper::traits::Diffable;
use assertion_diff_helper::diff_helpers::DiffOutput;
struct MyType {
a: i32,
b: String,
}
impl Diffable for MyType {
fn diff(&self, other: &Self) -> DiffOutput {
let a_diff = self.a.diff(&other.a);
let b_diff = self.b.diff(&other.b);
DiffOutput::combine_fields(vec![
("a", a_diff),
("b", b_diff)
])
}
}You can then diff MyType instances just like built-in types.
Works with:
- std::assert_eq!/assert_ne!
pretty_assertions- Custom test macros
ANSI color output can be toggled globally:
assertion_diff_helper::color::set_color_enabled(false);See the tests/ folder for usage patterns and behavior.
MIT OR Apache-2.0