Skip to content

Commit

Permalink
Test and fix macro hygiene
Browse files Browse the repository at this point in the history
  • Loading branch information
tommilligan committed Feb 10, 2021
1 parent 9d657c3 commit de7b965
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@

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

## Fixed

- Fix some unhygenic macro expansions (#41, @tommilligan)

## Internal

- Test Windows targets in CI (#46, @tommilligan)
3 changes: 1 addition & 2 deletions examples/pretty_assertion.rs
@@ -1,4 +1,3 @@
#[allow(unused_imports)]
use pretty_assertions::{assert_eq, assert_ne};
use pretty_assertions::assert_eq;

include!("standard_assertion.rs");
8 changes: 4 additions & 4 deletions src/lib.rs
Expand Up @@ -114,7 +114,7 @@ impl Display for Comparison {
#[macro_export]
macro_rules! assert_eq {
($left:expr , $right:expr,) => ({
assert_eq!($left, $right)
$crate::assert_eq!($left, $right)
});
($left:expr , $right:expr) => ({
match (&($left), &($right)) {
Expand Down Expand Up @@ -148,13 +148,13 @@ macro_rules! assert_eq {
#[macro_export]
macro_rules! assert_ne {
($left:expr, $right:expr) => ({
assert_ne!(@ $left, $right, "", "");
$crate::assert_ne!(@ $left, $right, "", "");
});
($left:expr, $right:expr,) => ({
assert_ne!(@ $left, $right, "", "");
$crate::assert_ne!(@ $left, $right, "", "");
});
($left:expr, $right:expr, $($arg:tt)+) => ({
assert_ne!(@ $left, $right, ": ", $($arg)+);
$crate::assert_ne!(@ $left, $right, ": ", $($arg)+);
});
(@ $left:expr, $right:expr, $maybe_semicolon:expr, $($arg:tt)+) => ({
match (&($left), &($right)) {
Expand Down
20 changes: 8 additions & 12 deletions tests/assert_eq.rs
@@ -1,9 +1,5 @@
#![allow(clippy::eq_op)]

#[allow(unused_imports)]
use pretty_assertions::{assert_eq, assert_ne};
extern crate difference;

#[test]
#[should_panic(expected = r#"assertion failed: `(left == right)`
Expand Down Expand Up @@ -40,7 +36,7 @@ fn assert_eq() {
dolor: Ok("hey ho!".to_string()),
});

assert_eq!(x, y);
pretty_assertions::assert_eq!(x, y);
}

#[test]
Expand Down Expand Up @@ -81,14 +77,14 @@ fn assert_eq_custom() {
dolor: Ok("hey ho!".to_string()),
});

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

#[test]
fn assert_eq_with_comparable_types() {
let s0: &'static str = "foo";
let s1: String = "foo".to_string();
assert_eq!(s0, s1);
pretty_assertions::assert_eq!(s0, s1);
}

#[test]
Expand All @@ -113,7 +109,7 @@ fn assert_eq_with_comparable_types() {
fn issue12() {
let left = vec![0, 0, 0, 128, 10, 191, 5, 64];
let right = vec![84, 248, 45, 64];
assert_eq!(left, right);
pretty_assertions::assert_eq!(left, right);
}

#[test]
Expand Down Expand Up @@ -152,7 +148,7 @@ fn assert_eq_trailing_comma() {
dolor: Ok("hey ho!".to_string()),
});

assert_eq!(x, y,);
pretty_assertions::assert_eq!(x, y,);
}

#[test]
Expand Down Expand Up @@ -193,13 +189,13 @@ fn assert_eq_custom_trailing_comma() {
dolor: Ok("hey ho!".to_string()),
});

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

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

#[test]
Expand All @@ -215,5 +211,5 @@ fn assert_eq_unsized() {
fn assert_eq_unsized_panic() {
let a: &[u8] = b"e";
let b: &[u8] = b"ee";
assert_eq!(*a, *b);
pretty_assertions::assert_eq!(*a, *b);
}
18 changes: 8 additions & 10 deletions tests/assert_ne.rs
@@ -1,7 +1,5 @@
#![allow(clippy::eq_op)]

#[allow(unused_imports)]
use pretty_assertions::{assert_eq, assert_ne};
#[test]
#[should_panic(expected = r#"assertion failed: `(left != right)`
Expand Down Expand Up @@ -31,7 +29,7 @@ fn assert_ne() {
dolor: Ok("hey".to_string()),
});

assert_ne!(x, x);
pretty_assertions::assert_ne!(x, x);
}

#[test]
Expand Down Expand Up @@ -65,14 +63,14 @@ fn assert_ne_custom() {
dolor: Ok("hey".to_string()),
});

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

#[test]
#[should_panic]
fn assert_ne_non_empty_return() {
fn not_zero(x: u32) -> u32 {
assert_ne!(x, 0);
pretty_assertions::assert_ne!(x, 0);
x
};
not_zero(0);
Expand Down Expand Up @@ -106,7 +104,7 @@ fn assert_ne_partial() {
}
}

assert_ne!(Foo(-0.0), Foo(0.0));
pretty_assertions::assert_ne!(Foo(-0.0), Foo(0.0));
}

#[test]
Expand Down Expand Up @@ -138,7 +136,7 @@ fn assert_ne_trailing_comma() {
dolor: Ok("hey".to_string()),
});

assert_ne!(x, x,);
pretty_assertions::assert_ne!(x, x,);
}

#[test]
Expand Down Expand Up @@ -172,14 +170,14 @@ fn assert_ne_custom_trailing_comma() {
dolor: Ok("hey".to_string()),
});

assert_ne!(x, x, "custom panic message",);
pretty_assertions::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);
pretty_assertions::assert_ne!(*a, *b);
}

#[test]
Expand All @@ -193,5 +191,5 @@ fn assert_ne_unsized() {
"#)]
fn assert_ne_unsized_panic() {
let a: &[u8] = b"e";
assert_ne!(*a, *a);
pretty_assertions::assert_ne!(*a, *a);
}
5 changes: 2 additions & 3 deletions tests/pretty_string.rs
@@ -1,6 +1,5 @@
#[allow(unused_imports)]
use pretty_assertions::{assert_eq, assert_ne};
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.
Expand All @@ -23,5 +22,5 @@ impl<'a> fmt::Debug for PrettyString<'a> {

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

0 comments on commit de7b965

Please sign in to comment.