1 // Copyright 2007, 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 // Author: wan (at) google.com (Zhanyong Wan) 31 32 // Google Mock - a framework for writing C++ mock classes. 33 // 34 // This file tests the built-in actions. 35 36 #include "gmock/gmock-actions.h" 37 #include <algorithm> 38 #include <iterator> 39 #include <string> 40 #include "gmock/gmock.h" 41 #include "gmock/internal/gmock-port.h" 42 #include "gtest/gtest.h" 43 #include "gtest/gtest-spi.h" 44 45 namespace { 46 47 using ::std::tr1::get; 48 using ::std::tr1::make_tuple; 49 using ::std::tr1::tuple; 50 using ::std::tr1::tuple_element; 51 using testing::internal::BuiltInDefaultValue; 52 using testing::internal::Int64; 53 using testing::internal::UInt64; 54 // This list should be kept sorted. 55 using testing::_; 56 using testing::Action; 57 using testing::ActionInterface; 58 using testing::Assign; 59 using testing::ByRef; 60 using testing::DefaultValue; 61 using testing::DoDefault; 62 using testing::IgnoreResult; 63 using testing::Invoke; 64 using testing::InvokeWithoutArgs; 65 using testing::MakePolymorphicAction; 66 using testing::Ne; 67 using testing::PolymorphicAction; 68 using testing::Return; 69 using testing::ReturnNull; 70 using testing::ReturnRef; 71 using testing::ReturnRefOfCopy; 72 using testing::SetArgPointee; 73 using testing::SetArgumentPointee; 74 75 #if !GTEST_OS_WINDOWS_MOBILE 76 using testing::SetErrnoAndReturn; 77 #endif 78 79 #if GTEST_HAS_PROTOBUF_ 80 using testing::internal::TestMessage; 81 #endif // GTEST_HAS_PROTOBUF_ 82 83 // Tests that BuiltInDefaultValue<T*>::Get() returns NULL. 84 TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) { 85 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL); 86 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL); 87 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL); 88 } 89 90 // Tests that BuiltInDefaultValue<T*>::Exists() return true. 91 TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) { 92 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists()); 93 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists()); 94 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists()); 95 } 96 97 // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a 98 // built-in numeric type. 99 TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) { 100 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get()); 101 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get()); 102 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get()); 103 #if GMOCK_HAS_SIGNED_WCHAR_T_ 104 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get()); 105 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get()); 106 #endif 107 #if GMOCK_WCHAR_T_IS_NATIVE_ 108 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get()); 109 #endif 110 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT 111 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT 112 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT 113 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get()); 114 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get()); 115 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get()); 116 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT 117 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT 118 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT 119 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get()); 120 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get()); 121 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get()); 122 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get()); 123 } 124 125 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a 126 // built-in numeric type. 127 TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) { 128 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists()); 129 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists()); 130 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists()); 131 #if GMOCK_HAS_SIGNED_WCHAR_T_ 132 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists()); 133 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists()); 134 #endif 135 #if GMOCK_WCHAR_T_IS_NATIVE_ 136 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists()); 137 #endif 138 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT 139 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT 140 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT 141 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists()); 142 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists()); 143 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists()); 144 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT 145 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT 146 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT 147 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists()); 148 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists()); 149 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists()); 150 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists()); 151 } 152 153 // Tests that BuiltInDefaultValue<bool>::Get() returns false. 154 TEST(BuiltInDefaultValueTest, IsFalseForBool) { 155 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get()); 156 } 157 158 // Tests that BuiltInDefaultValue<bool>::Exists() returns true. 159 TEST(BuiltInDefaultValueTest, BoolExists) { 160 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists()); 161 } 162 163 // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a 164 // string type. 165 TEST(BuiltInDefaultValueTest, IsEmptyStringForString) { 166 #if GTEST_HAS_GLOBAL_STRING 167 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get()); 168 #endif // GTEST_HAS_GLOBAL_STRING 169 170 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get()); 171 } 172 173 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a 174 // string type. 175 TEST(BuiltInDefaultValueTest, ExistsForString) { 176 #if GTEST_HAS_GLOBAL_STRING 177 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists()); 178 #endif // GTEST_HAS_GLOBAL_STRING 179 180 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists()); 181 } 182 183 // Tests that BuiltInDefaultValue<const T>::Get() returns the same 184 // value as BuiltInDefaultValue<T>::Get() does. 185 TEST(BuiltInDefaultValueTest, WorksForConstTypes) { 186 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get()); 187 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get()); 188 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL); 189 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get()); 190 } 191 192 // Tests that BuiltInDefaultValue<T>::Get() aborts the program with 193 // the correct error message when T is a user-defined type. 194 struct UserType { 195 UserType() : value(0) {} 196 197 int value; 198 }; 199 200 TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) { 201 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists()); 202 } 203 204 // Tests that BuiltInDefaultValue<T&>::Get() aborts the program. 205 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) { 206 EXPECT_DEATH_IF_SUPPORTED({ 207 BuiltInDefaultValue<int&>::Get(); 208 }, ""); 209 EXPECT_DEATH_IF_SUPPORTED({ 210 BuiltInDefaultValue<const char&>::Get(); 211 }, ""); 212 } 213 214 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) { 215 EXPECT_DEATH_IF_SUPPORTED({ 216 BuiltInDefaultValue<UserType>::Get(); 217 }, ""); 218 } 219 220 // Tests that DefaultValue<T>::IsSet() is false initially. 221 TEST(DefaultValueTest, IsInitiallyUnset) { 222 EXPECT_FALSE(DefaultValue<int>::IsSet()); 223 EXPECT_FALSE(DefaultValue<const UserType>::IsSet()); 224 } 225 226 // Tests that DefaultValue<T> can be set and then unset. 227 TEST(DefaultValueTest, CanBeSetAndUnset) { 228 EXPECT_TRUE(DefaultValue<int>::Exists()); 229 EXPECT_FALSE(DefaultValue<const UserType>::Exists()); 230 231 DefaultValue<int>::Set(1); 232 DefaultValue<const UserType>::Set(UserType()); 233 234 EXPECT_EQ(1, DefaultValue<int>::Get()); 235 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value); 236 237 EXPECT_TRUE(DefaultValue<int>::Exists()); 238 EXPECT_TRUE(DefaultValue<const UserType>::Exists()); 239 240 DefaultValue<int>::Clear(); 241 DefaultValue<const UserType>::Clear(); 242 243 EXPECT_FALSE(DefaultValue<int>::IsSet()); 244 EXPECT_FALSE(DefaultValue<const UserType>::IsSet()); 245 246 EXPECT_TRUE(DefaultValue<int>::Exists()); 247 EXPECT_FALSE(DefaultValue<const UserType>::Exists()); 248 } 249 250 // Tests that DefaultValue<T>::Get() returns the 251 // BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is 252 // false. 253 TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { 254 EXPECT_FALSE(DefaultValue<int>::IsSet()); 255 EXPECT_TRUE(DefaultValue<int>::Exists()); 256 EXPECT_FALSE(DefaultValue<UserType>::IsSet()); 257 EXPECT_FALSE(DefaultValue<UserType>::Exists()); 258 259 EXPECT_EQ(0, DefaultValue<int>::Get()); 260 261 EXPECT_DEATH_IF_SUPPORTED({ 262 DefaultValue<UserType>::Get(); 263 }, ""); 264 } 265 266 // Tests that DefaultValue<void>::Get() returns void. 267 TEST(DefaultValueTest, GetWorksForVoid) { 268 return DefaultValue<void>::Get(); 269 } 270 271 // Tests using DefaultValue with a reference type. 272 273 // Tests that DefaultValue<T&>::IsSet() is false initially. 274 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) { 275 EXPECT_FALSE(DefaultValue<int&>::IsSet()); 276 EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); 277 } 278 279 // Tests that DefaultValue<T&>::Exists is false initiallly. 280 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) { 281 EXPECT_FALSE(DefaultValue<int&>::Exists()); 282 EXPECT_FALSE(DefaultValue<UserType&>::Exists()); 283 } 284 285 // Tests that DefaultValue<T&> can be set and then unset. 286 TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) { 287 int n = 1; 288 DefaultValue<const int&>::Set(n); 289 UserType u; 290 DefaultValue<UserType&>::Set(u); 291 292 EXPECT_TRUE(DefaultValue<const int&>::Exists()); 293 EXPECT_TRUE(DefaultValue<UserType&>::Exists()); 294 295 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get())); 296 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get())); 297 298 DefaultValue<const int&>::Clear(); 299 DefaultValue<UserType&>::Clear(); 300 301 EXPECT_FALSE(DefaultValue<const int&>::Exists()); 302 EXPECT_FALSE(DefaultValue<UserType&>::Exists()); 303 304 EXPECT_FALSE(DefaultValue<const int&>::IsSet()); 305 EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); 306 } 307 308 // Tests that DefaultValue<T&>::Get() returns the 309 // BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is 310 // false. 311 TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { 312 EXPECT_FALSE(DefaultValue<int&>::IsSet()); 313 EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); 314 315 EXPECT_DEATH_IF_SUPPORTED({ 316 DefaultValue<int&>::Get(); 317 }, ""); 318 EXPECT_DEATH_IF_SUPPORTED({ 319 DefaultValue<UserType>::Get(); 320 }, ""); 321 } 322 323 // Tests that ActionInterface can be implemented by defining the 324 // Perform method. 325 326 typedef int MyFunction(bool, int); 327 328 class MyActionImpl : public ActionInterface<MyFunction> { 329 public: 330 virtual int Perform(const tuple<bool, int>& args) { 331 return get<0>(args) ? get<1>(args) : 0; 332 } 333 }; 334 335 TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) { 336 MyActionImpl my_action_impl; 337 (void)my_action_impl; 338 } 339 340 TEST(ActionInterfaceTest, MakeAction) { 341 Action<MyFunction> action = MakeAction(new MyActionImpl); 342 343 // When exercising the Perform() method of Action<F>, we must pass 344 // it a tuple whose size and type are compatible with F's argument 345 // types. For example, if F is int(), then Perform() takes a 346 // 0-tuple; if F is void(bool, int), then Perform() takes a 347 // tuple<bool, int>, and so on. 348 EXPECT_EQ(5, action.Perform(make_tuple(true, 5))); 349 } 350 351 // Tests that Action<F> can be contructed from a pointer to 352 // ActionInterface<F>. 353 TEST(ActionTest, CanBeConstructedFromActionInterface) { 354 Action<MyFunction> action(new MyActionImpl); 355 } 356 357 // Tests that Action<F> delegates actual work to ActionInterface<F>. 358 TEST(ActionTest, DelegatesWorkToActionInterface) { 359 const Action<MyFunction> action(new MyActionImpl); 360 361 EXPECT_EQ(5, action.Perform(make_tuple(true, 5))); 362 EXPECT_EQ(0, action.Perform(make_tuple(false, 1))); 363 } 364 365 // Tests that Action<F> can be copied. 366 TEST(ActionTest, IsCopyable) { 367 Action<MyFunction> a1(new MyActionImpl); 368 Action<MyFunction> a2(a1); // Tests the copy constructor. 369 370 // a1 should continue to work after being copied from. 371 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5))); 372 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1))); 373 374 // a2 should work like the action it was copied from. 375 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5))); 376 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1))); 377 378 a2 = a1; // Tests the assignment operator. 379 380 // a1 should continue to work after being copied from. 381 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5))); 382 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1))); 383 384 // a2 should work like the action it was copied from. 385 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5))); 386 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1))); 387 } 388 389 // Tests that an Action<From> object can be converted to a 390 // compatible Action<To> object. 391 392 class IsNotZero : public ActionInterface<bool(int)> { // NOLINT 393 public: 394 virtual bool Perform(const tuple<int>& arg) { 395 return get<0>(arg) != 0; 396 } 397 }; 398 399 #if !GTEST_OS_SYMBIAN 400 // Compiling this test on Nokia's Symbian compiler fails with: 401 // 'Result' is not a member of class 'testing::internal::Function<int>' 402 // (point of instantiation: '@unnamed@gmock_actions_test_cc@:: 403 // ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()') 404 // with no obvious fix. 405 TEST(ActionTest, CanBeConvertedToOtherActionType) { 406 const Action<bool(int)> a1(new IsNotZero); // NOLINT 407 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT 408 EXPECT_EQ(1, a2.Perform(make_tuple('a'))); 409 EXPECT_EQ(0, a2.Perform(make_tuple('\0'))); 410 } 411 #endif // !GTEST_OS_SYMBIAN 412 413 // The following two classes are for testing MakePolymorphicAction(). 414 415 // Implements a polymorphic action that returns the second of the 416 // arguments it receives. 417 class ReturnSecondArgumentAction { 418 public: 419 // We want to verify that MakePolymorphicAction() can work with a 420 // polymorphic action whose Perform() method template is either 421 // const or not. This lets us verify the non-const case. 422 template <typename Result, typename ArgumentTuple> 423 Result Perform(const ArgumentTuple& args) { return get<1>(args); } 424 }; 425 426 // Implements a polymorphic action that can be used in a nullary 427 // function to return 0. 428 class ReturnZeroFromNullaryFunctionAction { 429 public: 430 // For testing that MakePolymorphicAction() works when the 431 // implementation class' Perform() method template takes only one 432 // template parameter. 433 // 434 // We want to verify that MakePolymorphicAction() can work with a 435 // polymorphic action whose Perform() method template is either 436 // const or not. This lets us verify the const case. 437 template <typename Result> 438 Result Perform(const tuple<>&) const { return 0; } 439 }; 440 441 // These functions verify that MakePolymorphicAction() returns a 442 // PolymorphicAction<T> where T is the argument's type. 443 444 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() { 445 return MakePolymorphicAction(ReturnSecondArgumentAction()); 446 } 447 448 PolymorphicAction<ReturnZeroFromNullaryFunctionAction> 449 ReturnZeroFromNullaryFunction() { 450 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction()); 451 } 452 453 // Tests that MakePolymorphicAction() turns a polymorphic action 454 // implementation class into a polymorphic action. 455 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) { 456 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT 457 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0))); 458 } 459 460 // Tests that MakePolymorphicAction() works when the implementation 461 // class' Perform() method template has only one template parameter. 462 TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) { 463 Action<int()> a1 = ReturnZeroFromNullaryFunction(); 464 EXPECT_EQ(0, a1.Perform(make_tuple())); 465 466 Action<void*()> a2 = ReturnZeroFromNullaryFunction(); 467 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL); 468 } 469 470 // Tests that Return() works as an action for void-returning 471 // functions. 472 TEST(ReturnTest, WorksForVoid) { 473 const Action<void(int)> ret = Return(); // NOLINT 474 return ret.Perform(make_tuple(1)); 475 } 476 477 // Tests that Return(v) returns v. 478 TEST(ReturnTest, ReturnsGivenValue) { 479 Action<int()> ret = Return(1); // NOLINT 480 EXPECT_EQ(1, ret.Perform(make_tuple())); 481 482 ret = Return(-5); 483 EXPECT_EQ(-5, ret.Perform(make_tuple())); 484 } 485 486 // Tests that Return("string literal") works. 487 TEST(ReturnTest, AcceptsStringLiteral) { 488 Action<const char*()> a1 = Return("Hello"); 489 EXPECT_STREQ("Hello", a1.Perform(make_tuple())); 490 491 Action<std::string()> a2 = Return("world"); 492 EXPECT_EQ("world", a2.Perform(make_tuple())); 493 } 494 495 // Tests that Return(v) is covaraint. 496 497 struct Base { 498 bool operator==(const Base&) { return true; } 499 }; 500 501 struct Derived : public Base { 502 bool operator==(const Derived&) { return true; } 503 }; 504 505 TEST(ReturnTest, IsCovariant) { 506 Base base; 507 Derived derived; 508 Action<Base*()> ret = Return(&base); 509 EXPECT_EQ(&base, ret.Perform(make_tuple())); 510 511 ret = Return(&derived); 512 EXPECT_EQ(&derived, ret.Perform(make_tuple())); 513 } 514 515 // Tests that the type of the value passed into Return is converted into T 516 // when the action is cast to Action<T(...)> rather than when the action is 517 // performed. See comments on testing::internal::ReturnAction in 518 // gmock-actions.h for more information. 519 class FromType { 520 public: 521 FromType(bool* is_converted) : converted_(is_converted) {} 522 bool* converted() const { return converted_; } 523 524 private: 525 bool* const converted_; 526 527 GTEST_DISALLOW_ASSIGN_(FromType); 528 }; 529 530 class ToType { 531 public: 532 ToType(const FromType& x) { *x.converted() = true; } 533 }; 534 535 TEST(ReturnTest, ConvertsArgumentWhenConverted) { 536 bool converted = false; 537 FromType x(&converted); 538 Action<ToType()> action(Return(x)); 539 EXPECT_TRUE(converted) << "Return must convert its argument in its own " 540 << "conversion operator."; 541 converted = false; 542 action.Perform(tuple<>()); 543 EXPECT_FALSE(converted) << "Action must NOT convert its argument " 544 << "when performed." ; 545 } 546 547 class DestinationType {}; 548 549 class SourceType { 550 public: 551 // Note: a non-const typecast operator. 552 operator DestinationType() { return DestinationType(); } 553 }; 554 555 TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) { 556 SourceType s; 557 Action<DestinationType()> action(Return(s)); 558 } 559 560 // Tests that ReturnNull() returns NULL in a pointer-returning function. 561 TEST(ReturnNullTest, WorksInPointerReturningFunction) { 562 const Action<int*()> a1 = ReturnNull(); 563 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL); 564 565 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT 566 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL); 567 } 568 569 // Tests that ReturnRef(v) works for reference types. 570 TEST(ReturnRefTest, WorksForReference) { 571 const int n = 0; 572 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT 573 574 EXPECT_EQ(&n, &ret.Perform(make_tuple(true))); 575 } 576 577 // Tests that ReturnRef(v) is covariant. 578 TEST(ReturnRefTest, IsCovariant) { 579 Base base; 580 Derived derived; 581 Action<Base&()> a = ReturnRef(base); 582 EXPECT_EQ(&base, &a.Perform(make_tuple())); 583 584 a = ReturnRef(derived); 585 EXPECT_EQ(&derived, &a.Perform(make_tuple())); 586 } 587 588 // Tests that ReturnRefOfCopy(v) works for reference types. 589 TEST(ReturnRefOfCopyTest, WorksForReference) { 590 int n = 42; 591 const Action<const int&()> ret = ReturnRefOfCopy(n); 592 593 EXPECT_NE(&n, &ret.Perform(make_tuple())); 594 EXPECT_EQ(42, ret.Perform(make_tuple())); 595 596 n = 43; 597 EXPECT_NE(&n, &ret.Perform(make_tuple())); 598 EXPECT_EQ(42, ret.Perform(make_tuple())); 599 } 600 601 // Tests that ReturnRefOfCopy(v) is covariant. 602 TEST(ReturnRefOfCopyTest, IsCovariant) { 603 Base base; 604 Derived derived; 605 Action<Base&()> a = ReturnRefOfCopy(base); 606 EXPECT_NE(&base, &a.Perform(make_tuple())); 607 608 a = ReturnRefOfCopy(derived); 609 EXPECT_NE(&derived, &a.Perform(make_tuple())); 610 } 611 612 // Tests that DoDefault() does the default action for the mock method. 613 614 class MyClass {}; 615 616 class MockClass { 617 public: 618 MockClass() {} 619 620 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT 621 MOCK_METHOD0(Foo, MyClass()); 622 623 private: 624 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass); 625 }; 626 627 // Tests that DoDefault() returns the built-in default value for the 628 // return type by default. 629 TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) { 630 MockClass mock; 631 EXPECT_CALL(mock, IntFunc(_)) 632 .WillOnce(DoDefault()); 633 EXPECT_EQ(0, mock.IntFunc(true)); 634 } 635 636 // Tests that DoDefault() aborts the process when there is no built-in 637 // default value for the return type. 638 TEST(DoDefaultDeathTest, DiesForUnknowType) { 639 MockClass mock; 640 EXPECT_CALL(mock, Foo()) 641 .WillRepeatedly(DoDefault()); 642 EXPECT_DEATH_IF_SUPPORTED({ 643 mock.Foo(); 644 }, ""); 645 } 646 647 // Tests that using DoDefault() inside a composite action leads to a 648 // run-time error. 649 650 void VoidFunc(bool /* flag */) {} 651 652 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) { 653 MockClass mock; 654 EXPECT_CALL(mock, IntFunc(_)) 655 .WillRepeatedly(DoAll(Invoke(VoidFunc), 656 DoDefault())); 657 658 // Ideally we should verify the error message as well. Sadly, 659 // EXPECT_DEATH() can only capture stderr, while Google Mock's 660 // errors are printed on stdout. Therefore we have to settle for 661 // not verifying the message. 662 EXPECT_DEATH_IF_SUPPORTED({ 663 mock.IntFunc(true); 664 }, ""); 665 } 666 667 // Tests that DoDefault() returns the default value set by 668 // DefaultValue<T>::Set() when it's not overriden by an ON_CALL(). 669 TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) { 670 DefaultValue<int>::Set(1); 671 MockClass mock; 672 EXPECT_CALL(mock, IntFunc(_)) 673 .WillOnce(DoDefault()); 674 EXPECT_EQ(1, mock.IntFunc(false)); 675 DefaultValue<int>::Clear(); 676 } 677 678 // Tests that DoDefault() does the action specified by ON_CALL(). 679 TEST(DoDefaultTest, DoesWhatOnCallSpecifies) { 680 MockClass mock; 681 ON_CALL(mock, IntFunc(_)) 682 .WillByDefault(Return(2)); 683 EXPECT_CALL(mock, IntFunc(_)) 684 .WillOnce(DoDefault()); 685 EXPECT_EQ(2, mock.IntFunc(false)); 686 } 687 688 // Tests that using DoDefault() in ON_CALL() leads to a run-time failure. 689 TEST(DoDefaultTest, CannotBeUsedInOnCall) { 690 MockClass mock; 691 EXPECT_NONFATAL_FAILURE({ // NOLINT 692 ON_CALL(mock, IntFunc(_)) 693 .WillByDefault(DoDefault()); 694 }, "DoDefault() cannot be used in ON_CALL()"); 695 } 696 697 // Tests that SetArgPointee<N>(v) sets the variable pointed to by 698 // the N-th (0-based) argument to v. 699 TEST(SetArgPointeeTest, SetsTheNthPointee) { 700 typedef void MyFunction(bool, int*, char*); 701 Action<MyFunction> a = SetArgPointee<1>(2); 702 703 int n = 0; 704 char ch = '\0'; 705 a.Perform(make_tuple(true, &n, &ch)); 706 EXPECT_EQ(2, n); 707 EXPECT_EQ('\0', ch); 708 709 a = SetArgPointee<2>('a'); 710 n = 0; 711 ch = '\0'; 712 a.Perform(make_tuple(true, &n, &ch)); 713 EXPECT_EQ(0, n); 714 EXPECT_EQ('a', ch); 715 } 716 717 #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN) 718 // Tests that SetArgPointee<N>() accepts a string literal. 719 // GCC prior to v4.0 and the Symbian compiler do not support this. 720 TEST(SetArgPointeeTest, AcceptsStringLiteral) { 721 typedef void MyFunction(std::string*, const char**); 722 Action<MyFunction> a = SetArgPointee<0>("hi"); 723 std::string str; 724 const char* ptr = NULL; 725 a.Perform(make_tuple(&str, &ptr)); 726 EXPECT_EQ("hi", str); 727 EXPECT_TRUE(ptr == NULL); 728 729 a = SetArgPointee<1>("world"); 730 str = ""; 731 a.Perform(make_tuple(&str, &ptr)); 732 EXPECT_EQ("", str); 733 EXPECT_STREQ("world", ptr); 734 } 735 736 TEST(SetArgPointeeTest, AcceptsWideStringLiteral) { 737 typedef void MyFunction(const wchar_t**); 738 Action<MyFunction> a = SetArgPointee<0>(L"world"); 739 const wchar_t* ptr = NULL; 740 a.Perform(make_tuple(&ptr)); 741 EXPECT_STREQ(L"world", ptr); 742 743 # if GTEST_HAS_STD_WSTRING 744 745 typedef void MyStringFunction(std::wstring*); 746 Action<MyStringFunction> a2 = SetArgPointee<0>(L"world"); 747 std::wstring str = L""; 748 a2.Perform(make_tuple(&str)); 749 EXPECT_EQ(L"world", str); 750 751 # endif 752 } 753 #endif 754 755 // Tests that SetArgPointee<N>() accepts a char pointer. 756 TEST(SetArgPointeeTest, AcceptsCharPointer) { 757 typedef void MyFunction(bool, std::string*, const char**); 758 const char* const hi = "hi"; 759 Action<MyFunction> a = SetArgPointee<1>(hi); 760 std::string str; 761 const char* ptr = NULL; 762 a.Perform(make_tuple(true, &str, &ptr)); 763 EXPECT_EQ("hi", str); 764 EXPECT_TRUE(ptr == NULL); 765 766 char world_array[] = "world"; 767 char* const world = world_array; 768 a = SetArgPointee<2>(world); 769 str = ""; 770 a.Perform(make_tuple(true, &str, &ptr)); 771 EXPECT_EQ("", str); 772 EXPECT_EQ(world, ptr); 773 } 774 775 TEST(SetArgPointeeTest, AcceptsWideCharPointer) { 776 typedef void MyFunction(bool, const wchar_t**); 777 const wchar_t* const hi = L"hi"; 778 Action<MyFunction> a = SetArgPointee<1>(hi); 779 const wchar_t* ptr = NULL; 780 a.Perform(make_tuple(true, &ptr)); 781 EXPECT_EQ(hi, ptr); 782 783 # if GTEST_HAS_STD_WSTRING 784 785 typedef void MyStringFunction(bool, std::wstring*); 786 wchar_t world_array[] = L"world"; 787 wchar_t* const world = world_array; 788 Action<MyStringFunction> a2 = SetArgPointee<1>(world); 789 std::wstring str; 790 a2.Perform(make_tuple(true, &str)); 791 EXPECT_EQ(world_array, str); 792 # endif 793 } 794 795 #if GTEST_HAS_PROTOBUF_ 796 797 // Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf 798 // variable pointed to by the N-th (0-based) argument to proto_buffer. 799 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) { 800 TestMessage* const msg = new TestMessage; 801 msg->set_member("yes"); 802 TestMessage orig_msg; 803 orig_msg.CopyFrom(*msg); 804 805 Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg); 806 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer 807 // s.t. the action works even when the original proto_buffer has 808 // died. We ensure this behavior by deleting msg before using the 809 // action. 810 delete msg; 811 812 TestMessage dest; 813 EXPECT_FALSE(orig_msg.Equals(dest)); 814 a.Perform(make_tuple(true, &dest)); 815 EXPECT_TRUE(orig_msg.Equals(dest)); 816 } 817 818 // Tests that SetArgPointee<N>(proto_buffer) sets the 819 // ::ProtocolMessage variable pointed to by the N-th (0-based) 820 // argument to proto_buffer. 821 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) { 822 TestMessage* const msg = new TestMessage; 823 msg->set_member("yes"); 824 TestMessage orig_msg; 825 orig_msg.CopyFrom(*msg); 826 827 Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg); 828 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer 829 // s.t. the action works even when the original proto_buffer has 830 // died. We ensure this behavior by deleting msg before using the 831 // action. 832 delete msg; 833 834 TestMessage dest; 835 ::ProtocolMessage* const dest_base = &dest; 836 EXPECT_FALSE(orig_msg.Equals(dest)); 837 a.Perform(make_tuple(true, dest_base)); 838 EXPECT_TRUE(orig_msg.Equals(dest)); 839 } 840 841 // Tests that SetArgPointee<N>(proto2_buffer) sets the v2 842 // protobuf variable pointed to by the N-th (0-based) argument to 843 // proto2_buffer. 844 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) { 845 using testing::internal::FooMessage; 846 FooMessage* const msg = new FooMessage; 847 msg->set_int_field(2); 848 msg->set_string_field("hi"); 849 FooMessage orig_msg; 850 orig_msg.CopyFrom(*msg); 851 852 Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg); 853 // SetArgPointee<N>(proto2_buffer) makes a copy of 854 // proto2_buffer s.t. the action works even when the original 855 // proto2_buffer has died. We ensure this behavior by deleting msg 856 // before using the action. 857 delete msg; 858 859 FooMessage dest; 860 dest.set_int_field(0); 861 a.Perform(make_tuple(true, &dest)); 862 EXPECT_EQ(2, dest.int_field()); 863 EXPECT_EQ("hi", dest.string_field()); 864 } 865 866 // Tests that SetArgPointee<N>(proto2_buffer) sets the 867 // proto2::Message variable pointed to by the N-th (0-based) argument 868 // to proto2_buffer. 869 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) { 870 using testing::internal::FooMessage; 871 FooMessage* const msg = new FooMessage; 872 msg->set_int_field(2); 873 msg->set_string_field("hi"); 874 FooMessage orig_msg; 875 orig_msg.CopyFrom(*msg); 876 877 Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg); 878 // SetArgPointee<N>(proto2_buffer) makes a copy of 879 // proto2_buffer s.t. the action works even when the original 880 // proto2_buffer has died. We ensure this behavior by deleting msg 881 // before using the action. 882 delete msg; 883 884 FooMessage dest; 885 dest.set_int_field(0); 886 ::proto2::Message* const dest_base = &dest; 887 a.Perform(make_tuple(true, dest_base)); 888 EXPECT_EQ(2, dest.int_field()); 889 EXPECT_EQ("hi", dest.string_field()); 890 } 891 892 #endif // GTEST_HAS_PROTOBUF_ 893 894 // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by 895 // the N-th (0-based) argument to v. 896 TEST(SetArgumentPointeeTest, SetsTheNthPointee) { 897 typedef void MyFunction(bool, int*, char*); 898 Action<MyFunction> a = SetArgumentPointee<1>(2); 899 900 int n = 0; 901 char ch = '\0'; 902 a.Perform(make_tuple(true, &n, &ch)); 903 EXPECT_EQ(2, n); 904 EXPECT_EQ('\0', ch); 905 906 a = SetArgumentPointee<2>('a'); 907 n = 0; 908 ch = '\0'; 909 a.Perform(make_tuple(true, &n, &ch)); 910 EXPECT_EQ(0, n); 911 EXPECT_EQ('a', ch); 912 } 913 914 #if GTEST_HAS_PROTOBUF_ 915 916 // Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf 917 // variable pointed to by the N-th (0-based) argument to proto_buffer. 918 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) { 919 TestMessage* const msg = new TestMessage; 920 msg->set_member("yes"); 921 TestMessage orig_msg; 922 orig_msg.CopyFrom(*msg); 923 924 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg); 925 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer 926 // s.t. the action works even when the original proto_buffer has 927 // died. We ensure this behavior by deleting msg before using the 928 // action. 929 delete msg; 930 931 TestMessage dest; 932 EXPECT_FALSE(orig_msg.Equals(dest)); 933 a.Perform(make_tuple(true, &dest)); 934 EXPECT_TRUE(orig_msg.Equals(dest)); 935 } 936 937 // Tests that SetArgumentPointee<N>(proto_buffer) sets the 938 // ::ProtocolMessage variable pointed to by the N-th (0-based) 939 // argument to proto_buffer. 940 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) { 941 TestMessage* const msg = new TestMessage; 942 msg->set_member("yes"); 943 TestMessage orig_msg; 944 orig_msg.CopyFrom(*msg); 945 946 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg); 947 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer 948 // s.t. the action works even when the original proto_buffer has 949 // died. We ensure this behavior by deleting msg before using the 950 // action. 951 delete msg; 952 953 TestMessage dest; 954 ::ProtocolMessage* const dest_base = &dest; 955 EXPECT_FALSE(orig_msg.Equals(dest)); 956 a.Perform(make_tuple(true, dest_base)); 957 EXPECT_TRUE(orig_msg.Equals(dest)); 958 } 959 960 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2 961 // protobuf variable pointed to by the N-th (0-based) argument to 962 // proto2_buffer. 963 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) { 964 using testing::internal::FooMessage; 965 FooMessage* const msg = new FooMessage; 966 msg->set_int_field(2); 967 msg->set_string_field("hi"); 968 FooMessage orig_msg; 969 orig_msg.CopyFrom(*msg); 970 971 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg); 972 // SetArgumentPointee<N>(proto2_buffer) makes a copy of 973 // proto2_buffer s.t. the action works even when the original 974 // proto2_buffer has died. We ensure this behavior by deleting msg 975 // before using the action. 976 delete msg; 977 978 FooMessage dest; 979 dest.set_int_field(0); 980 a.Perform(make_tuple(true, &dest)); 981 EXPECT_EQ(2, dest.int_field()); 982 EXPECT_EQ("hi", dest.string_field()); 983 } 984 985 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the 986 // proto2::Message variable pointed to by the N-th (0-based) argument 987 // to proto2_buffer. 988 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) { 989 using testing::internal::FooMessage; 990 FooMessage* const msg = new FooMessage; 991 msg->set_int_field(2); 992 msg->set_string_field("hi"); 993 FooMessage orig_msg; 994 orig_msg.CopyFrom(*msg); 995 996 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg); 997 // SetArgumentPointee<N>(proto2_buffer) makes a copy of 998 // proto2_buffer s.t. the action works even when the original 999 // proto2_buffer has died. We ensure this behavior by deleting msg 1000 // before using the action. 1001 delete msg; 1002 1003 FooMessage dest; 1004 dest.set_int_field(0); 1005 ::proto2::Message* const dest_base = &dest; 1006 a.Perform(make_tuple(true, dest_base)); 1007 EXPECT_EQ(2, dest.int_field()); 1008 EXPECT_EQ("hi", dest.string_field()); 1009 } 1010 1011 #endif // GTEST_HAS_PROTOBUF_ 1012 1013 // Sample functions and functors for testing Invoke() and etc. 1014 int Nullary() { return 1; } 1015 1016 class NullaryFunctor { 1017 public: 1018 int operator()() { return 2; } 1019 }; 1020 1021 bool g_done = false; 1022 void VoidNullary() { g_done = true; } 1023 1024 class VoidNullaryFunctor { 1025 public: 1026 void operator()() { g_done = true; } 1027 }; 1028 1029 bool Unary(int x) { return x < 0; } 1030 1031 const char* Plus1(const char* s) { return s + 1; } 1032 1033 void VoidUnary(int /* n */) { g_done = true; } 1034 1035 bool ByConstRef(const std::string& s) { return s == "Hi"; } 1036 1037 const double g_double = 0; 1038 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; } 1039 1040 std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT 1041 1042 struct UnaryFunctor { 1043 int operator()(bool x) { return x ? 1 : -1; } 1044 }; 1045 1046 const char* Binary(const char* input, short n) { return input + n; } // NOLINT 1047 1048 void VoidBinary(int, char) { g_done = true; } 1049 1050 int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT 1051 1052 void VoidTernary(int, char, bool) { g_done = true; } 1053 1054 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } 1055 1056 void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } 1057 1058 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } 1059 1060 struct SumOf5Functor { 1061 int operator()(int a, int b, int c, int d, int e) { 1062 return a + b + c + d + e; 1063 } 1064 }; 1065 1066 int SumOf6(int a, int b, int c, int d, int e, int f) { 1067 return a + b + c + d + e + f; 1068 } 1069 1070 struct SumOf6Functor { 1071 int operator()(int a, int b, int c, int d, int e, int f) { 1072 return a + b + c + d + e + f; 1073 } 1074 }; 1075 1076 class Foo { 1077 public: 1078 Foo() : value_(123) {} 1079 1080 int Nullary() const { return value_; } 1081 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT 1082 std::string Binary(const std::string& str, char c) const { return str + c; } 1083 int Ternary(int x, bool y, char z) { return value_ + x + y*z; } 1084 int SumOf4(int a, int b, int c, int d) const { 1085 return a + b + c + d + value_; 1086 } 1087 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } 1088 int SumOf6(int a, int b, int c, int d, int e, int f) { 1089 return a + b + c + d + e + f; 1090 } 1091 private: 1092 int value_; 1093 }; 1094 1095 // Tests InvokeWithoutArgs(function). 1096 TEST(InvokeWithoutArgsTest, Function) { 1097 // As an action that takes one argument. 1098 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT 1099 EXPECT_EQ(1, a.Perform(make_tuple(2))); 1100 1101 // As an action that takes two arguments. 1102 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT 1103 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5))); 1104 1105 // As an action that returns void. 1106 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT 1107 g_done = false; 1108 a3.Perform(make_tuple(1)); 1109 EXPECT_TRUE(g_done); 1110 } 1111 1112 // Tests InvokeWithoutArgs(functor). 1113 TEST(InvokeWithoutArgsTest, Functor) { 1114 // As an action that takes no argument. 1115 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT 1116 EXPECT_EQ(2, a.Perform(make_tuple())); 1117 1118 // As an action that takes three arguments. 1119 Action<int(int, double, char)> a2 = // NOLINT 1120 InvokeWithoutArgs(NullaryFunctor()); 1121 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a'))); 1122 1123 // As an action that returns void. 1124 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor()); 1125 g_done = false; 1126 a3.Perform(make_tuple()); 1127 EXPECT_TRUE(g_done); 1128 } 1129 1130 // Tests InvokeWithoutArgs(obj_ptr, method). 1131 TEST(InvokeWithoutArgsTest, Method) { 1132 Foo foo; 1133 Action<int(bool, char)> a = // NOLINT 1134 InvokeWithoutArgs(&foo, &Foo::Nullary); 1135 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a'))); 1136 } 1137 1138 // Tests using IgnoreResult() on a polymorphic action. 1139 TEST(IgnoreResultTest, PolymorphicAction) { 1140 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT 1141 a.Perform(make_tuple(1)); 1142 } 1143 1144 // Tests using IgnoreResult() on a monomorphic action. 1145 1146 int ReturnOne() { 1147 g_done = true; 1148 return 1; 1149 } 1150 1151 TEST(IgnoreResultTest, MonomorphicAction) { 1152 g_done = false; 1153 Action<void()> a = IgnoreResult(Invoke(ReturnOne)); 1154 a.Perform(make_tuple()); 1155 EXPECT_TRUE(g_done); 1156 } 1157 1158 // Tests using IgnoreResult() on an action that returns a class type. 1159 1160 MyClass ReturnMyClass(double /* x */) { 1161 g_done = true; 1162 return MyClass(); 1163 } 1164 1165 TEST(IgnoreResultTest, ActionReturningClass) { 1166 g_done = false; 1167 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT 1168 a.Perform(make_tuple(2)); 1169 EXPECT_TRUE(g_done); 1170 } 1171 1172 TEST(AssignTest, Int) { 1173 int x = 0; 1174 Action<void(int)> a = Assign(&x, 5); 1175 a.Perform(make_tuple(0)); 1176 EXPECT_EQ(5, x); 1177 } 1178 1179 TEST(AssignTest, String) { 1180 ::std::string x; 1181 Action<void(void)> a = Assign(&x, "Hello, world"); 1182 a.Perform(make_tuple()); 1183 EXPECT_EQ("Hello, world", x); 1184 } 1185 1186 TEST(AssignTest, CompatibleTypes) { 1187 double x = 0; 1188 Action<void(int)> a = Assign(&x, 5); 1189 a.Perform(make_tuple(0)); 1190 EXPECT_DOUBLE_EQ(5, x); 1191 } 1192 1193 #if !GTEST_OS_WINDOWS_MOBILE 1194 1195 class SetErrnoAndReturnTest : public testing::Test { 1196 protected: 1197 virtual void SetUp() { errno = 0; } 1198 virtual void TearDown() { errno = 0; } 1199 }; 1200 1201 TEST_F(SetErrnoAndReturnTest, Int) { 1202 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5); 1203 EXPECT_EQ(-5, a.Perform(make_tuple())); 1204 EXPECT_EQ(ENOTTY, errno); 1205 } 1206 1207 TEST_F(SetErrnoAndReturnTest, Ptr) { 1208 int x; 1209 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x); 1210 EXPECT_EQ(&x, a.Perform(make_tuple())); 1211 EXPECT_EQ(ENOTTY, errno); 1212 } 1213 1214 TEST_F(SetErrnoAndReturnTest, CompatibleTypes) { 1215 Action<double()> a = SetErrnoAndReturn(EINVAL, 5); 1216 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple())); 1217 EXPECT_EQ(EINVAL, errno); 1218 } 1219 1220 #endif // !GTEST_OS_WINDOWS_MOBILE 1221 1222 // Tests ByRef(). 1223 1224 // Tests that ReferenceWrapper<T> is copyable. 1225 TEST(ByRefTest, IsCopyable) { 1226 const std::string s1 = "Hi"; 1227 const std::string s2 = "Hello"; 1228 1229 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s1); 1230 const std::string& r1 = ref_wrapper; 1231 EXPECT_EQ(&s1, &r1); 1232 1233 // Assigns a new value to ref_wrapper. 1234 ref_wrapper = ByRef(s2); 1235 const std::string& r2 = ref_wrapper; 1236 EXPECT_EQ(&s2, &r2); 1237 1238 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1); 1239 // Copies ref_wrapper1 to ref_wrapper. 1240 ref_wrapper = ref_wrapper1; 1241 const std::string& r3 = ref_wrapper; 1242 EXPECT_EQ(&s1, &r3); 1243 } 1244 1245 // Tests using ByRef() on a const value. 1246 TEST(ByRefTest, ConstValue) { 1247 const int n = 0; 1248 // int& ref = ByRef(n); // This shouldn't compile - we have a 1249 // negative compilation test to catch it. 1250 const int& const_ref = ByRef(n); 1251 EXPECT_EQ(&n, &const_ref); 1252 } 1253 1254 // Tests using ByRef() on a non-const value. 1255 TEST(ByRefTest, NonConstValue) { 1256 int n = 0; 1257 1258 // ByRef(n) can be used as either an int&, 1259 int& ref = ByRef(n); 1260 EXPECT_EQ(&n, &ref); 1261 1262 // or a const int&. 1263 const int& const_ref = ByRef(n); 1264 EXPECT_EQ(&n, &const_ref); 1265 } 1266 1267 // Tests explicitly specifying the type when using ByRef(). 1268 TEST(ByRefTest, ExplicitType) { 1269 int n = 0; 1270 const int& r1 = ByRef<const int>(n); 1271 EXPECT_EQ(&n, &r1); 1272 1273 // ByRef<char>(n); // This shouldn't compile - we have a negative 1274 // compilation test to catch it. 1275 1276 Derived d; 1277 Derived& r2 = ByRef<Derived>(d); 1278 EXPECT_EQ(&d, &r2); 1279 1280 const Derived& r3 = ByRef<const Derived>(d); 1281 EXPECT_EQ(&d, &r3); 1282 1283 Base& r4 = ByRef<Base>(d); 1284 EXPECT_EQ(&d, &r4); 1285 1286 const Base& r5 = ByRef<const Base>(d); 1287 EXPECT_EQ(&d, &r5); 1288 1289 // The following shouldn't compile - we have a negative compilation 1290 // test for it. 1291 // 1292 // Base b; 1293 // ByRef<Derived>(b); 1294 } 1295 1296 // Tests that Google Mock prints expression ByRef(x) as a reference to x. 1297 TEST(ByRefTest, PrintsCorrectly) { 1298 int n = 42; 1299 ::std::stringstream expected, actual; 1300 testing::internal::UniversalPrinter<const int&>::Print(n, &expected); 1301 testing::internal::UniversalPrint(ByRef(n), &actual); 1302 EXPECT_EQ(expected.str(), actual.str()); 1303 } 1304 1305 } // Unnamed namespace 1306