00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "gmock/gmock-actions.h"
00037 #include <algorithm>
00038 #include <iterator>
00039 #include <string>
00040 #include "gmock/gmock.h"
00041 #include "gmock/internal/gmock-port.h"
00042 #include "gtest/gtest.h"
00043 #include "gtest/gtest-spi.h"
00044
00045 namespace {
00046
00047 using ::std::tr1::get;
00048 using ::std::tr1::make_tuple;
00049 using ::std::tr1::tuple;
00050 using ::std::tr1::tuple_element;
00051 using testing::internal::BuiltInDefaultValue;
00052 using testing::internal::Int64;
00053 using testing::internal::UInt64;
00054
00055 using testing::_;
00056 using testing::Action;
00057 using testing::ActionInterface;
00058 using testing::Assign;
00059 using testing::ByRef;
00060 using testing::DefaultValue;
00061 using testing::DoDefault;
00062 using testing::IgnoreResult;
00063 using testing::Invoke;
00064 using testing::InvokeWithoutArgs;
00065 using testing::MakePolymorphicAction;
00066 using testing::Ne;
00067 using testing::PolymorphicAction;
00068 using testing::Return;
00069 using testing::ReturnNull;
00070 using testing::ReturnRef;
00071 using testing::ReturnRefOfCopy;
00072 using testing::SetArgPointee;
00073 using testing::SetArgumentPointee;
00074
00075 #if !GTEST_OS_WINDOWS_MOBILE
00076 using testing::SetErrnoAndReturn;
00077 #endif
00078
00079 #if GTEST_HAS_PROTOBUF_
00080 using testing::internal::TestMessage;
00081 #endif // GTEST_HAS_PROTOBUF_
00082
00083
00084 TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
00085 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
00086 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
00087 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
00088 }
00089
00090
00091 TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
00092 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
00093 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
00094 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
00095 }
00096
00097
00098
00099 TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
00100 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
00101 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
00102 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
00103 #if GMOCK_HAS_SIGNED_WCHAR_T_
00104 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
00105 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
00106 #endif
00107 #if GMOCK_WCHAR_T_IS_NATIVE_
00108 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
00109 #endif
00110 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get());
00111 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get());
00112 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get());
00113 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
00114 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
00115 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
00116 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get());
00117 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get());
00118 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get());
00119 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
00120 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
00121 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
00122 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
00123 }
00124
00125
00126
00127 TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
00128 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
00129 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
00130 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
00131 #if GMOCK_HAS_SIGNED_WCHAR_T_
00132 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
00133 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
00134 #endif
00135 #if GMOCK_WCHAR_T_IS_NATIVE_
00136 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
00137 #endif
00138 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists());
00139 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists());
00140 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists());
00141 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
00142 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
00143 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
00144 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists());
00145 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists());
00146 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists());
00147 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
00148 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
00149 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
00150 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
00151 }
00152
00153
00154 TEST(BuiltInDefaultValueTest, IsFalseForBool) {
00155 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
00156 }
00157
00158
00159 TEST(BuiltInDefaultValueTest, BoolExists) {
00160 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
00161 }
00162
00163
00164
00165 TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
00166 #if GTEST_HAS_GLOBAL_STRING
00167 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
00168 #endif // GTEST_HAS_GLOBAL_STRING
00169
00170 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
00171 }
00172
00173
00174
00175 TEST(BuiltInDefaultValueTest, ExistsForString) {
00176 #if GTEST_HAS_GLOBAL_STRING
00177 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
00178 #endif // GTEST_HAS_GLOBAL_STRING
00179
00180 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
00181 }
00182
00183
00184
00185 TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
00186 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
00187 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
00188 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
00189 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
00190 }
00191
00192
00193
00194 struct UserType {
00195 UserType() : value(0) {}
00196
00197 int value;
00198 };
00199
00200 TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
00201 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
00202 }
00203
00204
00205 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
00206 EXPECT_DEATH_IF_SUPPORTED({
00207 BuiltInDefaultValue<int&>::Get();
00208 }, "");
00209 EXPECT_DEATH_IF_SUPPORTED({
00210 BuiltInDefaultValue<const char&>::Get();
00211 }, "");
00212 }
00213
00214 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
00215 EXPECT_DEATH_IF_SUPPORTED({
00216 BuiltInDefaultValue<UserType>::Get();
00217 }, "");
00218 }
00219
00220
00221 TEST(DefaultValueTest, IsInitiallyUnset) {
00222 EXPECT_FALSE(DefaultValue<int>::IsSet());
00223 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
00224 }
00225
00226
00227 TEST(DefaultValueTest, CanBeSetAndUnset) {
00228 EXPECT_TRUE(DefaultValue<int>::Exists());
00229 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
00230
00231 DefaultValue<int>::Set(1);
00232 DefaultValue<const UserType>::Set(UserType());
00233
00234 EXPECT_EQ(1, DefaultValue<int>::Get());
00235 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
00236
00237 EXPECT_TRUE(DefaultValue<int>::Exists());
00238 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
00239
00240 DefaultValue<int>::Clear();
00241 DefaultValue<const UserType>::Clear();
00242
00243 EXPECT_FALSE(DefaultValue<int>::IsSet());
00244 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
00245
00246 EXPECT_TRUE(DefaultValue<int>::Exists());
00247 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
00248 }
00249
00250
00251
00252
00253 TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
00254 EXPECT_FALSE(DefaultValue<int>::IsSet());
00255 EXPECT_TRUE(DefaultValue<int>::Exists());
00256 EXPECT_FALSE(DefaultValue<UserType>::IsSet());
00257 EXPECT_FALSE(DefaultValue<UserType>::Exists());
00258
00259 EXPECT_EQ(0, DefaultValue<int>::Get());
00260
00261 EXPECT_DEATH_IF_SUPPORTED({
00262 DefaultValue<UserType>::Get();
00263 }, "");
00264 }
00265
00266
00267 TEST(DefaultValueTest, GetWorksForVoid) {
00268 return DefaultValue<void>::Get();
00269 }
00270
00271
00272
00273
00274 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
00275 EXPECT_FALSE(DefaultValue<int&>::IsSet());
00276 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
00277 }
00278
00279
00280 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
00281 EXPECT_FALSE(DefaultValue<int&>::Exists());
00282 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
00283 }
00284
00285
00286 TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
00287 int n = 1;
00288 DefaultValue<const int&>::Set(n);
00289 UserType u;
00290 DefaultValue<UserType&>::Set(u);
00291
00292 EXPECT_TRUE(DefaultValue<const int&>::Exists());
00293 EXPECT_TRUE(DefaultValue<UserType&>::Exists());
00294
00295 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
00296 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
00297
00298 DefaultValue<const int&>::Clear();
00299 DefaultValue<UserType&>::Clear();
00300
00301 EXPECT_FALSE(DefaultValue<const int&>::Exists());
00302 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
00303
00304 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
00305 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
00306 }
00307
00308
00309
00310
00311 TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
00312 EXPECT_FALSE(DefaultValue<int&>::IsSet());
00313 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
00314
00315 EXPECT_DEATH_IF_SUPPORTED({
00316 DefaultValue<int&>::Get();
00317 }, "");
00318 EXPECT_DEATH_IF_SUPPORTED({
00319 DefaultValue<UserType>::Get();
00320 }, "");
00321 }
00322
00323
00324
00325
00326 typedef int MyGlobalFunction(bool, int);
00327
00328 class MyActionImpl : public ActionInterface<MyGlobalFunction> {
00329 public:
00330 virtual int Perform(const tuple<bool, int>& args) {
00331 return get<0>(args) ? get<1>(args) : 0;
00332 }
00333 };
00334
00335 TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
00336 MyActionImpl my_action_impl;
00337 (void)my_action_impl;
00338 }
00339
00340 TEST(ActionInterfaceTest, MakeAction) {
00341 Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
00342
00343
00344
00345
00346
00347
00348 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
00349 }
00350
00351
00352
00353 TEST(ActionTest, CanBeConstructedFromActionInterface) {
00354 Action<MyGlobalFunction> action(new MyActionImpl);
00355 }
00356
00357
00358 TEST(ActionTest, DelegatesWorkToActionInterface) {
00359 const Action<MyGlobalFunction> action(new MyActionImpl);
00360
00361 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
00362 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
00363 }
00364
00365
00366 TEST(ActionTest, IsCopyable) {
00367 Action<MyGlobalFunction> a1(new MyActionImpl);
00368 Action<MyGlobalFunction> a2(a1);
00369
00370
00371 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
00372 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
00373
00374
00375 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
00376 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
00377
00378 a2 = a1;
00379
00380
00381 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
00382 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
00383
00384
00385 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
00386 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
00387 }
00388
00389
00390
00391
00392 class IsNotZero : public ActionInterface<bool(int)> {
00393 public:
00394 virtual bool Perform(const tuple<int>& arg) {
00395 return get<0>(arg) != 0;
00396 }
00397 };
00398
00399 #if !GTEST_OS_SYMBIAN
00400
00401
00402
00403
00404
00405 TEST(ActionTest, CanBeConvertedToOtherActionType) {
00406 const Action<bool(int)> a1(new IsNotZero);
00407 const Action<int(char)> a2 = Action<int(char)>(a1);
00408 EXPECT_EQ(1, a2.Perform(make_tuple('a')));
00409 EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
00410 }
00411 #endif // !GTEST_OS_SYMBIAN
00412
00413
00414
00415
00416
00417 class ReturnSecondArgumentAction {
00418 public:
00419
00420
00421
00422 template <typename Result, typename ArgumentTuple>
00423 Result Perform(const ArgumentTuple& args) { return get<1>(args); }
00424 };
00425
00426
00427
00428 class ReturnZeroFromNullaryFunctionAction {
00429 public:
00430
00431
00432
00433
00434
00435
00436
00437 template <typename Result>
00438 Result Perform(const tuple<>&) const { return 0; }
00439 };
00440
00441
00442
00443
00444 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
00445 return MakePolymorphicAction(ReturnSecondArgumentAction());
00446 }
00447
00448 PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
00449 ReturnZeroFromNullaryFunction() {
00450 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
00451 }
00452
00453
00454
00455 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
00456 Action<int(bool, int, double)> a1 = ReturnSecondArgument();
00457 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
00458 }
00459
00460
00461
00462 TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
00463 Action<int()> a1 = ReturnZeroFromNullaryFunction();
00464 EXPECT_EQ(0, a1.Perform(make_tuple()));
00465
00466 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
00467 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
00468 }
00469
00470
00471
00472 TEST(ReturnTest, WorksForVoid) {
00473 const Action<void(int)> ret = Return();
00474 return ret.Perform(make_tuple(1));
00475 }
00476
00477
00478 TEST(ReturnTest, ReturnsGivenValue) {
00479 Action<int()> ret = Return(1);
00480 EXPECT_EQ(1, ret.Perform(make_tuple()));
00481
00482 ret = Return(-5);
00483 EXPECT_EQ(-5, ret.Perform(make_tuple()));
00484 }
00485
00486
00487 TEST(ReturnTest, AcceptsStringLiteral) {
00488 Action<const char*()> a1 = Return("Hello");
00489 EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
00490
00491 Action<std::string()> a2 = Return("world");
00492 EXPECT_EQ("world", a2.Perform(make_tuple()));
00493 }
00494
00495
00496
00497 struct Base {
00498 bool operator==(const Base&) { return true; }
00499 };
00500
00501 struct Derived : public Base {
00502 bool operator==(const Derived&) { return true; }
00503 };
00504
00505 TEST(ReturnTest, IsCovariant) {
00506 Base base;
00507 Derived derived;
00508 Action<Base*()> ret = Return(&base);
00509 EXPECT_EQ(&base, ret.Perform(make_tuple()));
00510
00511 ret = Return(&derived);
00512 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
00513 }
00514
00515
00516
00517
00518
00519 class FromType {
00520 public:
00521 explicit FromType(bool* is_converted) : converted_(is_converted) {}
00522 bool* converted() const { return converted_; }
00523
00524 private:
00525 bool* const converted_;
00526
00527 GTEST_DISALLOW_ASSIGN_(FromType);
00528 };
00529
00530 class ToType {
00531 public:
00532
00533 ToType(const FromType& x) { *x.converted() = true; }
00534 };
00535
00536 TEST(ReturnTest, ConvertsArgumentWhenConverted) {
00537 bool converted = false;
00538 FromType x(&converted);
00539 Action<ToType()> action(Return(x));
00540 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
00541 << "conversion operator.";
00542 converted = false;
00543 action.Perform(tuple<>());
00544 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
00545 << "when performed.";
00546 }
00547
00548 class DestinationType {};
00549
00550 class SourceType {
00551 public:
00552
00553 operator DestinationType() { return DestinationType(); }
00554 };
00555
00556 TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
00557 SourceType s;
00558 Action<DestinationType()> action(Return(s));
00559 }
00560
00561
00562 TEST(ReturnNullTest, WorksInPointerReturningFunction) {
00563 const Action<int*()> a1 = ReturnNull();
00564 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
00565
00566 const Action<const char*(bool)> a2 = ReturnNull();
00567 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
00568 }
00569
00570
00571 TEST(ReturnRefTest, WorksForReference) {
00572 const int n = 0;
00573 const Action<const int&(bool)> ret = ReturnRef(n);
00574
00575 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
00576 }
00577
00578
00579 TEST(ReturnRefTest, IsCovariant) {
00580 Base base;
00581 Derived derived;
00582 Action<Base&()> a = ReturnRef(base);
00583 EXPECT_EQ(&base, &a.Perform(make_tuple()));
00584
00585 a = ReturnRef(derived);
00586 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
00587 }
00588
00589
00590 TEST(ReturnRefOfCopyTest, WorksForReference) {
00591 int n = 42;
00592 const Action<const int&()> ret = ReturnRefOfCopy(n);
00593
00594 EXPECT_NE(&n, &ret.Perform(make_tuple()));
00595 EXPECT_EQ(42, ret.Perform(make_tuple()));
00596
00597 n = 43;
00598 EXPECT_NE(&n, &ret.Perform(make_tuple()));
00599 EXPECT_EQ(42, ret.Perform(make_tuple()));
00600 }
00601
00602
00603 TEST(ReturnRefOfCopyTest, IsCovariant) {
00604 Base base;
00605 Derived derived;
00606 Action<Base&()> a = ReturnRefOfCopy(base);
00607 EXPECT_NE(&base, &a.Perform(make_tuple()));
00608
00609 a = ReturnRefOfCopy(derived);
00610 EXPECT_NE(&derived, &a.Perform(make_tuple()));
00611 }
00612
00613
00614
00615 class MyClass {};
00616
00617 class MockClass {
00618 public:
00619 MockClass() {}
00620
00621 MOCK_METHOD1(IntFunc, int(bool flag));
00622 MOCK_METHOD0(Foo, MyClass());
00623
00624 private:
00625 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
00626 };
00627
00628
00629
00630 TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
00631 MockClass mock;
00632 EXPECT_CALL(mock, IntFunc(_))
00633 .WillOnce(DoDefault());
00634 EXPECT_EQ(0, mock.IntFunc(true));
00635 }
00636
00637
00638
00639 TEST(DoDefaultDeathTest, DiesForUnknowType) {
00640 MockClass mock;
00641 EXPECT_CALL(mock, Foo())
00642 .WillRepeatedly(DoDefault());
00643 #if GTEST_HAS_EXCEPTIONS
00644 EXPECT_ANY_THROW(mock.Foo());
00645 #else
00646 EXPECT_DEATH_IF_SUPPORTED({
00647 mock.Foo();
00648 }, "");
00649 #endif
00650 }
00651
00652
00653
00654
00655 void VoidFunc(bool ) {}
00656
00657 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
00658 MockClass mock;
00659 EXPECT_CALL(mock, IntFunc(_))
00660 .WillRepeatedly(DoAll(Invoke(VoidFunc),
00661 DoDefault()));
00662
00663
00664
00665
00666
00667 EXPECT_DEATH_IF_SUPPORTED({
00668 mock.IntFunc(true);
00669 }, "");
00670 }
00671
00672
00673
00674 TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
00675 DefaultValue<int>::Set(1);
00676 MockClass mock;
00677 EXPECT_CALL(mock, IntFunc(_))
00678 .WillOnce(DoDefault());
00679 EXPECT_EQ(1, mock.IntFunc(false));
00680 DefaultValue<int>::Clear();
00681 }
00682
00683
00684 TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
00685 MockClass mock;
00686 ON_CALL(mock, IntFunc(_))
00687 .WillByDefault(Return(2));
00688 EXPECT_CALL(mock, IntFunc(_))
00689 .WillOnce(DoDefault());
00690 EXPECT_EQ(2, mock.IntFunc(false));
00691 }
00692
00693
00694 TEST(DoDefaultTest, CannotBeUsedInOnCall) {
00695 MockClass mock;
00696 EXPECT_NONFATAL_FAILURE({
00697 ON_CALL(mock, IntFunc(_))
00698 .WillByDefault(DoDefault());
00699 }, "DoDefault() cannot be used in ON_CALL()");
00700 }
00701
00702
00703
00704 TEST(SetArgPointeeTest, SetsTheNthPointee) {
00705 typedef void MyFunction(bool, int*, char*);
00706 Action<MyFunction> a = SetArgPointee<1>(2);
00707
00708 int n = 0;
00709 char ch = '\0';
00710 a.Perform(make_tuple(true, &n, &ch));
00711 EXPECT_EQ(2, n);
00712 EXPECT_EQ('\0', ch);
00713
00714 a = SetArgPointee<2>('a');
00715 n = 0;
00716 ch = '\0';
00717 a.Perform(make_tuple(true, &n, &ch));
00718 EXPECT_EQ(0, n);
00719 EXPECT_EQ('a', ch);
00720 }
00721
00722 #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
00723
00724
00725 TEST(SetArgPointeeTest, AcceptsStringLiteral) {
00726 typedef void MyFunction(std::string*, const char**);
00727 Action<MyFunction> a = SetArgPointee<0>("hi");
00728 std::string str;
00729 const char* ptr = NULL;
00730 a.Perform(make_tuple(&str, &ptr));
00731 EXPECT_EQ("hi", str);
00732 EXPECT_TRUE(ptr == NULL);
00733
00734 a = SetArgPointee<1>("world");
00735 str = "";
00736 a.Perform(make_tuple(&str, &ptr));
00737 EXPECT_EQ("", str);
00738 EXPECT_STREQ("world", ptr);
00739 }
00740
00741 TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
00742 typedef void MyFunction(const wchar_t**);
00743 Action<MyFunction> a = SetArgPointee<0>(L"world");
00744 const wchar_t* ptr = NULL;
00745 a.Perform(make_tuple(&ptr));
00746 EXPECT_STREQ(L"world", ptr);
00747
00748 # if GTEST_HAS_STD_WSTRING
00749
00750 typedef void MyStringFunction(std::wstring*);
00751 Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
00752 std::wstring str = L"";
00753 a2.Perform(make_tuple(&str));
00754 EXPECT_EQ(L"world", str);
00755
00756 # endif
00757 }
00758 #endif
00759
00760
00761 TEST(SetArgPointeeTest, AcceptsCharPointer) {
00762 typedef void MyFunction(bool, std::string*, const char**);
00763 const char* const hi = "hi";
00764 Action<MyFunction> a = SetArgPointee<1>(hi);
00765 std::string str;
00766 const char* ptr = NULL;
00767 a.Perform(make_tuple(true, &str, &ptr));
00768 EXPECT_EQ("hi", str);
00769 EXPECT_TRUE(ptr == NULL);
00770
00771 char world_array[] = "world";
00772 char* const world = world_array;
00773 a = SetArgPointee<2>(world);
00774 str = "";
00775 a.Perform(make_tuple(true, &str, &ptr));
00776 EXPECT_EQ("", str);
00777 EXPECT_EQ(world, ptr);
00778 }
00779
00780 TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
00781 typedef void MyFunction(bool, const wchar_t**);
00782 const wchar_t* const hi = L"hi";
00783 Action<MyFunction> a = SetArgPointee<1>(hi);
00784 const wchar_t* ptr = NULL;
00785 a.Perform(make_tuple(true, &ptr));
00786 EXPECT_EQ(hi, ptr);
00787
00788 # if GTEST_HAS_STD_WSTRING
00789
00790 typedef void MyStringFunction(bool, std::wstring*);
00791 wchar_t world_array[] = L"world";
00792 wchar_t* const world = world_array;
00793 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
00794 std::wstring str;
00795 a2.Perform(make_tuple(true, &str));
00796 EXPECT_EQ(world_array, str);
00797 # endif
00798 }
00799
00800 #if GTEST_HAS_PROTOBUF_
00801
00802
00803
00804 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
00805 TestMessage* const msg = new TestMessage;
00806 msg->set_member("yes");
00807 TestMessage orig_msg;
00808 orig_msg.CopyFrom(*msg);
00809
00810 Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg);
00811
00812
00813
00814
00815 delete msg;
00816
00817 TestMessage dest;
00818 EXPECT_FALSE(orig_msg.Equals(dest));
00819 a.Perform(make_tuple(true, &dest));
00820 EXPECT_TRUE(orig_msg.Equals(dest));
00821 }
00822
00823
00824
00825
00826 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
00827 TestMessage* const msg = new TestMessage;
00828 msg->set_member("yes");
00829 TestMessage orig_msg;
00830 orig_msg.CopyFrom(*msg);
00831
00832 Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg);
00833
00834
00835
00836
00837 delete msg;
00838
00839 TestMessage dest;
00840 ::ProtocolMessage* const dest_base = &dest;
00841 EXPECT_FALSE(orig_msg.Equals(dest));
00842 a.Perform(make_tuple(true, dest_base));
00843 EXPECT_TRUE(orig_msg.Equals(dest));
00844 }
00845
00846
00847
00848
00849 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
00850 using testing::internal::FooMessage;
00851 FooMessage* const msg = new FooMessage;
00852 msg->set_int_field(2);
00853 msg->set_string_field("hi");
00854 FooMessage orig_msg;
00855 orig_msg.CopyFrom(*msg);
00856
00857 Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg);
00858
00859
00860
00861
00862 delete msg;
00863
00864 FooMessage dest;
00865 dest.set_int_field(0);
00866 a.Perform(make_tuple(true, &dest));
00867 EXPECT_EQ(2, dest.int_field());
00868 EXPECT_EQ("hi", dest.string_field());
00869 }
00870
00871
00872
00873
00874 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
00875 using testing::internal::FooMessage;
00876 FooMessage* const msg = new FooMessage;
00877 msg->set_int_field(2);
00878 msg->set_string_field("hi");
00879 FooMessage orig_msg;
00880 orig_msg.CopyFrom(*msg);
00881
00882 Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg);
00883
00884
00885
00886
00887 delete msg;
00888
00889 FooMessage dest;
00890 dest.set_int_field(0);
00891 ::proto2::Message* const dest_base = &dest;
00892 a.Perform(make_tuple(true, dest_base));
00893 EXPECT_EQ(2, dest.int_field());
00894 EXPECT_EQ("hi", dest.string_field());
00895 }
00896
00897 #endif // GTEST_HAS_PROTOBUF_
00898
00899
00900
00901 TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
00902 typedef void MyFunction(bool, int*, char*);
00903 Action<MyFunction> a = SetArgumentPointee<1>(2);
00904
00905 int n = 0;
00906 char ch = '\0';
00907 a.Perform(make_tuple(true, &n, &ch));
00908 EXPECT_EQ(2, n);
00909 EXPECT_EQ('\0', ch);
00910
00911 a = SetArgumentPointee<2>('a');
00912 n = 0;
00913 ch = '\0';
00914 a.Perform(make_tuple(true, &n, &ch));
00915 EXPECT_EQ(0, n);
00916 EXPECT_EQ('a', ch);
00917 }
00918
00919 #if GTEST_HAS_PROTOBUF_
00920
00921
00922
00923 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
00924 TestMessage* const msg = new TestMessage;
00925 msg->set_member("yes");
00926 TestMessage orig_msg;
00927 orig_msg.CopyFrom(*msg);
00928
00929 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
00930
00931
00932
00933
00934 delete msg;
00935
00936 TestMessage dest;
00937 EXPECT_FALSE(orig_msg.Equals(dest));
00938 a.Perform(make_tuple(true, &dest));
00939 EXPECT_TRUE(orig_msg.Equals(dest));
00940 }
00941
00942
00943
00944
00945 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
00946 TestMessage* const msg = new TestMessage;
00947 msg->set_member("yes");
00948 TestMessage orig_msg;
00949 orig_msg.CopyFrom(*msg);
00950
00951 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
00952
00953
00954
00955
00956 delete msg;
00957
00958 TestMessage dest;
00959 ::ProtocolMessage* const dest_base = &dest;
00960 EXPECT_FALSE(orig_msg.Equals(dest));
00961 a.Perform(make_tuple(true, dest_base));
00962 EXPECT_TRUE(orig_msg.Equals(dest));
00963 }
00964
00965
00966
00967
00968 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
00969 using testing::internal::FooMessage;
00970 FooMessage* const msg = new FooMessage;
00971 msg->set_int_field(2);
00972 msg->set_string_field("hi");
00973 FooMessage orig_msg;
00974 orig_msg.CopyFrom(*msg);
00975
00976 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
00977
00978
00979
00980
00981 delete msg;
00982
00983 FooMessage dest;
00984 dest.set_int_field(0);
00985 a.Perform(make_tuple(true, &dest));
00986 EXPECT_EQ(2, dest.int_field());
00987 EXPECT_EQ("hi", dest.string_field());
00988 }
00989
00990
00991
00992
00993 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
00994 using testing::internal::FooMessage;
00995 FooMessage* const msg = new FooMessage;
00996 msg->set_int_field(2);
00997 msg->set_string_field("hi");
00998 FooMessage orig_msg;
00999 orig_msg.CopyFrom(*msg);
01000
01001 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
01002
01003
01004
01005
01006 delete msg;
01007
01008 FooMessage dest;
01009 dest.set_int_field(0);
01010 ::proto2::Message* const dest_base = &dest;
01011 a.Perform(make_tuple(true, dest_base));
01012 EXPECT_EQ(2, dest.int_field());
01013 EXPECT_EQ("hi", dest.string_field());
01014 }
01015
01016 #endif // GTEST_HAS_PROTOBUF_
01017
01018
01019 int Nullary() { return 1; }
01020
01021 class NullaryFunctor {
01022 public:
01023 int operator()() { return 2; }
01024 };
01025
01026 bool g_done = false;
01027 void VoidNullary() { g_done = true; }
01028
01029 class VoidNullaryFunctor {
01030 public:
01031 void operator()() { g_done = true; }
01032 };
01033
01034 class Foo {
01035 public:
01036 Foo() : value_(123) {}
01037
01038 int Nullary() const { return value_; }
01039
01040 private:
01041 int value_;
01042 };
01043
01044
01045 TEST(InvokeWithoutArgsTest, Function) {
01046
01047 Action<int(int)> a = InvokeWithoutArgs(Nullary);
01048 EXPECT_EQ(1, a.Perform(make_tuple(2)));
01049
01050
01051 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary);
01052 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
01053
01054
01055 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary);
01056 g_done = false;
01057 a3.Perform(make_tuple(1));
01058 EXPECT_TRUE(g_done);
01059 }
01060
01061
01062 TEST(InvokeWithoutArgsTest, Functor) {
01063
01064 Action<int()> a = InvokeWithoutArgs(NullaryFunctor());
01065 EXPECT_EQ(2, a.Perform(make_tuple()));
01066
01067
01068 Action<int(int, double, char)> a2 =
01069 InvokeWithoutArgs(NullaryFunctor());
01070 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
01071
01072
01073 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
01074 g_done = false;
01075 a3.Perform(make_tuple());
01076 EXPECT_TRUE(g_done);
01077 }
01078
01079
01080 TEST(InvokeWithoutArgsTest, Method) {
01081 Foo foo;
01082 Action<int(bool, char)> a =
01083 InvokeWithoutArgs(&foo, &Foo::Nullary);
01084 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
01085 }
01086
01087
01088 TEST(IgnoreResultTest, PolymorphicAction) {
01089 Action<void(int)> a = IgnoreResult(Return(5));
01090 a.Perform(make_tuple(1));
01091 }
01092
01093
01094
01095 int ReturnOne() {
01096 g_done = true;
01097 return 1;
01098 }
01099
01100 TEST(IgnoreResultTest, MonomorphicAction) {
01101 g_done = false;
01102 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
01103 a.Perform(make_tuple());
01104 EXPECT_TRUE(g_done);
01105 }
01106
01107
01108
01109 MyClass ReturnMyClass(double ) {
01110 g_done = true;
01111 return MyClass();
01112 }
01113
01114 TEST(IgnoreResultTest, ActionReturningClass) {
01115 g_done = false;
01116 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass));
01117 a.Perform(make_tuple(2));
01118 EXPECT_TRUE(g_done);
01119 }
01120
01121 TEST(AssignTest, Int) {
01122 int x = 0;
01123 Action<void(int)> a = Assign(&x, 5);
01124 a.Perform(make_tuple(0));
01125 EXPECT_EQ(5, x);
01126 }
01127
01128 TEST(AssignTest, String) {
01129 ::std::string x;
01130 Action<void(void)> a = Assign(&x, "Hello, world");
01131 a.Perform(make_tuple());
01132 EXPECT_EQ("Hello, world", x);
01133 }
01134
01135 TEST(AssignTest, CompatibleTypes) {
01136 double x = 0;
01137 Action<void(int)> a = Assign(&x, 5);
01138 a.Perform(make_tuple(0));
01139 EXPECT_DOUBLE_EQ(5, x);
01140 }
01141
01142 #if !GTEST_OS_WINDOWS_MOBILE
01143
01144 class SetErrnoAndReturnTest : public testing::Test {
01145 protected:
01146 virtual void SetUp() { errno = 0; }
01147 virtual void TearDown() { errno = 0; }
01148 };
01149
01150 TEST_F(SetErrnoAndReturnTest, Int) {
01151 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
01152 EXPECT_EQ(-5, a.Perform(make_tuple()));
01153 EXPECT_EQ(ENOTTY, errno);
01154 }
01155
01156 TEST_F(SetErrnoAndReturnTest, Ptr) {
01157 int x;
01158 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
01159 EXPECT_EQ(&x, a.Perform(make_tuple()));
01160 EXPECT_EQ(ENOTTY, errno);
01161 }
01162
01163 TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
01164 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
01165 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
01166 EXPECT_EQ(EINVAL, errno);
01167 }
01168
01169 #endif // !GTEST_OS_WINDOWS_MOBILE
01170
01171
01172
01173
01174 TEST(ByRefTest, IsCopyable) {
01175 const std::string s1 = "Hi";
01176 const std::string s2 = "Hello";
01177
01178 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper =
01179 ByRef(s1);
01180 const std::string& r1 = ref_wrapper;
01181 EXPECT_EQ(&s1, &r1);
01182
01183
01184 ref_wrapper = ByRef(s2);
01185 const std::string& r2 = ref_wrapper;
01186 EXPECT_EQ(&s2, &r2);
01187
01188 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 =
01189 ByRef(s1);
01190
01191 ref_wrapper = ref_wrapper1;
01192 const std::string& r3 = ref_wrapper;
01193 EXPECT_EQ(&s1, &r3);
01194 }
01195
01196
01197 TEST(ByRefTest, ConstValue) {
01198 const int n = 0;
01199
01200
01201 const int& const_ref = ByRef(n);
01202 EXPECT_EQ(&n, &const_ref);
01203 }
01204
01205
01206 TEST(ByRefTest, NonConstValue) {
01207 int n = 0;
01208
01209
01210 int& ref = ByRef(n);
01211 EXPECT_EQ(&n, &ref);
01212
01213
01214 const int& const_ref = ByRef(n);
01215 EXPECT_EQ(&n, &const_ref);
01216 }
01217
01218
01219 TEST(ByRefTest, ExplicitType) {
01220 int n = 0;
01221 const int& r1 = ByRef<const int>(n);
01222 EXPECT_EQ(&n, &r1);
01223
01224
01225
01226
01227 Derived d;
01228 Derived& r2 = ByRef<Derived>(d);
01229 EXPECT_EQ(&d, &r2);
01230
01231 const Derived& r3 = ByRef<const Derived>(d);
01232 EXPECT_EQ(&d, &r3);
01233
01234 Base& r4 = ByRef<Base>(d);
01235 EXPECT_EQ(&d, &r4);
01236
01237 const Base& r5 = ByRef<const Base>(d);
01238 EXPECT_EQ(&d, &r5);
01239
01240
01241
01242
01243
01244
01245 }
01246
01247
01248 TEST(ByRefTest, PrintsCorrectly) {
01249 int n = 42;
01250 ::std::stringstream expected, actual;
01251 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
01252 testing::internal::UniversalPrint(ByRef(n), &actual);
01253 EXPECT_EQ(expected.str(), actual.str());
01254 }
01255
01256 }