about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-06-22 01:05:56 -0700
committerGitHub <noreply@github.com>2016-06-22 01:05:56 -0700
commit3ee3267af39aa95fed707c67acb656845eb8f365 (patch)
tree4ab8e1587cc40ddd2f37e657413b993b4fef61db
parent66b82beb609aa65b76b93a43e6aa1f507dfb2cdc (diff)
parent45a63d3ff611b1412f9d811cd328b648bada5ca2 (diff)
downloadrust-3ee3267af39aa95fed707c67acb656845eb8f365.tar.gz
rust-3ee3267af39aa95fed707c67acb656845eb8f365.zip
Auto merge of #33976 - komamitsu:assert_eq_with_msg, r=alexcrichton
Add custom message parameter to `assert_eq!`

`assert!` macro accepts a custom message parameter and it's sometimes useful. But `assert_eq!` doesn't have it and users need to use `assert!` instead of `assert_eq!` when they want to output a custom message even if the assertion just compares two values. This pull request will resolve those cases.
-rw-r--r--src/libcore/macros.rs13
-rw-r--r--src/test/run-pass/assert-eq-macro-success.rs3
2 files changed, 15 insertions, 1 deletions
diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs
index a40608b0762..376d2792c44 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 9662e1ff33d..3110e22d5da 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);
 }