From c45600cf06af5735b7d7e2315d6e770ca2b770e8 Mon Sep 17 00:00:00 2001 From: Tom Milligan Date: Wed, 5 May 2021 16:22:27 +0100 Subject: [PATCH] chore: make macro tests external --- pretty_assertions/src/lib.rs | 225 ------------------------------ pretty_assertions/tests/macros.rs | 222 +++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+), 225 deletions(-) create mode 100644 pretty_assertions/tests/macros.rs diff --git a/pretty_assertions/src/lib.rs b/pretty_assertions/src/lib.rs index ea91552..caf7be9 100644 --- a/pretty_assertions/src/lib.rs +++ b/pretty_assertions/src/lib.rs @@ -238,228 +238,3 @@ macro_rules! assert_ne { } }); } - -#[cfg(test)] -#[allow(clippy::eq_op)] -#[no_implicit_prelude] -mod test { - mod assert_eq { - use ::std::string::{String, ToString}; - - #[test] - fn passes() { - let a = "some value"; - crate::assert_eq!(a, a); - } - - #[test] - fn passes_unsized() { - let a: &[u8] = b"e"; - crate::assert_eq!(*a, *a); - } - - #[test] - fn passes_comparable_types() { - let s0: &'static str = "foo"; - let s1: String = "foo".to_string(); - crate::assert_eq!(s0, s1); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left == right)` - -Diff < left / right > : -<666 ->999 - -"#)] - fn fails() { - crate::assert_eq!(666, 999); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left == right)` - -Diff < left / right > : -<666 ->999 - -"#)] - fn fails_trailing_comma() { - crate::assert_eq!(666, 999,); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left == right)` - -Diff < left / right > : - [ - 101, -> 101, - ] - -"#)] - fn fails_unsized() { - let a: &[u8] = b"e"; - let b: &[u8] = b"ee"; - crate::assert_eq!(*a, *b); - } - - #[test] - #[should_panic( - expected = r#"assertion failed: `(left == right)`: custom panic message - -Diff < left / right > : -<666 ->999 - -"# - )] - fn fails_custom() { - crate::assert_eq!(666, 999, "custom panic message"); - } - - #[test] - #[should_panic( - expected = r#"assertion failed: `(left == right)`: custom panic message - -Diff < left / right > : -<666 ->999 - -"# - )] - fn fails_custom_trailing_comma() { - crate::assert_eq!(666, 999, "custom panic message",); - } - } - - mod assert_ne { - use ::std::string::{String, ToString}; - - #[test] - fn passes() { - let a = "a"; - let b = "b"; - crate::assert_ne!(a, b); - } - - #[test] - fn passes_unsized() { - let a: &[u8] = b"e"; - let b: &[u8] = b"ee"; - crate::assert_ne!(*a, *b); - } - - #[test] - fn passes_comparable_types() { - let s0: &'static str = "foo"; - let s1: String = "bar".to_string(); - crate::assert_ne!(s0, s1); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left != right)` - -Both sides: -666 -"#)] - fn fails() { - crate::assert_ne!(666, 666); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left != right)` - -Both sides: -666 -"#)] - fn fails_trailing_comma() { - crate::assert_ne!(666, 666,); - } - - #[test] - #[should_panic(expected = r#"assertion failed: `(left != right)` - -Both sides: -[ - 101, -] - -"#)] - fn fails_unsized() { - let a: &[u8] = b"e"; - crate::assert_ne!(*a, *a); - } - - #[test] - #[should_panic( - expected = r#"assertion failed: `(left != right)`: custom panic message - -Both sides: -666 -"# - )] - fn fails_custom() { - crate::assert_ne!(666, 666, "custom panic message"); - } - - #[test] - #[should_panic( - expected = r#"assertion failed: `(left != right)`: custom panic message - -Both sides: -666 -"# - )] - fn fails_custom_trailing_comma() { - crate::assert_ne!(666, 666, "custom panic message",); - } - - // If the values are equal but their debug outputs are not - // show a specific warning - - #[test] - #[should_panic(expected = r#"assertion failed: `(left != right)` - -Diff < left / right > : -<-0.0 ->0.0 - -Note: According to the `PartialEq` implementation, both of the values are partially equivalent, even if the `Debug` outputs differ. - -"#)] - fn assert_ne_partial() { - // Workaround for https://github.com/rust-lang/rust/issues/47619 - // can be removed, when we require rust 1.25 or higher - struct Foo(f32); - - use ::std::fmt; - impl fmt::Debug for Foo { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - ::std::write!(f, "{:.1?}", self.0) - } - } - - impl ::std::cmp::PartialEq for Foo { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } - } - - crate::assert_ne!(Foo(-0.0), Foo(0.0)); - } - - // Regression tests - - #[test] - #[should_panic] - fn assert_ne_non_empty_return() { - fn not_zero(x: u32) -> u32 { - crate::assert_ne!(x, 0); - x - } - not_zero(0); - } - } -} diff --git a/pretty_assertions/tests/macros.rs b/pretty_assertions/tests/macros.rs new file mode 100644 index 0000000..28fcfaf --- /dev/null +++ b/pretty_assertions/tests/macros.rs @@ -0,0 +1,222 @@ +#![no_implicit_prelude] + +#[allow(clippy::eq_op)] +mod assert_eq { + use ::std::string::{String, ToString}; + + #[test] + fn passes() { + let a = "some value"; + ::pretty_assertions::assert_eq!(a, a); + } + + #[test] + fn passes_unsized() { + let a: &[u8] = b"e"; + ::pretty_assertions::assert_eq!(*a, *a); + } + + #[test] + fn passes_comparable_types() { + let s0: &'static str = "foo"; + let s1: String = "foo".to_string(); + ::pretty_assertions::assert_eq!(s0, s1); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +Diff < left / right > : +<666 +>999 + +"#)] + fn fails() { + ::pretty_assertions::assert_eq!(666, 999); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +Diff < left / right > : +<666 +>999 + +"#)] + fn fails_trailing_comma() { + ::pretty_assertions::assert_eq!(666, 999,); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +Diff < left / right > : + [ + 101, +> 101, + ] + +"#)] + fn fails_unsized() { + let a: &[u8] = b"e"; + let b: &[u8] = b"ee"; + ::pretty_assertions::assert_eq!(*a, *b); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left == right)`: custom panic message + +Diff < left / right > : +<666 +>999 + +"# + )] + fn fails_custom() { + ::pretty_assertions::assert_eq!(666, 999, "custom panic message"); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left == right)`: custom panic message + +Diff < left / right > : +<666 +>999 + +"# + )] + fn fails_custom_trailing_comma() { + ::pretty_assertions::assert_eq!(666, 999, "custom panic message",); + } +} + +mod assert_ne { + use ::std::string::{String, ToString}; + + #[test] + fn passes() { + let a = "a"; + let b = "b"; + ::pretty_assertions::assert_ne!(a, b); + } + + #[test] + fn passes_unsized() { + let a: &[u8] = b"e"; + let b: &[u8] = b"ee"; + ::pretty_assertions::assert_ne!(*a, *b); + } + + #[test] + fn passes_comparable_types() { + let s0: &'static str = "foo"; + let s1: String = "bar".to_string(); + ::pretty_assertions::assert_ne!(s0, s1); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +Both sides: +666 +"#)] + fn fails() { + ::pretty_assertions::assert_ne!(666, 666); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +Both sides: +666 +"#)] + fn fails_trailing_comma() { + ::pretty_assertions::assert_ne!(666, 666,); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +Both sides: +[ + 101, +] + +"#)] + fn fails_unsized() { + let a: &[u8] = b"e"; + ::pretty_assertions::assert_ne!(*a, *a); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left != right)`: custom panic message + +Both sides: +666 +"# + )] + fn fails_custom() { + ::pretty_assertions::assert_ne!(666, 666, "custom panic message"); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left != right)`: custom panic message + +Both sides: +666 +"# + )] + fn fails_custom_trailing_comma() { + ::pretty_assertions::assert_ne!(666, 666, "custom panic message",); + } + + // If the values are equal but their debug outputs are not + // show a specific warning + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +Diff < left / right > : +<-0.0 +>0.0 + +Note: According to the `PartialEq` implementation, both of the values are partially equivalent, even if the `Debug` outputs differ. + +"#)] + fn assert_ne_partial() { + // Workaround for https://github.com/rust-lang/rust/issues/47619 + // can be removed, when we require rust 1.25 or higher + struct Foo(f32); + + use ::std::fmt; + impl fmt::Debug for Foo { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + ::std::write!(f, "{:.1?}", self.0) + } + } + + impl ::std::cmp::PartialEq for Foo { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } + } + + ::pretty_assertions::assert_ne!(Foo(-0.0), Foo(0.0)); + } + + // Regression tests + + #[test] + #[should_panic] + fn assert_ne_non_empty_return() { + fn not_zero(x: u32) -> u32 { + ::pretty_assertions::assert_ne!(x, 0); + x + } + not_zero(0); + } +}