Skip to content

Commit

Permalink
Only show diffs for multiline values
Browse files Browse the repository at this point in the history
  • Loading branch information
TehPers committed Aug 18, 2024
1 parent b58517c commit 4c72d58
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/assertions/general/assertions/to_equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ where
return cx.fail("values not equal");
};

// Perform the diff
if let Some(diff) = fmt_diff(&expected_repr, &subject_repr) {
cx.add_page("diff", diff);
// Skip the diff if the representations aren't multiline to avoid
// cluttering the output
if subject_repr.contains('\n') || expected_repr.contains('\n') {
// Perform the diff
if let Some(diff) = fmt_diff(&expected_repr, &subject_repr) {
cx.add_page("diff", diff);
}
}

cx.fail("values not equal")
Expand All @@ -60,3 +64,31 @@ where
cx.pass_if(subject == self.expected.into_inner(), "values not equal")
}
}

#[cfg(test)]
mod tests {
use crate::prelude::*;

#[test]
fn no_diff() {
// Don't show diffs for short values
expect!(
try_expect!(1, to_equal(2)),
to_be_err_and,
as_display,
not,
to_contain_substr("diff"),
);
}

#[test]
fn do_diff() {
// Show diffs for longer values
expect!(
try_expect!("abc\ndef", to_equal("abc\ndeg")),
to_be_err_and,
as_display,
to_contain_substr("diff"),
);
}
}

0 comments on commit 4c72d58

Please sign in to comment.