shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 <sstream> |
| 38 | #include <string> |
| 39 | #include <vector> |
| 40 | |
| 41 | #include <gmock/gmock.h> |
| 42 | #include <gtest/gtest.h> |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame^] | 43 | #include <gtest/gtest-spi.h> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 44 | |
| 45 | namespace { |
| 46 | |
| 47 | using std::list; |
| 48 | using std::stringstream; |
| 49 | using std::vector; |
| 50 | using testing::_; |
| 51 | using testing::ElementsAre; |
| 52 | using testing::ElementsAreArray; |
| 53 | using testing::Eq; |
| 54 | using testing::Ge; |
| 55 | using testing::Gt; |
| 56 | using testing::MakeMatcher; |
| 57 | using testing::Matcher; |
| 58 | using testing::MatcherInterface; |
| 59 | using testing::Ne; |
| 60 | using testing::Not; |
| 61 | using testing::Pointee; |
| 62 | using testing::Ref; |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame^] | 63 | using testing::StaticAssertTypeEq; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 64 | using testing::StrEq; |
| 65 | using testing::internal::string; |
| 66 | |
| 67 | // Returns the description of the given matcher. |
| 68 | template <typename T> |
| 69 | string Describe(const Matcher<T>& m) { |
| 70 | stringstream ss; |
| 71 | m.DescribeTo(&ss); |
| 72 | return ss.str(); |
| 73 | } |
| 74 | |
| 75 | // Returns the description of the negation of the given matcher. |
| 76 | template <typename T> |
| 77 | string DescribeNegation(const Matcher<T>& m) { |
| 78 | stringstream ss; |
| 79 | m.DescribeNegationTo(&ss); |
| 80 | return ss.str(); |
| 81 | } |
| 82 | |
| 83 | // Returns the reason why x matches, or doesn't match, m. |
| 84 | template <typename MatcherType, typename Value> |
| 85 | string Explain(const MatcherType& m, const Value& x) { |
| 86 | stringstream ss; |
| 87 | m.ExplainMatchResultTo(x, &ss); |
| 88 | return ss.str(); |
| 89 | } |
| 90 | |
| 91 | // For testing ExplainMatchResultTo(). |
| 92 | class GreaterThanMatcher : public MatcherInterface<int> { |
| 93 | public: |
| 94 | explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
| 95 | |
| 96 | virtual bool Matches(int lhs) const { return lhs > rhs_; } |
| 97 | |
| 98 | virtual void DescribeTo(::std::ostream* os) const { |
| 99 | *os << "is greater than " << rhs_; |
| 100 | } |
| 101 | |
| 102 | virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { |
| 103 | const int diff = lhs - rhs_; |
| 104 | if (diff > 0) { |
| 105 | *os << "is " << diff << " more than " << rhs_; |
| 106 | } else if (diff == 0) { |
| 107 | *os << "is the same as " << rhs_; |
| 108 | } else { |
| 109 | *os << "is " << -diff << " less than " << rhs_; |
| 110 | } |
| 111 | } |
| 112 | private: |
| 113 | const int rhs_; |
| 114 | }; |
| 115 | |
| 116 | Matcher<int> GreaterThan(int n) { |
| 117 | return MakeMatcher(new GreaterThanMatcher(n)); |
| 118 | } |
| 119 | |
| 120 | // Tests for ElementsAre(). |
| 121 | |
| 122 | // Evaluates to the number of elements in 'array'. |
| 123 | #define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0])) |
| 124 | |
| 125 | TEST(ElementsAreTest, CanDescribeExpectingNoElement) { |
| 126 | Matcher<const vector<int>&> m = ElementsAre(); |
| 127 | EXPECT_EQ("is empty", Describe(m)); |
| 128 | } |
| 129 | |
| 130 | TEST(ElementsAreTest, CanDescribeExpectingOneElement) { |
| 131 | Matcher<vector<int> > m = ElementsAre(Gt(5)); |
| 132 | EXPECT_EQ("has 1 element that is greater than 5", Describe(m)); |
| 133 | } |
| 134 | |
| 135 | TEST(ElementsAreTest, CanDescribeExpectingManyElements) { |
| 136 | Matcher<list<string> > m = ElementsAre(StrEq("one"), "two"); |
| 137 | EXPECT_EQ("has 2 elements where\n" |
| 138 | "element 0 is equal to \"one\",\n" |
| 139 | "element 1 is equal to \"two\"", Describe(m)); |
| 140 | } |
| 141 | |
| 142 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) { |
| 143 | Matcher<vector<int> > m = ElementsAre(); |
| 144 | EXPECT_EQ("is not empty", DescribeNegation(m)); |
| 145 | } |
| 146 | |
| 147 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) { |
| 148 | Matcher<const list<int>& > m = ElementsAre(Gt(5)); |
| 149 | EXPECT_EQ("does not have 1 element, or\n" |
| 150 | "element 0 is not greater than 5", DescribeNegation(m)); |
| 151 | } |
| 152 | |
| 153 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) { |
| 154 | Matcher<const list<string>& > m = ElementsAre("one", "two"); |
| 155 | EXPECT_EQ("does not have 2 elements, or\n" |
| 156 | "element 0 is not equal to \"one\", or\n" |
| 157 | "element 1 is not equal to \"two\"", DescribeNegation(m)); |
| 158 | } |
| 159 | |
| 160 | TEST(ElementsAreTest, DoesNotExplainTrivialMatch) { |
| 161 | Matcher<const list<int>& > m = ElementsAre(1, Ne(2)); |
| 162 | |
| 163 | list<int> test_list; |
| 164 | test_list.push_back(1); |
| 165 | test_list.push_back(3); |
| 166 | EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything. |
| 167 | } |
| 168 | |
| 169 | TEST(ElementsAreTest, ExplainsNonTrivialMatch) { |
| 170 | Matcher<const vector<int>& > m = |
| 171 | ElementsAre(GreaterThan(1), 0, GreaterThan(2)); |
| 172 | |
| 173 | const int a[] = { 10, 0, 100 }; |
| 174 | vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 175 | EXPECT_EQ("element 0 is 9 more than 1,\n" |
| 176 | "element 2 is 98 more than 2", Explain(m, test_vector)); |
| 177 | } |
| 178 | |
| 179 | TEST(ElementsAreTest, CanExplainMismatchWrongSize) { |
| 180 | Matcher<const list<int>& > m = ElementsAre(1, 3); |
| 181 | |
| 182 | list<int> test_list; |
| 183 | // No need to explain when the container is empty. |
| 184 | EXPECT_EQ("", Explain(m, test_list)); |
| 185 | |
| 186 | test_list.push_back(1); |
| 187 | EXPECT_EQ("has 1 element", Explain(m, test_list)); |
| 188 | } |
| 189 | |
| 190 | TEST(ElementsAreTest, CanExplainMismatchRightSize) { |
| 191 | Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5)); |
| 192 | |
| 193 | vector<int> v; |
| 194 | v.push_back(2); |
| 195 | v.push_back(1); |
| 196 | EXPECT_EQ("element 0 doesn't match", Explain(m, v)); |
| 197 | |
| 198 | v[0] = 1; |
| 199 | EXPECT_EQ("element 1 doesn't match (is 4 less than 5)", Explain(m, v)); |
| 200 | } |
| 201 | |
| 202 | TEST(ElementsAreTest, MatchesOneElementVector) { |
| 203 | vector<string> test_vector; |
| 204 | test_vector.push_back("test string"); |
| 205 | |
| 206 | EXPECT_THAT(test_vector, ElementsAre(StrEq("test string"))); |
| 207 | } |
| 208 | |
| 209 | TEST(ElementsAreTest, MatchesOneElementList) { |
| 210 | list<string> test_list; |
| 211 | test_list.push_back("test string"); |
| 212 | |
| 213 | EXPECT_THAT(test_list, ElementsAre("test string")); |
| 214 | } |
| 215 | |
| 216 | TEST(ElementsAreTest, MatchesThreeElementVector) { |
| 217 | vector<string> test_vector; |
| 218 | test_vector.push_back("one"); |
| 219 | test_vector.push_back("two"); |
| 220 | test_vector.push_back("three"); |
| 221 | |
| 222 | EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _)); |
| 223 | } |
| 224 | |
| 225 | TEST(ElementsAreTest, MatchesOneElementEqMatcher) { |
| 226 | vector<int> test_vector; |
| 227 | test_vector.push_back(4); |
| 228 | |
| 229 | EXPECT_THAT(test_vector, ElementsAre(Eq(4))); |
| 230 | } |
| 231 | |
| 232 | TEST(ElementsAreTest, MatchesOneElementAnyMatcher) { |
| 233 | vector<int> test_vector; |
| 234 | test_vector.push_back(4); |
| 235 | |
| 236 | EXPECT_THAT(test_vector, ElementsAre(_)); |
| 237 | } |
| 238 | |
| 239 | TEST(ElementsAreTest, MatchesOneElementValue) { |
| 240 | vector<int> test_vector; |
| 241 | test_vector.push_back(4); |
| 242 | |
| 243 | EXPECT_THAT(test_vector, ElementsAre(4)); |
| 244 | } |
| 245 | |
| 246 | TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) { |
| 247 | vector<int> test_vector; |
| 248 | test_vector.push_back(1); |
| 249 | test_vector.push_back(2); |
| 250 | test_vector.push_back(3); |
| 251 | |
| 252 | EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _)); |
| 253 | } |
| 254 | |
| 255 | TEST(ElementsAreTest, MatchesTenElementVector) { |
| 256 | const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 257 | vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 258 | |
| 259 | EXPECT_THAT(test_vector, |
| 260 | // The element list can contain values and/or matchers |
| 261 | // of different types. |
| 262 | ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _)); |
| 263 | } |
| 264 | |
| 265 | TEST(ElementsAreTest, DoesNotMatchWrongSize) { |
| 266 | vector<string> test_vector; |
| 267 | test_vector.push_back("test string"); |
| 268 | test_vector.push_back("test string"); |
| 269 | |
| 270 | Matcher<vector<string> > m = ElementsAre(StrEq("test string")); |
| 271 | EXPECT_FALSE(m.Matches(test_vector)); |
| 272 | } |
| 273 | |
| 274 | TEST(ElementsAreTest, DoesNotMatchWrongValue) { |
| 275 | vector<string> test_vector; |
| 276 | test_vector.push_back("other string"); |
| 277 | |
| 278 | Matcher<vector<string> > m = ElementsAre(StrEq("test string")); |
| 279 | EXPECT_FALSE(m.Matches(test_vector)); |
| 280 | } |
| 281 | |
| 282 | TEST(ElementsAreTest, DoesNotMatchWrongOrder) { |
| 283 | vector<string> test_vector; |
| 284 | test_vector.push_back("one"); |
| 285 | test_vector.push_back("three"); |
| 286 | test_vector.push_back("two"); |
| 287 | |
| 288 | Matcher<vector<string> > m = ElementsAre( |
| 289 | StrEq("one"), StrEq("two"), StrEq("three")); |
| 290 | EXPECT_FALSE(m.Matches(test_vector)); |
| 291 | } |
| 292 | |
| 293 | TEST(ElementsAreTest, WorksForNestedContainer) { |
| 294 | const char* strings[] = { |
| 295 | "Hi", |
| 296 | "world" |
| 297 | }; |
| 298 | |
| 299 | vector<list<char> > nested; |
| 300 | for (int i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) { |
| 301 | nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i]))); |
| 302 | } |
| 303 | |
| 304 | EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')), |
| 305 | ElementsAre('w', 'o', _, _, 'd'))); |
| 306 | EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'), |
| 307 | ElementsAre('w', 'o', _, _, 'd')))); |
| 308 | } |
| 309 | |
| 310 | TEST(ElementsAreTest, WorksWithByRefElementMatchers) { |
| 311 | int a[] = { 0, 1, 2 }; |
| 312 | vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 313 | |
| 314 | EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2]))); |
| 315 | EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2])))); |
| 316 | } |
| 317 | |
| 318 | TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { |
| 319 | int a[] = { 0, 1, 2 }; |
| 320 | vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 321 | |
| 322 | EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); |
| 323 | EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); |
| 324 | } |
| 325 | |
| 326 | // Tests for ElementsAreArray(). Since ElementsAreArray() shares most |
| 327 | // of the implementation with ElementsAre(), we don't test it as |
| 328 | // thoroughly here. |
| 329 | |
| 330 | TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { |
| 331 | const int a[] = { 1, 2, 3 }; |
| 332 | |
| 333 | vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 334 | EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 335 | |
| 336 | test_vector[2] = 0; |
| 337 | EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
| 338 | } |
| 339 | |
| 340 | TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) { |
| 341 | const char* a[] = { "one", "two", "three" }; |
| 342 | |
| 343 | vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 344 | EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a))); |
| 345 | |
| 346 | const char** p = a; |
| 347 | test_vector[0] = "1"; |
| 348 | EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a)))); |
| 349 | } |
| 350 | |
| 351 | TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) { |
| 352 | const char* a[] = { "one", "two", "three" }; |
| 353 | |
| 354 | vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 355 | EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 356 | |
| 357 | test_vector[0] = "1"; |
| 358 | EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
| 359 | } |
| 360 | |
| 361 | TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { |
| 362 | const Matcher<string> kMatcherArray[] = |
| 363 | { StrEq("one"), StrEq("two"), StrEq("three") }; |
| 364 | |
| 365 | vector<string> test_vector; |
| 366 | test_vector.push_back("one"); |
| 367 | test_vector.push_back("two"); |
| 368 | test_vector.push_back("three"); |
| 369 | EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray)); |
| 370 | |
| 371 | test_vector.push_back("three"); |
| 372 | EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); |
| 373 | } |
| 374 | |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame^] | 375 | // Tests for the MATCHER*() macro family. |
| 376 | |
| 377 | // Tests that a simple MATCHER() definition works. |
| 378 | |
| 379 | MATCHER(IsEven, "") { return (arg % 2) == 0; } |
| 380 | |
| 381 | TEST(MatcherMacroTest, Works) { |
| 382 | const Matcher<int> m = IsEven(); |
| 383 | EXPECT_TRUE(m.Matches(6)); |
| 384 | EXPECT_FALSE(m.Matches(7)); |
| 385 | |
| 386 | EXPECT_EQ("is even", Describe(m)); |
| 387 | EXPECT_EQ("not (is even)", DescribeNegation(m)); |
| 388 | EXPECT_EQ("", Explain(m, 6)); |
| 389 | EXPECT_EQ("", Explain(m, 7)); |
| 390 | } |
| 391 | |
| 392 | // Tests that the description string supplied to MATCHER() must be |
| 393 | // empty. |
| 394 | |
| 395 | MATCHER(HasBadDescription, "not empty?") { |
| 396 | return true; |
| 397 | } |
| 398 | |
| 399 | TEST(MatcherMacroTest, |
| 400 | CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
| 401 | EXPECT_NONFATAL_FAILURE(HasBadDescription(), |
| 402 | "The description string in a MATCHER*() macro " |
| 403 | "must be \"\" at this moment"); |
| 404 | } |
| 405 | |
| 406 | // Tests that the body of MATCHER() can reference the type of the |
| 407 | // value being matched. |
| 408 | |
| 409 | MATCHER(IsEmptyString, "") { |
| 410 | StaticAssertTypeEq< ::std::string, arg_type>(); |
| 411 | return arg == ""; |
| 412 | } |
| 413 | |
| 414 | MATCHER(IsEmptyStringByRef, "") { |
| 415 | StaticAssertTypeEq<const ::std::string&, arg_type>(); |
| 416 | return arg == ""; |
| 417 | } |
| 418 | |
| 419 | TEST(MatcherMacroTest, CanReferenceArgType) { |
| 420 | const Matcher< ::std::string> m1 = IsEmptyString(); |
| 421 | EXPECT_TRUE(m1.Matches("")); |
| 422 | |
| 423 | const Matcher<const ::std::string&> m2 = IsEmptyStringByRef(); |
| 424 | EXPECT_TRUE(m2.Matches("")); |
| 425 | } |
| 426 | |
| 427 | // Tests that MATCHER() can be used in a namespace. |
| 428 | |
| 429 | namespace matcher_test { |
| 430 | MATCHER(IsOdd, "") { return (arg % 2) != 0; } |
| 431 | } // namespace matcher_test |
| 432 | |
| 433 | TEST(MatcherTest, WorksInNamespace) { |
| 434 | Matcher<int> m = matcher_test::IsOdd(); |
| 435 | EXPECT_FALSE(m.Matches(4)); |
| 436 | EXPECT_TRUE(m.Matches(5)); |
| 437 | } |
| 438 | |
| 439 | // Tests that a simple MATCHER_P() definition works. |
| 440 | |
| 441 | MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; } |
| 442 | |
| 443 | TEST(MatcherPMacroTest, Works) { |
| 444 | const Matcher<int> m = IsGreaterThan32And(5); |
| 445 | EXPECT_TRUE(m.Matches(36)); |
| 446 | EXPECT_FALSE(m.Matches(5)); |
| 447 | |
| 448 | EXPECT_EQ("is greater than 32 and 5", Describe(m)); |
| 449 | EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m)); |
| 450 | EXPECT_EQ("", Explain(m, 36)); |
| 451 | EXPECT_EQ("", Explain(m, 5)); |
| 452 | } |
| 453 | |
| 454 | // Tests that the description string supplied to MATCHER_P() must be |
| 455 | // empty. |
| 456 | |
| 457 | MATCHER_P(HasBadDescription1, n, "not empty?") { |
| 458 | return arg > n; |
| 459 | } |
| 460 | |
| 461 | TEST(MatcherPMacroTest, |
| 462 | CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
| 463 | EXPECT_NONFATAL_FAILURE(HasBadDescription1(2), |
| 464 | "The description string in a MATCHER*() macro " |
| 465 | "must be \"\" at this moment"); |
| 466 | } |
| 467 | |
| 468 | // Tests that the description is calculated correctly from the matcher name. |
| 469 | MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; } |
| 470 | |
| 471 | TEST(MatcherPMacroTest, GeneratesCorrectDescription) { |
| 472 | const Matcher<int> m = _is_Greater_Than32and_(5); |
| 473 | |
| 474 | EXPECT_EQ("is greater than 32 and 5", Describe(m)); |
| 475 | EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m)); |
| 476 | EXPECT_EQ("", Explain(m, 36)); |
| 477 | EXPECT_EQ("", Explain(m, 5)); |
| 478 | } |
| 479 | |
| 480 | // Tests that a MATCHER_P matcher can be explicitly instantiated with |
| 481 | // a reference parameter type. |
| 482 | |
| 483 | class UncopyableFoo { |
| 484 | public: |
| 485 | explicit UncopyableFoo(char value) : value_(value) {} |
| 486 | private: |
| 487 | UncopyableFoo(const UncopyableFoo&); |
| 488 | void operator=(const UncopyableFoo&); |
| 489 | |
| 490 | char value_; |
| 491 | }; |
| 492 | |
| 493 | MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; } |
| 494 | |
| 495 | TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) { |
| 496 | UncopyableFoo foo1('1'), foo2('2'); |
| 497 | const Matcher<const UncopyableFoo&> m = |
| 498 | ReferencesUncopyable<const UncopyableFoo&>(foo1); |
| 499 | |
| 500 | EXPECT_TRUE(m.Matches(foo1)); |
| 501 | EXPECT_FALSE(m.Matches(foo2)); |
| 502 | |
| 503 | // We don't want the address of the parameter printed, as most |
| 504 | // likely it will just annoy the user. If the address is |
| 505 | // interesting, the user should consider passing the parameter by |
| 506 | // pointer instead. |
| 507 | EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m)); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | // Tests that the description string supplied to MATCHER_Pn() must be |
| 512 | // empty. |
| 513 | |
| 514 | MATCHER_P2(HasBadDescription2, m, n, "not empty?") { |
| 515 | return arg > m + n; |
| 516 | } |
| 517 | |
| 518 | TEST(MatcherPnMacroTest, |
| 519 | CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
| 520 | EXPECT_NONFATAL_FAILURE(HasBadDescription2(3, 4), |
| 521 | "The description string in a MATCHER*() macro " |
| 522 | "must be \"\" at this moment"); |
| 523 | } |
| 524 | |
| 525 | // Tests that the body of MATCHER_Pn() can reference the parameter |
| 526 | // types. |
| 527 | |
| 528 | MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") { |
| 529 | StaticAssertTypeEq<int, foo_type>(); |
| 530 | StaticAssertTypeEq<long, bar_type>(); // NOLINT |
| 531 | StaticAssertTypeEq<char, baz_type>(); |
| 532 | return arg == 0; |
| 533 | } |
| 534 | |
| 535 | TEST(MatcherPnMacroTest, CanReferenceParamTypes) { |
| 536 | EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a')); |
| 537 | } |
| 538 | |
| 539 | // Tests that a MATCHER_Pn matcher can be explicitly instantiated with |
| 540 | // reference parameter types. |
| 541 | |
| 542 | MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") { |
| 543 | return &arg == &variable1 || &arg == &variable2; |
| 544 | } |
| 545 | |
| 546 | TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) { |
| 547 | UncopyableFoo foo1('1'), foo2('2'), foo3('3'); |
| 548 | const Matcher<const UncopyableFoo&> m = |
| 549 | ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); |
| 550 | |
| 551 | EXPECT_TRUE(m.Matches(foo1)); |
| 552 | EXPECT_TRUE(m.Matches(foo2)); |
| 553 | EXPECT_FALSE(m.Matches(foo3)); |
| 554 | } |
| 555 | |
| 556 | TEST(MatcherPnMacroTest, |
| 557 | GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) { |
| 558 | UncopyableFoo foo1('1'), foo2('2'); |
| 559 | const Matcher<const UncopyableFoo&> m = |
| 560 | ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); |
| 561 | |
| 562 | // We don't want the addresses of the parameters printed, as most |
| 563 | // likely they will just annoy the user. If the addresses are |
| 564 | // interesting, the user should consider passing the parameters by |
| 565 | // pointers instead. |
| 566 | EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)", |
| 567 | Describe(m)); |
| 568 | } |
| 569 | |
| 570 | // Tests that a simple MATCHER_P2() definition works. |
| 571 | |
| 572 | MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; } |
| 573 | |
| 574 | TEST(MatcherPnMacroTest, Works) { |
| 575 | const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT |
| 576 | EXPECT_TRUE(m.Matches(36L)); |
| 577 | EXPECT_FALSE(m.Matches(15L)); |
| 578 | |
| 579 | EXPECT_EQ("is not in closed range (10, 20)", Describe(m)); |
| 580 | EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m)); |
| 581 | EXPECT_EQ("", Explain(m, 36L)); |
| 582 | EXPECT_EQ("", Explain(m, 15L)); |
| 583 | } |
| 584 | |
| 585 | // Tests that MATCHER*() definitions can be overloaded on the number |
| 586 | // of parameters; also tests MATCHER_Pn() where n >= 3. |
| 587 | |
| 588 | MATCHER(EqualsSumOf, "") { return arg == 0; } |
| 589 | MATCHER_P(EqualsSumOf, a, "") { return arg == a; } |
| 590 | MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; } |
| 591 | MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; } |
| 592 | MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; } |
| 593 | MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; } |
| 594 | MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") { |
| 595 | return arg == a + b + c + d + e + f; |
| 596 | } |
| 597 | MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") { |
| 598 | return arg == a + b + c + d + e + f + g; |
| 599 | } |
| 600 | MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") { |
| 601 | return arg == a + b + c + d + e + f + g + h; |
| 602 | } |
| 603 | MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") { |
| 604 | return arg == a + b + c + d + e + f + g + h + i; |
| 605 | } |
| 606 | MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") { |
| 607 | return arg == a + b + c + d + e + f + g + h + i + j; |
| 608 | } |
| 609 | |
| 610 | TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) { |
| 611 | EXPECT_THAT(0, EqualsSumOf()); |
| 612 | EXPECT_THAT(1, EqualsSumOf(1)); |
| 613 | EXPECT_THAT(12, EqualsSumOf(10, 2)); |
| 614 | EXPECT_THAT(123, EqualsSumOf(100, 20, 3)); |
| 615 | EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4)); |
| 616 | EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5)); |
| 617 | EXPECT_THAT("abcdef", |
| 618 | EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')); |
| 619 | EXPECT_THAT("abcdefg", |
| 620 | EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g')); |
| 621 | EXPECT_THAT("abcdefgh", |
| 622 | EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 623 | "h")); |
| 624 | EXPECT_THAT("abcdefghi", |
| 625 | EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 626 | "h", 'i')); |
| 627 | EXPECT_THAT("abcdefghij", |
| 628 | EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 629 | "h", 'i', ::std::string("j"))); |
| 630 | |
| 631 | EXPECT_THAT(1, Not(EqualsSumOf())); |
| 632 | EXPECT_THAT(-1, Not(EqualsSumOf(1))); |
| 633 | EXPECT_THAT(-12, Not(EqualsSumOf(10, 2))); |
| 634 | EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3))); |
| 635 | EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4))); |
| 636 | EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5))); |
| 637 | EXPECT_THAT("abcdef ", |
| 638 | Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'))); |
| 639 | EXPECT_THAT("abcdefg ", |
| 640 | Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', |
| 641 | 'g'))); |
| 642 | EXPECT_THAT("abcdefgh ", |
| 643 | Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 644 | "h"))); |
| 645 | EXPECT_THAT("abcdefghi ", |
| 646 | Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 647 | "h", 'i'))); |
| 648 | EXPECT_THAT("abcdefghij ", |
| 649 | Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 650 | "h", 'i', ::std::string("j")))); |
| 651 | } |
| 652 | |
| 653 | // Tests that a MATCHER_Pn() definition can be instantiated with any |
| 654 | // compatible parameter types. |
| 655 | TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) { |
| 656 | EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3))); |
| 657 | EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d")); |
| 658 | |
| 659 | EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3)))); |
| 660 | EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d"))); |
| 661 | } |
| 662 | |
| 663 | // Tests that the matcher body can promote the parameter types. |
| 664 | |
| 665 | MATCHER_P2(EqConcat, prefix, suffix, "") { |
| 666 | // The following lines promote the two parameters to desired types. |
| 667 | std::string prefix_str(prefix); |
| 668 | char suffix_char(suffix); |
| 669 | return arg == prefix_str + suffix_char; |
| 670 | } |
| 671 | |
| 672 | TEST(MatcherPnMacroTest, SimpleTypePromotion) { |
| 673 | Matcher<std::string> no_promo = |
| 674 | EqConcat(std::string("foo"), 't'); |
| 675 | Matcher<const std::string&> promo = |
| 676 | EqConcat("foo", static_cast<int>('t')); |
| 677 | EXPECT_FALSE(no_promo.Matches("fool")); |
| 678 | EXPECT_FALSE(promo.Matches("fool")); |
| 679 | EXPECT_TRUE(no_promo.Matches("foot")); |
| 680 | EXPECT_TRUE(promo.Matches("foot")); |
| 681 | } |
| 682 | |
| 683 | // Verifies the type of a MATCHER*. |
| 684 | |
| 685 | TEST(MatcherPnMacroTest, TypesAreCorrect) { |
| 686 | // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable. |
| 687 | EqualsSumOfMatcher a0 = EqualsSumOf(); |
| 688 | |
| 689 | // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable. |
| 690 | EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1); |
| 691 | |
| 692 | // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk |
| 693 | // variable, and so on. |
| 694 | EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2'); |
| 695 | EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3'); |
| 696 | EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4'); |
| 697 | EqualsSumOfMatcherP5<int, int, int, int, char> a5 = |
| 698 | EqualsSumOf(1, 2, 3, 4, '5'); |
| 699 | EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 = |
| 700 | EqualsSumOf(1, 2, 3, 4, 5, '6'); |
| 701 | EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 = |
| 702 | EqualsSumOf(1, 2, 3, 4, 5, 6, '7'); |
| 703 | EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 = |
| 704 | EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8'); |
| 705 | EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 = |
| 706 | EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9'); |
| 707 | EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 = |
| 708 | EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); |
| 709 | } |
| 710 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 711 | } // namespace |