gmock-actions_test.cc
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Google Mock - a framework for writing C++ mock classes.
00033 //
00034 // This file tests the built-in actions.
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 // This list should be kept sorted.
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 // Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
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 // Tests that BuiltInDefaultValue<T*>::Exists() return true.
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 // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
00098 // built-in numeric type.
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());  // NOLINT
00111   EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get());  // NOLINT
00112   EXPECT_EQ(0, BuiltInDefaultValue<short>::Get());  // NOLINT
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());  // NOLINT
00117   EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get());  // NOLINT
00118   EXPECT_EQ(0, BuiltInDefaultValue<long>::Get());  // NOLINT
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 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
00126 // built-in numeric type.
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());  // NOLINT
00139   EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists());  // NOLINT
00140   EXPECT_TRUE(BuiltInDefaultValue<short>::Exists());  // NOLINT
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());  // NOLINT
00145   EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists());  // NOLINT
00146   EXPECT_TRUE(BuiltInDefaultValue<long>::Exists());  // NOLINT
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 // Tests that BuiltInDefaultValue<bool>::Get() returns false.
00154 TEST(BuiltInDefaultValueTest, IsFalseForBool) {
00155   EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
00156 }
00157 
00158 // Tests that BuiltInDefaultValue<bool>::Exists() returns true.
00159 TEST(BuiltInDefaultValueTest, BoolExists) {
00160   EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
00161 }
00162 
00163 // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
00164 // string type.
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 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
00174 // string type.
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 // Tests that BuiltInDefaultValue<const T>::Get() returns the same
00184 // value as BuiltInDefaultValue<T>::Get() does.
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 // Tests that BuiltInDefaultValue<T>::Get() aborts the program with
00193 // the correct error message when T is a user-defined type.
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 // Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
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 // Tests that DefaultValue<T>::IsSet() is false initially.
00221 TEST(DefaultValueTest, IsInitiallyUnset) {
00222   EXPECT_FALSE(DefaultValue<int>::IsSet());
00223   EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
00224 }
00225 
00226 // Tests that DefaultValue<T> can be set and then unset.
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 // Tests that DefaultValue<T>::Get() returns the
00251 // BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
00252 // false.
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 // Tests that DefaultValue<void>::Get() returns void.
00267 TEST(DefaultValueTest, GetWorksForVoid) {
00268   return DefaultValue<void>::Get();
00269 }
00270 
00271 // Tests using DefaultValue with a reference type.
00272 
00273 // Tests that DefaultValue<T&>::IsSet() is false initially.
00274 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
00275   EXPECT_FALSE(DefaultValue<int&>::IsSet());
00276   EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
00277 }
00278 
00279 // Tests that DefaultValue<T&>::Exists is false initiallly.
00280 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
00281   EXPECT_FALSE(DefaultValue<int&>::Exists());
00282   EXPECT_FALSE(DefaultValue<UserType&>::Exists());
00283 }
00284 
00285 // Tests that DefaultValue<T&> can be set and then unset.
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 // Tests that DefaultValue<T&>::Get() returns the
00309 // BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
00310 // false.
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 // Tests that ActionInterface can be implemented by defining the
00324 // Perform method.
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   // When exercising the Perform() method of Action<F>, we must pass
00344   // it a tuple whose size and type are compatible with F's argument
00345   // types.  For example, if F is int(), then Perform() takes a
00346   // 0-tuple; if F is void(bool, int), then Perform() takes a
00347   // tuple<bool, int>, and so on.
00348   EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
00349 }
00350 
00351 // Tests that Action<F> can be contructed from a pointer to
00352 // ActionInterface<F>.
00353 TEST(ActionTest, CanBeConstructedFromActionInterface) {
00354   Action<MyGlobalFunction> action(new MyActionImpl);
00355 }
00356 
00357 // Tests that Action<F> delegates actual work to ActionInterface<F>.
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 // Tests that Action<F> can be copied.
00366 TEST(ActionTest, IsCopyable) {
00367   Action<MyGlobalFunction> a1(new MyActionImpl);
00368   Action<MyGlobalFunction> a2(a1);  // Tests the copy constructor.
00369 
00370   // a1 should continue to work after being copied from.
00371   EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
00372   EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
00373 
00374   // a2 should work like the action it was copied from.
00375   EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
00376   EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
00377 
00378   a2 = a1;  // Tests the assignment operator.
00379 
00380   // a1 should continue to work after being copied from.
00381   EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
00382   EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
00383 
00384   // a2 should work like the action it was copied from.
00385   EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
00386   EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
00387 }
00388 
00389 // Tests that an Action<From> object can be converted to a
00390 // compatible Action<To> object.
00391 
00392 class IsNotZero : public ActionInterface<bool(int)> {  // NOLINT
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 // Compiling this test on Nokia's Symbian compiler fails with:
00401 //  'Result' is not a member of class 'testing::internal::Function<int>'
00402 //  (point of instantiation: '@unnamed@gmock_actions_test_cc@::
00403 //      ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()')
00404 // with no obvious fix.
00405 TEST(ActionTest, CanBeConvertedToOtherActionType) {
00406   const Action<bool(int)> a1(new IsNotZero);  // NOLINT
00407   const Action<int(char)> a2 = Action<int(char)>(a1);  // NOLINT
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 // The following two classes are for testing MakePolymorphicAction().
00414 
00415 // Implements a polymorphic action that returns the second of the
00416 // arguments it receives.
00417 class ReturnSecondArgumentAction {
00418  public:
00419   // We want to verify that MakePolymorphicAction() can work with a
00420   // polymorphic action whose Perform() method template is either
00421   // const or not.  This lets us verify the non-const case.
00422   template <typename Result, typename ArgumentTuple>
00423   Result Perform(const ArgumentTuple& args) { return get<1>(args); }
00424 };
00425 
00426 // Implements a polymorphic action that can be used in a nullary
00427 // function to return 0.
00428 class ReturnZeroFromNullaryFunctionAction {
00429  public:
00430   // For testing that MakePolymorphicAction() works when the
00431   // implementation class' Perform() method template takes only one
00432   // template parameter.
00433   //
00434   // We want to verify that MakePolymorphicAction() can work with a
00435   // polymorphic action whose Perform() method template is either
00436   // const or not.  This lets us verify the const case.
00437   template <typename Result>
00438   Result Perform(const tuple<>&) const { return 0; }
00439 };
00440 
00441 // These functions verify that MakePolymorphicAction() returns a
00442 // PolymorphicAction<T> where T is the argument's type.
00443 
00444 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
00445   return MakePolymorphicAction(ReturnSecondArgumentAction());
00446 }
00447 
00448 PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
00449 ReturnZeroFromNullaryFunction() {
00450   return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
00451 }
00452 
00453 // Tests that MakePolymorphicAction() turns a polymorphic action
00454 // implementation class into a polymorphic action.
00455 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
00456   Action<int(bool, int, double)> a1 = ReturnSecondArgument();  // NOLINT
00457   EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
00458 }
00459 
00460 // Tests that MakePolymorphicAction() works when the implementation
00461 // class' Perform() method template has only one template parameter.
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 // Tests that Return() works as an action for void-returning
00471 // functions.
00472 TEST(ReturnTest, WorksForVoid) {
00473   const Action<void(int)> ret = Return();  // NOLINT
00474   return ret.Perform(make_tuple(1));
00475 }
00476 
00477 // Tests that Return(v) returns v.
00478 TEST(ReturnTest, ReturnsGivenValue) {
00479   Action<int()> ret = Return(1);  // NOLINT
00480   EXPECT_EQ(1, ret.Perform(make_tuple()));
00481 
00482   ret = Return(-5);
00483   EXPECT_EQ(-5, ret.Perform(make_tuple()));
00484 }
00485 
00486 // Tests that Return("string literal") works.
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 // Tests that Return(v) is covaraint.
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 // Tests that the type of the value passed into Return is converted into T
00516 // when the action is cast to Action<T(...)> rather than when the action is
00517 // performed. See comments on testing::internal::ReturnAction in
00518 // gmock-actions.h for more information.
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   // Must allow implicit conversion due to use in ImplicitCast_<T>.
00533   ToType(const FromType& x) { *x.converted() = true; }  // NOLINT
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   // Note: a non-const typecast operator.
00553   operator DestinationType() { return DestinationType(); }
00554 };
00555 
00556 TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
00557   SourceType s;
00558   Action<DestinationType()> action(Return(s));
00559 }
00560 
00561 // Tests that ReturnNull() returns NULL in a pointer-returning function.
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();  // NOLINT
00567   EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
00568 }
00569 
00570 // Tests that ReturnRef(v) works for reference types.
00571 TEST(ReturnRefTest, WorksForReference) {
00572   const int n = 0;
00573   const Action<const int&(bool)> ret = ReturnRef(n);  // NOLINT
00574 
00575   EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
00576 }
00577 
00578 // Tests that ReturnRef(v) is covariant.
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 // Tests that ReturnRefOfCopy(v) works for reference types.
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 // Tests that ReturnRefOfCopy(v) is covariant.
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 // Tests that DoDefault() does the default action for the mock method.
00614 
00615 class MyClass {};
00616 
00617 class MockClass {
00618  public:
00619   MockClass() {}
00620 
00621   MOCK_METHOD1(IntFunc, int(bool flag));  // NOLINT
00622   MOCK_METHOD0(Foo, MyClass());
00623 
00624  private:
00625   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
00626 };
00627 
00628 // Tests that DoDefault() returns the built-in default value for the
00629 // return type by default.
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 // Tests that DoDefault() throws (when exceptions are enabled) or aborts
00638 // the process when there is no built-in default value for the return type.
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 // Tests that using DoDefault() inside a composite action leads to a
00653 // run-time error.
00654 
00655 void VoidFunc(bool /* flag */) {}
00656 
00657 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
00658   MockClass mock;
00659   EXPECT_CALL(mock, IntFunc(_))
00660       .WillRepeatedly(DoAll(Invoke(VoidFunc),
00661                             DoDefault()));
00662 
00663   // Ideally we should verify the error message as well.  Sadly,
00664   // EXPECT_DEATH() can only capture stderr, while Google Mock's
00665   // errors are printed on stdout.  Therefore we have to settle for
00666   // not verifying the message.
00667   EXPECT_DEATH_IF_SUPPORTED({
00668     mock.IntFunc(true);
00669   }, "");
00670 }
00671 
00672 // Tests that DoDefault() returns the default value set by
00673 // DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
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 // Tests that DoDefault() does the action specified by ON_CALL().
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 // Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
00694 TEST(DoDefaultTest, CannotBeUsedInOnCall) {
00695   MockClass mock;
00696   EXPECT_NONFATAL_FAILURE({  // NOLINT
00697     ON_CALL(mock, IntFunc(_))
00698       .WillByDefault(DoDefault());
00699   }, "DoDefault() cannot be used in ON_CALL()");
00700 }
00701 
00702 // Tests that SetArgPointee<N>(v) sets the variable pointed to by
00703 // the N-th (0-based) argument to v.
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 // Tests that SetArgPointee<N>() accepts a string literal.
00724 // GCC prior to v4.0 and the Symbian compiler do not support this.
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 // Tests that SetArgPointee<N>() accepts a char pointer.
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 // Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf
00803 // variable pointed to by the N-th (0-based) argument to proto_buffer.
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   // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
00812   // s.t. the action works even when the original proto_buffer has
00813   // died.  We ensure this behavior by deleting msg before using the
00814   // action.
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 // Tests that SetArgPointee<N>(proto_buffer) sets the
00824 // ::ProtocolMessage variable pointed to by the N-th (0-based)
00825 // argument to proto_buffer.
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   // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
00834   // s.t. the action works even when the original proto_buffer has
00835   // died.  We ensure this behavior by deleting msg before using the
00836   // action.
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 // Tests that SetArgPointee<N>(proto2_buffer) sets the v2
00847 // protobuf variable pointed to by the N-th (0-based) argument to
00848 // proto2_buffer.
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   // SetArgPointee<N>(proto2_buffer) makes a copy of
00859   // proto2_buffer s.t. the action works even when the original
00860   // proto2_buffer has died.  We ensure this behavior by deleting msg
00861   // before using the action.
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 // Tests that SetArgPointee<N>(proto2_buffer) sets the
00872 // proto2::Message variable pointed to by the N-th (0-based) argument
00873 // to proto2_buffer.
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   // SetArgPointee<N>(proto2_buffer) makes a copy of
00884   // proto2_buffer s.t. the action works even when the original
00885   // proto2_buffer has died.  We ensure this behavior by deleting msg
00886   // before using the action.
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 // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
00900 // the N-th (0-based) argument to v.
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 // Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
00922 // variable pointed to by the N-th (0-based) argument to proto_buffer.
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   // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
00931   // s.t. the action works even when the original proto_buffer has
00932   // died.  We ensure this behavior by deleting msg before using the
00933   // action.
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 // Tests that SetArgumentPointee<N>(proto_buffer) sets the
00943 // ::ProtocolMessage variable pointed to by the N-th (0-based)
00944 // argument to proto_buffer.
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   // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
00953   // s.t. the action works even when the original proto_buffer has
00954   // died.  We ensure this behavior by deleting msg before using the
00955   // action.
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 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
00966 // protobuf variable pointed to by the N-th (0-based) argument to
00967 // proto2_buffer.
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   // SetArgumentPointee<N>(proto2_buffer) makes a copy of
00978   // proto2_buffer s.t. the action works even when the original
00979   // proto2_buffer has died.  We ensure this behavior by deleting msg
00980   // before using the action.
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 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the
00991 // proto2::Message variable pointed to by the N-th (0-based) argument
00992 // to proto2_buffer.
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   // SetArgumentPointee<N>(proto2_buffer) makes a copy of
01003   // proto2_buffer s.t. the action works even when the original
01004   // proto2_buffer has died.  We ensure this behavior by deleting msg
01005   // before using the action.
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 // Sample functions and functors for testing Invoke() and etc.
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 // Tests InvokeWithoutArgs(function).
01045 TEST(InvokeWithoutArgsTest, Function) {
01046   // As an action that takes one argument.
01047   Action<int(int)> a = InvokeWithoutArgs(Nullary);  // NOLINT
01048   EXPECT_EQ(1, a.Perform(make_tuple(2)));
01049 
01050   // As an action that takes two arguments.
01051   Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary);  // NOLINT
01052   EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
01053 
01054   // As an action that returns void.
01055   Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary);  // NOLINT
01056   g_done = false;
01057   a3.Perform(make_tuple(1));
01058   EXPECT_TRUE(g_done);
01059 }
01060 
01061 // Tests InvokeWithoutArgs(functor).
01062 TEST(InvokeWithoutArgsTest, Functor) {
01063   // As an action that takes no argument.
01064   Action<int()> a = InvokeWithoutArgs(NullaryFunctor());  // NOLINT
01065   EXPECT_EQ(2, a.Perform(make_tuple()));
01066 
01067   // As an action that takes three arguments.
01068   Action<int(int, double, char)> a2 =  // NOLINT
01069       InvokeWithoutArgs(NullaryFunctor());
01070   EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
01071 
01072   // As an action that returns void.
01073   Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
01074   g_done = false;
01075   a3.Perform(make_tuple());
01076   EXPECT_TRUE(g_done);
01077 }
01078 
01079 // Tests InvokeWithoutArgs(obj_ptr, method).
01080 TEST(InvokeWithoutArgsTest, Method) {
01081   Foo foo;
01082   Action<int(bool, char)> a =  // NOLINT
01083       InvokeWithoutArgs(&foo, &Foo::Nullary);
01084   EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
01085 }
01086 
01087 // Tests using IgnoreResult() on a polymorphic action.
01088 TEST(IgnoreResultTest, PolymorphicAction) {
01089   Action<void(int)> a = IgnoreResult(Return(5));  // NOLINT
01090   a.Perform(make_tuple(1));
01091 }
01092 
01093 // Tests using IgnoreResult() on a monomorphic action.
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 // Tests using IgnoreResult() on an action that returns a class type.
01108 
01109 MyClass ReturnMyClass(double /* x */) {
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));  // NOLINT
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 // Tests ByRef().
01172 
01173 // Tests that ReferenceWrapper<T> is copyable.
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   // Assigns a new value to ref_wrapper.
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   // Copies ref_wrapper1 to ref_wrapper.
01191   ref_wrapper = ref_wrapper1;
01192   const std::string& r3 = ref_wrapper;
01193   EXPECT_EQ(&s1, &r3);
01194 }
01195 
01196 // Tests using ByRef() on a const value.
01197 TEST(ByRefTest, ConstValue) {
01198   const int n = 0;
01199   // int& ref = ByRef(n);  // This shouldn't compile - we have a
01200                            // negative compilation test to catch it.
01201   const int& const_ref = ByRef(n);
01202   EXPECT_EQ(&n, &const_ref);
01203 }
01204 
01205 // Tests using ByRef() on a non-const value.
01206 TEST(ByRefTest, NonConstValue) {
01207   int n = 0;
01208 
01209   // ByRef(n) can be used as either an int&,
01210   int& ref = ByRef(n);
01211   EXPECT_EQ(&n, &ref);
01212 
01213   // or a const int&.
01214   const int& const_ref = ByRef(n);
01215   EXPECT_EQ(&n, &const_ref);
01216 }
01217 
01218 // Tests explicitly specifying the type when using ByRef().
01219 TEST(ByRefTest, ExplicitType) {
01220   int n = 0;
01221   const int& r1 = ByRef<const int>(n);
01222   EXPECT_EQ(&n, &r1);
01223 
01224   // ByRef<char>(n);  // This shouldn't compile - we have a negative
01225                       // compilation test to catch it.
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   // The following shouldn't compile - we have a negative compilation
01241   // test for it.
01242   //
01243   // Base b;
01244   // ByRef<Derived>(b);
01245 }
01246 
01247 // Tests that Google Mock prints expression ByRef(x) as a reference to x.
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 }  // Unnamed namespace


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:40