Skip to content

Commit

Permalink
Merge pull request #92 from dtolnay-contrib/str
Browse files Browse the repository at this point in the history
Automatically use StrComparison for comparing strings
  • Loading branch information
tommilligan committed Mar 11, 2022
2 parents a8b02f1 + ab783cc commit 8a223f3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 13 deletions.
38 changes: 37 additions & 1 deletion pretty_assertions/src/lib.rs
Expand Up @@ -242,13 +242,14 @@ macro_rules! assert_eq {
match (&($left), &($right)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
use $crate::private::CreateComparison;
::core::panic!("assertion failed: `(left == right)`{}{}\
\n\
\n{}\
\n",
$maybe_colon,
format_args!($($arg)*),
$crate::Comparison::new(left_val, right_val)
(left_val, right_val).create_comparison()
)
}
}
Expand Down Expand Up @@ -430,3 +431,38 @@ macro_rules! assert_matches {
}
});
}

// Not public API. Used by the expansion of this crate's assert macros.
#[doc(hidden)]
pub mod private {
#[cfg(feature = "alloc")]
use alloc::string::String;

pub trait CompareAsStrByDefault: AsRef<str> {}
impl CompareAsStrByDefault for str {}
impl CompareAsStrByDefault for String {}
impl<T: CompareAsStrByDefault + ?Sized> CompareAsStrByDefault for &T {}

pub trait CreateComparison {
type Comparison;
fn create_comparison(self) -> Self::Comparison;
}

impl<'a, T, U> CreateComparison for &'a (T, U) {
type Comparison = crate::Comparison<'a, T, U>;
fn create_comparison(self) -> Self::Comparison {
crate::Comparison::new(&self.0, &self.1)
}
}

impl<'a, T, U> CreateComparison for (&'a T, &'a U)
where
T: CompareAsStrByDefault + ?Sized,
U: CompareAsStrByDefault + ?Sized,
{
type Comparison = crate::StrComparison<'a, T, U>;
fn create_comparison(self) -> Self::Comparison {
crate::StrComparison::new(self.0, self.1)
}
}
}
65 changes: 53 additions & 12 deletions pretty_assertions/tests/macros.rs
Expand Up @@ -32,23 +32,23 @@ mod assert_str_eq {
::pretty_assertions::assert_str_eq!(s0, s1);
}

#[test]
fn passes_as_ref_types() {
#[derive(PartialEq)]
struct MyString(String);
#[derive(PartialEq)]
struct MyString(String);

impl AsRef<str> for MyString {
fn as_ref(&self) -> &str {
&self.0
}
impl AsRef<str> for MyString {
fn as_ref(&self) -> &str {
&self.0
}
}

impl PartialEq<String> for MyString {
fn eq(&self, other: &String) -> bool {
&self.0 == other
}
impl PartialEq<String> for MyString {
fn eq(&self, other: &String) -> bool {
&self.0 == other
}
}

#[test]
fn passes_as_ref_types() {
let s0 = MyString("foo".to_string());
let s1 = "foo".to_string();
::pretty_assertions::assert_str_eq!(s0, s1);
Expand All @@ -62,6 +62,21 @@ mod assert_str_eq {
<bar
>baz
"#)]
fn fails_as_ref_types() {
let s0 = MyString("foo\nbar".to_string());
let s1 = "foo\nbaz".to_string();
::pretty_assertions::assert_str_eq!(s0, s1);
}

#[test]
#[should_panic(expected = r#"assertion failed: `(left == right)`
Diff < left / right > :
foo
<bar
>baz
"#)]
fn fails_foo() {
::pretty_assertions::assert_str_eq!("foo\nbar", "foo\nbaz");
Expand Down Expand Up @@ -161,6 +176,32 @@ mod assert_eq {
fn fails_custom_trailing_comma() {
::pretty_assertions::assert_eq!(666, 999, "custom panic message",);
}

#[test]
#[should_panic(expected = r#"assertion failed: `(left == right)`
Diff < left / right > :
foo
<bar
>baz
"#)]
fn fails_str() {
::pretty_assertions::assert_eq!("foo\nbar", "foo\nbaz");
}

#[test]
#[should_panic(expected = r#"assertion failed: `(left == right)`
Diff < left / right > :
foo
<bar
>baz
"#)]
fn fails_string() {
::pretty_assertions::assert_eq!("foo\nbar".to_string(), "foo\nbaz".to_string());
}
}

mod assert_ne {
Expand Down

0 comments on commit 8a223f3

Please sign in to comment.