@@ -674,6 +674,8 @@ TEST_P(MatcherTupleTestP, ExplainsMatchFailure) {
674
674
// explanation.
675
675
}
676
676
677
+ #if GTEST_HAS_TYPED_TEST
678
+
677
679
// Sample optional type implementation with minimal requirements for use with
678
680
// Optional matcher.
679
681
template <typename T>
@@ -691,38 +693,76 @@ class SampleOptional {
691
693
bool has_value_;
692
694
};
693
695
694
- TEST (OptionalTest, DescribesSelf) {
695
- const Matcher<SampleOptional<int >> m = Optional (Eq (1 ));
696
+ // Sample optional type implementation with alternative minimal requirements for
697
+ // use with Optional matcher. In particular, while it doesn't have a bool
698
+ // conversion operator, it does have a has_value() method.
699
+ template <typename T>
700
+ class SampleOptionalWithoutBoolConversion {
701
+ public:
702
+ using value_type = T;
703
+ explicit SampleOptionalWithoutBoolConversion (T value)
704
+ : value_(std::move(value)), has_value_(true ) {}
705
+ SampleOptionalWithoutBoolConversion () : value_(), has_value_(false ) {}
706
+ bool has_value () const { return has_value_; }
707
+ const T& operator *() const { return value_; }
708
+
709
+ private:
710
+ T value_;
711
+ bool has_value_;
712
+ };
713
+
714
+ template <typename T>
715
+ class OptionalTest : public testing ::Test {};
716
+
717
+ using OptionalTestTypes =
718
+ testing::Types<SampleOptional<int >,
719
+ SampleOptionalWithoutBoolConversion<int >>;
720
+
721
+ TYPED_TEST_SUITE (OptionalTest, OptionalTestTypes);
722
+
723
+ TYPED_TEST (OptionalTest, DescribesSelf) {
724
+ const Matcher<TypeParam> m = Optional (Eq (1 ));
696
725
EXPECT_EQ (" value is equal to 1" , Describe (m));
697
726
}
698
727
699
- TEST (OptionalTest, ExplainsSelf) {
700
- const Matcher<SampleOptional< int > > m = Optional (Eq (1 ));
701
- EXPECT_EQ (" whose value 1 matches" , Explain (m, SampleOptional< int > (1 )));
702
- EXPECT_EQ (" whose value 2 doesn't match" , Explain (m, SampleOptional< int > (2 )));
728
+ TYPED_TEST (OptionalTest, ExplainsSelf) {
729
+ const Matcher<TypeParam > m = Optional (Eq (1 ));
730
+ EXPECT_EQ (" whose value 1 matches" , Explain (m, TypeParam (1 )));
731
+ EXPECT_EQ (" whose value 2 doesn't match" , Explain (m, TypeParam (2 )));
703
732
}
704
733
705
- TEST (OptionalTest, MatchesNonEmptyOptional) {
706
- const Matcher<SampleOptional< int > > m1 = Optional (1 );
707
- const Matcher<SampleOptional< int > > m2 = Optional (Eq (2 ));
708
- const Matcher<SampleOptional< int > > m3 = Optional (Lt (3 ));
709
- SampleOptional< int > opt (1 );
734
+ TYPED_TEST (OptionalTest, MatchesNonEmptyOptional) {
735
+ const Matcher<TypeParam > m1 = Optional (1 );
736
+ const Matcher<TypeParam > m2 = Optional (Eq (2 ));
737
+ const Matcher<TypeParam > m3 = Optional (Lt (3 ));
738
+ TypeParam opt (1 );
710
739
EXPECT_TRUE (m1.Matches (opt));
711
740
EXPECT_FALSE (m2.Matches (opt));
712
741
EXPECT_TRUE (m3.Matches (opt));
713
742
}
714
743
715
- TEST (OptionalTest, DoesNotMatchNullopt) {
716
- const Matcher<SampleOptional< int > > m = Optional (1 );
717
- SampleOptional< int > empty;
744
+ TYPED_TEST (OptionalTest, DoesNotMatchNullopt) {
745
+ const Matcher<TypeParam > m = Optional (1 );
746
+ TypeParam empty;
718
747
EXPECT_FALSE (m.Matches (empty));
719
748
}
720
749
721
- TEST (OptionalTest, WorksWithMoveOnly) {
722
- Matcher<SampleOptional<std::unique_ptr<int >>> m = Optional (Eq (nullptr ));
723
- EXPECT_TRUE (m.Matches (SampleOptional<std::unique_ptr<int >>(nullptr )));
750
+ template <typename T>
751
+ class MoveOnlyOptionalTest : public testing ::Test {};
752
+
753
+ using MoveOnlyOptionalTestTypes =
754
+ testing::Types<SampleOptional<std::unique_ptr<int >>,
755
+ SampleOptionalWithoutBoolConversion<std::unique_ptr<int >>>;
756
+
757
+ TYPED_TEST_SUITE (MoveOnlyOptionalTest, MoveOnlyOptionalTestTypes);
758
+
759
+ TYPED_TEST (MoveOnlyOptionalTest, WorksWithMoveOnly) {
760
+ Matcher<TypeParam> m = Optional (Eq (nullptr ));
761
+ EXPECT_TRUE (m.Matches (TypeParam (nullptr )));
724
762
}
725
763
764
+ #endif // GTEST_HAS_TYPED_TEST
765
+
726
766
class SampleVariantIntString {
727
767
public:
728
768
SampleVariantIntString (int i) : i_(i), has_int_(true ) {}
0 commit comments