Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: testing/gmock/test/gmock-generated-matchers_test.cc

Issue 1151006: Update to current gtest/gmock. (Closed)
Patch Set: rebase Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2008, Google Inc. 1 // Copyright 2008, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 using testing::Contains; 61 using testing::Contains;
62 using testing::ElementsAre; 62 using testing::ElementsAre;
63 using testing::ElementsAreArray; 63 using testing::ElementsAreArray;
64 using testing::Eq; 64 using testing::Eq;
65 using testing::Ge; 65 using testing::Ge;
66 using testing::Gt; 66 using testing::Gt;
67 using testing::Lt; 67 using testing::Lt;
68 using testing::MakeMatcher; 68 using testing::MakeMatcher;
69 using testing::Matcher; 69 using testing::Matcher;
70 using testing::MatcherInterface; 70 using testing::MatcherInterface;
71 using testing::MatchResultListener;
71 using testing::Ne; 72 using testing::Ne;
72 using testing::Not; 73 using testing::Not;
73 using testing::Pointee; 74 using testing::Pointee;
74 using testing::Ref; 75 using testing::Ref;
75 using testing::StaticAssertTypeEq; 76 using testing::StaticAssertTypeEq;
76 using testing::StrEq; 77 using testing::StrEq;
77 using testing::Value; 78 using testing::Value;
78 using testing::internal::string; 79 using testing::internal::string;
79 80
80 // Returns the description of the given matcher. 81 // Returns the description of the given matcher.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 EXPECT_THAT(t, (Args<0, 0>(Eq()))); 130 EXPECT_THAT(t, (Args<0, 0>(Eq())));
130 EXPECT_THAT(t, Not(Args<1, 1>(Ne()))); 131 EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
131 } 132 }
132 133
133 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) { 134 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
134 const tuple<short, int, long> t(4, 5, 6L); // NOLINT 135 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
135 EXPECT_THAT(t, (Args<2, 0>(Gt()))); 136 EXPECT_THAT(t, (Args<2, 0>(Gt())));
136 EXPECT_THAT(t, Not(Args<2, 1>(Lt()))); 137 EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
137 } 138 }
138 139
140 // The MATCHER*() macros trigger warning C4100 (unreferenced formal
141 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
142 // the macro definition, as the warnings are generated when the macro
143 // is expanded and macro expansion cannot contain #pragma. Therefore
144 // we suppress them here.
145 #ifdef _MSC_VER
146 #pragma warning(push)
147 #pragma warning(disable:4100)
148 #endif
149
139 MATCHER(SumIsZero, "") { 150 MATCHER(SumIsZero, "") {
140 return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0; 151 return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
141 } 152 }
142 153
143 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) { 154 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
144 EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero()))); 155 EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
145 EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero()))); 156 EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
146 } 157 }
147 158
148 TEST(ArgsTest, CanBeNested) { 159 TEST(ArgsTest, CanBeNested) {
(...skipping 11 matching lines...) Expand all
160 171
161 TEST(ArgsTest, CanMatchTupleByReference) { 172 TEST(ArgsTest, CanMatchTupleByReference) {
162 typedef tuple<char, char, int> Tuple3; 173 typedef tuple<char, char, int> Tuple3;
163 const Matcher<const Tuple3&> m = Args<0, 1>(Lt()); 174 const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
164 EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2))); 175 EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
165 EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2))); 176 EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
166 } 177 }
167 178
168 // Validates that arg is printed as str. 179 // Validates that arg is printed as str.
169 MATCHER_P(PrintsAs, str, "") { 180 MATCHER_P(PrintsAs, str, "") {
170 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(arg_type)) RawTuple; 181 return testing::PrintToString(arg) == str;
171 return
172 testing::internal::UniversalPrinter<RawTuple>::PrintToString(arg) == str;
173 } 182 }
174 183
175 TEST(ArgsTest, AcceptsTenTemplateArgs) { 184 TEST(ArgsTest, AcceptsTenTemplateArgs) {
176 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), 185 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
177 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( 186 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
178 PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); 187 PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
179 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), 188 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
180 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( 189 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
181 PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); 190 PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
182 } 191 }
(...skipping 17 matching lines...) Expand all
200 EXPECT_EQ("are a tuple whose fields (#1, #0) are a pair (x, y) " 209 EXPECT_EQ("are a tuple whose fields (#1, #0) are a pair (x, y) "
201 "where x > y is false", 210 "where x > y is false",
202 DescribeNegation(m)); 211 DescribeNegation(m));
203 } 212 }
204 213
205 // For testing ExplainMatchResultTo(). 214 // For testing ExplainMatchResultTo().
206 class GreaterThanMatcher : public MatcherInterface<int> { 215 class GreaterThanMatcher : public MatcherInterface<int> {
207 public: 216 public:
208 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} 217 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
209 218
210 virtual bool Matches(int lhs) const { return lhs > rhs_; }
211
212 virtual void DescribeTo(::std::ostream* os) const { 219 virtual void DescribeTo(::std::ostream* os) const {
213 *os << "is greater than " << rhs_; 220 *os << "is greater than " << rhs_;
214 } 221 }
215 222
216 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { 223 virtual bool MatchAndExplain(int lhs,
224 MatchResultListener* listener) const {
217 const int diff = lhs - rhs_; 225 const int diff = lhs - rhs_;
218 if (diff > 0) { 226 if (diff > 0) {
219 *os << "is " << diff << " more than " << rhs_; 227 *listener << "is " << diff << " more than " << rhs_;
220 } else if (diff == 0) { 228 } else if (diff == 0) {
221 *os << "is the same as " << rhs_; 229 *listener << "is the same as " << rhs_;
222 } else { 230 } else {
223 *os << "is " << -diff << " less than " << rhs_; 231 *listener << "is " << -diff << " less than " << rhs_;
224 } 232 }
233
234 return lhs > rhs_;
225 } 235 }
226 236
227 private: 237 private:
228 int rhs_; 238 int rhs_;
229 }; 239 };
230 240
231 Matcher<int> GreaterThan(int n) { 241 Matcher<int> GreaterThan(int n) {
232 return MakeMatcher(new GreaterThanMatcher(n)); 242 return MakeMatcher(new GreaterThanMatcher(n));
233 } 243 }
234 244
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 const Matcher<int> m = IsEven(); 556 const Matcher<int> m = IsEven();
547 EXPECT_TRUE(m.Matches(6)); 557 EXPECT_TRUE(m.Matches(6));
548 EXPECT_FALSE(m.Matches(7)); 558 EXPECT_FALSE(m.Matches(7));
549 559
550 EXPECT_EQ("is even", Describe(m)); 560 EXPECT_EQ("is even", Describe(m));
551 EXPECT_EQ("not (is even)", DescribeNegation(m)); 561 EXPECT_EQ("not (is even)", DescribeNegation(m));
552 EXPECT_EQ("", Explain(m, 6)); 562 EXPECT_EQ("", Explain(m, 6));
553 EXPECT_EQ("", Explain(m, 7)); 563 EXPECT_EQ("", Explain(m, 7));
554 } 564 }
555 565
566 // Tests explaining match result in a MATCHER* macro.
567
568 MATCHER(IsEven2, "is even") {
569 if ((arg % 2) == 0) {
570 // Verifies that we can stream to result_listener, a listener
571 // supplied by the MATCHER macro implicitly.
572 *result_listener << "OK";
573 return true;
574 } else {
575 *result_listener << "% 2 == " << (arg % 2);
576 return false;
577 }
578 }
579
580 MATCHER_P2(EqSumOf, x, y, "") {
581 if (arg == (x + y)) {
582 *result_listener << "OK";
583 return true;
584 } else {
585 // Verifies that we can stream to the underlying stream of
586 // result_listener.
587 if (result_listener->stream() != NULL) {
588 *result_listener->stream() << "diff == " << (x + y - arg);
589 }
590 return false;
591 }
592 }
593
594 TEST(MatcherMacroTest, CanExplainMatchResult) {
595 const Matcher<int> m1 = IsEven2();
596 EXPECT_EQ("OK", Explain(m1, 4));
597 EXPECT_EQ("% 2 == 1", Explain(m1, 5));
598
599 const Matcher<int> m2 = EqSumOf(1, 2);
600 EXPECT_EQ("OK", Explain(m2, 3));
601 EXPECT_EQ("diff == -1", Explain(m2, 4));
602 }
603
556 // Tests that the description string supplied to MATCHER() must be 604 // Tests that the description string supplied to MATCHER() must be
557 // valid. 605 // valid.
558 606
559 MATCHER(HasBadDescription, "Invalid%") { 607 MATCHER(HasBadDescription, "Invalid%") {
560 // Uses arg to suppress "unused parameter" warning. 608 // Uses arg to suppress "unused parameter" warning.
561 return arg==arg; 609 return arg==arg;
562 } 610 }
563 611
564 TEST(MatcherMacroTest, 612 TEST(MatcherMacroTest,
565 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { 613 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 } 1093 }
1046 1094
1047 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { 1095 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1048 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; 1096 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1049 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6))); 1097 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
1050 EXPECT_THAT(a, Contains(Contains(5))); 1098 EXPECT_THAT(a, Contains(Contains(5)));
1051 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); 1099 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
1052 EXPECT_THAT(a, Contains(Not(Contains(5)))); 1100 EXPECT_THAT(a, Contains(Not(Contains(5))));
1053 } 1101 }
1054 1102
1103 #ifdef _MSC_VER
1104 #pragma warning(pop)
1105 #endif
1106
1055 } // namespace 1107 } // namespace
OLDNEW
« no previous file with comments | « testing/gmock/src/gmock-spec-builders.cc ('k') | testing/gmock/test/gmock-internal-utils_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698