approx icon indicating copy to clipboard operation
approx copied to clipboard

Adding messages to assert_* macros

Open arekfu opened this issue 7 years ago • 3 comments

It would be great to extend the assert_* macros to accept a custom panic message, the way assert_eq! does:

assert_eq!(a, b, "Failed to compare a and b; a={}, b={}", a, b);

arekfu avatar Nov 08 '18 21:11 arekfu

Yeah! This seems like a great idea!

brendanzab avatar Nov 09 '18 01:11 brendanzab

@brendanzab I've attempted a fix for this.

Due to parsing ambiguities arising from adding a second repeating pattern, I took the approach of separating the panic message args from the rest of the macro args with a ;, like so:

($given:expr, $expected:expr $(, $opt:ident = $val:expr)*; $($arg:tt)*)
                                                        ^^^

which allows us to do this:

assert_abs_diff_eq!(x, y, epsilon = epsilon; "Should panic. x = {:?}, y = {:?},
    epsilon: {:?}", x, y, epsilon);

It feels rather hacky though, and deviates from how Rust asserts behave. If you've got a better idea on how to tackle this, I'm all ears.

adrianwong avatar Nov 27 '18 02:11 adrianwong