@@ -587,6 +587,184 @@ macro_rules! expect_false {
587
587
} } ;
588
588
}
589
589
590
+ /// Checks whether the second argument is equal to the first argument.
591
+ ///
592
+ /// Evaluates to `Result::Ok(())` if they are equal and
593
+ /// `Result::Err(TestAssertionFailure)` if they are not. The caller must then
594
+ /// decide how to handle the `Err` variant. It has a few options:
595
+ /// * Abort the current function with the `?` operator. This requires that the
596
+ /// function return a suitable `Result`.
597
+ /// * Log the test failure and continue by calling the method
598
+ /// `and_log_failure`.
599
+ ///
600
+ /// Of course, one can also use all other standard methods on `Result`.
601
+ ///
602
+ /// **Invoking this macro by itself does not cause a test failure to be recorded
603
+ /// or output.** The resulting `Result` must be handled as described above to
604
+ /// cause the test to be recorded as a failure.
605
+ ///
606
+ /// Example:
607
+ /// ```ignore
608
+ /// use googletest::prelude::*;
609
+ ///
610
+ /// #[test]
611
+ /// fn should_fail() -> Result<()> {
612
+ /// verify_eq!(2, 1)
613
+ /// }
614
+ /// ```
615
+ ///
616
+ /// This macro has special support for matching against container. Namely:
617
+ /// * `verify_eq!(actual, [e1, e2, ...])` is equivalent to
618
+ /// `verify_that!(actual, elements_are![eq(e1), eq(e2), ...])`
619
+ /// * `verify_eq!(actual, {e1, e2, ...})` is equivalent to
620
+ /// `verify_that!(actual, unordered_elements_are![eq(e1), eq(e2), ...])`
621
+ #[ macro_export]
622
+ macro_rules! verify_eq {
623
+ ( $actual: expr, [ $( $expected: expr) ,+ $( , ) ?] $( , ) ?) => {
624
+ verify_that!( $actual, [ $( $crate:: matchers:: eq( & $expected) ) ,* ] )
625
+ } ;
626
+ ( $actual: expr, { $( $expected: expr) ,+ $( , ) ?} $( , ) ?) => {
627
+ verify_that!( $actual, { $( $crate:: matchers:: eq( & $expected) ) ,* } )
628
+ } ;
629
+ ( $actual: expr, $expected: expr $( , ) ?) => {
630
+ verify_that!( $actual, $crate:: matchers:: eq( $expected) )
631
+ } ;
632
+ }
633
+
634
+ /// Panics if the second argument is not equal to first argument.
635
+ ///
636
+ /// This is the same as calling `and_log_failure` on [`verify_eq`] macro Result.
637
+ ///
638
+ /// This can only be invoked inside tests with the
639
+ /// [`googletest::test`][crate::test] attribute. The failure must be generated
640
+ /// in the same thread as that running the thread itself.
641
+ ///
642
+ /// Example:
643
+ /// ```ignore
644
+ /// use googletest;
645
+ ///
646
+ /// #[googletest::test]
647
+ /// fn should_fail() {
648
+ /// googletest::assert_eq!(2, 1);
649
+ /// }
650
+ /// ```
651
+ ///
652
+ /// This macro has special support for matching against container. Namely:
653
+ /// * `assert_eq!(actual, [e1, e2, ...])` for checking actual contains "e1, e2,
654
+ /// ..." in order.
655
+ /// * `assert_eq!(actual, {e1, e2, ...})` for checking actual contains "e1, e2,
656
+ /// ..." in any order.
657
+ ///
658
+ /// One may include formatted arguments in the failure message:
659
+ ///```ignore
660
+ /// use googletest;
661
+ ///
662
+ /// #[googletest::test]
663
+ /// fn should_fail() {
664
+ /// let argument = "argument"
665
+ /// googletest::assert_eq!(2, 1, "custom failure message: {argument}");
666
+ /// }
667
+ /// ```
668
+ #[ macro_export]
669
+ macro_rules! assert_eq {
670
+ ( $actual: expr, [ $( $expected: expr) ,+ $( , ) ?] $( , ) ?) => {
671
+ verify_eq!( $actual, [ $( $expected) ,* ] ) . and_log_failure( ) ;
672
+ } ;
673
+ ( $actual: expr, [ $( $expected: expr) ,+ $( , ) ?] , $( $format_args: expr) ,* $( , ) ?) => {
674
+ verify_eq!( $actual, [ $( $expected) ,* ] )
675
+ . with_failure_message( || format!( $( $format_args) ,* ) )
676
+ . and_log_failure( ) ;
677
+ } ;
678
+ ( $actual: expr, { $( $expected: expr) ,+ $( , ) ?} $( , ) ?) => {
679
+ verify_eq!( $actual, { $( $expected) ,* } ) . and_log_failure( ) ;
680
+ } ;
681
+ ( $actual: expr, { $( $expected: expr) ,+ $( , ) ?} , $( $format_args: expr) ,* $( , ) ?) => {
682
+ verify_eq!( $actual, { $( $expected) ,* } )
683
+ . with_failure_message( || format!( $( $format_args) ,* ) )
684
+ . and_log_failure( ) ;
685
+ } ;
686
+ ( $actual: expr, $expected: expr $( , ) ?) => {
687
+ verify_eq!( $actual, $expected) . and_log_failure( ) ;
688
+ } ;
689
+ ( $actual: expr, $expected: expr, $( $format_args: expr) ,* $( , ) ?) => {
690
+ verify_eq!( $actual, $expected)
691
+ . with_failure_message( || format!( $( $format_args) ,* ) )
692
+ . and_log_failure( ) ;
693
+ } ;
694
+ }
695
+
696
+ /// Marks test as failed and continues execution if the second argument is not
697
+ /// equal to first argument.
698
+ ///
699
+ /// This is a **not-fatal** failure. The test continues execution even after the
700
+ /// macro execution.
701
+ ///
702
+ /// This can only be invoked inside tests with the
703
+ /// [`googletest::test`][crate::test] attribute. The failure must be generated
704
+ /// in the same thread as that running the thread itself.
705
+ ///
706
+ /// Example:
707
+ /// ```ignore
708
+ /// use googletest::prelude::*;
709
+ ///
710
+ /// #[googletest::test]
711
+ /// fn should_fail() {
712
+ /// expect_eq!(2, 1);
713
+ /// println!("This will print!");
714
+ /// }
715
+ /// ```
716
+ ///
717
+ /// This macro has special support for matching against container. Namely:
718
+ /// * `expect_eq!(actual, [e1, e2, ...])` for checking actual contains "e1, e2,
719
+ /// ..." in order.
720
+ /// * `expect_eq!(actual, {e1, e2, ...})` for checking actual contains "e1, e2,
721
+ /// ..." in any order.
722
+ ///
723
+ /// One may include formatted arguments in the failure message:
724
+ ///```ignore
725
+ /// use googletest::prelude::*;
726
+ ///
727
+ /// #[googletest::test]
728
+ /// fn should_fail() {
729
+ /// let argument = "argument"
730
+ /// expect_eq!(2, 1, "custom failure message: {argument}");
731
+ /// println!("This will print!");
732
+ /// }
733
+ /// ```
734
+ #[ macro_export]
735
+ macro_rules! expect_eq {
736
+ ( $actual: expr, [ $( $expected: expr) ,+ $( , ) ?] $( , ) ?) => {
737
+ use $crate:: GoogleTestSupport ;
738
+ verify_eq!( $actual, [ $( $expected) ,* ] ) . and_log_failure( ) ;
739
+ } ;
740
+ ( $actual: expr, [ $( $expected: expr) ,+ $( , ) ?] , $( $format_args: expr) ,* $( , ) ?) => {
741
+ use $crate:: GoogleTestSupport ;
742
+ verify_eq!( $actual, [ $( $expected) ,* ] )
743
+ . with_failure_message( || format!( $( $format_args) ,* ) )
744
+ . and_log_failure( ) ;
745
+ } ;
746
+ ( $actual: expr, { $( $expected: expr) ,+ $( , ) ?} $( , ) ?) => {
747
+ use $crate:: GoogleTestSupport ;
748
+ verify_eq!( $actual, { $( $expected) ,* } ) . and_log_failure( ) ;
749
+ } ;
750
+ ( $actual: expr, { $( $expected: expr) ,+ $( , ) ?} , $( $format_args: expr) ,* $( , ) ?) => {
751
+ use $crate:: GoogleTestSupport ;
752
+ verify_eq!( $actual, { $( $expected) ,* } )
753
+ . with_failure_message( || format!( $( $format_args) ,* ) )
754
+ . and_log_failure( ) ;
755
+ } ;
756
+ ( $actual: expr, $expected: expr $( , ) ?) => {
757
+ use $crate:: GoogleTestSupport ;
758
+ verify_eq!( $actual, $expected) . and_log_failure( ) ;
759
+ } ;
760
+ ( $actual: expr, $expected: expr, $( $format_args: expr) ,* $( , ) ?) => {
761
+ use $crate:: GoogleTestSupport ;
762
+ verify_eq!( $actual, $expected)
763
+ . with_failure_message( || format!( $( $format_args) ,* ) )
764
+ . and_log_failure( ) ;
765
+ } ;
766
+ }
767
+
590
768
/// Matches the given value against the given matcher, panicking if it does not
591
769
/// match.
592
770
///
0 commit comments