Skip to content

Commit 238e474

Browse files
Abseil Teamcopybara-github
Abseil Team
authored andcommitted
Generalize gmock-matchers_test to handle is_gtest_matcher-style matchers, too.
PiperOrigin-RevId: 444586594 Change-Id: I0de9b40b3773e3047a492f050266967ea935ae3e
1 parent 0498660 commit 238e474

File tree

5 files changed

+144
-50
lines changed

5 files changed

+144
-50
lines changed

googlemock/test/gmock-matchers-arithmetic_test.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ void AllOfMatches(int num, const Matcher<int>& m) {
429429
EXPECT_TRUE(m.Matches(num + 1));
430430
}
431431

432+
INSTANTIATE_GTEST_MATCHER_TEST_P(AllOfTest);
433+
432434
// Tests that AllOf(m1, ..., mn) matches any value that matches all of
433435
// the given matchers.
434436
TEST(AllOfTest, MatchesWhenAllMatch) {
@@ -552,7 +554,7 @@ TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
552554
Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
553555
}
554556

555-
TEST(AllOfTest, ExplainsResult) {
557+
TEST_P(AllOfTestP, ExplainsResult) {
556558
Matcher<int> m;
557559

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

621+
INSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfTest);
622+
619623
// Tests that AnyOf(m1, ..., mn) matches any value that matches at
620624
// least one of the given matchers.
621625
TEST(AnyOfTest, MatchesWhenAnyMatches) {
@@ -766,7 +770,7 @@ TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
766770
Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
767771
}
768772

769-
TEST(AnyOfTest, ExplainsResult) {
773+
TEST_P(AnyOfTestP, ExplainsResult) {
770774
Matcher<int> m;
771775

772776
// Failed match. Both matchers need to explain. The second

googlemock/test/gmock-matchers-comparisons_test.cc

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ namespace testing {
4545
namespace gmock_matchers_test {
4646
namespace {
4747

48-
TEST(MonotonicMatcherTest, IsPrintable) {
48+
INSTANTIATE_GTEST_MATCHER_TEST_P(MonotonicMatcherTest);
49+
50+
TEST_P(MonotonicMatcherTestP, IsPrintable) {
4951
stringstream ss;
5052
ss << GreaterThan(5);
5153
EXPECT_EQ("is > 5", ss.str());
@@ -130,6 +132,8 @@ TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
130132
EXPECT_EQ("value % 2 == 1", Explain(m, 3));
131133
}
132134

135+
INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherTest);
136+
133137
// Tests default-constructing a matcher.
134138
TEST(MatcherTest, CanBeDefaultConstructed) { Matcher<double> m; }
135139

@@ -192,7 +196,7 @@ TEST(MatcherTest, CanDescribeItself) {
192196
}
193197

194198
// Tests Matcher<T>::MatchAndExplain().
195-
TEST(MatcherTest, MatchAndExplain) {
199+
TEST_P(MatcherTestP, MatchAndExplain) {
196200
Matcher<int> m = GreaterThan(0);
197201
StringMatchResultListener listener1;
198202
EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
@@ -376,11 +380,18 @@ TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
376380
EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
377381
}
378382

383+
INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherCastTest);
384+
379385
// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
380-
TEST(MatcherCastTest, FromPolymorphicMatcher) {
381-
Matcher<int> m = MatcherCast<int>(Eq(5));
382-
EXPECT_TRUE(m.Matches(5));
383-
EXPECT_FALSE(m.Matches(6));
386+
TEST_P(MatcherCastTestP, FromPolymorphicMatcher) {
387+
Matcher<int16_t> m;
388+
if (use_gtest_matcher_) {
389+
m = MatcherCast<int16_t>(GtestGreaterThan(int64_t{5}));
390+
} else {
391+
m = MatcherCast<int16_t>(Gt(int64_t{5}));
392+
}
393+
EXPECT_TRUE(m.Matches(6));
394+
EXPECT_FALSE(m.Matches(4));
384395
}
385396

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

592603
class OtherDerived : public Base {};
593604

605+
INSTANTIATE_GTEST_MATCHER_TEST_P(SafeMatcherCastTest);
606+
594607
// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
595-
TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
596-
Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
597-
EXPECT_TRUE(m2.Matches(' '));
608+
TEST_P(SafeMatcherCastTestP, FromPolymorphicMatcher) {
609+
Matcher<char> m2;
610+
if (use_gtest_matcher_) {
611+
m2 = SafeMatcherCast<char>(GtestGreaterThan(32));
612+
} else {
613+
m2 = SafeMatcherCast<char>(Gt(32));
614+
}
615+
EXPECT_TRUE(m2.Matches('A'));
598616
EXPECT_FALSE(m2.Matches('\n'));
599617
}
600618

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

1340+
INSTANTIATE_GTEST_MATCHER_TEST_P(KeyTest);
1341+
13221342
TEST(KeyTest, CanDescribeSelf) {
13231343
Matcher<const pair<std::string, int>&> m = Key("foo");
13241344
EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
13251345
EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
13261346
}
13271347

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

1369+
INSTANTIATE_GTEST_MATCHER_TEST_P(PairTest);
1370+
13491371
template <size_t I>
13501372
struct Tag {};
13511373

@@ -1434,7 +1456,7 @@ TEST(PairTest, CanDescribeSelf) {
14341456
DescribeNegation(m2));
14351457
}
14361458

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

1547+
INSTANTIATE_GTEST_MATCHER_TEST_P(FieldsAreTest);
1548+
15251549
TEST(FieldsAreTest, MatchesCorrectly) {
15261550
std::tuple<int, std::string, double> p(25, "foo", .5);
15271551

@@ -1547,7 +1571,7 @@ TEST(FieldsAreTest, CanDescribeSelf) {
15471571
DescribeNegation(m1));
15481572
}
15491573

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

2264-
TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
2288+
INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest);
2289+
2290+
TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) {
22652291
const Matcher<int> m = GreaterThan(5);
22662292
EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
22672293
}

googlemock/test/gmock-matchers-containers_test.cc

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ TEST(ContainsTest, WorksWithMoveOnly) {
6565
helper.Call(MakeUniquePtrs({1, 2}));
6666
}
6767

68+
INSTANTIATE_GTEST_MATCHER_TEST_P(ElementsAreTest);
69+
6870
// Tests the variadic version of the ElementsAreMatcher
6971
TEST(ElementsAreTest, HugeMatcher) {
7072
vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
@@ -280,6 +282,8 @@ class ConstPropagatingPtr {
280282
T* val_;
281283
};
282284

285+
INSTANTIATE_GTEST_MATCHER_TEST_P(PointeeTest);
286+
283287
TEST(PointeeTest, WorksWithConstPropagatingPointers) {
284288
const Matcher<ConstPropagatingPtr<int>> m = Pointee(Lt(5));
285289
int three = 3;
@@ -314,7 +318,7 @@ TEST(PointeeTest, CanDescribeSelf) {
314318
EXPECT_EQ("does not point to a value that is > 3", DescribeNegation(m));
315319
}
316320

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

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

377+
INSTANTIATE_GTEST_MATCHER_TEST_P(FieldTest);
378+
373379
// Tests that Field(&Foo::field, ...) works when field is non-const.
374380
TEST(FieldTest, WorksForNonConstField) {
375381
Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
@@ -476,7 +482,7 @@ TEST(FieldTest, CanDescribeSelfWithFieldName) {
476482
}
477483

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

482488
AStruct a;
@@ -489,7 +495,7 @@ TEST(FieldTest, CanExplainMatchResult) {
489495
Explain(m, a));
490496
}
491497

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

495501
AStruct a;
@@ -502,6 +508,8 @@ TEST(FieldTest, CanExplainMatchResultWithFieldName) {
502508
Explain(m, a));
503509
}
504510

511+
INSTANTIATE_GTEST_MATCHER_TEST_P(FieldForPointerTest);
512+
505513
// Tests that Field() works when the argument is a pointer to const.
506514
TEST(FieldForPointerTest, WorksForPointerToConst) {
507515
Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
@@ -568,7 +576,7 @@ TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
568576
}
569577

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

574582
AStruct a;
@@ -583,7 +591,7 @@ TEST(FieldForPointerTest, CanExplainMatchResult) {
583591
Explain(m, &a));
584592
}
585593

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

589597
AStruct a;
@@ -637,6 +645,8 @@ class DerivedClass : public AClass {
637645
int k_;
638646
};
639647

648+
INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyTest);
649+
640650
// Tests that Property(&Foo::property, ...) works when property()
641651
// returns a non-reference.
642652
TEST(PropertyTest, WorksForNonReferenceProperty) {
@@ -763,7 +773,7 @@ TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
763773
}
764774

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

769779
AClass a;
@@ -776,7 +786,7 @@ TEST(PropertyTest, CanExplainMatchResult) {
776786
Explain(m, a));
777787
}
778788

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

782792
AClass a;
@@ -789,6 +799,8 @@ TEST(PropertyTest, CanExplainMatchResultWithPropertyName) {
789799
Explain(m, a));
790800
}
791801

802+
INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyForPointerTest);
803+
792804
// Tests that Property() works when the argument is a pointer to const.
793805
TEST(PropertyForPointerTest, WorksForPointerToConst) {
794806
Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
@@ -865,7 +877,7 @@ TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
865877
}
866878

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

