pub fn eq_diff<'a, Actual, Expected>(
expected: Expected
) -> Matcher<'a, Actual, Actual>where
Actual: Debug + PartialEq<Expected> + Eq + 'a,
Expected: Debug + Diffable<Actual> + 'a,
diff
only.Expand description
Succeeds when the actual value equals the expected value and shows a diff otherwise.
This matcher is functionally identical to equal
, except it shows a diff if the values are
not equal. You can use this matcher with any type that implements Diffable
, and you can
implement Diffable
for your own types.
Out of the box, you can diff strings, slices, sets, and maps.
Examples
use xpct::{expect, eq_diff};
expect!("diffing strings").to(eq_diff("diffing strings"));
use xpct::{expect, eq_diff};
expect!(["slices", "too"]).to(eq_diff(["slices", "too"]));
Custom Styling
This matcher relies on text styling to distinguish added and removed values in string diffs. If the provided text styling for diffs is inaccessible for you, or you prefer to have text styling disabled, you can override the provided styling.
To do this, write a custom style sheet using DiffStyle
and write your own matcher function
that calls EqDiffMatcher
.
This example makes string diffs readable without text styling:
use std::fmt;
use xpct::core::Matcher;
use xpct::format::diff::{DiffFormat, DiffSegmentStyle, DiffStyle};
use xpct::matchers::diff::{Diffable, EqDiffMatcher};
pub fn eq_diff<'a, Actual, Expected>(expected: Expected) -> Matcher<'a, Actual, Actual>
where
Actual: fmt::Debug + PartialEq<Expected> + Eq + 'a,
Expected: Diffable<Actual> + fmt::Debug + 'a,
{
let mut custom_style = DiffStyle::provided();
custom_style.string.format = DiffSegmentStyle {
insert: String::from("+(%s)"),
delete: String::from("-(%s)"),
equal: String::from("%s"),
};
Matcher::new(
EqDiffMatcher::new(expected),
DiffFormat::<Actual, Expected>::new(custom_style),
)
}