OLD | NEW |
(Empty) | |
| 1 // Copyright 2008, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 // Google Mock - a framework for writing C++ mock classes. |
| 31 // |
| 32 // This file tests the built-in matchers generated by a script. |
| 33 |
| 34 #include <gmock/gmock-generated-matchers.h> |
| 35 |
| 36 #include <list> |
| 37 #include <map> |
| 38 #include <set> |
| 39 #include <sstream> |
| 40 #include <string> |
| 41 #include <utility> |
| 42 #include <vector> |
| 43 |
| 44 #include <gmock/gmock.h> |
| 45 #include <gtest/gtest.h> |
| 46 #include <gtest/gtest-spi.h> |
| 47 |
| 48 namespace { |
| 49 |
| 50 using std::list; |
| 51 using std::map; |
| 52 using std::pair; |
| 53 using std::set; |
| 54 using std::stringstream; |
| 55 using std::vector; |
| 56 using testing::_; |
| 57 using testing::Contains; |
| 58 using testing::ElementsAre; |
| 59 using testing::ElementsAreArray; |
| 60 using testing::Eq; |
| 61 using testing::Ge; |
| 62 using testing::Gt; |
| 63 using testing::MakeMatcher; |
| 64 using testing::Matcher; |
| 65 using testing::MatcherInterface; |
| 66 using testing::Ne; |
| 67 using testing::Not; |
| 68 using testing::Pointee; |
| 69 using testing::Ref; |
| 70 using testing::StaticAssertTypeEq; |
| 71 using testing::StrEq; |
| 72 using testing::internal::string; |
| 73 |
| 74 // Returns the description of the given matcher. |
| 75 template <typename T> |
| 76 string Describe(const Matcher<T>& m) { |
| 77 stringstream ss; |
| 78 m.DescribeTo(&ss); |
| 79 return ss.str(); |
| 80 } |
| 81 |
| 82 // Returns the description of the negation of the given matcher. |
| 83 template <typename T> |
| 84 string DescribeNegation(const Matcher<T>& m) { |
| 85 stringstream ss; |
| 86 m.DescribeNegationTo(&ss); |
| 87 return ss.str(); |
| 88 } |
| 89 |
| 90 // Returns the reason why x matches, or doesn't match, m. |
| 91 template <typename MatcherType, typename Value> |
| 92 string Explain(const MatcherType& m, const Value& x) { |
| 93 stringstream ss; |
| 94 m.ExplainMatchResultTo(x, &ss); |
| 95 return ss.str(); |
| 96 } |
| 97 |
| 98 // For testing ExplainMatchResultTo(). |
| 99 class GreaterThanMatcher : public MatcherInterface<int> { |
| 100 public: |
| 101 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
| 102 |
| 103 virtual bool Matches(int lhs) const { return lhs > rhs_; } |
| 104 |
| 105 virtual void DescribeTo(::std::ostream* os) const { |
| 106 *os << "is greater than " << rhs_; |
| 107 } |
| 108 |
| 109 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { |
| 110 const int diff = lhs - rhs_; |
| 111 if (diff > 0) { |
| 112 *os << "is " << diff << " more than " << rhs_; |
| 113 } else if (diff == 0) { |
| 114 *os << "is the same as " << rhs_; |
| 115 } else { |
| 116 *os << "is " << -diff << " less than " << rhs_; |
| 117 } |
| 118 } |
| 119 private: |
| 120 const int rhs_; |
| 121 }; |
| 122 |
| 123 Matcher<int> GreaterThan(int n) { |
| 124 return MakeMatcher(new GreaterThanMatcher(n)); |
| 125 } |
| 126 |
| 127 // Tests for ElementsAre(). |
| 128 |
| 129 // Evaluates to the number of elements in 'array'. |
| 130 #define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0])) |
| 131 |
| 132 TEST(ElementsAreTest, CanDescribeExpectingNoElement) { |
| 133 Matcher<const vector<int>&> m = ElementsAre(); |
| 134 EXPECT_EQ("is empty", Describe(m)); |
| 135 } |
| 136 |
| 137 TEST(ElementsAreTest, CanDescribeExpectingOneElement) { |
| 138 Matcher<vector<int> > m = ElementsAre(Gt(5)); |
| 139 EXPECT_EQ("has 1 element that is greater than 5", Describe(m)); |
| 140 } |
| 141 |
| 142 TEST(ElementsAreTest, CanDescribeExpectingManyElements) { |
| 143 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two"); |
| 144 EXPECT_EQ("has 2 elements where\n" |
| 145 "element 0 is equal to \"one\",\n" |
| 146 "element 1 is equal to \"two\"", Describe(m)); |
| 147 } |
| 148 |
| 149 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) { |
| 150 Matcher<vector<int> > m = ElementsAre(); |
| 151 EXPECT_EQ("is not empty", DescribeNegation(m)); |
| 152 } |
| 153 |
| 154 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) { |
| 155 Matcher<const list<int>& > m = ElementsAre(Gt(5)); |
| 156 EXPECT_EQ("does not have 1 element, or\n" |
| 157 "element 0 is not greater than 5", DescribeNegation(m)); |
| 158 } |
| 159 |
| 160 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) { |
| 161 Matcher<const list<string>& > m = ElementsAre("one", "two"); |
| 162 EXPECT_EQ("does not have 2 elements, or\n" |
| 163 "element 0 is not equal to \"one\", or\n" |
| 164 "element 1 is not equal to \"two\"", DescribeNegation(m)); |
| 165 } |
| 166 |
| 167 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) { |
| 168 Matcher<const list<int>& > m = ElementsAre(1, Ne(2)); |
| 169 |
| 170 list<int> test_list; |
| 171 test_list.push_back(1); |
| 172 test_list.push_back(3); |
| 173 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything. |
| 174 } |
| 175 |
| 176 TEST(ElementsAreTest, ExplainsNonTrivialMatch) { |
| 177 Matcher<const vector<int>& > m = |
| 178 ElementsAre(GreaterThan(1), 0, GreaterThan(2)); |
| 179 |
| 180 const int a[] = { 10, 0, 100 }; |
| 181 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 182 EXPECT_EQ("element 0 is 9 more than 1,\n" |
| 183 "element 2 is 98 more than 2", Explain(m, test_vector)); |
| 184 } |
| 185 |
| 186 TEST(ElementsAreTest, CanExplainMismatchWrongSize) { |
| 187 Matcher<const list<int>& > m = ElementsAre(1, 3); |
| 188 |
| 189 list<int> test_list; |
| 190 // No need to explain when the container is empty. |
| 191 EXPECT_EQ("", Explain(m, test_list)); |
| 192 |
| 193 test_list.push_back(1); |
| 194 EXPECT_EQ("has 1 element", Explain(m, test_list)); |
| 195 } |
| 196 |
| 197 TEST(ElementsAreTest, CanExplainMismatchRightSize) { |
| 198 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5)); |
| 199 |
| 200 vector<int> v; |
| 201 v.push_back(2); |
| 202 v.push_back(1); |
| 203 EXPECT_EQ("element 0 doesn't match", Explain(m, v)); |
| 204 |
| 205 v[0] = 1; |
| 206 EXPECT_EQ("element 1 doesn't match (is 4 less than 5)", Explain(m, v)); |
| 207 } |
| 208 |
| 209 TEST(ElementsAreTest, MatchesOneElementVector) { |
| 210 vector<string> test_vector; |
| 211 test_vector.push_back("test string"); |
| 212 |
| 213 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string"))); |
| 214 } |
| 215 |
| 216 TEST(ElementsAreTest, MatchesOneElementList) { |
| 217 list<string> test_list; |
| 218 test_list.push_back("test string"); |
| 219 |
| 220 EXPECT_THAT(test_list, ElementsAre("test string")); |
| 221 } |
| 222 |
| 223 TEST(ElementsAreTest, MatchesThreeElementVector) { |
| 224 vector<string> test_vector; |
| 225 test_vector.push_back("one"); |
| 226 test_vector.push_back("two"); |
| 227 test_vector.push_back("three"); |
| 228 |
| 229 EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _)); |
| 230 } |
| 231 |
| 232 TEST(ElementsAreTest, MatchesOneElementEqMatcher) { |
| 233 vector<int> test_vector; |
| 234 test_vector.push_back(4); |
| 235 |
| 236 EXPECT_THAT(test_vector, ElementsAre(Eq(4))); |
| 237 } |
| 238 |
| 239 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) { |
| 240 vector<int> test_vector; |
| 241 test_vector.push_back(4); |
| 242 |
| 243 EXPECT_THAT(test_vector, ElementsAre(_)); |
| 244 } |
| 245 |
| 246 TEST(ElementsAreTest, MatchesOneElementValue) { |
| 247 vector<int> test_vector; |
| 248 test_vector.push_back(4); |
| 249 |
| 250 EXPECT_THAT(test_vector, ElementsAre(4)); |
| 251 } |
| 252 |
| 253 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) { |
| 254 vector<int> test_vector; |
| 255 test_vector.push_back(1); |
| 256 test_vector.push_back(2); |
| 257 test_vector.push_back(3); |
| 258 |
| 259 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _)); |
| 260 } |
| 261 |
| 262 TEST(ElementsAreTest, MatchesTenElementVector) { |
| 263 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 264 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 265 |
| 266 EXPECT_THAT(test_vector, |
| 267 // The element list can contain values and/or matchers |
| 268 // of different types. |
| 269 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _)); |
| 270 } |
| 271 |
| 272 TEST(ElementsAreTest, DoesNotMatchWrongSize) { |
| 273 vector<string> test_vector; |
| 274 test_vector.push_back("test string"); |
| 275 test_vector.push_back("test string"); |
| 276 |
| 277 Matcher<vector<string> > m = ElementsAre(StrEq("test string")); |
| 278 EXPECT_FALSE(m.Matches(test_vector)); |
| 279 } |
| 280 |
| 281 TEST(ElementsAreTest, DoesNotMatchWrongValue) { |
| 282 vector<string> test_vector; |
| 283 test_vector.push_back("other string"); |
| 284 |
| 285 Matcher<vector<string> > m = ElementsAre(StrEq("test string")); |
| 286 EXPECT_FALSE(m.Matches(test_vector)); |
| 287 } |
| 288 |
| 289 TEST(ElementsAreTest, DoesNotMatchWrongOrder) { |
| 290 vector<string> test_vector; |
| 291 test_vector.push_back("one"); |
| 292 test_vector.push_back("three"); |
| 293 test_vector.push_back("two"); |
| 294 |
| 295 Matcher<vector<string> > m = ElementsAre( |
| 296 StrEq("one"), StrEq("two"), StrEq("three")); |
| 297 EXPECT_FALSE(m.Matches(test_vector)); |
| 298 } |
| 299 |
| 300 TEST(ElementsAreTest, WorksForNestedContainer) { |
| 301 const char* strings[] = { |
| 302 "Hi", |
| 303 "world" |
| 304 }; |
| 305 |
| 306 vector<list<char> > nested; |
| 307 for (int i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) { |
| 308 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i]))); |
| 309 } |
| 310 |
| 311 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')), |
| 312 ElementsAre('w', 'o', _, _, 'd'))); |
| 313 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'), |
| 314 ElementsAre('w', 'o', _, _, 'd')))); |
| 315 } |
| 316 |
| 317 TEST(ElementsAreTest, WorksWithByRefElementMatchers) { |
| 318 int a[] = { 0, 1, 2 }; |
| 319 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 320 |
| 321 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2]))); |
| 322 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2])))); |
| 323 } |
| 324 |
| 325 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { |
| 326 int a[] = { 0, 1, 2 }; |
| 327 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 328 |
| 329 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); |
| 330 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); |
| 331 } |
| 332 |
| 333 // Tests for ElementsAreArray(). Since ElementsAreArray() shares most |
| 334 // of the implementation with ElementsAre(), we don't test it as |
| 335 // thoroughly here. |
| 336 |
| 337 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { |
| 338 const int a[] = { 1, 2, 3 }; |
| 339 |
| 340 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 341 EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 342 |
| 343 test_vector[2] = 0; |
| 344 EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
| 345 } |
| 346 |
| 347 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) { |
| 348 const char* a[] = { "one", "two", "three" }; |
| 349 |
| 350 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 351 EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a))); |
| 352 |
| 353 const char** p = a; |
| 354 test_vector[0] = "1"; |
| 355 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a)))); |
| 356 } |
| 357 |
| 358 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) { |
| 359 const char* a[] = { "one", "two", "three" }; |
| 360 |
| 361 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 362 EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 363 |
| 364 test_vector[0] = "1"; |
| 365 EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
| 366 } |
| 367 |
| 368 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { |
| 369 const Matcher<string> kMatcherArray[] = |
| 370 { StrEq("one"), StrEq("two"), StrEq("three") }; |
| 371 |
| 372 vector<string> test_vector; |
| 373 test_vector.push_back("one"); |
| 374 test_vector.push_back("two"); |
| 375 test_vector.push_back("three"); |
| 376 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray)); |
| 377 |
| 378 test_vector.push_back("three"); |
| 379 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); |
| 380 } |
| 381 |
| 382 // Tests for the MATCHER*() macro family. |
| 383 |
| 384 // Tests that a simple MATCHER() definition works. |
| 385 |
| 386 MATCHER(IsEven, "") { return (arg % 2) == 0; } |
| 387 |
| 388 TEST(MatcherMacroTest, Works) { |
| 389 const Matcher<int> m = IsEven(); |
| 390 EXPECT_TRUE(m.Matches(6)); |
| 391 EXPECT_FALSE(m.Matches(7)); |
| 392 |
| 393 EXPECT_EQ("is even", Describe(m)); |
| 394 EXPECT_EQ("not (is even)", DescribeNegation(m)); |
| 395 EXPECT_EQ("", Explain(m, 6)); |
| 396 EXPECT_EQ("", Explain(m, 7)); |
| 397 } |
| 398 |
| 399 // Tests that the description string supplied to MATCHER() must be |
| 400 // valid. |
| 401 |
| 402 MATCHER(HasBadDescription, "Invalid%") { return true; } |
| 403 |
| 404 TEST(MatcherMacroTest, |
| 405 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
| 406 EXPECT_NONFATAL_FAILURE( |
| 407 HasBadDescription(), |
| 408 "Syntax error at index 7 in matcher description \"Invalid%\": " |
| 409 "use \"%%\" instead of \"%\" to print \"%\"."); |
| 410 } |
| 411 |
| 412 MATCHER(HasGoodDescription, "good") { return true; } |
| 413 |
| 414 TEST(MatcherMacroTest, AcceptsValidDescription) { |
| 415 const Matcher<int> m = HasGoodDescription(); |
| 416 EXPECT_EQ("good", Describe(m)); |
| 417 } |
| 418 |
| 419 // Tests that the body of MATCHER() can reference the type of the |
| 420 // value being matched. |
| 421 |
| 422 MATCHER(IsEmptyString, "") { |
| 423 StaticAssertTypeEq< ::std::string, arg_type>(); |
| 424 return arg == ""; |
| 425 } |
| 426 |
| 427 MATCHER(IsEmptyStringByRef, "") { |
| 428 StaticAssertTypeEq<const ::std::string&, arg_type>(); |
| 429 return arg == ""; |
| 430 } |
| 431 |
| 432 TEST(MatcherMacroTest, CanReferenceArgType) { |
| 433 const Matcher< ::std::string> m1 = IsEmptyString(); |
| 434 EXPECT_TRUE(m1.Matches("")); |
| 435 |
| 436 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef(); |
| 437 EXPECT_TRUE(m2.Matches("")); |
| 438 } |
| 439 |
| 440 // Tests that MATCHER() can be used in a namespace. |
| 441 |
| 442 namespace matcher_test { |
| 443 MATCHER(IsOdd, "") { return (arg % 2) != 0; } |
| 444 } // namespace matcher_test |
| 445 |
| 446 TEST(MatcherTest, WorksInNamespace) { |
| 447 Matcher<int> m = matcher_test::IsOdd(); |
| 448 EXPECT_FALSE(m.Matches(4)); |
| 449 EXPECT_TRUE(m.Matches(5)); |
| 450 } |
| 451 |
| 452 // Tests that a simple MATCHER_P() definition works. |
| 453 |
| 454 MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; } |
| 455 |
| 456 TEST(MatcherPMacroTest, Works) { |
| 457 const Matcher<int> m = IsGreaterThan32And(5); |
| 458 EXPECT_TRUE(m.Matches(36)); |
| 459 EXPECT_FALSE(m.Matches(5)); |
| 460 |
| 461 EXPECT_EQ("is greater than 32 and 5", Describe(m)); |
| 462 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m)); |
| 463 EXPECT_EQ("", Explain(m, 36)); |
| 464 EXPECT_EQ("", Explain(m, 5)); |
| 465 } |
| 466 |
| 467 // Tests that the description string supplied to MATCHER_P() must be |
| 468 // valid. |
| 469 |
| 470 MATCHER_P(HasBadDescription1, n, "not %(m)s good") { |
| 471 return arg > n; |
| 472 } |
| 473 |
| 474 TEST(MatcherPMacroTest, |
| 475 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
| 476 EXPECT_NONFATAL_FAILURE( |
| 477 HasBadDescription1(2), |
| 478 "Syntax error at index 6 in matcher description \"not %(m)s good\": " |
| 479 "\"m\" is an invalid parameter name."); |
| 480 } |
| 481 |
| 482 |
| 483 MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return true; } |
| 484 |
| 485 TEST(MatcherPMacroTest, AcceptsValidDescription) { |
| 486 const Matcher<int> m = HasGoodDescription1(5); |
| 487 EXPECT_EQ("good 5", Describe(m)); |
| 488 } |
| 489 |
| 490 // Tests that the description is calculated correctly from the matcher name. |
| 491 MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; } |
| 492 |
| 493 TEST(MatcherPMacroTest, GeneratesCorrectDescription) { |
| 494 const Matcher<int> m = _is_Greater_Than32and_(5); |
| 495 |
| 496 EXPECT_EQ("is greater than 32 and 5", Describe(m)); |
| 497 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m)); |
| 498 EXPECT_EQ("", Explain(m, 36)); |
| 499 EXPECT_EQ("", Explain(m, 5)); |
| 500 } |
| 501 |
| 502 // Tests that a MATCHER_P matcher can be explicitly instantiated with |
| 503 // a reference parameter type. |
| 504 |
| 505 class UncopyableFoo { |
| 506 public: |
| 507 explicit UncopyableFoo(char value) : value_(value) {} |
| 508 private: |
| 509 UncopyableFoo(const UncopyableFoo&); |
| 510 void operator=(const UncopyableFoo&); |
| 511 |
| 512 char value_; |
| 513 }; |
| 514 |
| 515 MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; } |
| 516 |
| 517 TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) { |
| 518 UncopyableFoo foo1('1'), foo2('2'); |
| 519 const Matcher<const UncopyableFoo&> m = |
| 520 ReferencesUncopyable<const UncopyableFoo&>(foo1); |
| 521 |
| 522 EXPECT_TRUE(m.Matches(foo1)); |
| 523 EXPECT_FALSE(m.Matches(foo2)); |
| 524 |
| 525 // We don't want the address of the parameter printed, as most |
| 526 // likely it will just annoy the user. If the address is |
| 527 // interesting, the user should consider passing the parameter by |
| 528 // pointer instead. |
| 529 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m)); |
| 530 } |
| 531 |
| 532 |
| 533 // Tests that the description string supplied to MATCHER_Pn() must be |
| 534 // valid. |
| 535 |
| 536 MATCHER_P2(HasBadDescription2, m, n, "not %(good") { |
| 537 return arg > m + n; |
| 538 } |
| 539 |
| 540 TEST(MatcherPnMacroTest, |
| 541 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
| 542 EXPECT_NONFATAL_FAILURE( |
| 543 HasBadDescription2(3, 4), |
| 544 "Syntax error at index 4 in matcher description \"not %(good\": " |
| 545 "an interpolation must end with \")s\", but \"%(good\" does not."); |
| 546 } |
| 547 |
| 548 MATCHER_P2(HasComplexDescription, foo, bar, |
| 549 "is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") { |
| 550 return true; |
| 551 } |
| 552 |
| 553 TEST(MatcherPnMacroTest, AcceptsValidDescription) { |
| 554 Matcher<int> m = HasComplexDescription(100, "ducks"); |
| 555 EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)", |
| 556 Describe(m)); |
| 557 } |
| 558 |
| 559 // Tests that the body of MATCHER_Pn() can reference the parameter |
| 560 // types. |
| 561 |
| 562 MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") { |
| 563 StaticAssertTypeEq<int, foo_type>(); |
| 564 StaticAssertTypeEq<long, bar_type>(); // NOLINT |
| 565 StaticAssertTypeEq<char, baz_type>(); |
| 566 return arg == 0; |
| 567 } |
| 568 |
| 569 TEST(MatcherPnMacroTest, CanReferenceParamTypes) { |
| 570 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a')); |
| 571 } |
| 572 |
| 573 // Tests that a MATCHER_Pn matcher can be explicitly instantiated with |
| 574 // reference parameter types. |
| 575 |
| 576 MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") { |
| 577 return &arg == &variable1 || &arg == &variable2; |
| 578 } |
| 579 |
| 580 TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) { |
| 581 UncopyableFoo foo1('1'), foo2('2'), foo3('3'); |
| 582 const Matcher<const UncopyableFoo&> m = |
| 583 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); |
| 584 |
| 585 EXPECT_TRUE(m.Matches(foo1)); |
| 586 EXPECT_TRUE(m.Matches(foo2)); |
| 587 EXPECT_FALSE(m.Matches(foo3)); |
| 588 } |
| 589 |
| 590 TEST(MatcherPnMacroTest, |
| 591 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) { |
| 592 UncopyableFoo foo1('1'), foo2('2'); |
| 593 const Matcher<const UncopyableFoo&> m = |
| 594 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); |
| 595 |
| 596 // We don't want the addresses of the parameters printed, as most |
| 597 // likely they will just annoy the user. If the addresses are |
| 598 // interesting, the user should consider passing the parameters by |
| 599 // pointers instead. |
| 600 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)", |
| 601 Describe(m)); |
| 602 } |
| 603 |
| 604 // Tests that a simple MATCHER_P2() definition works. |
| 605 |
| 606 MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; } |
| 607 |
| 608 TEST(MatcherPnMacroTest, Works) { |
| 609 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT |
| 610 EXPECT_TRUE(m.Matches(36L)); |
| 611 EXPECT_FALSE(m.Matches(15L)); |
| 612 |
| 613 EXPECT_EQ("is not in closed range (10, 20)", Describe(m)); |
| 614 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m)); |
| 615 EXPECT_EQ("", Explain(m, 36L)); |
| 616 EXPECT_EQ("", Explain(m, 15L)); |
| 617 } |
| 618 |
| 619 // Tests that MATCHER*() definitions can be overloaded on the number |
| 620 // of parameters; also tests MATCHER_Pn() where n >= 3. |
| 621 |
| 622 MATCHER(EqualsSumOf, "") { return arg == 0; } |
| 623 MATCHER_P(EqualsSumOf, a, "") { return arg == a; } |
| 624 MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; } |
| 625 MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; } |
| 626 MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; } |
| 627 MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; } |
| 628 MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") { |
| 629 return arg == a + b + c + d + e + f; |
| 630 } |
| 631 MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") { |
| 632 return arg == a + b + c + d + e + f + g; |
| 633 } |
| 634 MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") { |
| 635 return arg == a + b + c + d + e + f + g + h; |
| 636 } |
| 637 MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") { |
| 638 return arg == a + b + c + d + e + f + g + h + i; |
| 639 } |
| 640 MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") { |
| 641 return arg == a + b + c + d + e + f + g + h + i + j; |
| 642 } |
| 643 |
| 644 TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) { |
| 645 EXPECT_THAT(0, EqualsSumOf()); |
| 646 EXPECT_THAT(1, EqualsSumOf(1)); |
| 647 EXPECT_THAT(12, EqualsSumOf(10, 2)); |
| 648 EXPECT_THAT(123, EqualsSumOf(100, 20, 3)); |
| 649 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4)); |
| 650 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5)); |
| 651 EXPECT_THAT("abcdef", |
| 652 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')); |
| 653 EXPECT_THAT("abcdefg", |
| 654 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g')); |
| 655 EXPECT_THAT("abcdefgh", |
| 656 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 657 "h")); |
| 658 EXPECT_THAT("abcdefghi", |
| 659 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 660 "h", 'i')); |
| 661 EXPECT_THAT("abcdefghij", |
| 662 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 663 "h", 'i', ::std::string("j"))); |
| 664 |
| 665 EXPECT_THAT(1, Not(EqualsSumOf())); |
| 666 EXPECT_THAT(-1, Not(EqualsSumOf(1))); |
| 667 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2))); |
| 668 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3))); |
| 669 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4))); |
| 670 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5))); |
| 671 EXPECT_THAT("abcdef ", |
| 672 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'))); |
| 673 EXPECT_THAT("abcdefg ", |
| 674 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', |
| 675 'g'))); |
| 676 EXPECT_THAT("abcdefgh ", |
| 677 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 678 "h"))); |
| 679 EXPECT_THAT("abcdefghi ", |
| 680 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 681 "h", 'i'))); |
| 682 EXPECT_THAT("abcdefghij ", |
| 683 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 684 "h", 'i', ::std::string("j")))); |
| 685 } |
| 686 |
| 687 // Tests that a MATCHER_Pn() definition can be instantiated with any |
| 688 // compatible parameter types. |
| 689 TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) { |
| 690 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3))); |
| 691 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d")); |
| 692 |
| 693 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3)))); |
| 694 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d"))); |
| 695 } |
| 696 |
| 697 // Tests that the matcher body can promote the parameter types. |
| 698 |
| 699 MATCHER_P2(EqConcat, prefix, suffix, "") { |
| 700 // The following lines promote the two parameters to desired types. |
| 701 std::string prefix_str(prefix); |
| 702 char suffix_char(suffix); |
| 703 return arg == prefix_str + suffix_char; |
| 704 } |
| 705 |
| 706 TEST(MatcherPnMacroTest, SimpleTypePromotion) { |
| 707 Matcher<std::string> no_promo = |
| 708 EqConcat(std::string("foo"), 't'); |
| 709 Matcher<const std::string&> promo = |
| 710 EqConcat("foo", static_cast<int>('t')); |
| 711 EXPECT_FALSE(no_promo.Matches("fool")); |
| 712 EXPECT_FALSE(promo.Matches("fool")); |
| 713 EXPECT_TRUE(no_promo.Matches("foot")); |
| 714 EXPECT_TRUE(promo.Matches("foot")); |
| 715 } |
| 716 |
| 717 // Verifies the type of a MATCHER*. |
| 718 |
| 719 TEST(MatcherPnMacroTest, TypesAreCorrect) { |
| 720 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable. |
| 721 EqualsSumOfMatcher a0 = EqualsSumOf(); |
| 722 |
| 723 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable. |
| 724 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1); |
| 725 |
| 726 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk |
| 727 // variable, and so on. |
| 728 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2'); |
| 729 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3'); |
| 730 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4'); |
| 731 EqualsSumOfMatcherP5<int, int, int, int, char> a5 = |
| 732 EqualsSumOf(1, 2, 3, 4, '5'); |
| 733 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 = |
| 734 EqualsSumOf(1, 2, 3, 4, 5, '6'); |
| 735 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 = |
| 736 EqualsSumOf(1, 2, 3, 4, 5, 6, '7'); |
| 737 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 = |
| 738 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8'); |
| 739 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 = |
| 740 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9'); |
| 741 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 = |
| 742 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); |
| 743 } |
| 744 |
| 745 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) { |
| 746 list<int> some_list; |
| 747 some_list.push_back(3); |
| 748 some_list.push_back(1); |
| 749 some_list.push_back(2); |
| 750 EXPECT_THAT(some_list, Contains(1)); |
| 751 EXPECT_THAT(some_list, Contains(3.0)); |
| 752 EXPECT_THAT(some_list, Contains(2.0f)); |
| 753 |
| 754 list<string> another_list; |
| 755 another_list.push_back("fee"); |
| 756 another_list.push_back("fie"); |
| 757 another_list.push_back("foe"); |
| 758 another_list.push_back("fum"); |
| 759 EXPECT_THAT(another_list, Contains(string("fee"))); |
| 760 } |
| 761 |
| 762 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) { |
| 763 list<int> some_list; |
| 764 some_list.push_back(3); |
| 765 some_list.push_back(1); |
| 766 EXPECT_THAT(some_list, Not(Contains(4))); |
| 767 } |
| 768 |
| 769 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) { |
| 770 set<int> some_set; |
| 771 some_set.insert(3); |
| 772 some_set.insert(1); |
| 773 some_set.insert(2); |
| 774 EXPECT_THAT(some_set, Contains(1.0)); |
| 775 EXPECT_THAT(some_set, Contains(3.0f)); |
| 776 EXPECT_THAT(some_set, Contains(2)); |
| 777 |
| 778 set<const char*> another_set; |
| 779 another_set.insert("fee"); |
| 780 another_set.insert("fie"); |
| 781 another_set.insert("foe"); |
| 782 another_set.insert("fum"); |
| 783 EXPECT_THAT(another_set, Contains(string("fum"))); |
| 784 } |
| 785 |
| 786 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { |
| 787 set<int> some_set; |
| 788 some_set.insert(3); |
| 789 some_set.insert(1); |
| 790 EXPECT_THAT(some_set, Not(Contains(4))); |
| 791 |
| 792 set<const char*> c_string_set; |
| 793 c_string_set.insert("hello"); |
| 794 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str()))); |
| 795 } |
| 796 |
| 797 TEST(ContainsTest, DescribesItselfCorrectly) { |
| 798 Matcher<vector<int> > m = Contains(1); |
| 799 EXPECT_EQ("contains 1", Describe(m)); |
| 800 } |
| 801 |
| 802 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { |
| 803 map<const char*, int> my_map; |
| 804 const char* bar = "a string"; |
| 805 my_map[bar] = 2; |
| 806 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2))); |
| 807 |
| 808 map<string, int> another_map; |
| 809 another_map["fee"] = 1; |
| 810 another_map["fie"] = 2; |
| 811 another_map["foe"] = 3; |
| 812 another_map["fum"] = 4; |
| 813 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1))); |
| 814 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2))); |
| 815 } |
| 816 |
| 817 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) { |
| 818 map<int, int> some_map; |
| 819 some_map[1] = 11; |
| 820 some_map[2] = 22; |
| 821 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23)))); |
| 822 } |
| 823 |
| 824 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) { |
| 825 const char* string_array[] = { "fee", "fie", "foe", "fum" }; |
| 826 EXPECT_THAT(string_array, Contains(string("fum"))); |
| 827 } |
| 828 |
| 829 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) { |
| 830 int int_array[] = { 1, 2, 3, 4 }; |
| 831 EXPECT_THAT(int_array, Not(Contains(5))); |
| 832 } |
| 833 |
| 834 } // namespace |
OLD | NEW |