Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix panic with custom Debug impl returning an empty string
The panic was 'attempt to subtract with overflow' because the first difference is addition. PrettyString is from idubrov: #24 (comment)
- Loading branch information
1 parent
965f11b
commit 4edb2ae
Showing
2 changed files
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)` | ||
[1mDiff[0m [31m< left[0m / [32mright >[0m : | ||
[32m>foo | ||
[0m | ||
"#)] | ||
fn assert_eq_empty_first() { | ||
assert_eq!(PrettyString(""), PrettyString("foo")); | ||
} |