Skip to content

Commit 3ee3267

Browse files
authored
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.
2 parents 66b82be + 45a63d3 commit 3ee3267

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/libcore/macros.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,18 @@ macro_rules! assert_eq {
9494
}
9595
}
9696
}
97-
})
97+
});
98+
($left:expr , $right:expr, $($arg:tt)*) => ({
99+
match (&($left), &($right)) {
100+
(left_val, right_val) => {
101+
if !(*left_val == *right_val) {
102+
panic!("assertion failed: `(left == right)` \
103+
(left: `{:?}`, right: `{:?}`): {}", left_val, right_val,
104+
format_args!($($arg)*))
105+
}
106+
}
107+
}
108+
});
98109
}
99110

100111
/// Ensure that a boolean expression is `true` at runtime.

src/test/run-pass/assert-eq-macro-success.rs

+3
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ pub fn main() {
1616
assert_eq!("abc".to_string(),"abc".to_string());
1717
assert_eq!(Box::new(Point{x:34}),Box::new(Point{x:34}));
1818
assert_eq!(&Point{x:34},&Point{x:34});
19+
assert_eq!(42, 42, "foo bar");
20+
assert_eq!(42, 42, "a {} c", "b");
21+
assert_eq!(42, 42, "{x}, {y}, {z}", x = 1, y = 2, z = 3);
1922
}

0 commit comments

Comments
 (0)