| 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 std::tr1::get; | |
| 57 using std::tr1::make_tuple; | |
| 58 using std::tr1::tuple; | |
| 59 using testing::_; | |
| 60 using testing::Args; | |
| 61 using testing::Contains; | |
| 62 using testing::ElementsAre; | |
| 63 using testing::ElementsAreArray; | |
| 64 using testing::Eq; | |
| 65 using testing::Ge; | |
| 66 using testing::Gt; | |
| 67 using testing::Lt; | |
| 68 using testing::MakeMatcher; | |
| 69 using testing::Matcher; | |
| 70 using testing::MatcherInterface; | |
| 71 using testing::MatchResultListener; | |
| 72 using testing::Ne; | |
| 73 using testing::Not; | |
| 74 using testing::Pointee; | |
| 75 using testing::Ref; | |
| 76 using testing::StaticAssertTypeEq; | |
| 77 using testing::StrEq; | |
| 78 using testing::Value; | |
| 79 using testing::internal::string; | |
| 80 | |
| 81 // Returns the description of the given matcher. | |
| 82 template <typename T> | |
| 83 string Describe(const Matcher<T>& m) { | |
| 84 stringstream ss; | |
| 85 m.DescribeTo(&ss); | |
| 86 return ss.str(); | |
| 87 } | |
| 88 | |
| 89 // Returns the description of the negation of the given matcher. | |
| 90 template <typename T> | |
| 91 string DescribeNegation(const Matcher<T>& m) { | |
| 92 stringstream ss; | |
| 93 m.DescribeNegationTo(&ss); | |
| 94 return ss.str(); | |
| 95 } | |
| 96 | |
| 97 // Returns the reason why x matches, or doesn't match, m. | |
| 98 template <typename MatcherType, typename Value> | |
| 99 string Explain(const MatcherType& m, const Value& x) { | |
| 100 stringstream ss; | |
| 101 m.ExplainMatchResultTo(x, &ss); | |
| 102 return ss.str(); | |
| 103 } | |
| 104 | |
| 105 // Tests Args<k0, ..., kn>(m). | |
| 106 | |
| 107 TEST(ArgsTest, AcceptsZeroTemplateArg) { | |
| 108 const tuple<int, bool> t(5, true); | |
| 109 EXPECT_THAT(t, Args<>(Eq(tuple<>()))); | |
| 110 EXPECT_THAT(t, Not(Args<>(Ne(tuple<>())))); | |
| 111 } | |
| 112 | |
| 113 TEST(ArgsTest, AcceptsOneTemplateArg) { | |
| 114 const tuple<int, bool> t(5, true); | |
| 115 EXPECT_THAT(t, Args<0>(Eq(make_tuple(5)))); | |
| 116 EXPECT_THAT(t, Args<1>(Eq(make_tuple(true)))); | |
| 117 EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false))))); | |
| 118 } | |
| 119 | |
| 120 TEST(ArgsTest, AcceptsTwoTemplateArgs) { | |
| 121 const tuple<short, int, long> t(4, 5, 6L); // NOLINT | |
| 122 | |
| 123 EXPECT_THAT(t, (Args<0, 1>(Lt()))); | |
| 124 EXPECT_THAT(t, (Args<1, 2>(Lt()))); | |
| 125 EXPECT_THAT(t, Not(Args<0, 2>(Gt()))); | |
| 126 } | |
| 127 | |
| 128 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) { | |
| 129 const tuple<short, int, long> t(4, 5, 6L); // NOLINT | |
| 130 EXPECT_THAT(t, (Args<0, 0>(Eq()))); | |
| 131 EXPECT_THAT(t, Not(Args<1, 1>(Ne()))); | |
| 132 } | |
| 133 | |
| 134 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) { | |
| 135 const tuple<short, int, long> t(4, 5, 6L); // NOLINT | |
| 136 EXPECT_THAT(t, (Args<2, 0>(Gt()))); | |
| 137 EXPECT_THAT(t, Not(Args<2, 1>(Lt()))); | |
| 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 | |
| 150 MATCHER(SumIsZero, "") { | |
| 151 return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0; | |
| 152 } | |
| 153 | |
| 154 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) { | |
| 155 EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero()))); | |
| 156 EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero()))); | |
| 157 } | |
| 158 | |
| 159 TEST(ArgsTest, CanBeNested) { | |
| 160 const tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT | |
| 161 EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq())))); | |
| 162 EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt())))); | |
| 163 } | |
| 164 | |
| 165 TEST(ArgsTest, CanMatchTupleByValue) { | |
| 166 typedef tuple<char, int, int> Tuple3; | |
| 167 const Matcher<Tuple3> m = Args<1, 2>(Lt()); | |
| 168 EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2))); | |
| 169 EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2))); | |
| 170 } | |
| 171 | |
| 172 TEST(ArgsTest, CanMatchTupleByReference) { | |
| 173 typedef tuple<char, char, int> Tuple3; | |
| 174 const Matcher<const Tuple3&> m = Args<0, 1>(Lt()); | |
| 175 EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2))); | |
| 176 EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2))); | |
| 177 } | |
| 178 | |
| 179 // Validates that arg is printed as str. | |
| 180 MATCHER_P(PrintsAs, str, "") { | |
| 181 return testing::PrintToString(arg) == str; | |
| 182 } | |
| 183 | |
| 184 TEST(ArgsTest, AcceptsTenTemplateArgs) { | |
| 185 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), | |
| 186 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( | |
| 187 PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); | |
| 188 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), | |
| 189 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( | |
| 190 PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); | |
| 191 } | |
| 192 | |
| 193 TEST(ArgsTest, DescirbesSelfCorrectly) { | |
| 194 const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt()); | |
| 195 EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where " | |
| 196 "the first < the second", | |
| 197 Describe(m)); | |
| 198 } | |
| 199 | |
| 200 TEST(ArgsTest, DescirbesNestedArgsCorrectly) { | |
| 201 const Matcher<const tuple<int, bool, char, int>&> m = | |
| 202 Args<0, 2, 3>(Args<2, 0>(Lt())); | |
| 203 EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple " | |
| 204 "whose fields (#2, #0) are a pair where the first < the second", | |
| 205 Describe(m)); | |
| 206 } | |
| 207 | |
| 208 TEST(ArgsTest, DescribesNegationCorrectly) { | |
| 209 const Matcher<tuple<int, char> > m = Args<1, 0>(Gt()); | |
| 210 EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair " | |
| 211 "where the first > the second", | |
| 212 DescribeNegation(m)); | |
| 213 } | |
| 214 | |
| 215 TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) { | |
| 216 const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq()); | |
| 217 EXPECT_EQ("whose fields (#1, #2) are (42, 42)", | |
| 218 Explain(m, make_tuple(false, 42, 42))); | |
| 219 EXPECT_EQ("whose fields (#1, #2) are (42, 43)", | |
| 220 Explain(m, make_tuple(false, 42, 43))); | |
| 221 } | |
| 222 | |
| 223 // For testing Args<>'s explanation. | |
| 224 class LessThanMatcher : public MatcherInterface<tuple<char, int> > { | |
| 225 public: | |
| 226 virtual void DescribeTo(::std::ostream* os) const {} | |
| 227 | |
| 228 virtual bool MatchAndExplain(tuple<char, int> value, | |
| 229 MatchResultListener* listener) const { | |
| 230 const int diff = get<0>(value) - get<1>(value); | |
| 231 if (diff > 0) { | |
| 232 *listener << "where the first value is " << diff | |
| 233 << " more than the second"; | |
| 234 } | |
| 235 return diff < 0; | |
| 236 } | |
| 237 }; | |
| 238 | |
| 239 Matcher<tuple<char, int> > LessThan() { | |
| 240 return MakeMatcher(new LessThanMatcher); | |
| 241 } | |
| 242 | |
| 243 TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) { | |
| 244 const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan()); | |
| 245 EXPECT_EQ("whose fields (#0, #2) are ('a' (97), 42), " | |
| 246 "where the first value is 55 more than the second", | |
| 247 Explain(m, make_tuple('a', 42, 42))); | |
| 248 EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)", | |
| 249 Explain(m, make_tuple('\0', 42, 43))); | |
| 250 } | |
| 251 | |
| 252 // For testing ExplainMatchResultTo(). | |
| 253 class GreaterThanMatcher : public MatcherInterface<int> { | |
| 254 public: | |
| 255 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} | |
| 256 | |
| 257 virtual void DescribeTo(::std::ostream* os) const { | |
| 258 *os << "is greater than " << rhs_; | |
| 259 } | |
| 260 | |
| 261 virtual bool MatchAndExplain(int lhs, | |
| 262 MatchResultListener* listener) const { | |
| 263 const int diff = lhs - rhs_; | |
| 264 if (diff > 0) { | |
| 265 *listener << "which is " << diff << " more than " << rhs_; | |
| 266 } else if (diff == 0) { | |
| 267 *listener << "which is the same as " << rhs_; | |
| 268 } else { | |
| 269 *listener << "which is " << -diff << " less than " << rhs_; | |
| 270 } | |
| 271 | |
| 272 return lhs > rhs_; | |
| 273 } | |
| 274 | |
| 275 private: | |
| 276 int rhs_; | |
| 277 }; | |
| 278 | |
| 279 Matcher<int> GreaterThan(int n) { | |
| 280 return MakeMatcher(new GreaterThanMatcher(n)); | |
| 281 } | |
| 282 | |
| 283 // Tests for ElementsAre(). | |
| 284 | |
| 285 // Evaluates to the number of elements in 'array'. | |
| 286 #define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0])) | |
| 287 | |
| 288 TEST(ElementsAreTest, CanDescribeExpectingNoElement) { | |
| 289 Matcher<const vector<int>&> m = ElementsAre(); | |
| 290 EXPECT_EQ("is empty", Describe(m)); | |
| 291 } | |
| 292 | |
| 293 TEST(ElementsAreTest, CanDescribeExpectingOneElement) { | |
| 294 Matcher<vector<int> > m = ElementsAre(Gt(5)); | |
| 295 EXPECT_EQ("has 1 element that is > 5", Describe(m)); | |
| 296 } | |
| 297 | |
| 298 TEST(ElementsAreTest, CanDescribeExpectingManyElements) { | |
| 299 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two"); | |
| 300 EXPECT_EQ("has 2 elements where\n" | |
| 301 "element #0 is equal to \"one\",\n" | |
| 302 "element #1 is equal to \"two\"", Describe(m)); | |
| 303 } | |
| 304 | |
| 305 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) { | |
| 306 Matcher<vector<int> > m = ElementsAre(); | |
| 307 EXPECT_EQ("isn't empty", DescribeNegation(m)); | |
| 308 } | |
| 309 | |
| 310 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) { | |
| 311 Matcher<const list<int>& > m = ElementsAre(Gt(5)); | |
| 312 EXPECT_EQ("doesn't have 1 element, or\n" | |
| 313 "element #0 isn't > 5", DescribeNegation(m)); | |
| 314 } | |
| 315 | |
| 316 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) { | |
| 317 Matcher<const list<string>& > m = ElementsAre("one", "two"); | |
| 318 EXPECT_EQ("doesn't have 2 elements, or\n" | |
| 319 "element #0 isn't equal to \"one\", or\n" | |
| 320 "element #1 isn't equal to \"two\"", DescribeNegation(m)); | |
| 321 } | |
| 322 | |
| 323 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) { | |
| 324 Matcher<const list<int>& > m = ElementsAre(1, Ne(2)); | |
| 325 | |
| 326 list<int> test_list; | |
| 327 test_list.push_back(1); | |
| 328 test_list.push_back(3); | |
| 329 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything. | |
| 330 } | |
| 331 | |
| 332 TEST(ElementsAreTest, ExplainsNonTrivialMatch) { | |
| 333 Matcher<const vector<int>& > m = | |
| 334 ElementsAre(GreaterThan(1), 0, GreaterThan(2)); | |
| 335 | |
| 336 const int a[] = { 10, 0, 100 }; | |
| 337 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); | |
| 338 EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n" | |
| 339 "and whose element #2 matches, which is 98 more than 2", | |
| 340 Explain(m, test_vector)); | |
| 341 } | |
| 342 | |
| 343 TEST(ElementsAreTest, CanExplainMismatchWrongSize) { | |
| 344 Matcher<const list<int>& > m = ElementsAre(1, 3); | |
| 345 | |
| 346 list<int> test_list; | |
| 347 // No need to explain when the container is empty. | |
| 348 EXPECT_EQ("", Explain(m, test_list)); | |
| 349 | |
| 350 test_list.push_back(1); | |
| 351 EXPECT_EQ("which has 1 element", Explain(m, test_list)); | |
| 352 } | |
| 353 | |
| 354 TEST(ElementsAreTest, CanExplainMismatchRightSize) { | |
| 355 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5)); | |
| 356 | |
| 357 vector<int> v; | |
| 358 v.push_back(2); | |
| 359 v.push_back(1); | |
| 360 EXPECT_EQ("whose element #0 doesn't match", Explain(m, v)); | |
| 361 | |
| 362 v[0] = 1; | |
| 363 EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5", | |
| 364 Explain(m, v)); | |
| 365 } | |
| 366 | |
| 367 TEST(ElementsAreTest, MatchesOneElementVector) { | |
| 368 vector<string> test_vector; | |
| 369 test_vector.push_back("test string"); | |
| 370 | |
| 371 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string"))); | |
| 372 } | |
| 373 | |
| 374 TEST(ElementsAreTest, MatchesOneElementList) { | |
| 375 list<string> test_list; | |
| 376 test_list.push_back("test string"); | |
| 377 | |
| 378 EXPECT_THAT(test_list, ElementsAre("test string")); | |
| 379 } | |
| 380 | |
| 381 TEST(ElementsAreTest, MatchesThreeElementVector) { | |
| 382 vector<string> test_vector; | |
| 383 test_vector.push_back("one"); | |
| 384 test_vector.push_back("two"); | |
| 385 test_vector.push_back("three"); | |
| 386 | |
| 387 EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _)); | |
| 388 } | |
| 389 | |
| 390 TEST(ElementsAreTest, MatchesOneElementEqMatcher) { | |
| 391 vector<int> test_vector; | |
| 392 test_vector.push_back(4); | |
| 393 | |
| 394 EXPECT_THAT(test_vector, ElementsAre(Eq(4))); | |
| 395 } | |
| 396 | |
| 397 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) { | |
| 398 vector<int> test_vector; | |
| 399 test_vector.push_back(4); | |
| 400 | |
| 401 EXPECT_THAT(test_vector, ElementsAre(_)); | |
| 402 } | |
| 403 | |
| 404 TEST(ElementsAreTest, MatchesOneElementValue) { | |
| 405 vector<int> test_vector; | |
| 406 test_vector.push_back(4); | |
| 407 | |
| 408 EXPECT_THAT(test_vector, ElementsAre(4)); | |
| 409 } | |
| 410 | |
| 411 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) { | |
| 412 vector<int> test_vector; | |
| 413 test_vector.push_back(1); | |
| 414 test_vector.push_back(2); | |
| 415 test_vector.push_back(3); | |
| 416 | |
| 417 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _)); | |
| 418 } | |
| 419 | |
| 420 TEST(ElementsAreTest, MatchesTenElementVector) { | |
| 421 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; | |
| 422 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); | |
| 423 | |
| 424 EXPECT_THAT(test_vector, | |
| 425 // The element list can contain values and/or matchers | |
| 426 // of different types. | |
| 427 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _)); | |
| 428 } | |
| 429 | |
| 430 TEST(ElementsAreTest, DoesNotMatchWrongSize) { | |
| 431 vector<string> test_vector; | |
| 432 test_vector.push_back("test string"); | |
| 433 test_vector.push_back("test string"); | |
| 434 | |
| 435 Matcher<vector<string> > m = ElementsAre(StrEq("test string")); | |
| 436 EXPECT_FALSE(m.Matches(test_vector)); | |
| 437 } | |
| 438 | |
| 439 TEST(ElementsAreTest, DoesNotMatchWrongValue) { | |
| 440 vector<string> test_vector; | |
| 441 test_vector.push_back("other string"); | |
| 442 | |
| 443 Matcher<vector<string> > m = ElementsAre(StrEq("test string")); | |
| 444 EXPECT_FALSE(m.Matches(test_vector)); | |
| 445 } | |
| 446 | |
| 447 TEST(ElementsAreTest, DoesNotMatchWrongOrder) { | |
| 448 vector<string> test_vector; | |
| 449 test_vector.push_back("one"); | |
| 450 test_vector.push_back("three"); | |
| 451 test_vector.push_back("two"); | |
| 452 | |
| 453 Matcher<vector<string> > m = ElementsAre( | |
| 454 StrEq("one"), StrEq("two"), StrEq("three")); | |
| 455 EXPECT_FALSE(m.Matches(test_vector)); | |
| 456 } | |
| 457 | |
| 458 TEST(ElementsAreTest, WorksForNestedContainer) { | |
| 459 const char* strings[] = { | |
| 460 "Hi", | |
| 461 "world" | |
| 462 }; | |
| 463 | |
| 464 vector<list<char> > nested; | |
| 465 for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) { | |
| 466 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i]))); | |
| 467 } | |
| 468 | |
| 469 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')), | |
| 470 ElementsAre('w', 'o', _, _, 'd'))); | |
| 471 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'), | |
| 472 ElementsAre('w', 'o', _, _, 'd')))); | |
| 473 } | |
| 474 | |
| 475 TEST(ElementsAreTest, WorksWithByRefElementMatchers) { | |
| 476 int a[] = { 0, 1, 2 }; | |
| 477 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); | |
| 478 | |
| 479 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2]))); | |
| 480 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2])))); | |
| 481 } | |
| 482 | |
| 483 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { | |
| 484 int a[] = { 0, 1, 2 }; | |
| 485 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); | |
| 486 | |
| 487 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); | |
| 488 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); | |
| 489 } | |
| 490 | |
| 491 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) { | |
| 492 int array[] = { 0, 1, 2 }; | |
| 493 EXPECT_THAT(array, ElementsAre(0, 1, _)); | |
| 494 EXPECT_THAT(array, Not(ElementsAre(1, _, _))); | |
| 495 EXPECT_THAT(array, Not(ElementsAre(0, _))); | |
| 496 } | |
| 497 | |
| 498 class NativeArrayPassedAsPointerAndSize { | |
| 499 public: | |
| 500 NativeArrayPassedAsPointerAndSize() {} | |
| 501 | |
| 502 MOCK_METHOD2(Helper, void(int* array, int size)); | |
| 503 | |
| 504 private: | |
| 505 GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize); | |
| 506 }; | |
| 507 | |
| 508 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) { | |
| 509 int array[] = { 0, 1 }; | |
| 510 ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2); | |
| 511 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1)); | |
| 512 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0))); | |
| 513 | |
| 514 NativeArrayPassedAsPointerAndSize helper; | |
| 515 EXPECT_CALL(helper, Helper(_, _)) | |
| 516 .With(ElementsAre(0, 1)); | |
| 517 helper.Helper(array, 2); | |
| 518 } | |
| 519 | |
| 520 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) { | |
| 521 const char a2[][3] = { "hi", "lo" }; | |
| 522 EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'), | |
| 523 ElementsAre('l', 'o', '\0'))); | |
| 524 EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo"))); | |
| 525 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')), | |
| 526 ElementsAre('l', 'o', '\0'))); | |
| 527 } | |
| 528 | |
| 529 // Tests for ElementsAreArray(). Since ElementsAreArray() shares most | |
| 530 // of the implementation with ElementsAre(), we don't test it as | |
| 531 // thoroughly here. | |
| 532 | |
| 533 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { | |
| 534 const int a[] = { 1, 2, 3 }; | |
| 535 | |
| 536 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); | |
| 537 EXPECT_THAT(test_vector, ElementsAreArray(a)); | |
| 538 | |
| 539 test_vector[2] = 0; | |
| 540 EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); | |
| 541 } | |
| 542 | |
| 543 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) { | |
| 544 const char* a[] = { "one", "two", "three" }; | |
| 545 | |
| 546 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); | |
| 547 EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a))); | |
| 548 | |
| 549 const char** p = a; | |
| 550 test_vector[0] = "1"; | |
| 551 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a)))); | |
| 552 } | |
| 553 | |
| 554 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) { | |
| 555 const char* a[] = { "one", "two", "three" }; | |
| 556 | |
| 557 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); | |
| 558 EXPECT_THAT(test_vector, ElementsAreArray(a)); | |
| 559 | |
| 560 test_vector[0] = "1"; | |
| 561 EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); | |
| 562 } | |
| 563 | |
| 564 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { | |
| 565 const Matcher<string> kMatcherArray[] = | |
| 566 { StrEq("one"), StrEq("two"), StrEq("three") }; | |
| 567 | |
| 568 vector<string> test_vector; | |
| 569 test_vector.push_back("one"); | |
| 570 test_vector.push_back("two"); | |
| 571 test_vector.push_back("three"); | |
| 572 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray)); | |
| 573 | |
| 574 test_vector.push_back("three"); | |
| 575 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); | |
| 576 } | |
| 577 | |
| 578 // Since ElementsAre() and ElementsAreArray() share much of the | |
| 579 // implementation, we only do a sanity test for native arrays here. | |
| 580 TEST(ElementsAreArrayTest, WorksWithNativeArray) { | |
| 581 ::std::string a[] = { "hi", "ho" }; | |
| 582 ::std::string b[] = { "hi", "ho" }; | |
| 583 | |
| 584 EXPECT_THAT(a, ElementsAreArray(b)); | |
| 585 EXPECT_THAT(a, ElementsAreArray(b, 2)); | |
| 586 EXPECT_THAT(a, Not(ElementsAreArray(b, 1))); | |
| 587 } | |
| 588 | |
| 589 // Tests for the MATCHER*() macro family. | |
| 590 | |
| 591 // Tests that a simple MATCHER() definition works. | |
| 592 | |
| 593 MATCHER(IsEven, "") { return (arg % 2) == 0; } | |
| 594 | |
| 595 TEST(MatcherMacroTest, Works) { | |
| 596 const Matcher<int> m = IsEven(); | |
| 597 EXPECT_TRUE(m.Matches(6)); | |
| 598 EXPECT_FALSE(m.Matches(7)); | |
| 599 | |
| 600 EXPECT_EQ("is even", Describe(m)); | |
| 601 EXPECT_EQ("not (is even)", DescribeNegation(m)); | |
| 602 EXPECT_EQ("", Explain(m, 6)); | |
| 603 EXPECT_EQ("", Explain(m, 7)); | |
| 604 } | |
| 605 | |
| 606 // Tests explaining match result in a MATCHER* macro. | |
| 607 | |
| 608 MATCHER(IsEven2, "is even") { | |
| 609 if ((arg % 2) == 0) { | |
| 610 // Verifies that we can stream to result_listener, a listener | |
| 611 // supplied by the MATCHER macro implicitly. | |
| 612 *result_listener << "OK"; | |
| 613 return true; | |
| 614 } else { | |
| 615 *result_listener << "% 2 == " << (arg % 2); | |
| 616 return false; | |
| 617 } | |
| 618 } | |
| 619 | |
| 620 MATCHER_P2(EqSumOf, x, y, "") { | |
| 621 if (arg == (x + y)) { | |
| 622 *result_listener << "OK"; | |
| 623 return true; | |
| 624 } else { | |
| 625 // Verifies that we can stream to the underlying stream of | |
| 626 // result_listener. | |
| 627 if (result_listener->stream() != NULL) { | |
| 628 *result_listener->stream() << "diff == " << (x + y - arg); | |
| 629 } | |
| 630 return false; | |
| 631 } | |
| 632 } | |
| 633 | |
| 634 TEST(MatcherMacroTest, CanExplainMatchResult) { | |
| 635 const Matcher<int> m1 = IsEven2(); | |
| 636 EXPECT_EQ("OK", Explain(m1, 4)); | |
| 637 EXPECT_EQ("% 2 == 1", Explain(m1, 5)); | |
| 638 | |
| 639 const Matcher<int> m2 = EqSumOf(1, 2); | |
| 640 EXPECT_EQ("OK", Explain(m2, 3)); | |
| 641 EXPECT_EQ("diff == -1", Explain(m2, 4)); | |
| 642 } | |
| 643 | |
| 644 // Tests that the description string supplied to MATCHER() must be | |
| 645 // valid. | |
| 646 | |
| 647 MATCHER(HasBadDescription, "Invalid%") { | |
| 648 // Uses arg to suppress "unused parameter" warning. | |
| 649 return arg==arg; | |
| 650 } | |
| 651 | |
| 652 TEST(MatcherMacroTest, | |
| 653 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { | |
| 654 EXPECT_NONFATAL_FAILURE( | |
| 655 HasBadDescription(), | |
| 656 "Syntax error at index 7 in matcher description \"Invalid%\": " | |
| 657 "use \"%%\" instead of \"%\" to print \"%\"."); | |
| 658 } | |
| 659 | |
| 660 MATCHER(HasGoodDescription, "good") { return arg==arg; } | |
| 661 | |
| 662 TEST(MatcherMacroTest, AcceptsValidDescription) { | |
| 663 const Matcher<int> m = HasGoodDescription(); | |
| 664 EXPECT_EQ("good", Describe(m)); | |
| 665 } | |
| 666 | |
| 667 // Tests that the body of MATCHER() can reference the type of the | |
| 668 // value being matched. | |
| 669 | |
| 670 MATCHER(IsEmptyString, "") { | |
| 671 StaticAssertTypeEq< ::std::string, arg_type>(); | |
| 672 return arg == ""; | |
| 673 } | |
| 674 | |
| 675 MATCHER(IsEmptyStringByRef, "") { | |
| 676 StaticAssertTypeEq<const ::std::string&, arg_type>(); | |
| 677 return arg == ""; | |
| 678 } | |
| 679 | |
| 680 TEST(MatcherMacroTest, CanReferenceArgType) { | |
| 681 const Matcher< ::std::string> m1 = IsEmptyString(); | |
| 682 EXPECT_TRUE(m1.Matches("")); | |
| 683 | |
| 684 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef(); | |
| 685 EXPECT_TRUE(m2.Matches("")); | |
| 686 } | |
| 687 | |
| 688 // Tests that MATCHER() can be used in a namespace. | |
| 689 | |
| 690 namespace matcher_test { | |
| 691 MATCHER(IsOdd, "") { return (arg % 2) != 0; } | |
| 692 } // namespace matcher_test | |
| 693 | |
| 694 TEST(MatcherMacroTest, WorksInNamespace) { | |
| 695 Matcher<int> m = matcher_test::IsOdd(); | |
| 696 EXPECT_FALSE(m.Matches(4)); | |
| 697 EXPECT_TRUE(m.Matches(5)); | |
| 698 } | |
| 699 | |
| 700 // Tests that Value() can be used to compose matchers. | |
| 701 MATCHER(IsPositiveOdd, "") { | |
| 702 return Value(arg, matcher_test::IsOdd()) && arg > 0; | |
| 703 } | |
| 704 | |
| 705 TEST(MatcherMacroTest, CanBeComposedUsingValue) { | |
| 706 EXPECT_THAT(3, IsPositiveOdd()); | |
| 707 EXPECT_THAT(4, Not(IsPositiveOdd())); | |
| 708 EXPECT_THAT(-1, Not(IsPositiveOdd())); | |
| 709 } | |
| 710 | |
| 711 // Tests that a simple MATCHER_P() definition works. | |
| 712 | |
| 713 MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; } | |
| 714 | |
| 715 TEST(MatcherPMacroTest, Works) { | |
| 716 const Matcher<int> m = IsGreaterThan32And(5); | |
| 717 EXPECT_TRUE(m.Matches(36)); | |
| 718 EXPECT_FALSE(m.Matches(5)); | |
| 719 | |
| 720 EXPECT_EQ("is greater than 32 and 5", Describe(m)); | |
| 721 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m)); | |
| 722 EXPECT_EQ("", Explain(m, 36)); | |
| 723 EXPECT_EQ("", Explain(m, 5)); | |
| 724 } | |
| 725 | |
| 726 // Tests that the description string supplied to MATCHER_P() must be | |
| 727 // valid. | |
| 728 | |
| 729 MATCHER_P(HasBadDescription1, n, "not %(m)s good") { | |
| 730 return arg > n; | |
| 731 } | |
| 732 | |
| 733 TEST(MatcherPMacroTest, | |
| 734 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { | |
| 735 EXPECT_NONFATAL_FAILURE( | |
| 736 HasBadDescription1(2), | |
| 737 "Syntax error at index 6 in matcher description \"not %(m)s good\": " | |
| 738 "\"m\" is an invalid parameter name."); | |
| 739 } | |
| 740 | |
| 741 | |
| 742 MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return arg==arg; } | |
| 743 | |
| 744 TEST(MatcherPMacroTest, AcceptsValidDescription) { | |
| 745 const Matcher<int> m = HasGoodDescription1(5); | |
| 746 EXPECT_EQ("good 5", Describe(m)); | |
| 747 } | |
| 748 | |
| 749 // Tests that the description is calculated correctly from the matcher name. | |
| 750 MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; } | |
| 751 | |
| 752 TEST(MatcherPMacroTest, GeneratesCorrectDescription) { | |
| 753 const Matcher<int> m = _is_Greater_Than32and_(5); | |
| 754 | |
| 755 EXPECT_EQ("is greater than 32 and 5", Describe(m)); | |
| 756 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m)); | |
| 757 EXPECT_EQ("", Explain(m, 36)); | |
| 758 EXPECT_EQ("", Explain(m, 5)); | |
| 759 } | |
| 760 | |
| 761 // Tests that a MATCHER_P matcher can be explicitly instantiated with | |
| 762 // a reference parameter type. | |
| 763 | |
| 764 class UncopyableFoo { | |
| 765 public: | |
| 766 explicit UncopyableFoo(char value) : value_(value) {} | |
| 767 private: | |
| 768 UncopyableFoo(const UncopyableFoo&); | |
| 769 void operator=(const UncopyableFoo&); | |
| 770 | |
| 771 char value_; | |
| 772 }; | |
| 773 | |
| 774 MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; } | |
| 775 | |
| 776 TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) { | |
| 777 UncopyableFoo foo1('1'), foo2('2'); | |
| 778 const Matcher<const UncopyableFoo&> m = | |
| 779 ReferencesUncopyable<const UncopyableFoo&>(foo1); | |
| 780 | |
| 781 EXPECT_TRUE(m.Matches(foo1)); | |
| 782 EXPECT_FALSE(m.Matches(foo2)); | |
| 783 | |
| 784 // We don't want the address of the parameter printed, as most | |
| 785 // likely it will just annoy the user. If the address is | |
| 786 // interesting, the user should consider passing the parameter by | |
| 787 // pointer instead. | |
| 788 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m)); | |
| 789 } | |
| 790 | |
| 791 | |
| 792 // Tests that the description string supplied to MATCHER_Pn() must be | |
| 793 // valid. | |
| 794 | |
| 795 MATCHER_P2(HasBadDescription2, m, n, "not %(good") { | |
| 796 return arg > m + n; | |
| 797 } | |
| 798 | |
| 799 TEST(MatcherPnMacroTest, | |
| 800 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { | |
| 801 EXPECT_NONFATAL_FAILURE( | |
| 802 HasBadDescription2(3, 4), | |
| 803 "Syntax error at index 4 in matcher description \"not %(good\": " | |
| 804 "an interpolation must end with \")s\", but \"%(good\" does not."); | |
| 805 } | |
| 806 | |
| 807 MATCHER_P2(HasComplexDescription, foo, bar, | |
| 808 "is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") { | |
| 809 return arg==arg; | |
| 810 } | |
| 811 | |
| 812 TEST(MatcherPnMacroTest, AcceptsValidDescription) { | |
| 813 Matcher<int> m = HasComplexDescription(100, "ducks"); | |
| 814 EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)", | |
| 815 Describe(m)); | |
| 816 } | |
| 817 | |
| 818 // Tests that the body of MATCHER_Pn() can reference the parameter | |
| 819 // types. | |
| 820 | |
| 821 MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") { | |
| 822 StaticAssertTypeEq<int, foo_type>(); | |
| 823 StaticAssertTypeEq<long, bar_type>(); // NOLINT | |
| 824 StaticAssertTypeEq<char, baz_type>(); | |
| 825 return arg == 0; | |
| 826 } | |
| 827 | |
| 828 TEST(MatcherPnMacroTest, CanReferenceParamTypes) { | |
| 829 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a')); | |
| 830 } | |
| 831 | |
| 832 // Tests that a MATCHER_Pn matcher can be explicitly instantiated with | |
| 833 // reference parameter types. | |
| 834 | |
| 835 MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") { | |
| 836 return &arg == &variable1 || &arg == &variable2; | |
| 837 } | |
| 838 | |
| 839 TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) { | |
| 840 UncopyableFoo foo1('1'), foo2('2'), foo3('3'); | |
| 841 const Matcher<const UncopyableFoo&> m = | |
| 842 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); | |
| 843 | |
| 844 EXPECT_TRUE(m.Matches(foo1)); | |
| 845 EXPECT_TRUE(m.Matches(foo2)); | |
| 846 EXPECT_FALSE(m.Matches(foo3)); | |
| 847 } | |
| 848 | |
| 849 TEST(MatcherPnMacroTest, | |
| 850 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) { | |
| 851 UncopyableFoo foo1('1'), foo2('2'); | |
| 852 const Matcher<const UncopyableFoo&> m = | |
| 853 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); | |
| 854 | |
| 855 // We don't want the addresses of the parameters printed, as most | |
| 856 // likely they will just annoy the user. If the addresses are | |
| 857 // interesting, the user should consider passing the parameters by | |
| 858 // pointers instead. | |
| 859 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)", | |
| 860 Describe(m)); | |
| 861 } | |
| 862 | |
| 863 // Tests that a simple MATCHER_P2() definition works. | |
| 864 | |
| 865 MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; } | |
| 866 | |
| 867 TEST(MatcherPnMacroTest, Works) { | |
| 868 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT | |
| 869 EXPECT_TRUE(m.Matches(36L)); | |
| 870 EXPECT_FALSE(m.Matches(15L)); | |
| 871 | |
| 872 EXPECT_EQ("is not in closed range (10, 20)", Describe(m)); | |
| 873 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m)); | |
| 874 EXPECT_EQ("", Explain(m, 36L)); | |
| 875 EXPECT_EQ("", Explain(m, 15L)); | |
| 876 } | |
| 877 | |
| 878 // Tests that MATCHER*() definitions can be overloaded on the number | |
| 879 // of parameters; also tests MATCHER_Pn() where n >= 3. | |
| 880 | |
| 881 MATCHER(EqualsSumOf, "") { return arg == 0; } | |
| 882 MATCHER_P(EqualsSumOf, a, "") { return arg == a; } | |
| 883 MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; } | |
| 884 MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; } | |
| 885 MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; } | |
| 886 MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; } | |
| 887 MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") { | |
| 888 return arg == a + b + c + d + e + f; | |
| 889 } | |
| 890 MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") { | |
| 891 return arg == a + b + c + d + e + f + g; | |
| 892 } | |
| 893 MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") { | |
| 894 return arg == a + b + c + d + e + f + g + h; | |
| 895 } | |
| 896 MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") { | |
| 897 return arg == a + b + c + d + e + f + g + h + i; | |
| 898 } | |
| 899 MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") { | |
| 900 return arg == a + b + c + d + e + f + g + h + i + j; | |
| 901 } | |
| 902 | |
| 903 TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) { | |
| 904 EXPECT_THAT(0, EqualsSumOf()); | |
| 905 EXPECT_THAT(1, EqualsSumOf(1)); | |
| 906 EXPECT_THAT(12, EqualsSumOf(10, 2)); | |
| 907 EXPECT_THAT(123, EqualsSumOf(100, 20, 3)); | |
| 908 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4)); | |
| 909 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5)); | |
| 910 EXPECT_THAT("abcdef", | |
| 911 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')); | |
| 912 EXPECT_THAT("abcdefg", | |
| 913 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g')); | |
| 914 EXPECT_THAT("abcdefgh", | |
| 915 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', | |
| 916 "h")); | |
| 917 EXPECT_THAT("abcdefghi", | |
| 918 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', | |
| 919 "h", 'i')); | |
| 920 EXPECT_THAT("abcdefghij", | |
| 921 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', | |
| 922 "h", 'i', ::std::string("j"))); | |
| 923 | |
| 924 EXPECT_THAT(1, Not(EqualsSumOf())); | |
| 925 EXPECT_THAT(-1, Not(EqualsSumOf(1))); | |
| 926 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2))); | |
| 927 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3))); | |
| 928 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4))); | |
| 929 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5))); | |
| 930 EXPECT_THAT("abcdef ", | |
| 931 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'))); | |
| 932 EXPECT_THAT("abcdefg ", | |
| 933 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', | |
| 934 'g'))); | |
| 935 EXPECT_THAT("abcdefgh ", | |
| 936 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', | |
| 937 "h"))); | |
| 938 EXPECT_THAT("abcdefghi ", | |
| 939 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', | |
| 940 "h", 'i'))); | |
| 941 EXPECT_THAT("abcdefghij ", | |
| 942 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', | |
| 943 "h", 'i', ::std::string("j")))); | |
| 944 } | |
| 945 | |
| 946 // Tests that a MATCHER_Pn() definition can be instantiated with any | |
| 947 // compatible parameter types. | |
| 948 TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) { | |
| 949 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3))); | |
| 950 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d")); | |
| 951 | |
| 952 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3)))); | |
| 953 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d"))); | |
| 954 } | |
| 955 | |
| 956 // Tests that the matcher body can promote the parameter types. | |
| 957 | |
| 958 MATCHER_P2(EqConcat, prefix, suffix, "") { | |
| 959 // The following lines promote the two parameters to desired types. | |
| 960 std::string prefix_str(prefix); | |
| 961 char suffix_char = static_cast<char>(suffix); | |
| 962 return arg == prefix_str + suffix_char; | |
| 963 } | |
| 964 | |
| 965 TEST(MatcherPnMacroTest, SimpleTypePromotion) { | |
| 966 Matcher<std::string> no_promo = | |
| 967 EqConcat(std::string("foo"), 't'); | |
| 968 Matcher<const std::string&> promo = | |
| 969 EqConcat("foo", static_cast<int>('t')); | |
| 970 EXPECT_FALSE(no_promo.Matches("fool")); | |
| 971 EXPECT_FALSE(promo.Matches("fool")); | |
| 972 EXPECT_TRUE(no_promo.Matches("foot")); | |
| 973 EXPECT_TRUE(promo.Matches("foot")); | |
| 974 } | |
| 975 | |
| 976 // Verifies the type of a MATCHER*. | |
| 977 | |
| 978 TEST(MatcherPnMacroTest, TypesAreCorrect) { | |
| 979 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable. | |
| 980 EqualsSumOfMatcher a0 = EqualsSumOf(); | |
| 981 | |
| 982 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable. | |
| 983 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1); | |
| 984 | |
| 985 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk | |
| 986 // variable, and so on. | |
| 987 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2'); | |
| 988 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3'); | |
| 989 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4'); | |
| 990 EqualsSumOfMatcherP5<int, int, int, int, char> a5 = | |
| 991 EqualsSumOf(1, 2, 3, 4, '5'); | |
| 992 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 = | |
| 993 EqualsSumOf(1, 2, 3, 4, 5, '6'); | |
| 994 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 = | |
| 995 EqualsSumOf(1, 2, 3, 4, 5, 6, '7'); | |
| 996 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 = | |
| 997 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8'); | |
| 998 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 = | |
| 999 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9'); | |
| 1000 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 = | |
| 1001 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); | |
| 1002 } | |
| 1003 | |
| 1004 // Tests that matcher-typed parameters can be used in Value() inside a | |
| 1005 // MATCHER_Pn definition. | |
| 1006 | |
| 1007 // Succeeds if arg matches exactly 2 of the 3 matchers. | |
| 1008 MATCHER_P3(TwoOf, m1, m2, m3, "") { | |
| 1009 const int count = static_cast<int>(Value(arg, m1)) | |
| 1010 + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3)); | |
| 1011 return count == 2; | |
| 1012 } | |
| 1013 | |
| 1014 TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) { | |
| 1015 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10))); | |
| 1016 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0)))); | |
| 1017 } | |
| 1018 | |
| 1019 // Tests Contains(). | |
| 1020 | |
| 1021 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) { | |
| 1022 list<int> some_list; | |
| 1023 some_list.push_back(3); | |
| 1024 some_list.push_back(1); | |
| 1025 some_list.push_back(2); | |
| 1026 EXPECT_THAT(some_list, Contains(1)); | |
| 1027 EXPECT_THAT(some_list, Contains(Gt(2.5))); | |
| 1028 EXPECT_THAT(some_list, Contains(Eq(2.0f))); | |
| 1029 | |
| 1030 list<string> another_list; | |
| 1031 another_list.push_back("fee"); | |
| 1032 another_list.push_back("fie"); | |
| 1033 another_list.push_back("foe"); | |
| 1034 another_list.push_back("fum"); | |
| 1035 EXPECT_THAT(another_list, Contains(string("fee"))); | |
| 1036 } | |
| 1037 | |
| 1038 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) { | |
| 1039 list<int> some_list; | |
| 1040 some_list.push_back(3); | |
| 1041 some_list.push_back(1); | |
| 1042 EXPECT_THAT(some_list, Not(Contains(4))); | |
| 1043 } | |
| 1044 | |
| 1045 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) { | |
| 1046 set<int> some_set; | |
| 1047 some_set.insert(3); | |
| 1048 some_set.insert(1); | |
| 1049 some_set.insert(2); | |
| 1050 EXPECT_THAT(some_set, Contains(Eq(1.0))); | |
| 1051 EXPECT_THAT(some_set, Contains(Eq(3.0f))); | |
| 1052 EXPECT_THAT(some_set, Contains(2)); | |
| 1053 | |
| 1054 set<const char*> another_set; | |
| 1055 another_set.insert("fee"); | |
| 1056 another_set.insert("fie"); | |
| 1057 another_set.insert("foe"); | |
| 1058 another_set.insert("fum"); | |
| 1059 EXPECT_THAT(another_set, Contains(Eq(string("fum")))); | |
| 1060 } | |
| 1061 | |
| 1062 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { | |
| 1063 set<int> some_set; | |
| 1064 some_set.insert(3); | |
| 1065 some_set.insert(1); | |
| 1066 EXPECT_THAT(some_set, Not(Contains(4))); | |
| 1067 | |
| 1068 set<const char*> c_string_set; | |
| 1069 c_string_set.insert("hello"); | |
| 1070 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str()))); | |
| 1071 } | |
| 1072 | |
| 1073 TEST(ContainsTest, ExplainsMatchResultCorrectly) { | |
| 1074 const int a[2] = { 1, 2 }; | |
| 1075 Matcher<const int(&)[2]> m = Contains(2); | |
| 1076 EXPECT_EQ("whose element #1 matches", Explain(m, a)); | |
| 1077 | |
| 1078 m = Contains(3); | |
| 1079 EXPECT_EQ("", Explain(m, a)); | |
| 1080 | |
| 1081 m = Contains(GreaterThan(0)); | |
| 1082 EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a)); | |
| 1083 | |
| 1084 m = Contains(GreaterThan(10)); | |
| 1085 EXPECT_EQ("", Explain(m, a)); | |
| 1086 } | |
| 1087 | |
| 1088 TEST(ContainsTest, DescribesItselfCorrectly) { | |
| 1089 Matcher<vector<int> > m = Contains(1); | |
| 1090 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m)); | |
| 1091 | |
| 1092 Matcher<vector<int> > m2 = Not(m); | |
| 1093 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2)); | |
| 1094 } | |
| 1095 | |
| 1096 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { | |
| 1097 map<const char*, int> my_map; | |
| 1098 const char* bar = "a string"; | |
| 1099 my_map[bar] = 2; | |
| 1100 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2))); | |
| 1101 | |
| 1102 map<string, int> another_map; | |
| 1103 another_map["fee"] = 1; | |
| 1104 another_map["fie"] = 2; | |
| 1105 another_map["foe"] = 3; | |
| 1106 another_map["fum"] = 4; | |
| 1107 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1))); | |
| 1108 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2))); | |
| 1109 } | |
| 1110 | |
| 1111 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) { | |
| 1112 map<int, int> some_map; | |
| 1113 some_map[1] = 11; | |
| 1114 some_map[2] = 22; | |
| 1115 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23)))); | |
| 1116 } | |
| 1117 | |
| 1118 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) { | |
| 1119 const char* string_array[] = { "fee", "fie", "foe", "fum" }; | |
| 1120 EXPECT_THAT(string_array, Contains(Eq(string("fum")))); | |
| 1121 } | |
| 1122 | |
| 1123 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) { | |
| 1124 int int_array[] = { 1, 2, 3, 4 }; | |
| 1125 EXPECT_THAT(int_array, Not(Contains(5))); | |
| 1126 } | |
| 1127 | |
| 1128 TEST(ContainsTest, AcceptsMatcher) { | |
| 1129 const int a[] = { 1, 2, 3 }; | |
| 1130 EXPECT_THAT(a, Contains(Gt(2))); | |
| 1131 EXPECT_THAT(a, Not(Contains(Gt(4)))); | |
| 1132 } | |
| 1133 | |
| 1134 TEST(ContainsTest, WorksForNativeArrayAsTuple) { | |
| 1135 const int a[] = { 1, 2 }; | |
| 1136 const int* const pointer = a; | |
| 1137 EXPECT_THAT(make_tuple(pointer, 2), Contains(1)); | |
| 1138 EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3)))); | |
| 1139 } | |
| 1140 | |
| 1141 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { | |
| 1142 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; | |
| 1143 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6))); | |
| 1144 EXPECT_THAT(a, Contains(Contains(5))); | |
| 1145 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); | |
| 1146 EXPECT_THAT(a, Contains(Not(Contains(5)))); | |
| 1147 } | |
| 1148 | |
| 1149 #ifdef _MSC_VER | |
| 1150 #pragma warning(pop) | |
| 1151 #endif | |
| 1152 | |
| 1153 } // namespace | |
| OLD | NEW |