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