Created
October 20, 2021 05:00
-
-
Save mkhan45/cd0d28b17249125c87b3d3b7a1e8d391 to your computer and use it in GitHub Desktop.
fancy assert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| macro_rules! function { | |
| () => {{ | |
| fn f() {} | |
| fn type_name_of<T>(_: T) -> &'static str { | |
| std::any::type_name::<T>() | |
| } | |
| let name = type_name_of(f); | |
| &name[..name.len() - 3] | |
| }} | |
| } | |
| macro_rules! fancy_assert_eq { | |
| ($lhs:expr, $rhs:expr) => { | |
| if ($lhs != $rhs) { | |
| println!("Assertion failed at {}:{}: {}", file!(), line!(), function!()); | |
| println!("Where:"); | |
| println!("\t{} => {}", stringify!($lhs), $lhs); | |
| println!("\t{} => {}", stringify!($rhs), $rhs); | |
| } | |
| } | |
| } | |
| fn main() { | |
| fancy_assert_eq!(5 + 1 * 2, 1); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ~ Δ rustc fancy_assert.rs && ./fancy_assert | |
| Assertion failed at fancy_assert.rs:24: fancy_assert::main | |
| Where: | |
| 5 + 1 * 2 => 7 | |
| 1 => 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment