gmock-more-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 in gmock-more-actions.h.
00035 
00036 #include "gmock/gmock-more-actions.h"
00037 
00038 #include <functional>
00039 #include <sstream>
00040 #include <string>
00041 #include "gmock/gmock.h"
00042 #include "gtest/gtest.h"
00043 #include "gtest/internal/gtest-linked_ptr.h"
00044 
00045 namespace testing {
00046 namespace gmock_more_actions_test {
00047 
00048 using ::std::plus;
00049 using ::std::string;
00050 using ::std::tr1::get;
00051 using ::std::tr1::make_tuple;
00052 using ::std::tr1::tuple;
00053 using ::std::tr1::tuple_element;
00054 using testing::_;
00055 using testing::Action;
00056 using testing::ActionInterface;
00057 using testing::DeleteArg;
00058 using testing::Invoke;
00059 using testing::Return;
00060 using testing::ReturnArg;
00061 using testing::ReturnPointee;
00062 using testing::SaveArg;
00063 using testing::SaveArgPointee;
00064 using testing::SetArgReferee;
00065 using testing::StaticAssertTypeEq;
00066 using testing::Unused;
00067 using testing::WithArg;
00068 using testing::WithoutArgs;
00069 using testing::internal::linked_ptr;
00070 
00071 // For suppressing compiler warnings on conversion possibly losing precision.
00072 inline short Short(short n) { return n; }  // NOLINT
00073 inline char Char(char ch) { return ch; }
00074 
00075 // Sample functions and functors for testing Invoke() and etc.
00076 int Nullary() { return 1; }
00077 
00078 class NullaryFunctor {
00079  public:
00080   int operator()() { return 2; }
00081 };
00082 
00083 bool g_done = false;
00084 void VoidNullary() { g_done = true; }
00085 
00086 class VoidNullaryFunctor {
00087  public:
00088   void operator()() { g_done = true; }
00089 };
00090 
00091 bool Unary(int x) { return x < 0; }
00092 
00093 const char* Plus1(const char* s) { return s + 1; }
00094 
00095 void VoidUnary(int /* n */) { g_done = true; }
00096 
00097 bool ByConstRef(const string& s) { return s == "Hi"; }
00098 
00099 const double g_double = 0;
00100 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
00101 
00102 string ByNonConstRef(string& s) { return s += "+"; }  // NOLINT
00103 
00104 struct UnaryFunctor {
00105   int operator()(bool x) { return x ? 1 : -1; }
00106 };
00107 
00108 const char* Binary(const char* input, short n) { return input + n; }  // NOLINT
00109 
00110 void VoidBinary(int, char) { g_done = true; }
00111 
00112 int Ternary(int x, char y, short z) { return x + y + z; }  // NOLINT
00113 
00114 void VoidTernary(int, char, bool) { g_done = true; }
00115 
00116 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
00117 
00118 int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
00119 
00120 void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
00121 
00122 string Concat4(const char* s1, const char* s2, const char* s3,
00123                const char* s4) {
00124   return string(s1) + s2 + s3 + s4;
00125 }
00126 
00127 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
00128 
00129 struct SumOf5Functor {
00130   int operator()(int a, int b, int c, int d, int e) {
00131     return a + b + c + d + e;
00132   }
00133 };
00134 
00135 string Concat5(const char* s1, const char* s2, const char* s3,
00136                const char* s4, const char* s5) {
00137   return string(s1) + s2 + s3 + s4 + s5;
00138 }
00139 
00140 int SumOf6(int a, int b, int c, int d, int e, int f) {
00141   return a + b + c + d + e + f;
00142 }
00143 
00144 struct SumOf6Functor {
00145   int operator()(int a, int b, int c, int d, int e, int f) {
00146     return a + b + c + d + e + f;
00147   }
00148 };
00149 
00150 string Concat6(const char* s1, const char* s2, const char* s3,
00151                const char* s4, const char* s5, const char* s6) {
00152   return string(s1) + s2 + s3 + s4 + s5 + s6;
00153 }
00154 
00155 string Concat7(const char* s1, const char* s2, const char* s3,
00156                const char* s4, const char* s5, const char* s6,
00157                const char* s7) {
00158   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
00159 }
00160 
00161 string Concat8(const char* s1, const char* s2, const char* s3,
00162                const char* s4, const char* s5, const char* s6,
00163                const char* s7, const char* s8) {
00164   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
00165 }
00166 
00167 string Concat9(const char* s1, const char* s2, const char* s3,
00168                const char* s4, const char* s5, const char* s6,
00169                const char* s7, const char* s8, const char* s9) {
00170   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
00171 }
00172 
00173 string Concat10(const char* s1, const char* s2, const char* s3,
00174                 const char* s4, const char* s5, const char* s6,
00175                 const char* s7, const char* s8, const char* s9,
00176                 const char* s10) {
00177   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
00178 }
00179 
00180 class Foo {
00181  public:
00182   Foo() : value_(123) {}
00183 
00184   int Nullary() const { return value_; }
00185 
00186   short Unary(long x) { return static_cast<short>(value_ + x); }  // NOLINT
00187 
00188   string Binary(const string& str, char c) const { return str + c; }
00189 
00190   int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
00191 
00192   int SumOf4(int a, int b, int c, int d) const {
00193     return a + b + c + d + value_;
00194   }
00195 
00196   int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
00197 
00198   int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
00199 
00200   int SumOf6(int a, int b, int c, int d, int e, int f) {
00201     return a + b + c + d + e + f;
00202   }
00203 
00204   string Concat7(const char* s1, const char* s2, const char* s3,
00205                  const char* s4, const char* s5, const char* s6,
00206                  const char* s7) {
00207     return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
00208   }
00209 
00210   string Concat8(const char* s1, const char* s2, const char* s3,
00211                  const char* s4, const char* s5, const char* s6,
00212                  const char* s7, const char* s8) {
00213     return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
00214   }
00215 
00216   string Concat9(const char* s1, const char* s2, const char* s3,
00217                  const char* s4, const char* s5, const char* s6,
00218                  const char* s7, const char* s8, const char* s9) {
00219     return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
00220   }
00221 
00222   string Concat10(const char* s1, const char* s2, const char* s3,
00223                   const char* s4, const char* s5, const char* s6,
00224                   const char* s7, const char* s8, const char* s9,
00225                   const char* s10) {
00226     return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
00227   }
00228 
00229  private:
00230   int value_;
00231 };
00232 
00233 // Tests using Invoke() with a nullary function.
00234 TEST(InvokeTest, Nullary) {
00235   Action<int()> a = Invoke(Nullary);  // NOLINT
00236   EXPECT_EQ(1, a.Perform(make_tuple()));
00237 }
00238 
00239 // Tests using Invoke() with a unary function.
00240 TEST(InvokeTest, Unary) {
00241   Action<bool(int)> a = Invoke(Unary);  // NOLINT
00242   EXPECT_FALSE(a.Perform(make_tuple(1)));
00243   EXPECT_TRUE(a.Perform(make_tuple(-1)));
00244 }
00245 
00246 // Tests using Invoke() with a binary function.
00247 TEST(InvokeTest, Binary) {
00248   Action<const char*(const char*, short)> a = Invoke(Binary);  // NOLINT
00249   const char* p = "Hello";
00250   EXPECT_EQ(p + 2, a.Perform(make_tuple(p, Short(2))));
00251 }
00252 
00253 // Tests using Invoke() with a ternary function.
00254 TEST(InvokeTest, Ternary) {
00255   Action<int(int, char, short)> a = Invoke(Ternary);  // NOLINT
00256   EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', Short(3))));
00257 }
00258 
00259 // Tests using Invoke() with a 4-argument function.
00260 TEST(InvokeTest, FunctionThatTakes4Arguments) {
00261   Action<int(int, int, int, int)> a = Invoke(SumOf4);  // NOLINT
00262   EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4)));
00263 }
00264 
00265 // Tests using Invoke() with a 5-argument function.
00266 TEST(InvokeTest, FunctionThatTakes5Arguments) {
00267   Action<int(int, int, int, int, int)> a = Invoke(SumOf5);  // NOLINT
00268   EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
00269 }
00270 
00271 // Tests using Invoke() with a 6-argument function.
00272 TEST(InvokeTest, FunctionThatTakes6Arguments) {
00273   Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6);  // NOLINT
00274   EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
00275 }
00276 
00277 // A helper that turns the type of a C-string literal from const
00278 // char[N] to const char*.
00279 inline const char* CharPtr(const char* s) { return s; }
00280 
00281 // Tests using Invoke() with a 7-argument function.
00282 TEST(InvokeTest, FunctionThatTakes7Arguments) {
00283   Action<string(const char*, const char*, const char*, const char*,
00284                 const char*, const char*, const char*)> a =
00285       Invoke(Concat7);
00286   EXPECT_EQ("1234567",
00287             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
00288                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
00289                                  CharPtr("7"))));
00290 }
00291 
00292 // Tests using Invoke() with a 8-argument function.
00293 TEST(InvokeTest, FunctionThatTakes8Arguments) {
00294   Action<string(const char*, const char*, const char*, const char*,
00295                 const char*, const char*, const char*, const char*)> a =
00296       Invoke(Concat8);
00297   EXPECT_EQ("12345678",
00298             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
00299                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
00300                                  CharPtr("7"), CharPtr("8"))));
00301 }
00302 
00303 // Tests using Invoke() with a 9-argument function.
00304 TEST(InvokeTest, FunctionThatTakes9Arguments) {
00305   Action<string(const char*, const char*, const char*, const char*,
00306                 const char*, const char*, const char*, const char*,
00307                 const char*)> a = Invoke(Concat9);
00308   EXPECT_EQ("123456789",
00309             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
00310                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
00311                                  CharPtr("7"), CharPtr("8"), CharPtr("9"))));
00312 }
00313 
00314 // Tests using Invoke() with a 10-argument function.
00315 TEST(InvokeTest, FunctionThatTakes10Arguments) {
00316   Action<string(const char*, const char*, const char*, const char*,
00317                 const char*, const char*, const char*, const char*,
00318                 const char*, const char*)> a = Invoke(Concat10);
00319   EXPECT_EQ("1234567890",
00320             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
00321                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
00322                                  CharPtr("7"), CharPtr("8"), CharPtr("9"),
00323                                  CharPtr("0"))));
00324 }
00325 
00326 // Tests using Invoke() with functions with parameters declared as Unused.
00327 TEST(InvokeTest, FunctionWithUnusedParameters) {
00328   Action<int(int, int, double, const string&)> a1 =
00329       Invoke(SumOfFirst2);
00330   EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, string("hi"))));
00331 
00332   Action<int(int, int, bool, int*)> a2 =
00333       Invoke(SumOfFirst2);
00334   EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL))));
00335 }
00336 
00337 // Tests using Invoke() with methods with parameters declared as Unused.
00338 TEST(InvokeTest, MethodWithUnusedParameters) {
00339   Foo foo;
00340   Action<int(string, bool, int, int)> a1 =
00341       Invoke(&foo, &Foo::SumOfLast2);
00342   EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2)));
00343 
00344   Action<int(char, double, int, int)> a2 =
00345       Invoke(&foo, &Foo::SumOfLast2);
00346   EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3)));
00347 }
00348 
00349 // Tests using Invoke() with a functor.
00350 TEST(InvokeTest, Functor) {
00351   Action<long(long, int)> a = Invoke(plus<long>());  // NOLINT
00352   EXPECT_EQ(3L, a.Perform(make_tuple(1, 2)));
00353 }
00354 
00355 // Tests using Invoke(f) as an action of a compatible type.
00356 TEST(InvokeTest, FunctionWithCompatibleType) {
00357   Action<long(int, short, char, bool)> a = Invoke(SumOf4);  // NOLINT
00358   EXPECT_EQ(4321, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
00359 }
00360 
00361 // Tests using Invoke() with an object pointer and a method pointer.
00362 
00363 // Tests using Invoke() with a nullary method.
00364 TEST(InvokeMethodTest, Nullary) {
00365   Foo foo;
00366   Action<int()> a = Invoke(&foo, &Foo::Nullary);  // NOLINT
00367   EXPECT_EQ(123, a.Perform(make_tuple()));
00368 }
00369 
00370 // Tests using Invoke() with a unary method.
00371 TEST(InvokeMethodTest, Unary) {
00372   Foo foo;
00373   Action<short(long)> a = Invoke(&foo, &Foo::Unary);  // NOLINT
00374   EXPECT_EQ(4123, a.Perform(make_tuple(4000)));
00375 }
00376 
00377 // Tests using Invoke() with a binary method.
00378 TEST(InvokeMethodTest, Binary) {
00379   Foo foo;
00380   Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary);
00381   string s("Hell");
00382   EXPECT_EQ("Hello", a.Perform(make_tuple(s, 'o')));
00383 }
00384 
00385 // Tests using Invoke() with a ternary method.
00386 TEST(InvokeMethodTest, Ternary) {
00387   Foo foo;
00388   Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary);  // NOLINT
00389   EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, Char(1))));
00390 }
00391 
00392 // Tests using Invoke() with a 4-argument method.
00393 TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
00394   Foo foo;
00395   Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4);  // NOLINT
00396   EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4)));
00397 }
00398 
00399 // Tests using Invoke() with a 5-argument method.
00400 TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
00401   Foo foo;
00402   Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5);  // NOLINT
00403   EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
00404 }
00405 
00406 // Tests using Invoke() with a 6-argument method.
00407 TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
00408   Foo foo;
00409   Action<int(int, int, int, int, int, int)> a =  // NOLINT
00410       Invoke(&foo, &Foo::SumOf6);
00411   EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
00412 }
00413 
00414 // Tests using Invoke() with a 7-argument method.
00415 TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
00416   Foo foo;
00417   Action<string(const char*, const char*, const char*, const char*,
00418                 const char*, const char*, const char*)> a =
00419       Invoke(&foo, &Foo::Concat7);
00420   EXPECT_EQ("1234567",
00421             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
00422                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
00423                                  CharPtr("7"))));
00424 }
00425 
00426 // Tests using Invoke() with a 8-argument method.
00427 TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
00428   Foo foo;
00429   Action<string(const char*, const char*, const char*, const char*,
00430                 const char*, const char*, const char*, const char*)> a =
00431       Invoke(&foo, &Foo::Concat8);
00432   EXPECT_EQ("12345678",
00433             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
00434                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
00435                                  CharPtr("7"), CharPtr("8"))));
00436 }
00437 
00438 // Tests using Invoke() with a 9-argument method.
00439 TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
00440   Foo foo;
00441   Action<string(const char*, const char*, const char*, const char*,
00442                 const char*, const char*, const char*, const char*,
00443                 const char*)> a = Invoke(&foo, &Foo::Concat9);
00444   EXPECT_EQ("123456789",
00445             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
00446                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
00447                                  CharPtr("7"), CharPtr("8"), CharPtr("9"))));
00448 }
00449 
00450 // Tests using Invoke() with a 10-argument method.
00451 TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
00452   Foo foo;
00453   Action<string(const char*, const char*, const char*, const char*,
00454                 const char*, const char*, const char*, const char*,
00455                 const char*, const char*)> a = Invoke(&foo, &Foo::Concat10);
00456   EXPECT_EQ("1234567890",
00457             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
00458                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
00459                                  CharPtr("7"), CharPtr("8"), CharPtr("9"),
00460                                  CharPtr("0"))));
00461 }
00462 
00463 // Tests using Invoke(f) as an action of a compatible type.
00464 TEST(InvokeMethodTest, MethodWithCompatibleType) {
00465   Foo foo;
00466   Action<long(int, short, char, bool)> a =  // NOLINT
00467       Invoke(&foo, &Foo::SumOf4);
00468   EXPECT_EQ(4444, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
00469 }
00470 
00471 // Tests using WithoutArgs with an action that takes no argument.
00472 TEST(WithoutArgsTest, NoArg) {
00473   Action<int(int n)> a = WithoutArgs(Invoke(Nullary));  // NOLINT
00474   EXPECT_EQ(1, a.Perform(make_tuple(2)));
00475 }
00476 
00477 // Tests using WithArg with an action that takes 1 argument.
00478 TEST(WithArgTest, OneArg) {
00479   Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary));  // NOLINT
00480   EXPECT_TRUE(b.Perform(make_tuple(1.5, -1)));
00481   EXPECT_FALSE(b.Perform(make_tuple(1.5, 1)));
00482 }
00483 
00484 TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
00485   const Action<int(int)> a = ReturnArg<0>();
00486   EXPECT_EQ(5, a.Perform(make_tuple(5)));
00487 }
00488 
00489 TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
00490   const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
00491   EXPECT_TRUE(a.Perform(make_tuple(true, false, false)));
00492 }
00493 
00494 TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
00495   const Action<string(int, int, string, int)> a = ReturnArg<2>();
00496   EXPECT_EQ("seven", a.Perform(make_tuple(5, 6, string("seven"), 8)));
00497 }
00498 
00499 TEST(SaveArgActionTest, WorksForSameType) {
00500   int result = 0;
00501   const Action<void(int n)> a1 = SaveArg<0>(&result);
00502   a1.Perform(make_tuple(5));
00503   EXPECT_EQ(5, result);
00504 }
00505 
00506 TEST(SaveArgActionTest, WorksForCompatibleType) {
00507   int result = 0;
00508   const Action<void(bool, char)> a1 = SaveArg<1>(&result);
00509   a1.Perform(make_tuple(true, 'a'));
00510   EXPECT_EQ('a', result);
00511 }
00512 
00513 TEST(SaveArgPointeeActionTest, WorksForSameType) {
00514   int result = 0;
00515   const int value = 5;
00516   const Action<void(const int*)> a1 = SaveArgPointee<0>(&result);
00517   a1.Perform(make_tuple(&value));
00518   EXPECT_EQ(5, result);
00519 }
00520 
00521 TEST(SaveArgPointeeActionTest, WorksForCompatibleType) {
00522   int result = 0;
00523   char value = 'a';
00524   const Action<void(bool, char*)> a1 = SaveArgPointee<1>(&result);
00525   a1.Perform(make_tuple(true, &value));
00526   EXPECT_EQ('a', result);
00527 }
00528 
00529 TEST(SaveArgPointeeActionTest, WorksForLinkedPtr) {
00530   int result = 0;
00531   linked_ptr<int> value(new int(5));
00532   const Action<void(linked_ptr<int>)> a1 = SaveArgPointee<0>(&result);
00533   a1.Perform(make_tuple(value));
00534   EXPECT_EQ(5, result);
00535 }
00536 
00537 TEST(SetArgRefereeActionTest, WorksForSameType) {
00538   int value = 0;
00539   const Action<void(int&)> a1 = SetArgReferee<0>(1);
00540   a1.Perform(tuple<int&>(value));
00541   EXPECT_EQ(1, value);
00542 }
00543 
00544 TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
00545   int value = 0;
00546   const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
00547   a1.Perform(tuple<int, int&>(0, value));
00548   EXPECT_EQ('a', value);
00549 }
00550 
00551 TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
00552   int value = 0;
00553   const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
00554   a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
00555   EXPECT_EQ('a', value);
00556 }
00557 
00558 // A class that can be used to verify that its destructor is called: it will set
00559 // the bool provided to the constructor to true when destroyed.
00560 class DeletionTester {
00561  public:
00562   explicit DeletionTester(bool* is_deleted)
00563     : is_deleted_(is_deleted) {
00564     // Make sure the bit is set to false.
00565     *is_deleted_ = false;
00566   }
00567 
00568   ~DeletionTester() {
00569     *is_deleted_ = true;
00570   }
00571 
00572  private:
00573   bool* is_deleted_;
00574 };
00575 
00576 TEST(DeleteArgActionTest, OneArg) {
00577   bool is_deleted = false;
00578   DeletionTester* t = new DeletionTester(&is_deleted);
00579   const Action<void(DeletionTester*)> a1 = DeleteArg<0>();      // NOLINT
00580   EXPECT_FALSE(is_deleted);
00581   a1.Perform(make_tuple(t));
00582   EXPECT_TRUE(is_deleted);
00583 }
00584 
00585 TEST(DeleteArgActionTest, TenArgs) {
00586   bool is_deleted = false;
00587   DeletionTester* t = new DeletionTester(&is_deleted);
00588   const Action<void(bool, int, int, const char*, bool,
00589                     int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>();
00590   EXPECT_FALSE(is_deleted);
00591   a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
00592   EXPECT_TRUE(is_deleted);
00593 }
00594 
00595 #if GTEST_HAS_EXCEPTIONS
00596 
00597 TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
00598   const Action<void(int n)> a = Throw('a');
00599   EXPECT_THROW(a.Perform(make_tuple(0)), char);
00600 }
00601 
00602 class MyException {};
00603 
00604 TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
00605   const Action<double(char ch)> a = Throw(MyException());
00606   EXPECT_THROW(a.Perform(make_tuple('0')), MyException);
00607 }
00608 
00609 TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
00610   const Action<double()> a = Throw(MyException());
00611   EXPECT_THROW(a.Perform(make_tuple()), MyException);
00612 }
00613 
00614 #endif  // GTEST_HAS_EXCEPTIONS
00615 
00616 // Tests that SetArrayArgument<N>(first, last) sets the elements of the array
00617 // pointed to by the N-th (0-based) argument to values in range [first, last).
00618 TEST(SetArrayArgumentTest, SetsTheNthArray) {
00619   typedef void MyFunction(bool, int*, char*);
00620   int numbers[] = { 1, 2, 3 };
00621   Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
00622 
00623   int n[4] = {};
00624   int* pn = n;
00625   char ch[4] = {};
00626   char* pch = ch;
00627   a.Perform(make_tuple(true, pn, pch));
00628   EXPECT_EQ(1, n[0]);
00629   EXPECT_EQ(2, n[1]);
00630   EXPECT_EQ(3, n[2]);
00631   EXPECT_EQ(0, n[3]);
00632   EXPECT_EQ('\0', ch[0]);
00633   EXPECT_EQ('\0', ch[1]);
00634   EXPECT_EQ('\0', ch[2]);
00635   EXPECT_EQ('\0', ch[3]);
00636 
00637   // Tests first and last are iterators.
00638   std::string letters = "abc";
00639   a = SetArrayArgument<2>(letters.begin(), letters.end());
00640   std::fill_n(n, 4, 0);
00641   std::fill_n(ch, 4, '\0');
00642   a.Perform(make_tuple(true, pn, pch));
00643   EXPECT_EQ(0, n[0]);
00644   EXPECT_EQ(0, n[1]);
00645   EXPECT_EQ(0, n[2]);
00646   EXPECT_EQ(0, n[3]);
00647   EXPECT_EQ('a', ch[0]);
00648   EXPECT_EQ('b', ch[1]);
00649   EXPECT_EQ('c', ch[2]);
00650   EXPECT_EQ('\0', ch[3]);
00651 }
00652 
00653 // Tests SetArrayArgument<N>(first, last) where first == last.
00654 TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
00655   typedef void MyFunction(bool, int*);
00656   int numbers[] = { 1, 2, 3 };
00657   Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
00658 
00659   int n[4] = {};
00660   int* pn = n;
00661   a.Perform(make_tuple(true, pn));
00662   EXPECT_EQ(0, n[0]);
00663   EXPECT_EQ(0, n[1]);
00664   EXPECT_EQ(0, n[2]);
00665   EXPECT_EQ(0, n[3]);
00666 }
00667 
00668 // Tests SetArrayArgument<N>(first, last) where *first is convertible
00669 // (but not equal) to the argument type.
00670 TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
00671   typedef void MyFunction(bool, char*);
00672   int codes[] = { 97, 98, 99 };
00673   Action<MyFunction> a = SetArrayArgument<1>(codes, codes + 3);
00674 
00675   char ch[4] = {};
00676   char* pch = ch;
00677   a.Perform(make_tuple(true, pch));
00678   EXPECT_EQ('a', ch[0]);
00679   EXPECT_EQ('b', ch[1]);
00680   EXPECT_EQ('c', ch[2]);
00681   EXPECT_EQ('\0', ch[3]);
00682 }
00683 
00684 // Test SetArrayArgument<N>(first, last) with iterator as argument.
00685 TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
00686   typedef void MyFunction(bool, std::back_insert_iterator<std::string>);
00687   std::string letters = "abc";
00688   Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
00689 
00690   std::string s;
00691   a.Perform(make_tuple(true, back_inserter(s)));
00692   EXPECT_EQ(letters, s);
00693 }
00694 
00695 TEST(ReturnPointeeTest, Works) {
00696   int n = 42;
00697   const Action<int()> a = ReturnPointee(&n);
00698   EXPECT_EQ(42, a.Perform(make_tuple()));
00699 
00700   n = 43;
00701   EXPECT_EQ(43, a.Perform(make_tuple()));
00702 }
00703 
00704 }  // namespace gmock_generated_actions_test
00705 }  // namespace testing


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