@@ -309,9 +309,42 @@ macro_rules! fail {
309
309
/// Matches the given value against the given matcher, panicking if it does not
310
310
/// match.
311
311
///
312
+ /// ```should_panic
313
+ /// # use googletest::prelude::*;
314
+ /// # fn should_fail() {
315
+ /// let value = 2;
316
+ /// assert_that!(value, eq(3)); // Fails and panics.
317
+ /// # }
318
+ /// # should_fail();
319
+ /// ```
320
+ ///
312
321
/// This is analogous to assertions in most Rust test libraries, where a failed
313
322
/// assertion causes a panic.
314
323
///
324
+ /// One may optionally add arguments which will be formatted and appended to a
325
+ /// failure message. For example:
326
+ ///
327
+ /// ```should_panic
328
+ /// # use googletest::prelude::*;
329
+ /// # fn should_fail() {
330
+ /// let value = 2;
331
+ /// let extra_information = "Some additional information";
332
+ /// assert_that!(value, eq(3), "Test failed. Extra information: {extra_information}.");
333
+ /// # }
334
+ /// # should_fail();
335
+ /// ```
336
+ ///
337
+ /// This is output as follows:
338
+ ///
339
+ /// ```text
340
+ /// Value of: value
341
+ /// Expected: is equal to 3
342
+ /// Actual: 2,
343
+ /// which isn't equal to 3
344
+ /// at ...
345
+ /// Test failed. Extra information: Some additional information.
346
+ /// ```
347
+ ///
315
348
/// **Note for users of [GoogleTest for C++](http://google.github.io/googletest/):**
316
349
/// This differs from the `ASSERT_THAT` macro in that it panics rather
317
350
/// than triggering an early return from the invoking function. To get behaviour
@@ -328,6 +361,19 @@ macro_rules! assert_that {
328
361
}
329
362
}
330
363
} ;
364
+
365
+ ( $actual: expr, $expected: expr, $( $format_args: expr) ,* $( , ) ?) => {
366
+ match $crate:: verify_that!( $actual, $expected)
367
+ . with_failure_message( || format!( $( $format_args) ,* ) )
368
+ {
369
+ Ok ( _) => { }
370
+ Err ( e) => {
371
+ // The extra newline before the assertion failure message makes the failure a
372
+ // bit easier to read when there's some generic boilerplate from the panic.
373
+ panic!( "\n {}" , e) ;
374
+ }
375
+ }
376
+ } ;
331
377
}
332
378
333
379
/// Asserts that the given predicate applied to the given arguments returns
@@ -368,12 +414,44 @@ macro_rules! assert_pred {
368
414
/// ```ignore
369
415
/// verify_that!(actual, expected).and_log_failure()
370
416
/// ```
417
+ ///
418
+ /// One may optionally add arguments which will be formatted and appended to a
419
+ /// failure message. For example:
420
+ ///
421
+ /// ```
422
+ /// # use googletest::prelude::*;
423
+ /// # fn should_fail() -> std::result::Result<(), googletest::internal::test_outcome::TestFailure> {
424
+ /// # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
425
+ /// let value = 2;
426
+ /// let extra_information = "Some additional information";
427
+ /// expect_that!(value, eq(3), "Test failed. Extra information: {extra_information}.");
428
+ /// # googletest::internal::test_outcome::TestOutcome::close_current_test_outcome::<&str>(Ok(()))
429
+ /// # }
430
+ /// # should_fail().unwrap_err();
431
+ /// ```
432
+ ///
433
+ /// This is output as follows:
434
+ ///
435
+ /// ```text
436
+ /// Value of: value
437
+ /// Expected: is equal to 3
438
+ /// Actual: 2,
439
+ /// which isn't equal to 3
440
+ /// at ...
441
+ /// Test failed. Extra information: Some additional information.
442
+ /// ```
371
443
#[ macro_export]
372
444
macro_rules! expect_that {
373
445
( $actual: expr, $expected: expr) => { {
374
446
use $crate:: GoogleTestSupport ;
375
447
$crate:: verify_that!( $actual, $expected) . and_log_failure( ) ;
376
448
} } ;
449
+
450
+ ( $actual: expr, $expected: expr, $( $format_args: expr) ,* $( , ) ?) => {
451
+ $crate:: verify_that!( $actual, $expected)
452
+ . with_failure_message( || format!( $( $format_args) ,* ) )
453
+ . and_log_failure( )
454
+ } ;
377
455
}
378
456
379
457
/// Asserts that the given predicate applied to the given arguments returns
0 commit comments