Skip to content

Commit

Permalink
Generalize gmock-matchers_test to handle is_gtest_matcher-style match…
Browse files Browse the repository at this point in the history
…ers, too.

PiperOrigin-RevId: 444586594
Change-Id: I0de9b40b3773e3047a492f050266967ea935ae3e
  • Loading branch information
Abseil Team authored and copybara-github committed Apr 26, 2022
1 parent 0498660 commit 238e474
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 50 deletions.
8 changes: 6 additions & 2 deletions googlemock/test/gmock-matchers-arithmetic_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ void AllOfMatches(int num, const Matcher<int>& m) {
EXPECT_TRUE(m.Matches(num + 1));
}

INSTANTIATE_GTEST_MATCHER_TEST_P(AllOfTest);

// Tests that AllOf(m1, ..., mn) matches any value that matches all of
// the given matchers.
TEST(AllOfTest, MatchesWhenAllMatch) {
Expand Down Expand Up @@ -552,7 +554,7 @@ TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
}

TEST(AllOfTest, ExplainsResult) {
TEST_P(AllOfTestP, ExplainsResult) {
Matcher<int> m;

// Successful match. Both matchers need to explain. The second
Expand Down Expand Up @@ -616,6 +618,8 @@ static void AnyOfStringMatches(int num, const Matcher<std::string>& m) {
EXPECT_FALSE(m.Matches(std::to_string(num + 1)));
}

INSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfTest);

// Tests that AnyOf(m1, ..., mn) matches any value that matches at
// least one of the given matchers.
TEST(AnyOfTest, MatchesWhenAnyMatches) {
Expand Down Expand Up @@ -766,7 +770,7 @@ TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
}

TEST(AnyOfTest, ExplainsResult) {
TEST_P(AnyOfTestP, ExplainsResult) {
Matcher<int> m;

// Failed match. Both matchers need to explain. The second
Expand Down
52 changes: 39 additions & 13 deletions googlemock/test/gmock-matchers-comparisons_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ namespace testing {
namespace gmock_matchers_test {
namespace {

TEST(MonotonicMatcherTest, IsPrintable) {
INSTANTIATE_GTEST_MATCHER_TEST_P(MonotonicMatcherTest);

TEST_P(MonotonicMatcherTestP, IsPrintable) {
stringstream ss;
ss << GreaterThan(5);
EXPECT_EQ("is > 5", ss.str());
Expand Down Expand Up @@ -130,6 +132,8 @@ TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
EXPECT_EQ("value % 2 == 1", Explain(m, 3));
}

INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherTest);

// Tests default-constructing a matcher.
TEST(MatcherTest, CanBeDefaultConstructed) { Matcher<double> m; }

Expand Down Expand Up @@ -192,7 +196,7 @@ TEST(MatcherTest, CanDescribeItself) {
}

// Tests Matcher<T>::MatchAndExplain().
TEST(MatcherTest, MatchAndExplain) {
TEST_P(MatcherTestP, MatchAndExplain) {
Matcher<int> m = GreaterThan(0);
StringMatchResultListener listener1;
EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
Expand Down Expand Up @@ -376,11 +380,18 @@ TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
}

INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherCastTest);

// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
TEST(MatcherCastTest, FromPolymorphicMatcher) {
Matcher<int> m = MatcherCast<int>(Eq(5));
EXPECT_TRUE(m.Matches(5));
EXPECT_FALSE(m.Matches(6));
TEST_P(MatcherCastTestP, FromPolymorphicMatcher) {
Matcher<int16_t> m;
if (use_gtest_matcher_) {
m = MatcherCast<int16_t>(GtestGreaterThan(int64_t{5}));
} else {
m = MatcherCast<int16_t>(Gt(int64_t{5}));
}
EXPECT_TRUE(m.Matches(6));
EXPECT_FALSE(m.Matches(4));
}

// For testing casting matchers between compatible types.
Expand Down Expand Up @@ -591,10 +602,17 @@ class Derived : public Base {

class OtherDerived : public Base {};

INSTANTIATE_GTEST_MATCHER_TEST_P(SafeMatcherCastTest);

// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
EXPECT_TRUE(m2.Matches(' '));
TEST_P(SafeMatcherCastTestP, FromPolymorphicMatcher) {
Matcher<char> m2;
if (use_gtest_matcher_) {
m2 = SafeMatcherCast<char>(GtestGreaterThan(32));
} else {
m2 = SafeMatcherCast<char>(Gt(32));
}
EXPECT_TRUE(m2.Matches('A'));
EXPECT_FALSE(m2.Matches('\n'));
}

Expand Down Expand Up @@ -1319,13 +1337,15 @@ TEST(HasSubstrTest, CanDescribeSelf) {
EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
}

INSTANTIATE_GTEST_MATCHER_TEST_P(KeyTest);

TEST(KeyTest, CanDescribeSelf) {
Matcher<const pair<std::string, int>&> m = Key("foo");
EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
}

TEST(KeyTest, ExplainsResult) {
TEST_P(KeyTestP, ExplainsResult) {
Matcher<pair<int, bool>> m = Key(GreaterThan(10));
EXPECT_EQ("whose first field is a value which is 5 less than 10",
Explain(m, make_pair(5, true)));
Expand All @@ -1346,6 +1366,8 @@ TEST(KeyTest, WorksWithMoveOnly) {
EXPECT_THAT(p, Key(Eq(nullptr)));
}

INSTANTIATE_GTEST_MATCHER_TEST_P(PairTest);

template <size_t I>
struct Tag {};

Expand Down Expand Up @@ -1434,7 +1456,7 @@ TEST(PairTest, CanDescribeSelf) {
DescribeNegation(m2));
}

TEST(PairTest, CanExplainMatchResultTo) {
TEST_P(PairTestP, CanExplainMatchResultTo) {
// If neither field matches, Pair() should explain about the first
// field.
const Matcher<pair<int, int>> m = Pair(GreaterThan(0), GreaterThan(0));
Expand Down Expand Up @@ -1522,6 +1544,8 @@ TEST(PairTest, InsideContainsUsingMap) {
EXPECT_THAT(container, Not(Contains(Pair(3, _))));
}

INSTANTIATE_GTEST_MATCHER_TEST_P(FieldsAreTest);

TEST(FieldsAreTest, MatchesCorrectly) {
std::tuple<int, std::string, double> p(25, "foo", .5);

Expand All @@ -1547,7 +1571,7 @@ TEST(FieldsAreTest, CanDescribeSelf) {
DescribeNegation(m1));
}

TEST(FieldsAreTest, CanExplainMatchResultTo) {
TEST_P(FieldsAreTestP, CanExplainMatchResultTo) {
// The first one that fails is the one that gives the error.
Matcher<std::tuple<int, int, int>> m =
FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));
Expand Down Expand Up @@ -2261,7 +2285,9 @@ TEST(ExplainMatchResultTest, AllOf_True_True_2) {
EXPECT_EQ("", Explain(m, 2));
}

TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest);

TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) {
const Matcher<int> m = GreaterThan(5);
EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
}
Expand Down
52 changes: 36 additions & 16 deletions googlemock/test/gmock-matchers-containers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ TEST(ContainsTest, WorksWithMoveOnly) {
helper.Call(MakeUniquePtrs({1, 2}));
}

INSTANTIATE_GTEST_MATCHER_TEST_P(ElementsAreTest);

// Tests the variadic version of the ElementsAreMatcher
TEST(ElementsAreTest, HugeMatcher) {
vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Expand Down Expand Up @@ -280,6 +282,8 @@ class ConstPropagatingPtr {
T* val_;
};

INSTANTIATE_GTEST_MATCHER_TEST_P(PointeeTest);

TEST(PointeeTest, WorksWithConstPropagatingPointers) {
const Matcher<ConstPropagatingPtr<int>> m = Pointee(Lt(5));
int three = 3;
Expand Down Expand Up @@ -314,7 +318,7 @@ TEST(PointeeTest, CanDescribeSelf) {
EXPECT_EQ("does not point to a value that is > 3", DescribeNegation(m));
}

TEST(PointeeTest, CanExplainMatchResult) {
TEST_P(PointeeTestP, CanExplainMatchResult) {
const Matcher<const std::string*> m = Pointee(StartsWith("Hi"));

EXPECT_EQ("", Explain(m, static_cast<const std::string*>(nullptr)));
Expand Down Expand Up @@ -370,6 +374,8 @@ struct DerivedStruct : public AStruct {
char ch;
};

INSTANTIATE_GTEST_MATCHER_TEST_P(FieldTest);

// Tests that Field(&Foo::field, ...) works when field is non-const.
TEST(FieldTest, WorksForNonConstField) {
Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
Expand Down Expand Up @@ -476,7 +482,7 @@ TEST(FieldTest, CanDescribeSelfWithFieldName) {
}

// Tests that Field() can explain the match result.
TEST(FieldTest, CanExplainMatchResult) {
TEST_P(FieldTestP, CanExplainMatchResult) {
Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));

AStruct a;
Expand All @@ -489,7 +495,7 @@ TEST(FieldTest, CanExplainMatchResult) {
Explain(m, a));
}

TEST(FieldTest, CanExplainMatchResultWithFieldName) {
TEST_P(FieldTestP, CanExplainMatchResultWithFieldName) {
Matcher<const AStruct&> m = Field("field_name", &AStruct::x, Ge(0));

AStruct a;
Expand All @@ -502,6 +508,8 @@ TEST(FieldTest, CanExplainMatchResultWithFieldName) {
Explain(m, a));
}

INSTANTIATE_GTEST_MATCHER_TEST_P(FieldForPointerTest);

// Tests that Field() works when the argument is a pointer to const.
TEST(FieldForPointerTest, WorksForPointerToConst) {
Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
Expand Down Expand Up @@ -568,7 +576,7 @@ TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
}

// Tests that Field() can explain the result of matching a pointer.
TEST(FieldForPointerTest, CanExplainMatchResult) {
TEST_P(FieldForPointerTestP, CanExplainMatchResult) {
Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));

AStruct a;
Expand All @@ -583,7 +591,7 @@ TEST(FieldForPointerTest, CanExplainMatchResult) {
Explain(m, &a));
}

TEST(FieldForPointerTest, CanExplainMatchResultWithFieldName) {
TEST_P(FieldForPointerTestP, CanExplainMatchResultWithFieldName) {
Matcher<const AStruct*> m = Field("field_name", &AStruct::x, Ge(0));

AStruct a;
Expand Down Expand Up @@ -637,6 +645,8 @@ class DerivedClass : public AClass {
int k_;
};

INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyTest);

// Tests that Property(&Foo::property, ...) works when property()
// returns a non-reference.
TEST(PropertyTest, WorksForNonReferenceProperty) {
Expand Down Expand Up @@ -763,7 +773,7 @@ TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
}

// Tests that Property() can explain the match result.
TEST(PropertyTest, CanExplainMatchResult) {
TEST_P(PropertyTestP, CanExplainMatchResult) {
Matcher<const AClass&> m = Property(&AClass::n, Ge(0));

AClass a;
Expand All @@ -776,7 +786,7 @@ TEST(PropertyTest, CanExplainMatchResult) {
Explain(m, a));
}

TEST(PropertyTest, CanExplainMatchResultWithPropertyName) {
TEST_P(PropertyTestP, CanExplainMatchResultWithPropertyName) {
Matcher<const AClass&> m = Property("fancy_name", &AClass::n, Ge(0));

AClass a;
Expand All @@ -789,6 +799,8 @@ TEST(PropertyTest, CanExplainMatchResultWithPropertyName) {
Explain(m, a));
}

INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyForPointerTest);

// Tests that Property() works when the argument is a pointer to const.
TEST(PropertyForPointerTest, WorksForPointerToConst) {
Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
Expand Down Expand Up @@ -865,7 +877,7 @@ TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
}

// Tests that Property() can explain the result of matching a pointer.
TEST(PropertyForPointerTest, CanExplainMatchResult) {
TEST_P(PropertyForPointerTestP, CanExplainMatchResult) {
Matcher<const AClass*> m = Property(&AClass::n, Ge(0));

AClass a;
Expand All @@ -881,7 +893,7 @@ TEST(PropertyForPointerTest, CanExplainMatchResult) {
Explain(m, &a));
}

TEST(PropertyForPointerTest, CanExplainMatchResultWithPropertyName) {
TEST_P(PropertyForPointerTestP, CanExplainMatchResultWithPropertyName) {
Matcher<const AClass*> m = Property("fancy_name", &AClass::n, Ge(0));

AClass a;
Expand All @@ -905,6 +917,8 @@ std::string IntToStringFunction(int input) {
return input == 1 ? "foo" : "bar";
}

INSTANTIATE_GTEST_MATCHER_TEST_P(ResultOfTest);

TEST(ResultOfTest, WorksForFunctionPointers) {
Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string("foo")));

Expand Down Expand Up @@ -939,7 +953,7 @@ TEST(ResultOfTest, CanDescribeItselfWithResultDescription) {
// Tests that ResultOf() can explain the match result.
int IntFunction(int input) { return input == 42 ? 80 : 90; }

TEST(ResultOfTest, CanExplainMatchResult) {
TEST_P(ResultOfTestP, CanExplainMatchResult) {
Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
Explain(matcher, 36));
Expand All @@ -950,7 +964,7 @@ TEST(ResultOfTest, CanExplainMatchResult) {
Explain(matcher, 36));
}

TEST(ResultOfTest, CanExplainMatchResultWithResultDescription) {
TEST_P(ResultOfTestP, CanExplainMatchResultWithResultDescription) {
Matcher<int> matcher = ResultOf("magic int conversion", &IntFunction, Ge(85));
EXPECT_EQ("whose magic int conversion is 90" + OfType("int"),
Explain(matcher, 36));
Expand Down Expand Up @@ -1408,6 +1422,8 @@ TEST(StreamlikeTest, Iteration) {
}
}

INSTANTIATE_GTEST_MATCHER_TEST_P(BeginEndDistanceIsTest);

TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
std::forward_list<int> container;
EXPECT_THAT(container, BeginEndDistanceIs(0));
Expand Down Expand Up @@ -1439,7 +1455,7 @@ TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
helper.Call(MakeUniquePtrs({1, 2}));
}

TEST(BeginEndDistanceIsTest, ExplainsResult) {
TEST_P(BeginEndDistanceIsTestP, ExplainsResult) {
Matcher<vector<int>> m1 = BeginEndDistanceIs(2);
Matcher<vector<int>> m2 = BeginEndDistanceIs(Lt(2));
Matcher<vector<int>> m3 = BeginEndDistanceIs(AnyOf(0, 3));
Expand Down Expand Up @@ -2103,7 +2119,9 @@ TEST_F(UnorderedElementsAreTest, DescribeNegation) {

// Tests Each().

TEST(EachTest, ExplainsMatchResultCorrectly) {
INSTANTIATE_GTEST_MATCHER_TEST_P(EachTest);

TEST_P(EachTestP, ExplainsMatchResultCorrectly) {
set<int> a; // empty

Matcher<set<int>> m = Each(2);
Expand Down Expand Up @@ -2594,7 +2612,7 @@ TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
}

TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
TEST_P(ElementsAreTestP, ExplainsNonTrivialMatch) {
Matcher<const vector<int>&> m =
ElementsAre(GreaterThan(1), 0, GreaterThan(2));

Expand All @@ -2617,7 +2635,7 @@ TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
EXPECT_EQ("which has 1 element", Explain(m, test_list));
}

TEST(ElementsAreTest, CanExplainMismatchRightSize) {
TEST_P(ElementsAreTestP, CanExplainMismatchRightSize) {
Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5));

vector<int> v;
Expand Down Expand Up @@ -2970,6 +2988,8 @@ TEST(ElementsAreArrayTest, SourceLifeSpan) {

// Tests Contains().

INSTANTIATE_GTEST_MATCHER_TEST_P(ContainsTest);

TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
list<int> some_list;
some_list.push_back(3);
Expand Down Expand Up @@ -3023,7 +3043,7 @@ TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
EXPECT_THAT(c_string_set, Not(Contains(std::string("goodbye"))));
}

TEST(ContainsTest, ExplainsMatchResultCorrectly) {
TEST_P(ContainsTestP, ExplainsMatchResultCorrectly) {
const int a[2] = {1, 2};
Matcher<const int(&)[2]> m = Contains(2);
EXPECT_EQ("whose element #1 matches", Explain(m, a));
Expand Down
Loading

0 comments on commit 238e474

Please sign in to comment.