Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic with custom Debug impl returning an empty string #26

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/format_changeset.rs
Expand Up @@ -36,7 +36,7 @@ pub fn format_changeset(f: &mut fmt::Formatter, changeset: &Changeset) -> fmt::R
}
}
Difference::Add(ref added) => {
match diffs.get(i - 1) {
match diffs.get(i.wrapping_sub(1)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to rely on the last index to be None, but instead handle i=0 explicitly, e.g.

let prev = i.checked_sub(1).and_then(|x| diffs.get(x));
match prev {

Some(&Difference::Rem(ref removed)) => {
// The addition is preceded by an removal.
//
Expand Down
28 changes: 28 additions & 0 deletions tests/pretty_string.rs
@@ -0,0 +1,28 @@
#[macro_use]
extern crate pretty_assertions;

use std::fmt;
/// Wrapper around string slice that makes debug output `{:?}` to print string same way as `{}`.
/// Used in different `assert*!` macros in combination with `pretty_assertions` crate to make
/// test failures to show nice diffs.
#[derive(PartialEq, Eq)]
#[doc(hidden)]
pub struct PrettyString<'a>(pub &'a str);

/// Make diff to display string as multi-line string
impl<'a> fmt::Debug for PrettyString<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.0)
}
}

#[test]
#[should_panic(expected = r#"assertion failed: `(left == right)`

Diff < left / right > :
>foo

"#)]
fn assert_eq_empty_first() {
assert_eq!(PrettyString(""), PrettyString("foo"));
}