@@ -484,6 +484,29 @@ pub use verify_true;
484
484
/// println!("This will print");
485
485
/// }
486
486
/// ```
487
+ ///
488
+ /// One may optionally add arguments which will be formatted and appended to a
489
+ /// failure message. For example:
490
+ ///
491
+ /// ```ignore
492
+ /// use googletest::prelude::*;
493
+ ///
494
+ /// #[gtest]
495
+ /// fn should_fail() {
496
+ /// let extra_information = "Some additional information";
497
+ /// expect_true!(false, "Test failed. Extra information: {extra_information}.");
498
+ /// }
499
+ /// ```
500
+ ///
501
+ /// The output is as follows:
502
+ ///
503
+ /// ```text
504
+ /// Value of: false
505
+ /// Expected: is equal to true
506
+ /// Actual: false,
507
+ /// which isn't equal to true
508
+ /// Test failed. Extra information: Some additional information.
509
+ /// ```
487
510
#[ macro_export]
488
511
macro_rules! expect_true {
489
512
( $condition: expr) => { {
@@ -550,6 +573,29 @@ pub use verify_false;
550
573
/// println!("This will print");
551
574
/// }
552
575
/// ```
576
+ ///
577
+ /// One may optionally add arguments which will be formatted and appended to a
578
+ /// failure message. For example:
579
+ ///
580
+ /// ``` ignore
581
+ /// use googletest::prelude::*;
582
+ ///
583
+ /// #[gtest]
584
+ /// fn should_fail() {
585
+ /// let extra_information = "Some additional information";
586
+ /// expect_false!(true, "Test failed. Extra information: {extra_information}.");
587
+ /// }
588
+ /// ```
589
+ ///
590
+ /// The output is as follows:
591
+ ///
592
+ /// ```text
593
+ /// Value of: true
594
+ /// Expected: is equal to false
595
+ /// Actual: true,
596
+ /// which isn't equal to false
597
+ /// Test failed. Extra information: Some additional information.
598
+ /// ```
553
599
#[ macro_export]
554
600
macro_rules! expect_false {
555
601
( $condition: expr) => { {
@@ -1405,6 +1451,26 @@ pub use assert_that;
1405
1451
/// Asserts that the given predicate applied to the given arguments returns
1406
1452
/// true, panicking if it does not.
1407
1453
///
1454
+ /// One may optionally add arguments which will be formatted and appended to a
1455
+ /// failure message. For example:
1456
+ ///
1457
+ /// ```should_panic
1458
+ /// use googletest::prelude::*;
1459
+ ///
1460
+ /// #[gtest]
1461
+ /// fn should_fail() {
1462
+ /// let extra_information = "Some additional information";
1463
+ /// assert_pred!(1 == 2, "Test failed. Extra information: {extra_information}.");
1464
+ /// }
1465
+ /// ```
1466
+ ///
1467
+ /// The output is as follows:
1468
+ ///
1469
+ /// ```text
1470
+ /// 1 == 2 was false with
1471
+ /// Test failed. Extra information: Some additional information.
1472
+ /// ```
1473
+ ///
1408
1474
/// **Note for users of [GoogleTest for C++](http://google.github.io/googletest/):**
1409
1475
/// This differs from the `ASSERT_PRED*` family of macros in that it panics
1410
1476
/// rather than triggering an early return from the invoking function. To get
@@ -1536,6 +1602,26 @@ pub use expect_that;
1536
1602
/// ```ignore
1537
1603
/// verify_pred!(predicate(...)).and_log_failure()
1538
1604
/// ```
1605
+ ///
1606
+ /// One may optionally add arguments which will be formatted and appended to a
1607
+ /// failure message. For example:
1608
+ ///
1609
+ /// ```ignore
1610
+ /// use googletest::prelude::*;
1611
+ ///
1612
+ /// #[gtest]
1613
+ /// fn should_fail() {
1614
+ /// let extra_information = "Some additional information";
1615
+ /// expect_pred!(1 == 2, "Test failed. Extra information: {extra_information}.");
1616
+ /// }
1617
+ /// ```
1618
+ ///
1619
+ /// The output is as follows:
1620
+ ///
1621
+ /// ```text
1622
+ /// 1 == 2 was false with
1623
+ /// Test failed. Extra information: Some additional information.
1624
+ /// ```
1539
1625
#[ macro_export]
1540
1626
macro_rules! expect_pred {
1541
1627
( $content: expr $( , ) ?) => { {
@@ -1550,6 +1636,123 @@ macro_rules! expect_pred {
1550
1636
}
1551
1637
pub use expect_pred;
1552
1638
1639
+ /// Matches the given expr Result against the Result::Ok(anything()) matcher,
1640
+ /// marking the test as failed but continuing execution if it does not match.
1641
+ ///
1642
+ /// This is a *non-fatal* assertion: the test continues
1643
+ /// execution in the event of assertion failure.
1644
+ ///
1645
+ /// This can only be invoked inside tests with the
1646
+ /// [`gtest`][crate::gtest] attribute. The assertion must
1647
+ /// occur in the same thread as that running the test itself.
1648
+ ///
1649
+ /// Invoking this macro is equivalent to using
1650
+ /// [`and_log_failure`](crate::GoogleTestSupport::and_log_failure) as follows:
1651
+ ///
1652
+ /// ```ignore
1653
+ /// verify_that!(actual, ok(anything())).and_log_failure()
1654
+ /// ```
1655
+ ///
1656
+ /// One may optionally add arguments which will be formatted and appended to a
1657
+ /// failure message. For example:
1658
+ ///
1659
+ /// ```
1660
+ /// # use googletest::prelude::*;
1661
+ /// # fn should_fail() -> std::result::Result<(), googletest::internal::test_outcome::TestFailure> {
1662
+ /// # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
1663
+ /// let extra_information = "Some additional information";
1664
+ /// expect_ok!(Err::<&str, _>("An error"), "Test failed. Extra information: {extra_information}.");
1665
+ /// # googletest::internal::test_outcome::TestOutcome::close_current_test_outcome::<&str>(Ok(()))
1666
+ /// # }
1667
+ /// # should_fail().unwrap_err();
1668
+ /// ```
1669
+ ///
1670
+ /// This is output as follows:
1671
+ ///
1672
+ /// ```text
1673
+ /// Value of: Err::<&str, _>("An error")
1674
+ /// Expected: is a success containing a value, which is anything
1675
+ /// Actual: Err("An error"),
1676
+ /// which is an error
1677
+ /// Test failed. Extra information: Some additional information.
1678
+ /// ```
1679
+ #[ macro_export]
1680
+ macro_rules! expect_ok {
1681
+ ( $actual: expr $( , ) ?) => { {
1682
+ $crate:: GoogleTestSupport :: and_log_failure( $crate:: verify_that!( $actual, $crate:: matchers:: ok( anything( ) ) ) ) ;
1683
+ } } ;
1684
+ // w/ format args
1685
+ ( $actual: expr, $( $format_args: expr) ,* $( , ) ?) => { {
1686
+ $crate:: GoogleTestSupport :: and_log_failure( $crate:: verify_that!( $actual, $crate:: matchers:: ok( anything( ) ) )
1687
+ . with_failure_message( || format!( $( $format_args) ,* ) )
1688
+ ) ;
1689
+ } } ;
1690
+ }
1691
+ pub use expect_ok;
1692
+
1693
+ /// Matches the given expr Result against the Result::Ok(anything()) matcher,
1694
+ /// panicking if it does not match.
1695
+ ///
1696
+ /// ```should_panic
1697
+ /// # use googletest::prelude::*;
1698
+ /// # fn should_fail() {
1699
+ /// assert_ok!(Err::<&str, _>("An error")); // Fails and panics.
1700
+ /// # }
1701
+ /// # should_fail();
1702
+ /// ```
1703
+ ///
1704
+ /// This is analogous to assertions in most Rust test libraries, where a failed
1705
+ /// assertion causes a panic.
1706
+ ///
1707
+ /// One may optionally add arguments which will be formatted and appended to a
1708
+ /// failure message. For example:
1709
+ ///
1710
+ /// ```should_panic
1711
+ /// # use googletest::prelude::*;
1712
+ /// # fn should_fail() {
1713
+ /// let extra_information = "Some additional information";
1714
+ /// assert_ok!(Err::<&str, _>("An error"), "Test failed. Extra information: {extra_information}.");
1715
+ /// # }
1716
+ /// # should_fail();
1717
+ /// ```
1718
+ ///
1719
+ /// This is output as follows:
1720
+ ///
1721
+ /// ```text
1722
+ /// Value of: Err::<&str, _>("An error")
1723
+ /// Expected: is a success containing a value, which is anything
1724
+ /// Actual: Err("An error"),
1725
+ /// which is an error
1726
+ /// Test failed. Extra information: Some additional information.
1727
+ /// ```
1728
+ #[ macro_export]
1729
+ macro_rules! assert_ok {
1730
+ ( $actual: expr $( , ) ?) => {
1731
+ match $crate:: verify_that!( $actual, $crate:: matchers:: ok( anything( ) ) ) {
1732
+ Ok ( _) => { }
1733
+ Err ( e) => {
1734
+ // The extra newline before the assertion failure message makes the failure a
1735
+ // bit easier to read when there's some generic boilerplate from the panic.
1736
+ panic!( "\n {}" , e) ;
1737
+ }
1738
+ }
1739
+ } ;
1740
+ // w/ format args
1741
+ ( $actual: expr, $( $format_args: expr) , * $( , ) ?) => {
1742
+ match $crate:: verify_that!( $actual, $crate:: matchers:: ok( anything( ) ) )
1743
+ . with_failure_message( || format!( $( $format_args) ,* ) )
1744
+ {
1745
+ Ok ( _) => { }
1746
+ Err ( e) => {
1747
+ // The extra newline before the assertion failure message makes the failure a
1748
+ // bit easier to read when there's some generic boilerplate from the panic.
1749
+ panic!( "\n {}" , e) ;
1750
+ }
1751
+ }
1752
+ } ;
1753
+ }
1754
+ pub use assert_ok;
1755
+
1553
1756
/// Functions for use only by the procedural macros in this module.
1554
1757
///
1555
1758
/// **For internal use only. API stablility is not guaranteed!**
0 commit comments