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.
This commit is contained in:
bors 2016-06-22 01:05:56 -07:00 committed by GitHub
commit 3ee3267af3
2 changed files with 15 additions and 1 deletions

View File

@ -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.

View File

@ -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);
}