Skip to content

Commit

Permalink
Add more macro tests for assert_eq's handling of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tommilligan authored and dtolnay committed Mar 9, 2022
1 parent edc4533 commit ab783cc
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 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 @@ -174,6 +189,19 @@ mod assert_eq {
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 ab783cc

Please sign in to comment.