From ed2eef654373c17b96bf5a007bb481a6e96ba629 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Fri, 23 Aug 2019 15:30:05 -0400 Subject: Googletest export Add tuple version of Optional() matches. This allows Optional() to be used in Pointwise matchers. PiperOrigin-RevId: 265110864 --- googlemock/test/gmock-matchers_test.cc | 35 +++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'googlemock/test/gmock-matchers_test.cc') diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index a61d040b..f5e25e07 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -6387,7 +6387,7 @@ class SampleOptional { explicit SampleOptional(T value) : value_(std::move(value)), has_value_(true) {} SampleOptional() : value_(), has_value_(false) {} - operator bool() const { return has_value_; } + explicit operator bool() const { return has_value_; } const T& operator*() const { return value_; } private: @@ -6427,6 +6427,39 @@ TEST(OptionalTest, WorksWithMoveOnly) { EXPECT_TRUE(m.Matches(SampleOptional>(nullptr))); } +TEST(OptionalTest, TupleDescribesSelf) { + const Matcher, int>> m = Optional(Eq()); + EXPECT_EQ("are optionals where the values are an equal pair", Describe(m)); +} + +TEST(OptionalTest, TupleExplainsSelf) { + const Matcher, int>> m = Optional(Eq()); + EXPECT_EQ("which match", + Explain(m, std::make_tuple(SampleOptional(1), 1))); + EXPECT_EQ("whose values don't match", + Explain(m, std::make_tuple(SampleOptional(1), 2))); +} + +TEST(OptionalTest, TupleMatchesNonEmpty) { + const Matcher, int>> m1 = Optional(Eq()); + const Matcher, int>> m2 = Optional(Lt()); + EXPECT_TRUE(m1.Matches(std::make_tuple(SampleOptional(1), 1))); + EXPECT_FALSE(m1.Matches(std::make_tuple(SampleOptional(1), 2))); + EXPECT_FALSE(m2.Matches(std::make_tuple(SampleOptional(1), 1))); + EXPECT_TRUE(m2.Matches(std::make_tuple(SampleOptional(1), 2))); +} + +TEST(OptionalTest, TupleDoesNotMatchNullopt) { + const Matcher, int>> m1 = Optional(Eq()); + EXPECT_FALSE(m1.Matches(std::make_tuple(SampleOptional(), 1))); +} + +TEST(OptionalTest, TupleWorksInPointwise) { + std::vector> v = { + SampleOptional(1), SampleOptional(2), SampleOptional(3)}; + EXPECT_THAT(v, Pointwise(Optional(Eq()), {1, 2, 3})); +} + class SampleVariantIntString { public: SampleVariantIntString(int i) : i_(i), has_int_(true) {} -- cgit v1.2.3