From 45a63d3ff611b1412f9d811cd328b648bada5ca2 Mon Sep 17 00:00:00 2001 From: Mitsunori Komatsu Date: Tue, 31 May 2016 01:53:14 +0900 Subject: [PATCH] Add message argument to `assert_eq` macro --- src/libcore/macros.rs | 13 ++++++++++++- src/test/run-pass/assert-eq-macro-success.rs | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index a40608b0762d6..376d2792c445c 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -94,7 +94,18 @@ macro_rules! assert_eq { } } } - }) + }); + ($left:expr , $right:expr, $($arg:tt)*) => ({ + match (&($left), &($right)) { + (left_val, right_val) => { + if !(*left_val == *right_val) { + panic!("assertion failed: `(left == right)` \ + (left: `{:?}`, right: `{:?}`): {}", left_val, right_val, + format_args!($($arg)*)) + } + } + } + }); } /// Ensure that a boolean expression is `true` at runtime. diff --git a/src/test/run-pass/assert-eq-macro-success.rs b/src/test/run-pass/assert-eq-macro-success.rs index 9662e1ff33d11..3110e22d5da13 100644 --- a/src/test/run-pass/assert-eq-macro-success.rs +++ b/src/test/run-pass/assert-eq-macro-success.rs @@ -16,4 +16,7 @@ pub fn main() { assert_eq!("abc".to_string(),"abc".to_string()); assert_eq!(Box::new(Point{x:34}),Box::new(Point{x:34})); assert_eq!(&Point{x:34},&Point{x:34}); + assert_eq!(42, 42, "foo bar"); + assert_eq!(42, 42, "a {} c", "b"); + assert_eq!(42, 42, "{x}, {y}, {z}", x = 1, y = 2, z = 3); }