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

Support unsized values in assertions #43

Merged
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Unreleased

## Added

- Support for unsized values (#42, @stanislav-tkach)

## Internal

- Test Windows targets in CI (#46, @tommilligan)
13 changes: 8 additions & 5 deletions src/lib.rs
Expand Up @@ -93,9 +93,12 @@ fn init() {
pub struct Comparison(Changeset);

impl Comparison {
pub fn new<TLeft: Debug, TRight: Debug>(left: &TLeft, right: &TRight) -> Comparison {
let left_dbg = format!("{:#?}", *left);
let right_dbg = format!("{:#?}", *right);
pub fn new<TLeft: Debug + ?Sized, TRight: Debug + ?Sized>(
left: &TLeft,
right: &TRight,
) -> Comparison {
let left_dbg = format!("{:#?}", &*left);
let right_dbg = format!("{:#?}", &*right);
let changeset = Changeset::new(&left_dbg, &right_dbg, "\n");

Comparison(changeset)
Expand Down Expand Up @@ -157,8 +160,8 @@ macro_rules! assert_ne {
match (&($left), &($right)) {
(left_val, right_val) => {
if *left_val == *right_val {
let left_dbg = format!("{:?}", *left_val);
let right_dbg = format!("{:?}", *right_val);
let left_dbg = format!("{:?}", &*left_val);
let right_dbg = format!("{:?}", &*right_val);
if left_dbg != right_dbg {

panic!("assertion failed: `(left != right)`{}{}\
Expand Down
24 changes: 24 additions & 0 deletions tests/assert_eq.rs
@@ -1,3 +1,5 @@
#![allow(clippy::eq_op)]

#[allow(unused_imports)]
use pretty_assertions::{assert_eq, assert_ne};
extern crate difference;
Expand Down Expand Up @@ -193,3 +195,25 @@ fn assert_eq_custom_trailing_comma() {

assert_eq!(x, y, "custom panic message",);
}

#[test]
fn assert_eq_unsized() {
let a: &[u8] = b"e";
assert_eq!(*a, *a);
}

#[test]
#[should_panic(expected = r#"assertion failed: `(left == right)`
Diff < left / right > :
[
101,
> 101,
 ]
"#)]
fn assert_eq_unsized_panic() {
let a: &[u8] = b"e";
let b: &[u8] = b"ee";
assert_eq!(*a, *b);
}
21 changes: 21 additions & 0 deletions tests/assert_ne.rs
Expand Up @@ -174,3 +174,24 @@ fn assert_ne_custom_trailing_comma() {

assert_ne!(x, x, "custom panic message",);
}

#[test]
fn assert_ne_unsized() {
let a: &[u8] = b"e";
let b: &[u8] = b"ee";
assert_ne!(*a, *b);
}

#[test]
#[should_panic(expected = r#"assertion failed: `(left != right)`
Both sides:
[
101,
]
"#)]
fn assert_ne_unsized_panic() {
let a: &[u8] = b"e";
assert_ne!(*a, *a);
}