871883
AClass a;
@@ -881,7 +893,7 @@ TEST(PropertyForPointerTest, CanExplainMatchResult) {
881893
Explain(m, &a));
882894
}
883895

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

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

920+
INSTANTIATE_GTEST_MATCHER_TEST_P(ResultOfTest);
921+
908922
TEST(ResultOfTest, WorksForFunctionPointers) {
909923
Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string("foo")));
910924

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

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

953-
TEST(ResultOfTest, CanExplainMatchResultWithResultDescription) {
967+
TEST_P(ResultOfTestP, CanExplainMatchResultWithResultDescription) {
954968
Matcher<int> matcher = ResultOf("magic int conversion", &IntFunction, Ge(85));
955969
EXPECT_EQ("whose magic int conversion is 90" + OfType("int"),
956970
Explain(matcher, 36));
@@ -1408,6 +1422,8 @@ TEST(StreamlikeTest, Iteration) {
14081422
}
14091423
}
14101424

1425+
INSTANTIATE_GTEST_MATCHER_TEST_P(BeginEndDistanceIsTest);
1426+
14111427
TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
14121428
std::forward_list<int> container;
14131429
EXPECT_THAT(container, BeginEndDistanceIs(0));
@@ -1439,7 +1455,7 @@ TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
14391455
helper.Call(MakeUniquePtrs({1, 2}));
14401456
}
14411457

