OLD | NEW |
(Empty) | |
| 1 // Copyright 2007, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 // |
| 30 // Author: wan@google.com (Zhanyong Wan) |
| 31 |
| 32 // Google Mock - a framework for writing C++ mock classes. |
| 33 // |
| 34 // This file tests the built-in actions generated by a script. |
| 35 |
| 36 #include <gmock/gmock-generated-actions.h> |
| 37 |
| 38 #include <functional> |
| 39 #include <string> |
| 40 #include <gmock/gmock.h> |
| 41 #include <gtest/gtest.h> |
| 42 |
| 43 namespace testing { |
| 44 namespace gmock_generated_actions_test { |
| 45 |
| 46 using ::std::plus; |
| 47 using ::std::string; |
| 48 using ::std::tr1::get; |
| 49 using ::std::tr1::make_tuple; |
| 50 using ::std::tr1::tuple; |
| 51 using ::std::tr1::tuple_element; |
| 52 using testing::_; |
| 53 using testing::Action; |
| 54 using testing::ActionInterface; |
| 55 using testing::ByRef; |
| 56 using testing::DeleteArg; |
| 57 using testing::DoAll; |
| 58 using testing::Invoke; |
| 59 using testing::InvokeArgument; |
| 60 using testing::Return; |
| 61 using testing::ReturnNew; |
| 62 using testing::SaveArg; |
| 63 using testing::SetArgReferee; |
| 64 using testing::SetArgumentPointee; |
| 65 using testing::StaticAssertTypeEq; |
| 66 using testing::Unused; |
| 67 using testing::WithArg; |
| 68 using testing::WithArgs; |
| 69 using testing::WithoutArgs; |
| 70 |
| 71 // Sample functions and functors for testing Invoke() and etc. |
| 72 int Nullary() { return 1; } |
| 73 |
| 74 class NullaryFunctor { |
| 75 public: |
| 76 int operator()() { return 2; } |
| 77 }; |
| 78 |
| 79 bool g_done = false; |
| 80 void VoidNullary() { g_done = true; } |
| 81 |
| 82 class VoidNullaryFunctor { |
| 83 public: |
| 84 void operator()() { g_done = true; } |
| 85 }; |
| 86 |
| 87 bool Unary(int x) { return x < 0; } |
| 88 |
| 89 const char* Plus1(const char* s) { return s + 1; } |
| 90 |
| 91 void VoidUnary(int n) { g_done = true; } |
| 92 |
| 93 bool ByConstRef(const string& s) { return s == "Hi"; } |
| 94 |
| 95 const double g_double = 0; |
| 96 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; } |
| 97 |
| 98 string ByNonConstRef(string& s) { return s += "+"; } // NOLINT |
| 99 |
| 100 struct UnaryFunctor { |
| 101 int operator()(bool x) { return x ? 1 : -1; } |
| 102 }; |
| 103 |
| 104 const char* Binary(const char* input, short n) { return input + n; } // NOLINT |
| 105 |
| 106 void VoidBinary(int, char) { g_done = true; } |
| 107 |
| 108 int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT |
| 109 |
| 110 void VoidTernary(int, char, bool) { g_done = true; } |
| 111 |
| 112 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } |
| 113 |
| 114 int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; } |
| 115 |
| 116 void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } |
| 117 |
| 118 string Concat4(const char* s1, const char* s2, const char* s3, |
| 119 const char* s4) { |
| 120 return string(s1) + s2 + s3 + s4; |
| 121 } |
| 122 |
| 123 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } |
| 124 |
| 125 struct SumOf5Functor { |
| 126 int operator()(int a, int b, int c, int d, int e) { |
| 127 return a + b + c + d + e; |
| 128 } |
| 129 }; |
| 130 |
| 131 string Concat5(const char* s1, const char* s2, const char* s3, |
| 132 const char* s4, const char* s5) { |
| 133 return string(s1) + s2 + s3 + s4 + s5; |
| 134 } |
| 135 |
| 136 int SumOf6(int a, int b, int c, int d, int e, int f) { |
| 137 return a + b + c + d + e + f; |
| 138 } |
| 139 |
| 140 struct SumOf6Functor { |
| 141 int operator()(int a, int b, int c, int d, int e, int f) { |
| 142 return a + b + c + d + e + f; |
| 143 } |
| 144 }; |
| 145 |
| 146 string Concat6(const char* s1, const char* s2, const char* s3, |
| 147 const char* s4, const char* s5, const char* s6) { |
| 148 return string(s1) + s2 + s3 + s4 + s5 + s6; |
| 149 } |
| 150 |
| 151 string Concat7(const char* s1, const char* s2, const char* s3, |
| 152 const char* s4, const char* s5, const char* s6, |
| 153 const char* s7) { |
| 154 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; |
| 155 } |
| 156 |
| 157 string Concat8(const char* s1, const char* s2, const char* s3, |
| 158 const char* s4, const char* s5, const char* s6, |
| 159 const char* s7, const char* s8) { |
| 160 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; |
| 161 } |
| 162 |
| 163 string Concat9(const char* s1, const char* s2, const char* s3, |
| 164 const char* s4, const char* s5, const char* s6, |
| 165 const char* s7, const char* s8, const char* s9) { |
| 166 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; |
| 167 } |
| 168 |
| 169 string Concat10(const char* s1, const char* s2, const char* s3, |
| 170 const char* s4, const char* s5, const char* s6, |
| 171 const char* s7, const char* s8, const char* s9, |
| 172 const char* s10) { |
| 173 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; |
| 174 } |
| 175 |
| 176 class Foo { |
| 177 public: |
| 178 Foo() : value_(123) {} |
| 179 |
| 180 int Nullary() const { return value_; } |
| 181 |
| 182 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT |
| 183 |
| 184 string Binary(const string& str, char c) const { return str + c; } |
| 185 |
| 186 int Ternary(int x, bool y, char z) { return value_ + x + y*z; } |
| 187 |
| 188 int SumOf4(int a, int b, int c, int d) const { |
| 189 return a + b + c + d + value_; |
| 190 } |
| 191 |
| 192 int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; } |
| 193 |
| 194 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } |
| 195 |
| 196 int SumOf6(int a, int b, int c, int d, int e, int f) { |
| 197 return a + b + c + d + e + f; |
| 198 } |
| 199 |
| 200 string Concat7(const char* s1, const char* s2, const char* s3, |
| 201 const char* s4, const char* s5, const char* s6, |
| 202 const char* s7) { |
| 203 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; |
| 204 } |
| 205 |
| 206 string Concat8(const char* s1, const char* s2, const char* s3, |
| 207 const char* s4, const char* s5, const char* s6, |
| 208 const char* s7, const char* s8) { |
| 209 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; |
| 210 } |
| 211 |
| 212 string Concat9(const char* s1, const char* s2, const char* s3, |
| 213 const char* s4, const char* s5, const char* s6, |
| 214 const char* s7, const char* s8, const char* s9) { |
| 215 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; |
| 216 } |
| 217 |
| 218 string Concat10(const char* s1, const char* s2, const char* s3, |
| 219 const char* s4, const char* s5, const char* s6, |
| 220 const char* s7, const char* s8, const char* s9, |
| 221 const char* s10) { |
| 222 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; |
| 223 } |
| 224 private: |
| 225 int value_; |
| 226 }; |
| 227 |
| 228 // Tests using Invoke() with a nullary function. |
| 229 TEST(InvokeTest, Nullary) { |
| 230 Action<int()> a = Invoke(Nullary); // NOLINT |
| 231 EXPECT_EQ(1, a.Perform(make_tuple())); |
| 232 } |
| 233 |
| 234 // Tests using Invoke() with a unary function. |
| 235 TEST(InvokeTest, Unary) { |
| 236 Action<bool(int)> a = Invoke(Unary); // NOLINT |
| 237 EXPECT_FALSE(a.Perform(make_tuple(1))); |
| 238 EXPECT_TRUE(a.Perform(make_tuple(-1))); |
| 239 } |
| 240 |
| 241 // Tests using Invoke() with a binary function. |
| 242 TEST(InvokeTest, Binary) { |
| 243 Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT |
| 244 const char* p = "Hello"; |
| 245 EXPECT_EQ(p + 2, a.Perform(make_tuple(p, 2))); |
| 246 } |
| 247 |
| 248 // Tests using Invoke() with a ternary function. |
| 249 TEST(InvokeTest, Ternary) { |
| 250 Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT |
| 251 EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', 3))); |
| 252 } |
| 253 |
| 254 // Tests using Invoke() with a 4-argument function. |
| 255 TEST(InvokeTest, FunctionThatTakes4Arguments) { |
| 256 Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT |
| 257 EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4))); |
| 258 } |
| 259 |
| 260 // Tests using Invoke() with a 5-argument function. |
| 261 TEST(InvokeTest, FunctionThatTakes5Arguments) { |
| 262 Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT |
| 263 EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); |
| 264 } |
| 265 |
| 266 // Tests using Invoke() with a 6-argument function. |
| 267 TEST(InvokeTest, FunctionThatTakes6Arguments) { |
| 268 Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT |
| 269 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); |
| 270 } |
| 271 |
| 272 // Tests using Invoke() with a 7-argument function. |
| 273 TEST(InvokeTest, FunctionThatTakes7Arguments) { |
| 274 Action<string(const char*, const char*, const char*, const char*, |
| 275 const char*, const char*, const char*)> a = |
| 276 Invoke(Concat7); |
| 277 EXPECT_EQ("1234567", |
| 278 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7"))); |
| 279 } |
| 280 |
| 281 // Tests using Invoke() with a 8-argument function. |
| 282 TEST(InvokeTest, FunctionThatTakes8Arguments) { |
| 283 Action<string(const char*, const char*, const char*, const char*, |
| 284 const char*, const char*, const char*, const char*)> a = |
| 285 Invoke(Concat8); |
| 286 EXPECT_EQ("12345678", |
| 287 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8"))); |
| 288 } |
| 289 |
| 290 // Tests using Invoke() with a 9-argument function. |
| 291 TEST(InvokeTest, FunctionThatTakes9Arguments) { |
| 292 Action<string(const char*, const char*, const char*, const char*, |
| 293 const char*, const char*, const char*, const char*, |
| 294 const char*)> a = Invoke(Concat9); |
| 295 EXPECT_EQ("123456789", |
| 296 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9"))); |
| 297 } |
| 298 |
| 299 // Tests using Invoke() with a 10-argument function. |
| 300 TEST(InvokeTest, FunctionThatTakes10Arguments) { |
| 301 Action<string(const char*, const char*, const char*, const char*, |
| 302 const char*, const char*, const char*, const char*, |
| 303 const char*, const char*)> a = Invoke(Concat10); |
| 304 EXPECT_EQ("1234567890", a.Perform(make_tuple("1", "2", "3", "4", "5", "6", |
| 305 "7", "8", "9", "0"))); |
| 306 } |
| 307 |
| 308 // Tests using Invoke() with functions with parameters declared as Unused. |
| 309 TEST(InvokeTest, FunctionWithUnusedParameters) { |
| 310 Action<int(int, int, double, const string&)> a1 = |
| 311 Invoke(SumOfFirst2); |
| 312 EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, "hi"))); |
| 313 |
| 314 Action<int(int, int, bool, int*)> a2 = |
| 315 Invoke(SumOfFirst2); |
| 316 EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL)))); |
| 317 } |
| 318 |
| 319 // Tests using Invoke() with methods with parameters declared as Unused. |
| 320 TEST(InvokeTest, MethodWithUnusedParameters) { |
| 321 Foo foo; |
| 322 Action<int(string, bool, int, int)> a1 = |
| 323 Invoke(&foo, &Foo::SumOfLast2); |
| 324 EXPECT_EQ(12, a1.Perform(make_tuple("hi", true, 10, 2))); |
| 325 |
| 326 Action<int(char, double, int, int)> a2 = |
| 327 Invoke(&foo, &Foo::SumOfLast2); |
| 328 EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3))); |
| 329 } |
| 330 |
| 331 // Tests using Invoke() with a functor. |
| 332 TEST(InvokeTest, Functor) { |
| 333 Action<int(short, char)> a = Invoke(plus<short>()); // NOLINT |
| 334 EXPECT_EQ(3, a.Perform(make_tuple(1, 2))); |
| 335 } |
| 336 |
| 337 // Tests using Invoke(f) as an action of a compatible type. |
| 338 TEST(InvokeTest, FunctionWithCompatibleType) { |
| 339 Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT |
| 340 EXPECT_EQ(4321, a.Perform(make_tuple(4000, 300, 20, true))); |
| 341 } |
| 342 |
| 343 // Tests using Invoke() with an object pointer and a method pointer. |
| 344 |
| 345 // Tests using Invoke() with a nullary method. |
| 346 TEST(InvokeMethodTest, Nullary) { |
| 347 Foo foo; |
| 348 Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT |
| 349 EXPECT_EQ(123, a.Perform(make_tuple())); |
| 350 } |
| 351 |
| 352 // Tests using Invoke() with a unary method. |
| 353 TEST(InvokeMethodTest, Unary) { |
| 354 Foo foo; |
| 355 Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT |
| 356 EXPECT_EQ(4123, a.Perform(make_tuple(4000))); |
| 357 } |
| 358 |
| 359 // Tests using Invoke() with a binary method. |
| 360 TEST(InvokeMethodTest, Binary) { |
| 361 Foo foo; |
| 362 Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary); |
| 363 string s("Hell"); |
| 364 EXPECT_EQ("Hello", a.Perform(make_tuple(s, 'o'))); |
| 365 } |
| 366 |
| 367 // Tests using Invoke() with a ternary method. |
| 368 TEST(InvokeMethodTest, Ternary) { |
| 369 Foo foo; |
| 370 Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT |
| 371 EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, 1))); |
| 372 } |
| 373 |
| 374 // Tests using Invoke() with a 4-argument method. |
| 375 TEST(InvokeMethodTest, MethodThatTakes4Arguments) { |
| 376 Foo foo; |
| 377 Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT |
| 378 EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4))); |
| 379 } |
| 380 |
| 381 // Tests using Invoke() with a 5-argument method. |
| 382 TEST(InvokeMethodTest, MethodThatTakes5Arguments) { |
| 383 Foo foo; |
| 384 Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLIN
T |
| 385 EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); |
| 386 } |
| 387 |
| 388 // Tests using Invoke() with a 6-argument method. |
| 389 TEST(InvokeMethodTest, MethodThatTakes6Arguments) { |
| 390 Foo foo; |
| 391 Action<int(int, int, int, int, int, int)> a = // NOLINT |
| 392 Invoke(&foo, &Foo::SumOf6); |
| 393 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); |
| 394 } |
| 395 |
| 396 // Tests using Invoke() with a 7-argument method. |
| 397 TEST(InvokeMethodTest, MethodThatTakes7Arguments) { |
| 398 Foo foo; |
| 399 Action<string(const char*, const char*, const char*, const char*, |
| 400 const char*, const char*, const char*)> a = |
| 401 Invoke(&foo, &Foo::Concat7); |
| 402 EXPECT_EQ("1234567", |
| 403 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7"))); |
| 404 } |
| 405 |
| 406 // Tests using Invoke() with a 8-argument method. |
| 407 TEST(InvokeMethodTest, MethodThatTakes8Arguments) { |
| 408 Foo foo; |
| 409 Action<string(const char*, const char*, const char*, const char*, |
| 410 const char*, const char*, const char*, const char*)> a = |
| 411 Invoke(&foo, &Foo::Concat8); |
| 412 EXPECT_EQ("12345678", |
| 413 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8"))); |
| 414 } |
| 415 |
| 416 // Tests using Invoke() with a 9-argument method. |
| 417 TEST(InvokeMethodTest, MethodThatTakes9Arguments) { |
| 418 Foo foo; |
| 419 Action<string(const char*, const char*, const char*, const char*, |
| 420 const char*, const char*, const char*, const char*, |
| 421 const char*)> a = Invoke(&foo, &Foo::Concat9); |
| 422 EXPECT_EQ("123456789", |
| 423 a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9"))); |
| 424 } |
| 425 |
| 426 // Tests using Invoke() with a 10-argument method. |
| 427 TEST(InvokeMethodTest, MethodThatTakes10Arguments) { |
| 428 Foo foo; |
| 429 Action<string(const char*, const char*, const char*, const char*, |
| 430 const char*, const char*, const char*, const char*, |
| 431 const char*, const char*)> a = Invoke(&foo, &Foo::Concat10); |
| 432 EXPECT_EQ("1234567890", a.Perform(make_tuple("1", "2", "3", "4", "5", "6", |
| 433 "7", "8", "9", "0"))); |
| 434 } |
| 435 |
| 436 // Tests using Invoke(f) as an action of a compatible type. |
| 437 TEST(InvokeMethodTest, MethodWithCompatibleType) { |
| 438 Foo foo; |
| 439 Action<long(int, short, char, bool)> a = // NOLINT |
| 440 Invoke(&foo, &Foo::SumOf4); |
| 441 EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true))); |
| 442 } |
| 443 |
| 444 // Tests ByRef(). |
| 445 |
| 446 // Tests that ReferenceWrapper<T> is copyable. |
| 447 TEST(ByRefTest, IsCopyable) { |
| 448 const string s1 = "Hi"; |
| 449 const string s2 = "Hello"; |
| 450 |
| 451 ::testing::internal::ReferenceWrapper<const string> ref_wrapper = ByRef(s1); |
| 452 const string& r1 = ref_wrapper; |
| 453 EXPECT_EQ(&s1, &r1); |
| 454 |
| 455 // Assigns a new value to ref_wrapper. |
| 456 ref_wrapper = ByRef(s2); |
| 457 const string& r2 = ref_wrapper; |
| 458 EXPECT_EQ(&s2, &r2); |
| 459 |
| 460 ::testing::internal::ReferenceWrapper<const string> ref_wrapper1 = ByRef(s1); |
| 461 // Copies ref_wrapper1 to ref_wrapper. |
| 462 ref_wrapper = ref_wrapper1; |
| 463 const string& r3 = ref_wrapper; |
| 464 EXPECT_EQ(&s1, &r3); |
| 465 } |
| 466 |
| 467 // Tests using ByRef() on a const value. |
| 468 TEST(ByRefTest, ConstValue) { |
| 469 const int n = 0; |
| 470 // int& ref = ByRef(n); // This shouldn't compile - we have a |
| 471 // negative compilation test to catch it. |
| 472 const int& const_ref = ByRef(n); |
| 473 EXPECT_EQ(&n, &const_ref); |
| 474 } |
| 475 |
| 476 // Tests using ByRef() on a non-const value. |
| 477 TEST(ByRefTest, NonConstValue) { |
| 478 int n = 0; |
| 479 |
| 480 // ByRef(n) can be used as either an int&, |
| 481 int& ref = ByRef(n); |
| 482 EXPECT_EQ(&n, &ref); |
| 483 |
| 484 // or a const int&. |
| 485 const int& const_ref = ByRef(n); |
| 486 EXPECT_EQ(&n, &const_ref); |
| 487 } |
| 488 |
| 489 struct Base { |
| 490 bool operator==(const Base&) { return true; } |
| 491 }; |
| 492 |
| 493 struct Derived : public Base { |
| 494 bool operator==(const Derived&) { return true; } |
| 495 }; |
| 496 |
| 497 // Tests explicitly specifying the type when using ByRef(). |
| 498 TEST(ByRefTest, ExplicitType) { |
| 499 int n = 0; |
| 500 const int& r1 = ByRef<const int>(n); |
| 501 EXPECT_EQ(&n, &r1); |
| 502 |
| 503 // ByRef<char>(n); // This shouldn't compile - we have a negative |
| 504 // compilation test to catch it. |
| 505 |
| 506 |
| 507 Derived d; |
| 508 Derived& r2 = ByRef<Derived>(d); |
| 509 EXPECT_EQ(&d, &r2); |
| 510 |
| 511 const Derived& r3 = ByRef<const Derived>(d); |
| 512 EXPECT_EQ(&d, &r3); |
| 513 |
| 514 Base& r4 = ByRef<Base>(d); |
| 515 EXPECT_EQ(&d, &r4); |
| 516 |
| 517 const Base& r5 = ByRef<const Base>(d); |
| 518 EXPECT_EQ(&d, &r5); |
| 519 |
| 520 // The following shouldn't compile - we have a negative compilation |
| 521 // test for it. |
| 522 // |
| 523 // Base b; |
| 524 // ByRef<Derived>(b); |
| 525 } |
| 526 |
| 527 // Tests InvokeArgument<N>(...). |
| 528 |
| 529 // Tests using InvokeArgument with a nullary function. |
| 530 TEST(InvokeArgumentTest, Function0) { |
| 531 Action<int(int, int(*)())> a = InvokeArgument<1>(); // NOLINT |
| 532 EXPECT_EQ(1, a.Perform(make_tuple(2, &Nullary))); |
| 533 } |
| 534 |
| 535 // Tests using InvokeArgument with a unary function. |
| 536 TEST(InvokeArgumentTest, Functor1) { |
| 537 Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT |
| 538 EXPECT_EQ(1, a.Perform(make_tuple(UnaryFunctor()))); |
| 539 } |
| 540 |
| 541 // Tests using InvokeArgument with a 5-ary function. |
| 542 TEST(InvokeArgumentTest, Function5) { |
| 543 Action<int(int(*)(int, int, int, int, int))> a = // NOLINT |
| 544 InvokeArgument<0>(10000, 2000, 300, 40, 5); |
| 545 EXPECT_EQ(12345, a.Perform(make_tuple(&SumOf5))); |
| 546 } |
| 547 |
| 548 // Tests using InvokeArgument with a 5-ary functor. |
| 549 TEST(InvokeArgumentTest, Functor5) { |
| 550 Action<int(SumOf5Functor)> a = // NOLINT |
| 551 InvokeArgument<0>(10000, 2000, 300, 40, 5); |
| 552 EXPECT_EQ(12345, a.Perform(make_tuple(SumOf5Functor()))); |
| 553 } |
| 554 |
| 555 // Tests using InvokeArgument with a 6-ary function. |
| 556 TEST(InvokeArgumentTest, Function6) { |
| 557 Action<int(int(*)(int, int, int, int, int, int))> a = // NOLINT |
| 558 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6); |
| 559 EXPECT_EQ(123456, a.Perform(make_tuple(&SumOf6))); |
| 560 } |
| 561 |
| 562 // Tests using InvokeArgument with a 6-ary functor. |
| 563 TEST(InvokeArgumentTest, Functor6) { |
| 564 Action<int(SumOf6Functor)> a = // NOLINT |
| 565 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6); |
| 566 EXPECT_EQ(123456, a.Perform(make_tuple(SumOf6Functor()))); |
| 567 } |
| 568 |
| 569 // Tests using InvokeArgument with a 7-ary function. |
| 570 TEST(InvokeArgumentTest, Function7) { |
| 571 Action<string(string(*)(const char*, const char*, const char*, |
| 572 const char*, const char*, const char*, |
| 573 const char*))> a = |
| 574 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7"); |
| 575 EXPECT_EQ("1234567", a.Perform(make_tuple(&Concat7))); |
| 576 } |
| 577 |
| 578 // Tests using InvokeArgument with a 8-ary function. |
| 579 TEST(InvokeArgumentTest, Function8) { |
| 580 Action<string(string(*)(const char*, const char*, const char*, |
| 581 const char*, const char*, const char*, |
| 582 const char*, const char*))> a = |
| 583 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8"); |
| 584 EXPECT_EQ("12345678", a.Perform(make_tuple(&Concat8))); |
| 585 } |
| 586 |
| 587 // Tests using InvokeArgument with a 9-ary function. |
| 588 TEST(InvokeArgumentTest, Function9) { |
| 589 Action<string(string(*)(const char*, const char*, const char*, |
| 590 const char*, const char*, const char*, |
| 591 const char*, const char*, const char*))> a = |
| 592 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9"); |
| 593 EXPECT_EQ("123456789", a.Perform(make_tuple(&Concat9))); |
| 594 } |
| 595 |
| 596 // Tests using InvokeArgument with a 10-ary function. |
| 597 TEST(InvokeArgumentTest, Function10) { |
| 598 Action<string(string(*)(const char*, const char*, const char*, |
| 599 const char*, const char*, const char*, |
| 600 const char*, const char*, const char*, |
| 601 const char*))> a = |
| 602 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0"); |
| 603 EXPECT_EQ("1234567890", a.Perform(make_tuple(&Concat10))); |
| 604 } |
| 605 |
| 606 // Tests using InvokeArgument with a function that takes a pointer argument. |
| 607 TEST(InvokeArgumentTest, ByPointerFunction) { |
| 608 Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT |
| 609 InvokeArgument<0>(static_cast<const char*>("Hi"), 1); |
| 610 EXPECT_STREQ("i", a.Perform(make_tuple(&Binary))); |
| 611 } |
| 612 |
| 613 // Tests using InvokeArgument with a function that takes a const char* |
| 614 // by passing it a C-string literal. |
| 615 TEST(InvokeArgumentTest, FunctionWithCStringLiteral) { |
| 616 Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT |
| 617 InvokeArgument<0>("Hi", 1); |
| 618 EXPECT_STREQ("i", a.Perform(make_tuple(&Binary))); |
| 619 } |
| 620 |
| 621 // Tests using InvokeArgument with a function that takes a const reference. |
| 622 TEST(InvokeArgumentTest, ByConstReferenceFunction) { |
| 623 Action<bool(bool(*function)(const string& s))> a = // NOLINT |
| 624 InvokeArgument<0>(string("Hi")); |
| 625 // When action 'a' is constructed, it makes a copy of the temporary |
| 626 // string object passed to it, so it's OK to use 'a' later, when the |
| 627 // temporary object has already died. |
| 628 EXPECT_TRUE(a.Perform(make_tuple(&ByConstRef))); |
| 629 } |
| 630 |
| 631 // Tests using InvokeArgument with ByRef() and a function that takes a |
| 632 // const reference. |
| 633 TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) { |
| 634 Action<bool(bool(*)(const double& x))> a = // NOLINT |
| 635 InvokeArgument<0>(ByRef(g_double)); |
| 636 // The above line calls ByRef() on a const value. |
| 637 EXPECT_TRUE(a.Perform(make_tuple(&ReferencesGlobalDouble))); |
| 638 |
| 639 double x = 0; |
| 640 a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const. |
| 641 EXPECT_FALSE(a.Perform(make_tuple(&ReferencesGlobalDouble))); |
| 642 } |
| 643 |
| 644 // Tests using WithoutArgs with an action that takes no argument. |
| 645 TEST(WithoutArgsTest, NoArg) { |
| 646 Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT |
| 647 EXPECT_EQ(1, a.Perform(make_tuple(2))); |
| 648 } |
| 649 |
| 650 // Tests using WithArgs and WithArg with an action that takes 1 argument. |
| 651 TEST(WithArgsTest, OneArg) { |
| 652 Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT |
| 653 EXPECT_TRUE(a.Perform(make_tuple(1.5, -1))); |
| 654 EXPECT_FALSE(a.Perform(make_tuple(1.5, 1))); |
| 655 |
| 656 // Also tests the synonym WithArg. |
| 657 Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT |
| 658 EXPECT_TRUE(a.Perform(make_tuple(1.5, -1))); |
| 659 EXPECT_FALSE(a.Perform(make_tuple(1.5, 1))); |
| 660 |
| 661 } |
| 662 |
| 663 // Tests using WithArgs with an action that takes 2 arguments. |
| 664 TEST(WithArgsTest, TwoArgs) { |
| 665 Action<const char*(const char* s, double x, int n)> a = |
| 666 WithArgs<0, 2>(Invoke(Binary)); |
| 667 const char s[] = "Hello"; |
| 668 EXPECT_EQ(s + 2, a.Perform(make_tuple(s, 0.5, 2))); |
| 669 } |
| 670 |
| 671 // Tests using WithArgs with an action that takes 3 arguments. |
| 672 TEST(WithArgsTest, ThreeArgs) { |
| 673 Action<int(int, double, char, short)> a = // NOLINT |
| 674 WithArgs<0, 2, 3>(Invoke(Ternary)); |
| 675 EXPECT_EQ(123, a.Perform(make_tuple(100, 6.5, 20, 3))); |
| 676 } |
| 677 |
| 678 // Tests using WithArgs with an action that takes 4 arguments. |
| 679 TEST(WithArgsTest, FourArgs) { |
| 680 Action<string(const char*, const char*, double, const char*, const char*)> a = |
| 681 WithArgs<4, 3, 1, 0>(Invoke(Concat4)); |
| 682 EXPECT_EQ("4310", a.Perform(make_tuple("0", "1", 2.5, "3", "4"))); |
| 683 } |
| 684 |
| 685 // Tests using WithArgs with an action that takes 5 arguments. |
| 686 TEST(WithArgsTest, FiveArgs) { |
| 687 Action<string(const char*, const char*, const char*, |
| 688 const char*, const char*)> a = |
| 689 WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5)); |
| 690 EXPECT_EQ("43210", a.Perform(make_tuple("0", "1", "2", "3", "4"))); |
| 691 } |
| 692 |
| 693 // Tests using WithArgs with an action that takes 6 arguments. |
| 694 TEST(WithArgsTest, SixArgs) { |
| 695 Action<string(const char*, const char*, const char*)> a = |
| 696 WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6)); |
| 697 EXPECT_EQ("012210", a.Perform(make_tuple("0", "1", "2"))); |
| 698 } |
| 699 |
| 700 // Tests using WithArgs with an action that takes 7 arguments. |
| 701 TEST(WithArgsTest, SevenArgs) { |
| 702 Action<string(const char*, const char*, const char*, const char*)> a = |
| 703 WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7)); |
| 704 EXPECT_EQ("0123210", a.Perform(make_tuple("0", "1", "2", "3"))); |
| 705 } |
| 706 |
| 707 // Tests using WithArgs with an action that takes 8 arguments. |
| 708 TEST(WithArgsTest, EightArgs) { |
| 709 Action<string(const char*, const char*, const char*, const char*)> a = |
| 710 WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8)); |
| 711 EXPECT_EQ("01230123", a.Perform(make_tuple("0", "1", "2", "3"))); |
| 712 } |
| 713 |
| 714 // Tests using WithArgs with an action that takes 9 arguments. |
| 715 TEST(WithArgsTest, NineArgs) { |
| 716 Action<string(const char*, const char*, const char*, const char*)> a = |
| 717 WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9)); |
| 718 EXPECT_EQ("012312323", a.Perform(make_tuple("0", "1", "2", "3"))); |
| 719 } |
| 720 |
| 721 // Tests using WithArgs with an action that takes 10 arguments. |
| 722 TEST(WithArgsTest, TenArgs) { |
| 723 Action<string(const char*, const char*, const char*, const char*)> a = |
| 724 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10)); |
| 725 EXPECT_EQ("0123210123", a.Perform(make_tuple("0", "1", "2", "3"))); |
| 726 } |
| 727 |
| 728 // Tests using WithArgs with an action that is not Invoke(). |
| 729 class SubstractAction : public ActionInterface<int(int, int)> { // NOLINT |
| 730 public: |
| 731 virtual int Perform(const tuple<int, int>& args) { |
| 732 return get<0>(args) - get<1>(args); |
| 733 } |
| 734 }; |
| 735 |
| 736 TEST(WithArgsTest, NonInvokeAction) { |
| 737 Action<int(const string&, int, int)> a = // NOLINT |
| 738 WithArgs<2, 1>(MakeAction(new SubstractAction)); |
| 739 EXPECT_EQ(8, a.Perform(make_tuple("hi", 2, 10))); |
| 740 } |
| 741 |
| 742 // Tests using WithArgs to pass all original arguments in the original order. |
| 743 TEST(WithArgsTest, Identity) { |
| 744 Action<int(int x, char y, short z)> a = // NOLINT |
| 745 WithArgs<0, 1, 2>(Invoke(Ternary)); |
| 746 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 3))); |
| 747 } |
| 748 |
| 749 // Tests using WithArgs with repeated arguments. |
| 750 TEST(WithArgsTest, RepeatedArguments) { |
| 751 Action<int(bool, int m, int n)> a = // NOLINT |
| 752 WithArgs<1, 1, 1, 1>(Invoke(SumOf4)); |
| 753 EXPECT_EQ(4, a.Perform(make_tuple(false, 1, 10))); |
| 754 } |
| 755 |
| 756 // Tests using WithArgs with reversed argument order. |
| 757 TEST(WithArgsTest, ReversedArgumentOrder) { |
| 758 Action<const char*(short n, const char* input)> a = // NOLINT |
| 759 WithArgs<1, 0>(Invoke(Binary)); |
| 760 const char s[] = "Hello"; |
| 761 EXPECT_EQ(s + 2, a.Perform(make_tuple(2, s))); |
| 762 } |
| 763 |
| 764 // Tests using WithArgs with compatible, but not identical, argument types. |
| 765 TEST(WithArgsTest, ArgsOfCompatibleTypes) { |
| 766 Action<long(short x, int y, double z, char c)> a = // NOLINT |
| 767 WithArgs<0, 1, 3>(Invoke(Ternary)); |
| 768 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 5.6, 3))); |
| 769 } |
| 770 |
| 771 // Tests using WithArgs with an action that returns void. |
| 772 TEST(WithArgsTest, VoidAction) { |
| 773 Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary)); |
| 774 g_done = false; |
| 775 a.Perform(make_tuple(1.5, 'a', 3)); |
| 776 EXPECT_TRUE(g_done); |
| 777 } |
| 778 |
| 779 // Tests DoAll(a1, a2). |
| 780 TEST(DoAllTest, TwoActions) { |
| 781 int n = 0; |
| 782 Action<int(int*)> a = DoAll(SetArgumentPointee<0>(1), // NOLINT |
| 783 Return(2)); |
| 784 EXPECT_EQ(2, a.Perform(make_tuple(&n))); |
| 785 EXPECT_EQ(1, n); |
| 786 } |
| 787 |
| 788 // Tests DoAll(a1, a2, a3). |
| 789 TEST(DoAllTest, ThreeActions) { |
| 790 int m = 0, n = 0; |
| 791 Action<int(int*, int*)> a = DoAll(SetArgumentPointee<0>(1), // NOLINT |
| 792 SetArgumentPointee<1>(2), |
| 793 Return(3)); |
| 794 EXPECT_EQ(3, a.Perform(make_tuple(&m, &n))); |
| 795 EXPECT_EQ(1, m); |
| 796 EXPECT_EQ(2, n); |
| 797 } |
| 798 |
| 799 // Tests DoAll(a1, a2, a3, a4). |
| 800 TEST(DoAllTest, FourActions) { |
| 801 int m = 0, n = 0; |
| 802 char ch = '\0'; |
| 803 Action<int(int*, int*, char*)> a = // NOLINT |
| 804 DoAll(SetArgumentPointee<0>(1), |
| 805 SetArgumentPointee<1>(2), |
| 806 SetArgumentPointee<2>('a'), |
| 807 Return(3)); |
| 808 EXPECT_EQ(3, a.Perform(make_tuple(&m, &n, &ch))); |
| 809 EXPECT_EQ(1, m); |
| 810 EXPECT_EQ(2, n); |
| 811 EXPECT_EQ('a', ch); |
| 812 } |
| 813 |
| 814 // Tests DoAll(a1, a2, a3, a4, a5). |
| 815 TEST(DoAllTest, FiveActions) { |
| 816 int m = 0, n = 0; |
| 817 char a = '\0', b = '\0'; |
| 818 Action<int(int*, int*, char*, char*)> action = // NOLINT |
| 819 DoAll(SetArgumentPointee<0>(1), |
| 820 SetArgumentPointee<1>(2), |
| 821 SetArgumentPointee<2>('a'), |
| 822 SetArgumentPointee<3>('b'), |
| 823 Return(3)); |
| 824 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b))); |
| 825 EXPECT_EQ(1, m); |
| 826 EXPECT_EQ(2, n); |
| 827 EXPECT_EQ('a', a); |
| 828 EXPECT_EQ('b', b); |
| 829 } |
| 830 |
| 831 // Tests DoAll(a1, a2, ..., a6). |
| 832 TEST(DoAllTest, SixActions) { |
| 833 int m = 0, n = 0; |
| 834 char a = '\0', b = '\0', c = '\0'; |
| 835 Action<int(int*, int*, char*, char*, char*)> action = // NOLINT |
| 836 DoAll(SetArgumentPointee<0>(1), |
| 837 SetArgumentPointee<1>(2), |
| 838 SetArgumentPointee<2>('a'), |
| 839 SetArgumentPointee<3>('b'), |
| 840 SetArgumentPointee<4>('c'), |
| 841 Return(3)); |
| 842 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c))); |
| 843 EXPECT_EQ(1, m); |
| 844 EXPECT_EQ(2, n); |
| 845 EXPECT_EQ('a', a); |
| 846 EXPECT_EQ('b', b); |
| 847 EXPECT_EQ('c', c); |
| 848 } |
| 849 |
| 850 // Tests DoAll(a1, a2, ..., a7). |
| 851 TEST(DoAllTest, SevenActions) { |
| 852 int m = 0, n = 0; |
| 853 char a = '\0', b = '\0', c = '\0', d = '\0'; |
| 854 Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT |
| 855 DoAll(SetArgumentPointee<0>(1), |
| 856 SetArgumentPointee<1>(2), |
| 857 SetArgumentPointee<2>('a'), |
| 858 SetArgumentPointee<3>('b'), |
| 859 SetArgumentPointee<4>('c'), |
| 860 SetArgumentPointee<5>('d'), |
| 861 Return(3)); |
| 862 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d))); |
| 863 EXPECT_EQ(1, m); |
| 864 EXPECT_EQ(2, n); |
| 865 EXPECT_EQ('a', a); |
| 866 EXPECT_EQ('b', b); |
| 867 EXPECT_EQ('c', c); |
| 868 EXPECT_EQ('d', d); |
| 869 } |
| 870 |
| 871 // Tests DoAll(a1, a2, ..., a8). |
| 872 TEST(DoAllTest, EightActions) { |
| 873 int m = 0, n = 0; |
| 874 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0'; |
| 875 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT |
| 876 char*)> action = |
| 877 DoAll(SetArgumentPointee<0>(1), |
| 878 SetArgumentPointee<1>(2), |
| 879 SetArgumentPointee<2>('a'), |
| 880 SetArgumentPointee<3>('b'), |
| 881 SetArgumentPointee<4>('c'), |
| 882 SetArgumentPointee<5>('d'), |
| 883 SetArgumentPointee<6>('e'), |
| 884 Return(3)); |
| 885 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e))); |
| 886 EXPECT_EQ(1, m); |
| 887 EXPECT_EQ(2, n); |
| 888 EXPECT_EQ('a', a); |
| 889 EXPECT_EQ('b', b); |
| 890 EXPECT_EQ('c', c); |
| 891 EXPECT_EQ('d', d); |
| 892 EXPECT_EQ('e', e); |
| 893 } |
| 894 |
| 895 // Tests DoAll(a1, a2, ..., a9). |
| 896 TEST(DoAllTest, NineActions) { |
| 897 int m = 0, n = 0; |
| 898 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0'; |
| 899 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT |
| 900 char*, char*)> action = |
| 901 DoAll(SetArgumentPointee<0>(1), |
| 902 SetArgumentPointee<1>(2), |
| 903 SetArgumentPointee<2>('a'), |
| 904 SetArgumentPointee<3>('b'), |
| 905 SetArgumentPointee<4>('c'), |
| 906 SetArgumentPointee<5>('d'), |
| 907 SetArgumentPointee<6>('e'), |
| 908 SetArgumentPointee<7>('f'), |
| 909 Return(3)); |
| 910 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f))); |
| 911 EXPECT_EQ(1, m); |
| 912 EXPECT_EQ(2, n); |
| 913 EXPECT_EQ('a', a); |
| 914 EXPECT_EQ('b', b); |
| 915 EXPECT_EQ('c', c); |
| 916 EXPECT_EQ('d', d); |
| 917 EXPECT_EQ('e', e); |
| 918 EXPECT_EQ('f', f); |
| 919 } |
| 920 |
| 921 // Tests DoAll(a1, a2, ..., a10). |
| 922 TEST(DoAllTest, TenActions) { |
| 923 int m = 0, n = 0; |
| 924 char a = '\0', b = '\0', c = '\0', d = '\0'; |
| 925 char e = '\0', f = '\0', g = '\0'; |
| 926 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT |
| 927 char*, char*, char*)> action = |
| 928 DoAll(SetArgumentPointee<0>(1), |
| 929 SetArgumentPointee<1>(2), |
| 930 SetArgumentPointee<2>('a'), |
| 931 SetArgumentPointee<3>('b'), |
| 932 SetArgumentPointee<4>('c'), |
| 933 SetArgumentPointee<5>('d'), |
| 934 SetArgumentPointee<6>('e'), |
| 935 SetArgumentPointee<7>('f'), |
| 936 SetArgumentPointee<8>('g'), |
| 937 Return(3)); |
| 938 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g))); |
| 939 EXPECT_EQ(1, m); |
| 940 EXPECT_EQ(2, n); |
| 941 EXPECT_EQ('a', a); |
| 942 EXPECT_EQ('b', b); |
| 943 EXPECT_EQ('c', c); |
| 944 EXPECT_EQ('d', d); |
| 945 EXPECT_EQ('e', e); |
| 946 EXPECT_EQ('f', f); |
| 947 EXPECT_EQ('g', g); |
| 948 } |
| 949 |
| 950 // Tests the ACTION*() macro family. |
| 951 |
| 952 // Tests that ACTION() can define an action that doesn't reference the |
| 953 // mock function arguments. |
| 954 ACTION(Return5) { return 5; } |
| 955 |
| 956 TEST(ActionMacroTest, WorksWhenNotReferencingArguments) { |
| 957 Action<double()> a1 = Return5(); |
| 958 EXPECT_DOUBLE_EQ(5, a1.Perform(make_tuple())); |
| 959 |
| 960 Action<int(double, bool)> a2 = Return5(); |
| 961 EXPECT_EQ(5, a2.Perform(make_tuple(1, true))); |
| 962 } |
| 963 |
| 964 // Tests that ACTION() can define an action that returns void. |
| 965 ACTION(IncrementArg1) { (*arg1)++; } |
| 966 |
| 967 TEST(ActionMacroTest, WorksWhenReturningVoid) { |
| 968 Action<void(int, int*)> a1 = IncrementArg1(); |
| 969 int n = 0; |
| 970 a1.Perform(make_tuple(5, &n)); |
| 971 EXPECT_EQ(1, n); |
| 972 } |
| 973 |
| 974 // Tests that the body of ACTION() can reference the type of the |
| 975 // argument. |
| 976 ACTION(IncrementArg2) { |
| 977 StaticAssertTypeEq<int*, arg2_type>(); |
| 978 arg2_type temp = arg2; |
| 979 (*temp)++; |
| 980 } |
| 981 |
| 982 TEST(ActionMacroTest, CanReferenceArgumentType) { |
| 983 Action<void(int, bool, int*)> a1 = IncrementArg2(); |
| 984 int n = 0; |
| 985 a1.Perform(make_tuple(5, false, &n)); |
| 986 EXPECT_EQ(1, n); |
| 987 } |
| 988 |
| 989 // Tests that the body of ACTION() can reference the argument tuple |
| 990 // via args_type and args. |
| 991 ACTION(Sum2) { |
| 992 StaticAssertTypeEq< ::std::tr1::tuple<int, char, int*>, args_type>(); |
| 993 args_type args_copy = args; |
| 994 return get<0>(args_copy) + get<1>(args_copy); |
| 995 } |
| 996 |
| 997 TEST(ActionMacroTest, CanReferenceArgumentTuple) { |
| 998 Action<int(int, char, int*)> a1 = Sum2(); |
| 999 int dummy = 0; |
| 1000 EXPECT_EQ(11, a1.Perform(make_tuple(5, static_cast<char>(6), &dummy))); |
| 1001 } |
| 1002 |
| 1003 // Tests that the body of ACTION() can reference the mock function |
| 1004 // type. |
| 1005 int Dummy(bool flag) { return flag? 1 : 0; } |
| 1006 |
| 1007 ACTION(InvokeDummy) { |
| 1008 StaticAssertTypeEq<int(bool), function_type>(); |
| 1009 function_type* fp = &Dummy; |
| 1010 return (*fp)(true); |
| 1011 } |
| 1012 |
| 1013 TEST(ActionMacroTest, CanReferenceMockFunctionType) { |
| 1014 Action<int(bool)> a1 = InvokeDummy(); |
| 1015 EXPECT_EQ(1, a1.Perform(make_tuple(true))); |
| 1016 EXPECT_EQ(1, a1.Perform(make_tuple(false))); |
| 1017 } |
| 1018 |
| 1019 // Tests that the body of ACTION() can reference the mock function's |
| 1020 // return type. |
| 1021 ACTION(InvokeDummy2) { |
| 1022 StaticAssertTypeEq<int, return_type>(); |
| 1023 return_type result = Dummy(true); |
| 1024 return result; |
| 1025 } |
| 1026 |
| 1027 TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) { |
| 1028 Action<int(bool)> a1 = InvokeDummy2(); |
| 1029 EXPECT_EQ(1, a1.Perform(make_tuple(true))); |
| 1030 EXPECT_EQ(1, a1.Perform(make_tuple(false))); |
| 1031 } |
| 1032 |
| 1033 // Tests that ACTION() works for arguments passed by const reference. |
| 1034 ACTION(ReturnAddrOfConstBoolReferenceArg) { |
| 1035 StaticAssertTypeEq<const bool&, arg1_type>(); |
| 1036 return &arg1; |
| 1037 } |
| 1038 |
| 1039 TEST(ActionMacroTest, WorksForConstReferenceArg) { |
| 1040 Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg(); |
| 1041 const bool b = false; |
| 1042 EXPECT_EQ(&b, a.Perform(tuple<int, const bool&>(0, b))); |
| 1043 } |
| 1044 |
| 1045 // Tests that ACTION() works for arguments passed by non-const reference. |
| 1046 ACTION(ReturnAddrOfIntReferenceArg) { |
| 1047 StaticAssertTypeEq<int&, arg0_type>(); |
| 1048 return &arg0; |
| 1049 } |
| 1050 |
| 1051 TEST(ActionMacroTest, WorksForNonConstReferenceArg) { |
| 1052 Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg(); |
| 1053 int n = 0; |
| 1054 EXPECT_EQ(&n, a.Perform(tuple<int&, bool, int>(n, true, 1))); |
| 1055 } |
| 1056 |
| 1057 // Tests that ACTION() can be used in a namespace. |
| 1058 namespace action_test { |
| 1059 ACTION(Sum) { return arg0 + arg1; } |
| 1060 } // namespace action_test |
| 1061 |
| 1062 TEST(ActionMacroTest, WorksInNamespace) { |
| 1063 Action<int(int, int)> a1 = action_test::Sum(); |
| 1064 EXPECT_EQ(3, a1.Perform(make_tuple(1, 2))); |
| 1065 } |
| 1066 |
| 1067 // Tests that the same ACTION definition works for mock functions with |
| 1068 // different argument numbers. |
| 1069 ACTION(PlusTwo) { return arg0 + 2; } |
| 1070 |
| 1071 TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) { |
| 1072 Action<int(int)> a1 = PlusTwo(); |
| 1073 EXPECT_EQ(4, a1.Perform(make_tuple(2))); |
| 1074 |
| 1075 Action<double(float, void*)> a2 = PlusTwo(); |
| 1076 int dummy; |
| 1077 EXPECT_DOUBLE_EQ(6, a2.Perform(make_tuple(4.0f, &dummy))); |
| 1078 } |
| 1079 |
| 1080 // Tests that ACTION_P can define a parameterized action. |
| 1081 ACTION_P(Plus, n) { return arg0 + n; } |
| 1082 |
| 1083 TEST(ActionPMacroTest, DefinesParameterizedAction) { |
| 1084 Action<int(int m, bool t)> a1 = Plus(9); |
| 1085 EXPECT_EQ(10, a1.Perform(make_tuple(1, true))); |
| 1086 } |
| 1087 |
| 1088 // Tests that the body of ACTION_P can reference the argument types |
| 1089 // and the parameter type. |
| 1090 ACTION_P(TypedPlus, n) { |
| 1091 arg0_type t1 = arg0; |
| 1092 n_type t2 = n; |
| 1093 return t1 + t2; |
| 1094 } |
| 1095 |
| 1096 TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) { |
| 1097 Action<int(char m, bool t)> a1 = TypedPlus(9); |
| 1098 EXPECT_EQ(10, a1.Perform(make_tuple(static_cast<char>(1), true))); |
| 1099 } |
| 1100 |
| 1101 // Tests that a parameterized action can be used in any mock function |
| 1102 // whose type is compatible. |
| 1103 TEST(ActionPMacroTest, WorksInCompatibleMockFunction) { |
| 1104 Action<std::string(const std::string& s)> a1 = Plus("tail"); |
| 1105 const std::string re = "re"; |
| 1106 EXPECT_EQ("retail", a1.Perform(make_tuple(re))); |
| 1107 } |
| 1108 |
| 1109 // Tests that we can use ACTION*() to define actions overloaded on the |
| 1110 // number of parameters. |
| 1111 |
| 1112 ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; } |
| 1113 |
| 1114 ACTION_P(OverloadedAction, default_value) { |
| 1115 return arg0 ? arg1 : default_value; |
| 1116 } |
| 1117 |
| 1118 ACTION_P2(OverloadedAction, true_value, false_value) { |
| 1119 return arg0 ? true_value : false_value; |
| 1120 } |
| 1121 |
| 1122 TEST(ActionMacroTest, CanDefineOverloadedActions) { |
| 1123 typedef Action<const char*(bool, const char*)> MyAction; |
| 1124 |
| 1125 const MyAction a1 = OverloadedAction(); |
| 1126 EXPECT_STREQ("hello", a1.Perform(make_tuple(false, "world"))); |
| 1127 EXPECT_STREQ("world", a1.Perform(make_tuple(true, "world"))); |
| 1128 |
| 1129 const MyAction a2 = OverloadedAction("hi"); |
| 1130 EXPECT_STREQ("hi", a2.Perform(make_tuple(false, "world"))); |
| 1131 EXPECT_STREQ("world", a2.Perform(make_tuple(true, "world"))); |
| 1132 |
| 1133 const MyAction a3 = OverloadedAction("hi", "you"); |
| 1134 EXPECT_STREQ("hi", a3.Perform(make_tuple(true, "world"))); |
| 1135 EXPECT_STREQ("you", a3.Perform(make_tuple(false, "world"))); |
| 1136 } |
| 1137 |
| 1138 // Tests ACTION_Pn where n >= 3. |
| 1139 |
| 1140 ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; } |
| 1141 |
| 1142 TEST(ActionPnMacroTest, WorksFor3Parameters) { |
| 1143 Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4); |
| 1144 EXPECT_DOUBLE_EQ(3123.4, a1.Perform(make_tuple(3000, true))); |
| 1145 |
| 1146 Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">"); |
| 1147 const std::string re = "re"; |
| 1148 EXPECT_EQ("retail->", a2.Perform(make_tuple(re))); |
| 1149 } |
| 1150 |
| 1151 ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; } |
| 1152 |
| 1153 TEST(ActionPnMacroTest, WorksFor4Parameters) { |
| 1154 Action<int(int)> a1 = Plus(1, 2, 3, 4); |
| 1155 EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(make_tuple(10))); |
| 1156 } |
| 1157 |
| 1158 ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; } |
| 1159 |
| 1160 TEST(ActionPnMacroTest, WorksFor5Parameters) { |
| 1161 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5); |
| 1162 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(make_tuple(10))); |
| 1163 } |
| 1164 |
| 1165 ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) { |
| 1166 return arg0 + p0 + p1 + p2 + p3 + p4 + p5; |
| 1167 } |
| 1168 |
| 1169 TEST(ActionPnMacroTest, WorksFor6Parameters) { |
| 1170 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6); |
| 1171 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(make_tuple(10))); |
| 1172 } |
| 1173 |
| 1174 ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) { |
| 1175 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6; |
| 1176 } |
| 1177 |
| 1178 TEST(ActionPnMacroTest, WorksFor7Parameters) { |
| 1179 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7); |
| 1180 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(make_tuple(10))); |
| 1181 } |
| 1182 |
| 1183 ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) { |
| 1184 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7; |
| 1185 } |
| 1186 |
| 1187 TEST(ActionPnMacroTest, WorksFor8Parameters) { |
| 1188 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8); |
| 1189 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, a1.Perform(make_tuple(10))); |
| 1190 } |
| 1191 |
| 1192 ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) { |
| 1193 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8; |
| 1194 } |
| 1195 |
| 1196 TEST(ActionPnMacroTest, WorksFor9Parameters) { |
| 1197 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 1198 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9, a1.Perform(make_tuple(10))); |
| 1199 } |
| 1200 |
| 1201 ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) { |
| 1202 arg0_type t0 = arg0; |
| 1203 last_param_type t9 = last_param; |
| 1204 return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9; |
| 1205 } |
| 1206 |
| 1207 TEST(ActionPnMacroTest, WorksFor10Parameters) { |
| 1208 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 1209 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, |
| 1210 a1.Perform(make_tuple(10))); |
| 1211 } |
| 1212 |
| 1213 // Tests that the action body can promote the parameter types. |
| 1214 |
| 1215 ACTION_P2(PadArgument, prefix, suffix) { |
| 1216 // The following lines promote the two parameters to desired types. |
| 1217 std::string prefix_str(prefix); |
| 1218 char suffix_char(suffix); |
| 1219 return prefix_str + arg0 + suffix_char; |
| 1220 } |
| 1221 |
| 1222 TEST(ActionPnMacroTest, SimpleTypePromotion) { |
| 1223 Action<std::string(const char*)> no_promo = |
| 1224 PadArgument(std::string("foo"), 'r'); |
| 1225 Action<std::string(const char*)> promo = |
| 1226 PadArgument("foo", static_cast<int>('r')); |
| 1227 EXPECT_EQ("foobar", no_promo.Perform(make_tuple("ba"))); |
| 1228 EXPECT_EQ("foobar", promo.Perform(make_tuple("ba"))); |
| 1229 } |
| 1230 |
| 1231 // Tests that we can partially restrict parameter types using a |
| 1232 // straight-forward pattern. |
| 1233 |
| 1234 // Defines a generic action that doesn't restrict the types of its |
| 1235 // parameters. |
| 1236 ACTION_P3(ConcatImpl, a, b, c) { |
| 1237 std::stringstream ss; |
| 1238 ss << a << b << c; |
| 1239 return ss.str(); |
| 1240 } |
| 1241 |
| 1242 // Next, we try to restrict that either the first parameter is a |
| 1243 // string, or the second parameter is an int. |
| 1244 |
| 1245 // Defines a partially specialized wrapper that restricts the first |
| 1246 // parameter to std::string. |
| 1247 template <typename T1, typename T2> |
| 1248 // ConcatImplActionP3 is the class template ACTION_P3 uses to |
| 1249 // implement ConcatImpl. We shouldn't change the name as this |
| 1250 // pattern requires the user to use it directly. |
| 1251 ConcatImplActionP3<std::string, T1, T2> |
| 1252 Concat(const std::string& a, T1 b, T2 c) { |
| 1253 if (true) { |
| 1254 // This branch verifies that ConcatImpl() can be invoked without |
| 1255 // explicit template arguments. |
| 1256 return ConcatImpl(a, b, c); |
| 1257 } else { |
| 1258 // This branch verifies that ConcatImpl() can also be invoked with |
| 1259 // explicit template arguments. It doesn't really need to be |
| 1260 // executed as this is a compile-time verification. |
| 1261 return ConcatImpl<std::string, T1, T2>(a, b, c); |
| 1262 } |
| 1263 } |
| 1264 |
| 1265 // Defines another partially specialized wrapper that restricts the |
| 1266 // second parameter to int. |
| 1267 template <typename T1, typename T2> |
| 1268 ConcatImplActionP3<T1, int, T2> |
| 1269 Concat(T1 a, int b, T2 c) { |
| 1270 return ConcatImpl(a, b, c); |
| 1271 } |
| 1272 |
| 1273 TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) { |
| 1274 Action<const std::string()> a1 = Concat("Hello", "1", 2); |
| 1275 EXPECT_EQ("Hello12", a1.Perform(make_tuple())); |
| 1276 |
| 1277 a1 = Concat(1, 2, 3); |
| 1278 EXPECT_EQ("123", a1.Perform(make_tuple())); |
| 1279 } |
| 1280 |
| 1281 // Verifies the type of an ACTION*. |
| 1282 |
| 1283 ACTION(DoFoo) {} |
| 1284 ACTION_P(DoFoo, p) {} |
| 1285 ACTION_P2(DoFoo, p0, p1) {} |
| 1286 |
| 1287 TEST(ActionPnMacroTest, TypesAreCorrect) { |
| 1288 // DoFoo() must be assignable to a DoFooAction variable. |
| 1289 DoFooAction a0 = DoFoo(); |
| 1290 |
| 1291 // DoFoo(1) must be assignable to a DoFooActionP variable. |
| 1292 DoFooActionP<int> a1 = DoFoo(1); |
| 1293 |
| 1294 // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk |
| 1295 // variable, and so on. |
| 1296 DoFooActionP2<int, char> a2 = DoFoo(1, '2'); |
| 1297 PlusActionP3<int, int, char> a3 = Plus(1, 2, '3'); |
| 1298 PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4'); |
| 1299 PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5'); |
| 1300 PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6'); |
| 1301 PlusActionP7<int, int, int, int, int, int, char> a7 = |
| 1302 Plus(1, 2, 3, 4, 5, 6, '7'); |
| 1303 PlusActionP8<int, int, int, int, int, int, int, char> a8 = |
| 1304 Plus(1, 2, 3, 4, 5, 6, 7, '8'); |
| 1305 PlusActionP9<int, int, int, int, int, int, int, int, char> a9 = |
| 1306 Plus(1, 2, 3, 4, 5, 6, 7, 8, '9'); |
| 1307 PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 = |
| 1308 Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); |
| 1309 } |
| 1310 |
| 1311 // Tests that an ACTION_P*() action can be explicitly instantiated |
| 1312 // with reference-typed parameters. |
| 1313 |
| 1314 ACTION_P(Plus1, x) { return x; } |
| 1315 ACTION_P2(Plus2, x, y) { return x + y; } |
| 1316 ACTION_P3(Plus3, x, y, z) { return x + y + z; } |
| 1317 ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) { |
| 1318 return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9; |
| 1319 } |
| 1320 |
| 1321 TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) { |
| 1322 int x = 1, y = 2, z = 3; |
| 1323 const tuple<> empty = make_tuple(); |
| 1324 |
| 1325 Action<int()> a = Plus1<int&>(x); |
| 1326 EXPECT_EQ(1, a.Perform(empty)); |
| 1327 |
| 1328 a = Plus2<const int&, int&>(x, y); |
| 1329 EXPECT_EQ(3, a.Perform(empty)); |
| 1330 |
| 1331 a = Plus3<int&, const int&, int&>(x, y, z); |
| 1332 EXPECT_EQ(6, a.Perform(empty)); |
| 1333 |
| 1334 int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 1335 a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&, |
| 1336 int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], |
| 1337 n[8], n[9]); |
| 1338 EXPECT_EQ(55, a.Perform(empty)); |
| 1339 } |
| 1340 |
| 1341 TEST(SaveArgActionTest, WorksForSameType) { |
| 1342 int result = 0; |
| 1343 const Action<void(int n)> a1 = SaveArg<0>(&result); |
| 1344 a1.Perform(make_tuple(5)); |
| 1345 EXPECT_EQ(5, result); |
| 1346 } |
| 1347 |
| 1348 TEST(SaveArgActionTest, WorksForCompatibleType) { |
| 1349 int result = 0; |
| 1350 const Action<void(bool, char)> a1 = SaveArg<1>(&result); |
| 1351 a1.Perform(make_tuple(true, 'a')); |
| 1352 EXPECT_EQ('a', result); |
| 1353 } |
| 1354 |
| 1355 TEST(SetArgRefereeActionTest, WorksForSameType) { |
| 1356 int value = 0; |
| 1357 const Action<void(int&)> a1 = SetArgReferee<0>(1); |
| 1358 a1.Perform(tuple<int&>(value)); |
| 1359 EXPECT_EQ(1, value); |
| 1360 } |
| 1361 |
| 1362 TEST(SetArgRefereeActionTest, WorksForCompatibleType) { |
| 1363 int value = 0; |
| 1364 const Action<void(int, int&)> a1 = SetArgReferee<1>('a'); |
| 1365 a1.Perform(tuple<int, int&>(0, value)); |
| 1366 EXPECT_EQ('a', value); |
| 1367 } |
| 1368 |
| 1369 TEST(SetArgRefereeActionTest, WorksWithExtraArguments) { |
| 1370 int value = 0; |
| 1371 const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a'); |
| 1372 a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi")); |
| 1373 EXPECT_EQ('a', value); |
| 1374 } |
| 1375 |
| 1376 class NullaryConstructorClass { |
| 1377 public: |
| 1378 NullaryConstructorClass() : value_(123) {} |
| 1379 int value_; |
| 1380 }; |
| 1381 |
| 1382 // Tests using ReturnNew() with a nullary constructor. |
| 1383 TEST(ReturnNewTest, NoArgs) { |
| 1384 Action<NullaryConstructorClass*()> a = ReturnNew<NullaryConstructorClass>(); |
| 1385 NullaryConstructorClass* c = a.Perform(make_tuple()); |
| 1386 EXPECT_EQ(123, c->value_); |
| 1387 delete c; |
| 1388 } |
| 1389 |
| 1390 class UnaryConstructorClass { |
| 1391 public: |
| 1392 explicit UnaryConstructorClass(int value) : value_(value) {} |
| 1393 int value_; |
| 1394 }; |
| 1395 |
| 1396 // Tests using ReturnNew() with a unary constructor. |
| 1397 TEST(ReturnNewTest, Unary) { |
| 1398 Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000); |
| 1399 UnaryConstructorClass* c = a.Perform(make_tuple()); |
| 1400 EXPECT_EQ(4000, c->value_); |
| 1401 delete c; |
| 1402 } |
| 1403 |
| 1404 TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) { |
| 1405 Action<UnaryConstructorClass*(bool, int)> a = |
| 1406 ReturnNew<UnaryConstructorClass>(4000); |
| 1407 UnaryConstructorClass* c = a.Perform(make_tuple(false, 5)); |
| 1408 EXPECT_EQ(4000, c->value_); |
| 1409 delete c; |
| 1410 } |
| 1411 |
| 1412 TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) { |
| 1413 Action<const UnaryConstructorClass*()> a = |
| 1414 ReturnNew<UnaryConstructorClass>(4000); |
| 1415 const UnaryConstructorClass* c = a.Perform(make_tuple()); |
| 1416 EXPECT_EQ(4000, c->value_); |
| 1417 delete c; |
| 1418 } |
| 1419 |
| 1420 class TenArgConstructorClass { |
| 1421 public: |
| 1422 TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5, |
| 1423 int a6, int a7, int a8, int a9, int a10) |
| 1424 : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) { |
| 1425 } |
| 1426 int value_; |
| 1427 }; |
| 1428 |
| 1429 // Tests using ReturnNew() with a 10-argument constructor. |
| 1430 TEST(ReturnNewTest, ConstructorThatTakes10Arguments) { |
| 1431 Action<TenArgConstructorClass*()> a = |
| 1432 ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000, |
| 1433 4000000, 500000, 60000, |
| 1434 7000, 800, 90, 0); |
| 1435 TenArgConstructorClass* c = a.Perform(make_tuple()); |
| 1436 EXPECT_EQ(1234567890, c->value_); |
| 1437 delete c; |
| 1438 } |
| 1439 |
| 1440 // A class that can be used to verify that its destructor is called: it will set |
| 1441 // the bool provided to the constructor to true when destroyed. |
| 1442 class DeletionTester { |
| 1443 public: |
| 1444 explicit DeletionTester(bool* is_deleted) |
| 1445 : is_deleted_(is_deleted) { |
| 1446 // Make sure the bit is set to false. |
| 1447 *is_deleted_ = false; |
| 1448 } |
| 1449 |
| 1450 ~DeletionTester() { |
| 1451 *is_deleted_ = true; |
| 1452 } |
| 1453 |
| 1454 private: |
| 1455 bool* is_deleted_; |
| 1456 }; |
| 1457 |
| 1458 TEST(DeleteArgActionTest, OneArg) { |
| 1459 bool is_deleted = false; |
| 1460 DeletionTester* t = new DeletionTester(&is_deleted); |
| 1461 const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT |
| 1462 EXPECT_FALSE(is_deleted); |
| 1463 a1.Perform(make_tuple(t)); |
| 1464 EXPECT_TRUE(is_deleted); |
| 1465 } |
| 1466 |
| 1467 TEST(DeleteArgActionTest, TenArgs) { |
| 1468 bool is_deleted = false; |
| 1469 DeletionTester* t = new DeletionTester(&is_deleted); |
| 1470 const Action<void(bool, int, int, const char*, bool, |
| 1471 int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>(); |
| 1472 EXPECT_FALSE(is_deleted); |
| 1473 a1.Perform(make_tuple(true, 5, 6, "hi", false, 7, 8, 9, 10, t)); |
| 1474 EXPECT_TRUE(is_deleted); |
| 1475 } |
| 1476 |
| 1477 #if GTEST_HAS_EXCEPTIONS |
| 1478 |
| 1479 TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) { |
| 1480 const Action<void(int n)> a = Throw('a'); |
| 1481 EXPECT_THROW(a.Perform(make_tuple(0)), char); |
| 1482 } |
| 1483 |
| 1484 class MyException {}; |
| 1485 |
| 1486 TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) { |
| 1487 const Action<double(char ch)> a = Throw(MyException()); |
| 1488 EXPECT_THROW(a.Perform(make_tuple('0')), MyException); |
| 1489 } |
| 1490 |
| 1491 TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) { |
| 1492 const Action<double()> a = Throw(MyException()); |
| 1493 EXPECT_THROW(a.Perform(make_tuple()), MyException); |
| 1494 } |
| 1495 |
| 1496 #endif // GTEST_HAS_EXCEPTIONS |
| 1497 |
| 1498 // Tests that ACTION_TEMPLATE works when there is no value parameter. |
| 1499 ACTION_TEMPLATE(CreateNew, |
| 1500 HAS_1_TEMPLATE_PARAMS(typename, T), |
| 1501 AND_0_VALUE_PARAMS()) { |
| 1502 return new T; |
| 1503 } |
| 1504 |
| 1505 TEST(ActionTemplateTest, WorksWithoutValueParam) { |
| 1506 const Action<int*()> a = CreateNew<int>(); |
| 1507 int* p = a.Perform(make_tuple()); |
| 1508 delete p; |
| 1509 } |
| 1510 |
| 1511 // Tests that ACTION_TEMPLATE works when there are value parameters. |
| 1512 ACTION_TEMPLATE(CreateNew, |
| 1513 HAS_1_TEMPLATE_PARAMS(typename, T), |
| 1514 AND_1_VALUE_PARAMS(a0)) { |
| 1515 return new T(a0); |
| 1516 } |
| 1517 |
| 1518 TEST(ActionTemplateTest, WorksWithValueParams) { |
| 1519 const Action<int*()> a = CreateNew<int>(42); |
| 1520 int* p = a.Perform(make_tuple()); |
| 1521 EXPECT_EQ(42, *p); |
| 1522 delete p; |
| 1523 } |
| 1524 |
| 1525 // Tests that ACTION_TEMPLATE works for integral template parameters. |
| 1526 ACTION_TEMPLATE(MyDeleteArg, |
| 1527 HAS_1_TEMPLATE_PARAMS(int, k), |
| 1528 AND_0_VALUE_PARAMS()) { |
| 1529 delete std::tr1::get<k>(args); |
| 1530 } |
| 1531 |
| 1532 // Resets a bool variable in the destructor. |
| 1533 class BoolResetter { |
| 1534 public: |
| 1535 explicit BoolResetter(bool* value) : value_(value) {} |
| 1536 ~BoolResetter() { *value_ = false; } |
| 1537 private: |
| 1538 bool* const value_; |
| 1539 }; |
| 1540 |
| 1541 TEST(ActionTemplateTest, WorksForIntegralTemplateParams) { |
| 1542 const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>(); |
| 1543 int n = 0; |
| 1544 bool b = true; |
| 1545 BoolResetter* resetter = new BoolResetter(&b); |
| 1546 a.Perform(make_tuple(&n, resetter)); |
| 1547 EXPECT_FALSE(b); // Verifies that resetter is deleted. |
| 1548 } |
| 1549 |
| 1550 // Tests that ACTION_TEMPLATES works for template template parameters. |
| 1551 ACTION_TEMPLATE(ReturnSmartPointer, |
| 1552 HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class, |
| 1553 Pointer), |
| 1554 AND_1_VALUE_PARAMS(pointee)) { |
| 1555 return Pointer<pointee_type>(new pointee_type(pointee)); |
| 1556 } |
| 1557 |
| 1558 TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) { |
| 1559 using ::testing::internal::linked_ptr; |
| 1560 const Action<linked_ptr<int>()> a = ReturnSmartPointer<linked_ptr>(42); |
| 1561 linked_ptr<int> p = a.Perform(make_tuple()); |
| 1562 EXPECT_EQ(42, *p); |
| 1563 } |
| 1564 |
| 1565 // Tests that ACTION_TEMPLATE works for 10 template parameters. |
| 1566 template <typename T1, typename T2, typename T3, int k4, bool k5, |
| 1567 unsigned int k6, typename T7, typename T8, typename T9> |
| 1568 struct GiantTemplate { |
| 1569 public: |
| 1570 explicit GiantTemplate(int a_value) : value(a_value) {} |
| 1571 int value; |
| 1572 }; |
| 1573 |
| 1574 ACTION_TEMPLATE(ReturnGiant, |
| 1575 HAS_10_TEMPLATE_PARAMS( |
| 1576 typename, T1, |
| 1577 typename, T2, |
| 1578 typename, T3, |
| 1579 int, k4, |
| 1580 bool, k5, |
| 1581 unsigned int, k6, |
| 1582 class, T7, |
| 1583 class, T8, |
| 1584 class, T9, |
| 1585 template <typename T> class, T10), |
| 1586 AND_1_VALUE_PARAMS(value)) { |
| 1587 return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value); |
| 1588 } |
| 1589 |
| 1590 TEST(ActionTemplateTest, WorksFor10TemplateParameters) { |
| 1591 using ::testing::internal::linked_ptr; |
| 1592 typedef GiantTemplate<linked_ptr<int>, bool, double, 5, |
| 1593 true, 6, char, unsigned, int> Giant; |
| 1594 const Action<Giant()> a = ReturnGiant< |
| 1595 int, bool, double, 5, true, 6, char, unsigned, int, linked_ptr>(42); |
| 1596 Giant giant = a.Perform(make_tuple()); |
| 1597 EXPECT_EQ(42, giant.value); |
| 1598 } |
| 1599 |
| 1600 // Tests that ACTION_TEMPLATE works for 10 value parameters. |
| 1601 ACTION_TEMPLATE(ReturnSum, |
| 1602 HAS_1_TEMPLATE_PARAMS(typename, Number), |
| 1603 AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) { |
| 1604 return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10; |
| 1605 } |
| 1606 |
| 1607 TEST(ActionTemplateTest, WorksFor10ValueParameters) { |
| 1608 const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 1609 EXPECT_EQ(55, a.Perform(make_tuple())); |
| 1610 } |
| 1611 |
| 1612 // Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded |
| 1613 // on the number of value parameters. |
| 1614 |
| 1615 ACTION(ReturnSum) { return 0; } |
| 1616 |
| 1617 ACTION_P(ReturnSum, x) { return x; } |
| 1618 |
| 1619 ACTION_TEMPLATE(ReturnSum, |
| 1620 HAS_1_TEMPLATE_PARAMS(typename, Number), |
| 1621 AND_2_VALUE_PARAMS(v1, v2)) { |
| 1622 return static_cast<Number>(v1) + v2; |
| 1623 } |
| 1624 |
| 1625 ACTION_TEMPLATE(ReturnSum, |
| 1626 HAS_1_TEMPLATE_PARAMS(typename, Number), |
| 1627 AND_3_VALUE_PARAMS(v1, v2, v3)) { |
| 1628 return static_cast<Number>(v1) + v2 + v3; |
| 1629 } |
| 1630 |
| 1631 ACTION_TEMPLATE(ReturnSum, |
| 1632 HAS_2_TEMPLATE_PARAMS(typename, Number, int, k), |
| 1633 AND_4_VALUE_PARAMS(v1, v2, v3, v4)) { |
| 1634 return static_cast<Number>(v1) + v2 + v3 + v4 + k; |
| 1635 } |
| 1636 |
| 1637 TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) { |
| 1638 const Action<int()> a0 = ReturnSum(); |
| 1639 const Action<int()> a1 = ReturnSum(1); |
| 1640 const Action<int()> a2 = ReturnSum<int>(1, 2); |
| 1641 const Action<int()> a3 = ReturnSum<int>(1, 2, 3); |
| 1642 const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5); |
| 1643 EXPECT_EQ(0, a0.Perform(make_tuple())); |
| 1644 EXPECT_EQ(1, a1.Perform(make_tuple())); |
| 1645 EXPECT_EQ(3, a2.Perform(make_tuple())); |
| 1646 EXPECT_EQ(6, a3.Perform(make_tuple())); |
| 1647 EXPECT_EQ(12345, a4.Perform(make_tuple())); |
| 1648 } |
| 1649 |
| 1650 } // namespace gmock_generated_actions_test |
| 1651 } // namespace testing |
OLD | NEW |