1442-
TEST(BeginEndDistanceIsTest, ExplainsResult) {
1458+
TEST_P(BeginEndDistanceIsTestP, ExplainsResult) {
14431459
Matcher<vector<int>> m1 = BeginEndDistanceIs(2);
14441460
Matcher<vector<int>> m2 = BeginEndDistanceIs(Lt(2));
14451461
Matcher<vector<int>> m3 = BeginEndDistanceIs(AnyOf(0, 3));
@@ -2103,7 +2119,9 @@ TEST_F(UnorderedElementsAreTest, DescribeNegation) {
21032119

21042120
// Tests Each().
21052121

2106-
TEST(EachTest, ExplainsMatchResultCorrectly) {
2122+
INSTANTIATE_GTEST_MATCHER_TEST_P(EachTest);
2123+
2124+
TEST_P(EachTestP, ExplainsMatchResultCorrectly) {
21072125
set<int> a; // empty
21082126

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

2597-
TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
2615+
TEST_P(ElementsAreTestP, ExplainsNonTrivialMatch) {
25982616
Matcher<const vector<int>&> m =
25992617
ElementsAre(GreaterThan(1), 0, GreaterThan(2));
26002618

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

2620-
TEST(ElementsAreTest, CanExplainMismatchRightSize) {
2638+
TEST_P(ElementsAreTestP, CanExplainMismatchRightSize) {
26212639
Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5));
26222640

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

29712989
// Tests Contains().
29722990

2991+
INSTANTIATE_GTEST_MATCHER_TEST_P(ContainsTest);
2992+
29732993
TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
29742994
list<int> some_list;
29752995
some_list.push_back(3);
@@ -3023,7 +3043,7 @@ TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
30233043
EXPECT_THAT(c_string_set, Not(Contains(std::string("goodbye"))));
30243044
}
30253045

3026-
TEST(ContainsTest, ExplainsMatchResultCorrectly) {
3046+
TEST_P(ContainsTestP, ExplainsMatchResultCorrectly) {
30273047
const int a[2] = {1, 2};
30283048
Matcher<const int(&)[2]> m = Contains(2);
30293049
EXPECT_EQ("whose element #1 matches", Explain(m, a));

0 commit comments

Comments
 (0)