shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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::DoAll; |
| 57 | using testing::Invoke; |
| 58 | using testing::InvokeArgument; |
| 59 | using testing::Return; |
| 60 | using testing::SetArgumentPointee; |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 61 | using testing::StaticAssertTypeEq; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 62 | using testing::Unused; |
| 63 | using testing::WithArg; |
| 64 | using testing::WithArgs; |
| 65 | using testing::WithoutArgs; |
| 66 | |
| 67 | // Sample functions and functors for testing Invoke() and etc. |
| 68 | int Nullary() { return 1; } |
| 69 | |
| 70 | class NullaryFunctor { |
| 71 | public: |
| 72 | int operator()() { return 2; } |
| 73 | }; |
| 74 | |
| 75 | bool g_done = false; |
| 76 | void VoidNullary() { g_done = true; } |
| 77 | |
| 78 | class VoidNullaryFunctor { |
| 79 | public: |
| 80 | void operator()() { g_done = true; } |
| 81 | }; |
| 82 | |
| 83 | bool Unary(int x) { return x < 0; } |
| 84 | |
| 85 | const char* Plus1(const char* s) { return s + 1; } |
| 86 | |
| 87 | void VoidUnary(int n) { g_done = true; } |
| 88 | |
| 89 | bool ByConstRef(const string& s) { return s == "Hi"; } |
| 90 | |
| 91 | const double g_double = 0; |
| 92 | bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; } |
| 93 | |
| 94 | string ByNonConstRef(string& s) { return s += "+"; } // NOLINT |
| 95 | |
| 96 | struct UnaryFunctor { |
| 97 | int operator()(bool x) { return x ? 1 : -1; } |
| 98 | }; |
| 99 | |
| 100 | const char* Binary(const char* input, short n) { return input + n; } // NOLINT |
| 101 | |
| 102 | void VoidBinary(int, char) { g_done = true; } |
| 103 | |
| 104 | int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT |
| 105 | |
| 106 | void VoidTernary(int, char, bool) { g_done = true; } |
| 107 | |
| 108 | int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } |
| 109 | |
| 110 | int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; } |
| 111 | |
| 112 | void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } |
| 113 | |
| 114 | string Concat4(const char* s1, const char* s2, const char* s3, |
| 115 | const char* s4) { |
| 116 | return string(s1) + s2 + s3 + s4; |
| 117 | } |
| 118 | |
| 119 | int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } |
| 120 | |
| 121 | struct SumOf5Functor { |
| 122 | int operator()(int a, int b, int c, int d, int e) { |
| 123 | return a + b + c + d + e; |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | string Concat5(const char* s1, const char* s2, const char* s3, |
| 128 | const char* s4, const char* s5) { |
| 129 | return string(s1) + s2 + s3 + s4 + s5; |
| 130 | } |
| 131 | |
| 132 | int SumOf6(int a, int b, int c, int d, int e, int f) { |
| 133 | return a + b + c + d + e + f; |
| 134 | } |
| 135 | |
| 136 | struct SumOf6Functor { |
| 137 | int operator()(int a, int b, int c, int d, int e, int f) { |
| 138 | return a + b + c + d + e + f; |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | string Concat6(const char* s1, const char* s2, const char* s3, |
| 143 | const char* s4, const char* s5, const char* s6) { |
| 144 | return string(s1) + s2 + s3 + s4 + s5 + s6; |
| 145 | } |
| 146 | |
| 147 | string Concat7(const char* s1, const char* s2, const char* s3, |
| 148 | const char* s4, const char* s5, const char* s6, |
| 149 | const char* s7) { |
| 150 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; |
| 151 | } |
| 152 | |
| 153 | string Concat8(const char* s1, const char* s2, const char* s3, |
| 154 | const char* s4, const char* s5, const char* s6, |
| 155 | const char* s7, const char* s8) { |
| 156 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; |
| 157 | } |
| 158 | |
| 159 | string Concat9(const char* s1, const char* s2, const char* s3, |
| 160 | const char* s4, const char* s5, const char* s6, |
| 161 | const char* s7, const char* s8, const char* s9) { |
| 162 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; |
| 163 | } |
| 164 | |
| 165 | string Concat10(const char* s1, const char* s2, const char* s3, |
| 166 | const char* s4, const char* s5, const char* s6, |
| 167 | const char* s7, const char* s8, const char* s9, |
| 168 | const char* s10) { |
| 169 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; |
| 170 | } |
| 171 | |
| 172 | class Foo { |
| 173 | public: |
| 174 | Foo() : value_(123) {} |
| 175 | |
| 176 | int Nullary() const { return value_; } |
| 177 | |
| 178 | short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT |
| 179 | |
| 180 | string Binary(const string& str, char c) const { return str + c; } |
| 181 | |
| 182 | int Ternary(int x, bool y, char z) { return value_ + x + y*z; } |
| 183 | |
| 184 | int SumOf4(int a, int b, int c, int d) const { |
| 185 | return a + b + c + d + value_; |
| 186 | } |
| 187 | |
| 188 | int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; } |
| 189 | |
| 190 | int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } |
| 191 | |
| 192 | int SumOf6(int a, int b, int c, int d, int e, int f) { |
| 193 | return a + b + c + d + e + f; |
| 194 | } |
| 195 | |
| 196 | string Concat7(const char* s1, const char* s2, const char* s3, |
| 197 | const char* s4, const char* s5, const char* s6, |
| 198 | const char* s7) { |
| 199 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; |
| 200 | } |
| 201 | |
| 202 | string Concat8(const char* s1, const char* s2, const char* s3, |
| 203 | const char* s4, const char* s5, const char* s6, |
| 204 | const char* s7, const char* s8) { |
| 205 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; |
| 206 | } |
| 207 | |
| 208 | string Concat9(const char* s1, const char* s2, const char* s3, |
| 209 | const char* s4, const char* s5, const char* s6, |
| 210 | const char* s7, const char* s8, const char* s9) { |
| 211 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; |
| 212 | } |
| 213 | |
| 214 | string Concat10(const char* s1, const char* s2, const char* s3, |
| 215 | const char* s4, const char* s5, const char* s6, |
| 216 | const char* s7, const char* s8, const char* s9, |
| 217 | const char* s10) { |
| 218 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; |
| 219 | } |
| 220 | private: |
| 221 | int value_; |
| 222 | }; |
| 223 | |
| 224 | // Tests using Invoke() with a nullary function. |
| 225 | TEST(InvokeTest, Nullary) { |
| 226 | Action<int()> a = Invoke(Nullary); // NOLINT |
| 227 | EXPECT_EQ(1, a.Perform(make_tuple())); |
| 228 | } |
| 229 | |
| 230 | // Tests using Invoke() with a unary function. |
| 231 | TEST(InvokeTest, Unary) { |
| 232 | Action<bool(int)> a = Invoke(Unary); // NOLINT |
| 233 | EXPECT_FALSE(a.Perform(make_tuple(1))); |
| 234 | EXPECT_TRUE(a.Perform(make_tuple(-1))); |
| 235 | } |
| 236 | |
| 237 | // Tests using Invoke() with a binary function. |
| 238 | TEST(InvokeTest, Binary) { |
| 239 | Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT |
| 240 | const char* p = "Hello"; |
| 241 | EXPECT_EQ(p + 2, a.Perform(make_tuple(p, 2))); |
| 242 | } |
| 243 | |
| 244 | // Tests using Invoke() with a ternary function. |
| 245 | TEST(InvokeTest, Ternary) { |
| 246 | Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT |
| 247 | EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', 3))); |
| 248 | } |
| 249 | |
| 250 | // Tests using Invoke() with a 4-argument function. |
| 251 | TEST(InvokeTest, FunctionThatTakes4Arguments) { |
| 252 | Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT |
| 253 | EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4))); |
| 254 | } |
| 255 | |
| 256 | // Tests using Invoke() with a 5-argument function. |
| 257 | TEST(InvokeTest, FunctionThatTakes5Arguments) { |
| 258 | Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT |
| 259 | EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); |
| 260 | } |
| 261 | |
| 262 | // Tests using Invoke() with a 6-argument function. |
| 263 | TEST(InvokeTest, FunctionThatTakes6Arguments) { |
| 264 | Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT |
| 265 | EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); |
| 266 | } |
| 267 | |
| 268 | // Tests using Invoke() with a 7-argument function. |
| 269 | TEST(InvokeTest, FunctionThatTakes7Arguments) { |
| 270 | Action<string(const char*, const char*, const char*, const char*, |
| 271 | const char*, const char*, const char*)> a = |
| 272 | Invoke(Concat7); |
| 273 | EXPECT_EQ("1234567", |
| 274 | a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7"))); |
| 275 | } |
| 276 | |
| 277 | // Tests using Invoke() with a 8-argument function. |
| 278 | TEST(InvokeTest, FunctionThatTakes8Arguments) { |
| 279 | Action<string(const char*, const char*, const char*, const char*, |
| 280 | const char*, const char*, const char*, const char*)> a = |
| 281 | Invoke(Concat8); |
| 282 | EXPECT_EQ("12345678", |
| 283 | a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8"))); |
| 284 | } |
| 285 | |
| 286 | // Tests using Invoke() with a 9-argument function. |
| 287 | TEST(InvokeTest, FunctionThatTakes9Arguments) { |
| 288 | Action<string(const char*, const char*, const char*, const char*, |
| 289 | const char*, const char*, const char*, const char*, |
| 290 | const char*)> a = Invoke(Concat9); |
| 291 | EXPECT_EQ("123456789", |
| 292 | a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9"))); |
| 293 | } |
| 294 | |
| 295 | // Tests using Invoke() with a 10-argument function. |
| 296 | TEST(InvokeTest, FunctionThatTakes10Arguments) { |
| 297 | Action<string(const char*, const char*, const char*, const char*, |
| 298 | const char*, const char*, const char*, const char*, |
| 299 | const char*, const char*)> a = Invoke(Concat10); |
| 300 | EXPECT_EQ("1234567890", a.Perform(make_tuple("1", "2", "3", "4", "5", "6", |
| 301 | "7", "8", "9", "0"))); |
| 302 | } |
| 303 | |
| 304 | // Tests using Invoke() with functions with parameters declared as Unused. |
| 305 | TEST(InvokeTest, FunctionWithUnusedParameters) { |
| 306 | Action<int(int, int, double, const string&)> a1 = |
| 307 | Invoke(SumOfFirst2); |
| 308 | EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, "hi"))); |
| 309 | |
| 310 | Action<int(int, int, bool, int*)> a2 = |
| 311 | Invoke(SumOfFirst2); |
| 312 | EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL)))); |
| 313 | } |
| 314 | |
| 315 | // Tests using Invoke() with methods with parameters declared as Unused. |
| 316 | TEST(InvokeTest, MethodWithUnusedParameters) { |
| 317 | Foo foo; |
| 318 | Action<int(string, bool, int, int)> a1 = |
| 319 | Invoke(&foo, &Foo::SumOfLast2); |
| 320 | EXPECT_EQ(12, a1.Perform(make_tuple("hi", true, 10, 2))); |
| 321 | |
| 322 | Action<int(char, double, int, int)> a2 = |
| 323 | Invoke(&foo, &Foo::SumOfLast2); |
| 324 | EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3))); |
| 325 | } |
| 326 | |
| 327 | // Tests using Invoke() with a functor. |
| 328 | TEST(InvokeTest, Functor) { |
| 329 | Action<int(short, char)> a = Invoke(plus<short>()); // NOLINT |
| 330 | EXPECT_EQ(3, a.Perform(make_tuple(1, 2))); |
| 331 | } |
| 332 | |
| 333 | // Tests using Invoke(f) as an action of a compatible type. |
| 334 | TEST(InvokeTest, FunctionWithCompatibleType) { |
| 335 | Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT |
| 336 | EXPECT_EQ(4321, a.Perform(make_tuple(4000, 300, 20, true))); |
| 337 | } |
| 338 | |
| 339 | // Tests using Invoke() with an object pointer and a method pointer. |
| 340 | |
| 341 | // Tests using Invoke() with a nullary method. |
| 342 | TEST(InvokeMethodTest, Nullary) { |
| 343 | Foo foo; |
| 344 | Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT |
| 345 | EXPECT_EQ(123, a.Perform(make_tuple())); |
| 346 | } |
| 347 | |
| 348 | // Tests using Invoke() with a unary method. |
| 349 | TEST(InvokeMethodTest, Unary) { |
| 350 | Foo foo; |
| 351 | Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT |
| 352 | EXPECT_EQ(4123, a.Perform(make_tuple(4000))); |
| 353 | } |
| 354 | |
| 355 | // Tests using Invoke() with a binary method. |
| 356 | TEST(InvokeMethodTest, Binary) { |
| 357 | Foo foo; |
| 358 | Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary); |
| 359 | string s("Hell"); |
| 360 | EXPECT_EQ("Hello", a.Perform(make_tuple(s, 'o'))); |
| 361 | } |
| 362 | |
| 363 | // Tests using Invoke() with a ternary method. |
| 364 | TEST(InvokeMethodTest, Ternary) { |
| 365 | Foo foo; |
| 366 | Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT |
| 367 | EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, 1))); |
| 368 | } |
| 369 | |
| 370 | // Tests using Invoke() with a 4-argument method. |
| 371 | TEST(InvokeMethodTest, MethodThatTakes4Arguments) { |
| 372 | Foo foo; |
| 373 | Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT |
| 374 | EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4))); |
| 375 | } |
| 376 | |
| 377 | // Tests using Invoke() with a 5-argument method. |
| 378 | TEST(InvokeMethodTest, MethodThatTakes5Arguments) { |
| 379 | Foo foo; |
| 380 | Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT |
| 381 | EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); |
| 382 | } |
| 383 | |
| 384 | // Tests using Invoke() with a 6-argument method. |
| 385 | TEST(InvokeMethodTest, MethodThatTakes6Arguments) { |
| 386 | Foo foo; |
| 387 | Action<int(int, int, int, int, int, int)> a = // NOLINT |
| 388 | Invoke(&foo, &Foo::SumOf6); |
| 389 | EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); |
| 390 | } |
| 391 | |
| 392 | // Tests using Invoke() with a 7-argument method. |
| 393 | TEST(InvokeMethodTest, MethodThatTakes7Arguments) { |
| 394 | Foo foo; |
| 395 | Action<string(const char*, const char*, const char*, const char*, |
| 396 | const char*, const char*, const char*)> a = |
| 397 | Invoke(&foo, &Foo::Concat7); |
| 398 | EXPECT_EQ("1234567", |
| 399 | a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7"))); |
| 400 | } |
| 401 | |
| 402 | // Tests using Invoke() with a 8-argument method. |
| 403 | TEST(InvokeMethodTest, MethodThatTakes8Arguments) { |
| 404 | Foo foo; |
| 405 | Action<string(const char*, const char*, const char*, const char*, |
| 406 | const char*, const char*, const char*, const char*)> a = |
| 407 | Invoke(&foo, &Foo::Concat8); |
| 408 | EXPECT_EQ("12345678", |
| 409 | a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8"))); |
| 410 | } |
| 411 | |
| 412 | // Tests using Invoke() with a 9-argument method. |
| 413 | TEST(InvokeMethodTest, MethodThatTakes9Arguments) { |
| 414 | Foo foo; |
| 415 | Action<string(const char*, const char*, const char*, const char*, |
| 416 | const char*, const char*, const char*, const char*, |
| 417 | const char*)> a = Invoke(&foo, &Foo::Concat9); |
| 418 | EXPECT_EQ("123456789", |
| 419 | a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9"))); |
| 420 | } |
| 421 | |
| 422 | // Tests using Invoke() with a 10-argument method. |
| 423 | TEST(InvokeMethodTest, MethodThatTakes10Arguments) { |
| 424 | Foo foo; |
| 425 | Action<string(const char*, const char*, const char*, const char*, |
| 426 | const char*, const char*, const char*, const char*, |
| 427 | const char*, const char*)> a = Invoke(&foo, &Foo::Concat10); |
| 428 | EXPECT_EQ("1234567890", a.Perform(make_tuple("1", "2", "3", "4", "5", "6", |
| 429 | "7", "8", "9", "0"))); |
| 430 | } |
| 431 | |
| 432 | // Tests using Invoke(f) as an action of a compatible type. |
| 433 | TEST(InvokeMethodTest, MethodWithCompatibleType) { |
| 434 | Foo foo; |
| 435 | Action<long(int, short, char, bool)> a = // NOLINT |
| 436 | Invoke(&foo, &Foo::SumOf4); |
| 437 | EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true))); |
| 438 | } |
| 439 | |
| 440 | // Tests ByRef(). |
| 441 | |
| 442 | // Tests that ReferenceWrapper<T> is copyable. |
| 443 | TEST(ByRefTest, IsCopyable) { |
| 444 | const string s1 = "Hi"; |
| 445 | const string s2 = "Hello"; |
| 446 | |
| 447 | ::testing::internal::ReferenceWrapper<const string> ref_wrapper = ByRef(s1); |
| 448 | const string& r1 = ref_wrapper; |
| 449 | EXPECT_EQ(&s1, &r1); |
| 450 | |
| 451 | // Assigns a new value to ref_wrapper. |
| 452 | ref_wrapper = ByRef(s2); |
| 453 | const string& r2 = ref_wrapper; |
| 454 | EXPECT_EQ(&s2, &r2); |
| 455 | |
| 456 | ::testing::internal::ReferenceWrapper<const string> ref_wrapper1 = ByRef(s1); |
| 457 | // Copies ref_wrapper1 to ref_wrapper. |
| 458 | ref_wrapper = ref_wrapper1; |
| 459 | const string& r3 = ref_wrapper; |
| 460 | EXPECT_EQ(&s1, &r3); |
| 461 | } |
| 462 | |
| 463 | // Tests using ByRef() on a const value. |
| 464 | TEST(ByRefTest, ConstValue) { |
| 465 | const int n = 0; |
| 466 | // int& ref = ByRef(n); // This shouldn't compile - we have a |
| 467 | // negative compilation test to catch it. |
| 468 | const int& const_ref = ByRef(n); |
| 469 | EXPECT_EQ(&n, &const_ref); |
| 470 | } |
| 471 | |
| 472 | // Tests using ByRef() on a non-const value. |
| 473 | TEST(ByRefTest, NonConstValue) { |
| 474 | int n = 0; |
| 475 | |
| 476 | // ByRef(n) can be used as either an int&, |
| 477 | int& ref = ByRef(n); |
| 478 | EXPECT_EQ(&n, &ref); |
| 479 | |
| 480 | // or a const int&. |
| 481 | const int& const_ref = ByRef(n); |
| 482 | EXPECT_EQ(&n, &const_ref); |
| 483 | } |
| 484 | |
| 485 | struct Base { |
| 486 | bool operator==(const Base&) { return true; } |
| 487 | }; |
| 488 | |
| 489 | struct Derived : public Base { |
| 490 | bool operator==(const Derived&) { return true; } |
| 491 | }; |
| 492 | |
| 493 | // Tests explicitly specifying the type when using ByRef(). |
| 494 | TEST(ByRefTest, ExplicitType) { |
| 495 | int n = 0; |
| 496 | const int& r1 = ByRef<const int>(n); |
| 497 | EXPECT_EQ(&n, &r1); |
| 498 | |
| 499 | // ByRef<char>(n); // This shouldn't compile - we have a negative |
| 500 | // compilation test to catch it. |
| 501 | |
| 502 | |
| 503 | Derived d; |
| 504 | Derived& r2 = ByRef<Derived>(d); |
| 505 | EXPECT_EQ(&d, &r2); |
| 506 | |
| 507 | const Derived& r3 = ByRef<const Derived>(d); |
| 508 | EXPECT_EQ(&d, &r3); |
| 509 | |
| 510 | Base& r4 = ByRef<Base>(d); |
| 511 | EXPECT_EQ(&d, &r4); |
| 512 | |
| 513 | const Base& r5 = ByRef<const Base>(d); |
| 514 | EXPECT_EQ(&d, &r5); |
| 515 | |
| 516 | // The following shouldn't compile - we have a negative compilation |
| 517 | // test for it. |
| 518 | // |
| 519 | // Base b; |
| 520 | // ByRef<Derived>(b); |
| 521 | } |
| 522 | |
| 523 | // Tests InvokeArgument<N>(...). |
| 524 | |
| 525 | // Tests using InvokeArgument with a nullary function. |
| 526 | TEST(InvokeArgumentTest, Function0) { |
| 527 | Action<int(int, int(*)())> a = InvokeArgument<1>(); // NOLINT |
| 528 | EXPECT_EQ(1, a.Perform(make_tuple(2, &Nullary))); |
| 529 | } |
| 530 | |
| 531 | // Tests using InvokeArgument with a unary function. |
| 532 | TEST(InvokeArgumentTest, Functor1) { |
| 533 | Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT |
| 534 | EXPECT_EQ(1, a.Perform(make_tuple(UnaryFunctor()))); |
| 535 | } |
| 536 | |
| 537 | // Tests using InvokeArgument with a 5-ary function. |
| 538 | TEST(InvokeArgumentTest, Function5) { |
| 539 | Action<int(int(*)(int, int, int, int, int))> a = // NOLINT |
| 540 | InvokeArgument<0>(10000, 2000, 300, 40, 5); |
| 541 | EXPECT_EQ(12345, a.Perform(make_tuple(&SumOf5))); |
| 542 | } |
| 543 | |
| 544 | // Tests using InvokeArgument with a 5-ary functor. |
| 545 | TEST(InvokeArgumentTest, Functor5) { |
| 546 | Action<int(SumOf5Functor)> a = // NOLINT |
| 547 | InvokeArgument<0>(10000, 2000, 300, 40, 5); |
| 548 | EXPECT_EQ(12345, a.Perform(make_tuple(SumOf5Functor()))); |
| 549 | } |
| 550 | |
| 551 | // Tests using InvokeArgument with a 6-ary function. |
| 552 | TEST(InvokeArgumentTest, Function6) { |
| 553 | Action<int(int(*)(int, int, int, int, int, int))> a = // NOLINT |
| 554 | InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6); |
| 555 | EXPECT_EQ(123456, a.Perform(make_tuple(&SumOf6))); |
| 556 | } |
| 557 | |
| 558 | // Tests using InvokeArgument with a 6-ary functor. |
| 559 | TEST(InvokeArgumentTest, Functor6) { |
| 560 | Action<int(SumOf6Functor)> a = // NOLINT |
| 561 | InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6); |
| 562 | EXPECT_EQ(123456, a.Perform(make_tuple(SumOf6Functor()))); |
| 563 | } |
| 564 | |
| 565 | // Tests using InvokeArgument with a 7-ary function. |
| 566 | TEST(InvokeArgumentTest, Function7) { |
| 567 | Action<string(string(*)(const char*, const char*, const char*, |
| 568 | const char*, const char*, const char*, |
| 569 | const char*))> a = |
| 570 | InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7"); |
| 571 | EXPECT_EQ("1234567", a.Perform(make_tuple(&Concat7))); |
| 572 | } |
| 573 | |
| 574 | // Tests using InvokeArgument with a 8-ary function. |
| 575 | TEST(InvokeArgumentTest, Function8) { |
| 576 | Action<string(string(*)(const char*, const char*, const char*, |
| 577 | const char*, const char*, const char*, |
| 578 | const char*, const char*))> a = |
| 579 | InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8"); |
| 580 | EXPECT_EQ("12345678", a.Perform(make_tuple(&Concat8))); |
| 581 | } |
| 582 | |
| 583 | // Tests using InvokeArgument with a 9-ary function. |
| 584 | TEST(InvokeArgumentTest, Function9) { |
| 585 | Action<string(string(*)(const char*, const char*, const char*, |
| 586 | const char*, const char*, const char*, |
| 587 | const char*, const char*, const char*))> a = |
| 588 | InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9"); |
| 589 | EXPECT_EQ("123456789", a.Perform(make_tuple(&Concat9))); |
| 590 | } |
| 591 | |
| 592 | // Tests using InvokeArgument with a 10-ary function. |
| 593 | TEST(InvokeArgumentTest, Function10) { |
| 594 | Action<string(string(*)(const char*, const char*, const char*, |
| 595 | const char*, const char*, const char*, |
| 596 | const char*, const char*, const char*, |
| 597 | const char*))> a = |
| 598 | InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0"); |
| 599 | EXPECT_EQ("1234567890", a.Perform(make_tuple(&Concat10))); |
| 600 | } |
| 601 | |
| 602 | // Tests using InvokeArgument with a function that takes a pointer argument. |
| 603 | TEST(InvokeArgumentTest, ByPointerFunction) { |
| 604 | Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT |
| 605 | InvokeArgument<0>(static_cast<const char*>("Hi"), 1); |
| 606 | EXPECT_STREQ("i", a.Perform(make_tuple(&Binary))); |
| 607 | } |
| 608 | |
| 609 | // Tests using InvokeArgument with a function that takes a const char* |
| 610 | // by passing it a C-string literal. |
| 611 | TEST(InvokeArgumentTest, FunctionWithCStringLiteral) { |
| 612 | Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT |
| 613 | InvokeArgument<0>("Hi", 1); |
| 614 | EXPECT_STREQ("i", a.Perform(make_tuple(&Binary))); |
| 615 | } |
| 616 | |
| 617 | // Tests using InvokeArgument with a function that takes a const reference. |
| 618 | TEST(InvokeArgumentTest, ByConstReferenceFunction) { |
| 619 | Action<bool(bool(*function)(const string& s))> a = // NOLINT |
| 620 | InvokeArgument<0>(string("Hi")); |
| 621 | // When action 'a' is constructed, it makes a copy of the temporary |
| 622 | // string object passed to it, so it's OK to use 'a' later, when the |
| 623 | // temporary object has already died. |
| 624 | EXPECT_TRUE(a.Perform(make_tuple(&ByConstRef))); |
| 625 | } |
| 626 | |
| 627 | // Tests using InvokeArgument with ByRef() and a function that takes a |
| 628 | // const reference. |
| 629 | TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) { |
| 630 | Action<bool(bool(*)(const double& x))> a = // NOLINT |
| 631 | InvokeArgument<0>(ByRef(g_double)); |
| 632 | // The above line calls ByRef() on a const value. |
| 633 | EXPECT_TRUE(a.Perform(make_tuple(&ReferencesGlobalDouble))); |
| 634 | |
| 635 | double x = 0; |
| 636 | a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const. |
| 637 | EXPECT_FALSE(a.Perform(make_tuple(&ReferencesGlobalDouble))); |
| 638 | } |
| 639 | |
| 640 | // Tests using WithoutArgs with an action that takes no argument. |
| 641 | TEST(WithoutArgsTest, NoArg) { |
| 642 | Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT |
| 643 | EXPECT_EQ(1, a.Perform(make_tuple(2))); |
| 644 | } |
| 645 | |
| 646 | // Tests using WithArgs and WithArg with an action that takes 1 argument. |
| 647 | TEST(WithArgsTest, OneArg) { |
| 648 | Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT |
| 649 | EXPECT_TRUE(a.Perform(make_tuple(1.5, -1))); |
| 650 | EXPECT_FALSE(a.Perform(make_tuple(1.5, 1))); |
| 651 | |
| 652 | // Also tests the synonym WithArg. |
| 653 | Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT |
| 654 | EXPECT_TRUE(a.Perform(make_tuple(1.5, -1))); |
| 655 | EXPECT_FALSE(a.Perform(make_tuple(1.5, 1))); |
| 656 | |
| 657 | } |
| 658 | |
| 659 | // Tests using WithArgs with an action that takes 2 arguments. |
| 660 | TEST(WithArgsTest, TwoArgs) { |
| 661 | Action<const char*(const char* s, double x, int n)> a = |
| 662 | WithArgs<0, 2>(Invoke(Binary)); |
| 663 | const char s[] = "Hello"; |
| 664 | EXPECT_EQ(s + 2, a.Perform(make_tuple(s, 0.5, 2))); |
| 665 | } |
| 666 | |
| 667 | // Tests using WithArgs with an action that takes 3 arguments. |
| 668 | TEST(WithArgsTest, ThreeArgs) { |
| 669 | Action<int(int, double, char, short)> a = // NOLINT |
| 670 | WithArgs<0, 2, 3>(Invoke(Ternary)); |
| 671 | EXPECT_EQ(123, a.Perform(make_tuple(100, 6.5, 20, 3))); |
| 672 | } |
| 673 | |
| 674 | // Tests using WithArgs with an action that takes 4 arguments. |
| 675 | TEST(WithArgsTest, FourArgs) { |
| 676 | Action<string(const char*, const char*, double, const char*, const char*)> a = |
| 677 | WithArgs<4, 3, 1, 0>(Invoke(Concat4)); |
| 678 | EXPECT_EQ("4310", a.Perform(make_tuple("0", "1", 2.5, "3", "4"))); |
| 679 | } |
| 680 | |
| 681 | // Tests using WithArgs with an action that takes 5 arguments. |
| 682 | TEST(WithArgsTest, FiveArgs) { |
| 683 | Action<string(const char*, const char*, const char*, |
| 684 | const char*, const char*)> a = |
| 685 | WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5)); |
| 686 | EXPECT_EQ("43210", a.Perform(make_tuple("0", "1", "2", "3", "4"))); |
| 687 | } |
| 688 | |
| 689 | // Tests using WithArgs with an action that takes 6 arguments. |
| 690 | TEST(WithArgsTest, SixArgs) { |
| 691 | Action<string(const char*, const char*, const char*)> a = |
| 692 | WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6)); |
| 693 | EXPECT_EQ("012210", a.Perform(make_tuple("0", "1", "2"))); |
| 694 | } |
| 695 | |
| 696 | // Tests using WithArgs with an action that takes 7 arguments. |
| 697 | TEST(WithArgsTest, SevenArgs) { |
| 698 | Action<string(const char*, const char*, const char*, const char*)> a = |
| 699 | WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7)); |
| 700 | EXPECT_EQ("0123210", a.Perform(make_tuple("0", "1", "2", "3"))); |
| 701 | } |
| 702 | |
| 703 | // Tests using WithArgs with an action that takes 8 arguments. |
| 704 | TEST(WithArgsTest, EightArgs) { |
| 705 | Action<string(const char*, const char*, const char*, const char*)> a = |
| 706 | WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8)); |
| 707 | EXPECT_EQ("01230123", a.Perform(make_tuple("0", "1", "2", "3"))); |
| 708 | } |
| 709 | |
| 710 | // Tests using WithArgs with an action that takes 9 arguments. |
| 711 | TEST(WithArgsTest, NineArgs) { |
| 712 | Action<string(const char*, const char*, const char*, const char*)> a = |
| 713 | WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9)); |
| 714 | EXPECT_EQ("012312323", a.Perform(make_tuple("0", "1", "2", "3"))); |
| 715 | } |
| 716 | |
| 717 | // Tests using WithArgs with an action that takes 10 arguments. |
| 718 | TEST(WithArgsTest, TenArgs) { |
| 719 | Action<string(const char*, const char*, const char*, const char*)> a = |
| 720 | WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10)); |
| 721 | EXPECT_EQ("0123210123", a.Perform(make_tuple("0", "1", "2", "3"))); |
| 722 | } |
| 723 | |
| 724 | // Tests using WithArgs with an action that is not Invoke(). |
| 725 | class SubstractAction : public ActionInterface<int(int, int)> { // NOLINT |
| 726 | public: |
| 727 | virtual int Perform(const tuple<int, int>& args) { |
| 728 | return get<0>(args) - get<1>(args); |
| 729 | } |
| 730 | }; |
| 731 | |
| 732 | TEST(WithArgsTest, NonInvokeAction) { |
| 733 | Action<int(const string&, int, int)> a = // NOLINT |
| 734 | WithArgs<2, 1>(MakeAction(new SubstractAction)); |
| 735 | EXPECT_EQ(8, a.Perform(make_tuple("hi", 2, 10))); |
| 736 | } |
| 737 | |
| 738 | // Tests using WithArgs to pass all original arguments in the original order. |
| 739 | TEST(WithArgsTest, Identity) { |
| 740 | Action<int(int x, char y, short z)> a = // NOLINT |
| 741 | WithArgs<0, 1, 2>(Invoke(Ternary)); |
| 742 | EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 3))); |
| 743 | } |
| 744 | |
| 745 | // Tests using WithArgs with repeated arguments. |
| 746 | TEST(WithArgsTest, RepeatedArguments) { |
| 747 | Action<int(bool, int m, int n)> a = // NOLINT |
| 748 | WithArgs<1, 1, 1, 1>(Invoke(SumOf4)); |
| 749 | EXPECT_EQ(4, a.Perform(make_tuple(false, 1, 10))); |
| 750 | } |
| 751 | |
| 752 | // Tests using WithArgs with reversed argument order. |
| 753 | TEST(WithArgsTest, ReversedArgumentOrder) { |
| 754 | Action<const char*(short n, const char* input)> a = // NOLINT |
| 755 | WithArgs<1, 0>(Invoke(Binary)); |
| 756 | const char s[] = "Hello"; |
| 757 | EXPECT_EQ(s + 2, a.Perform(make_tuple(2, s))); |
| 758 | } |
| 759 | |
| 760 | // Tests using WithArgs with compatible, but not identical, argument types. |
| 761 | TEST(WithArgsTest, ArgsOfCompatibleTypes) { |
| 762 | Action<long(short x, int y, double z, char c)> a = // NOLINT |
| 763 | WithArgs<0, 1, 3>(Invoke(Ternary)); |
| 764 | EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 5.6, 3))); |
| 765 | } |
| 766 | |
| 767 | // Tests using WithArgs with an action that returns void. |
| 768 | TEST(WithArgsTest, VoidAction) { |
| 769 | Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary)); |
| 770 | g_done = false; |
| 771 | a.Perform(make_tuple(1.5, 'a', 3)); |
| 772 | EXPECT_TRUE(g_done); |
| 773 | } |
| 774 | |
| 775 | // Tests DoAll(a1, a2). |
| 776 | TEST(DoAllTest, TwoActions) { |
| 777 | int n = 0; |
| 778 | Action<int(int*)> a = DoAll(SetArgumentPointee<0>(1), // NOLINT |
| 779 | Return(2)); |
| 780 | EXPECT_EQ(2, a.Perform(make_tuple(&n))); |
| 781 | EXPECT_EQ(1, n); |
| 782 | } |
| 783 | |
| 784 | // Tests DoAll(a1, a2, a3). |
| 785 | TEST(DoAllTest, ThreeActions) { |
| 786 | int m = 0, n = 0; |
| 787 | Action<int(int*, int*)> a = DoAll(SetArgumentPointee<0>(1), // NOLINT |
| 788 | SetArgumentPointee<1>(2), |
| 789 | Return(3)); |
| 790 | EXPECT_EQ(3, a.Perform(make_tuple(&m, &n))); |
| 791 | EXPECT_EQ(1, m); |
| 792 | EXPECT_EQ(2, n); |
| 793 | } |
| 794 | |
| 795 | // Tests DoAll(a1, a2, a3, a4). |
| 796 | TEST(DoAllTest, FourActions) { |
| 797 | int m = 0, n = 0; |
| 798 | char ch = '\0'; |
| 799 | Action<int(int*, int*, char*)> a = // NOLINT |
| 800 | DoAll(SetArgumentPointee<0>(1), |
| 801 | SetArgumentPointee<1>(2), |
| 802 | SetArgumentPointee<2>('a'), |
| 803 | Return(3)); |
| 804 | EXPECT_EQ(3, a.Perform(make_tuple(&m, &n, &ch))); |
| 805 | EXPECT_EQ(1, m); |
| 806 | EXPECT_EQ(2, n); |
| 807 | EXPECT_EQ('a', ch); |
| 808 | } |
| 809 | |
| 810 | // Tests DoAll(a1, a2, a3, a4, a5). |
| 811 | TEST(DoAllTest, FiveActions) { |
| 812 | int m = 0, n = 0; |
| 813 | char a = '\0', b = '\0'; |
| 814 | Action<int(int*, int*, char*, char*)> action = // NOLINT |
| 815 | DoAll(SetArgumentPointee<0>(1), |
| 816 | SetArgumentPointee<1>(2), |
| 817 | SetArgumentPointee<2>('a'), |
| 818 | SetArgumentPointee<3>('b'), |
| 819 | Return(3)); |
| 820 | EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b))); |
| 821 | EXPECT_EQ(1, m); |
| 822 | EXPECT_EQ(2, n); |
| 823 | EXPECT_EQ('a', a); |
| 824 | EXPECT_EQ('b', b); |
| 825 | } |
| 826 | |
| 827 | // Tests DoAll(a1, a2, ..., a6). |
| 828 | TEST(DoAllTest, SixActions) { |
| 829 | int m = 0, n = 0; |
| 830 | char a = '\0', b = '\0', c = '\0'; |
| 831 | Action<int(int*, int*, char*, char*, char*)> action = // NOLINT |
| 832 | DoAll(SetArgumentPointee<0>(1), |
| 833 | SetArgumentPointee<1>(2), |
| 834 | SetArgumentPointee<2>('a'), |
| 835 | SetArgumentPointee<3>('b'), |
| 836 | SetArgumentPointee<4>('c'), |
| 837 | Return(3)); |
| 838 | EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c))); |
| 839 | EXPECT_EQ(1, m); |
| 840 | EXPECT_EQ(2, n); |
| 841 | EXPECT_EQ('a', a); |
| 842 | EXPECT_EQ('b', b); |
| 843 | EXPECT_EQ('c', c); |
| 844 | } |
| 845 | |
| 846 | // Tests DoAll(a1, a2, ..., a7). |
| 847 | TEST(DoAllTest, SevenActions) { |
| 848 | int m = 0, n = 0; |
| 849 | char a = '\0', b = '\0', c = '\0', d = '\0'; |
| 850 | Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT |
| 851 | DoAll(SetArgumentPointee<0>(1), |
| 852 | SetArgumentPointee<1>(2), |
| 853 | SetArgumentPointee<2>('a'), |
| 854 | SetArgumentPointee<3>('b'), |
| 855 | SetArgumentPointee<4>('c'), |
| 856 | SetArgumentPointee<5>('d'), |
| 857 | Return(3)); |
| 858 | EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d))); |
| 859 | EXPECT_EQ(1, m); |
| 860 | EXPECT_EQ(2, n); |
| 861 | EXPECT_EQ('a', a); |
| 862 | EXPECT_EQ('b', b); |
| 863 | EXPECT_EQ('c', c); |
| 864 | EXPECT_EQ('d', d); |
| 865 | } |
| 866 | |
| 867 | // Tests DoAll(a1, a2, ..., a8). |
| 868 | TEST(DoAllTest, EightActions) { |
| 869 | int m = 0, n = 0; |
| 870 | char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0'; |
| 871 | Action<int(int*, int*, char*, char*, char*, char*, // NOLINT |
| 872 | char*)> action = |
| 873 | DoAll(SetArgumentPointee<0>(1), |
| 874 | SetArgumentPointee<1>(2), |
| 875 | SetArgumentPointee<2>('a'), |
| 876 | SetArgumentPointee<3>('b'), |
| 877 | SetArgumentPointee<4>('c'), |
| 878 | SetArgumentPointee<5>('d'), |
| 879 | SetArgumentPointee<6>('e'), |
| 880 | Return(3)); |
| 881 | EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e))); |
| 882 | EXPECT_EQ(1, m); |
| 883 | EXPECT_EQ(2, n); |
| 884 | EXPECT_EQ('a', a); |
| 885 | EXPECT_EQ('b', b); |
| 886 | EXPECT_EQ('c', c); |
| 887 | EXPECT_EQ('d', d); |
| 888 | EXPECT_EQ('e', e); |
| 889 | } |
| 890 | |
| 891 | // Tests DoAll(a1, a2, ..., a9). |
| 892 | TEST(DoAllTest, NineActions) { |
| 893 | int m = 0, n = 0; |
| 894 | char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0'; |
| 895 | Action<int(int*, int*, char*, char*, char*, char*, // NOLINT |
| 896 | char*, char*)> action = |
| 897 | DoAll(SetArgumentPointee<0>(1), |
| 898 | SetArgumentPointee<1>(2), |
| 899 | SetArgumentPointee<2>('a'), |
| 900 | SetArgumentPointee<3>('b'), |
| 901 | SetArgumentPointee<4>('c'), |
| 902 | SetArgumentPointee<5>('d'), |
| 903 | SetArgumentPointee<6>('e'), |
| 904 | SetArgumentPointee<7>('f'), |
| 905 | Return(3)); |
| 906 | EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f))); |
| 907 | EXPECT_EQ(1, m); |
| 908 | EXPECT_EQ(2, n); |
| 909 | EXPECT_EQ('a', a); |
| 910 | EXPECT_EQ('b', b); |
| 911 | EXPECT_EQ('c', c); |
| 912 | EXPECT_EQ('d', d); |
| 913 | EXPECT_EQ('e', e); |
| 914 | EXPECT_EQ('f', f); |
| 915 | } |
| 916 | |
| 917 | // Tests DoAll(a1, a2, ..., a10). |
| 918 | TEST(DoAllTest, TenActions) { |
| 919 | int m = 0, n = 0; |
| 920 | char a = '\0', b = '\0', c = '\0', d = '\0'; |
| 921 | char e = '\0', f = '\0', g = '\0'; |
| 922 | Action<int(int*, int*, char*, char*, char*, char*, // NOLINT |
| 923 | char*, char*, char*)> action = |
| 924 | DoAll(SetArgumentPointee<0>(1), |
| 925 | SetArgumentPointee<1>(2), |
| 926 | SetArgumentPointee<2>('a'), |
| 927 | SetArgumentPointee<3>('b'), |
| 928 | SetArgumentPointee<4>('c'), |
| 929 | SetArgumentPointee<5>('d'), |
| 930 | SetArgumentPointee<6>('e'), |
| 931 | SetArgumentPointee<7>('f'), |
| 932 | SetArgumentPointee<8>('g'), |
| 933 | Return(3)); |
| 934 | EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g))); |
| 935 | EXPECT_EQ(1, m); |
| 936 | EXPECT_EQ(2, n); |
| 937 | EXPECT_EQ('a', a); |
| 938 | EXPECT_EQ('b', b); |
| 939 | EXPECT_EQ('c', c); |
| 940 | EXPECT_EQ('d', d); |
| 941 | EXPECT_EQ('e', e); |
| 942 | EXPECT_EQ('f', f); |
| 943 | EXPECT_EQ('g', g); |
| 944 | } |
| 945 | |
shiqian | 326aa56 | 2009-01-09 21:43:57 +0000 | [diff] [blame] | 946 | // Tests the ACTION*() macro family. |
| 947 | |
| 948 | // Tests that ACTION() can define an action that doesn't reference the |
| 949 | // mock function arguments. |
| 950 | ACTION(Return5) { return 5; } |
| 951 | |
| 952 | TEST(ActionMacroTest, WorksWhenNotReferencingArguments) { |
| 953 | Action<double()> a1 = Return5(); |
| 954 | EXPECT_DOUBLE_EQ(5, a1.Perform(make_tuple())); |
| 955 | |
| 956 | Action<int(double, bool)> a2 = Return5(); |
| 957 | EXPECT_EQ(5, a2.Perform(make_tuple(1, true))); |
| 958 | } |
| 959 | |
| 960 | // Tests that ACTION() can define an action that returns void. |
| 961 | ACTION(IncrementArg1) { (*arg1)++; } |
| 962 | |
| 963 | TEST(ActionMacroTest, WorksWhenReturningVoid) { |
| 964 | Action<void(int, int*)> a1 = IncrementArg1(); |
| 965 | int n = 0; |
| 966 | a1.Perform(make_tuple(5, &n)); |
| 967 | EXPECT_EQ(1, n); |
| 968 | } |
| 969 | |
| 970 | // Tests that the body of ACTION() can reference the type of the |
| 971 | // argument. |
| 972 | ACTION(IncrementArg2) { |
| 973 | StaticAssertTypeEq<int*, arg2_type>(); |
| 974 | arg2_type temp = arg2; |
| 975 | (*temp)++; |
| 976 | } |
| 977 | |
| 978 | TEST(ActionMacroTest, CanReferenceArgumentType) { |
| 979 | Action<void(int, bool, int*)> a1 = IncrementArg2(); |
| 980 | int n = 0; |
| 981 | a1.Perform(make_tuple(5, false, &n)); |
| 982 | EXPECT_EQ(1, n); |
| 983 | } |
| 984 | |
| 985 | // Tests that the body of ACTION() can reference the argument tuple |
| 986 | // via args_type and args. |
| 987 | ACTION(Sum2) { |
| 988 | StaticAssertTypeEq< ::std::tr1::tuple<int, char, int*>, args_type>(); |
| 989 | args_type args_copy = args; |
| 990 | return get<0>(args_copy) + get<1>(args_copy); |
| 991 | } |
| 992 | |
| 993 | TEST(ActionMacroTest, CanReferenceArgumentTuple) { |
| 994 | Action<int(int, char, int*)> a1 = Sum2(); |
| 995 | int dummy = 0; |
| 996 | EXPECT_EQ(11, a1.Perform(make_tuple(5, static_cast<char>(6), &dummy))); |
| 997 | } |
| 998 | |
| 999 | // Tests that the body of ACTION() can reference the mock function |
| 1000 | // type. |
| 1001 | int Dummy(bool flag) { return flag? 1 : 0; } |
| 1002 | |
| 1003 | ACTION(InvokeDummy) { |
| 1004 | StaticAssertTypeEq<int(bool), function_type>(); |
| 1005 | function_type* fp = &Dummy; |
| 1006 | return (*fp)(true); |
| 1007 | } |
| 1008 | |
| 1009 | TEST(ActionMacroTest, CanReferenceMockFunctionType) { |
| 1010 | Action<int(bool)> a1 = InvokeDummy(); |
| 1011 | EXPECT_EQ(1, a1.Perform(make_tuple(true))); |
| 1012 | EXPECT_EQ(1, a1.Perform(make_tuple(false))); |
| 1013 | } |
| 1014 | |
| 1015 | // Tests that the body of ACTION() can reference the mock function's |
| 1016 | // return type. |
| 1017 | ACTION(InvokeDummy2) { |
| 1018 | StaticAssertTypeEq<int, return_type>(); |
| 1019 | return_type result = Dummy(true); |
| 1020 | return result; |
| 1021 | } |
| 1022 | |
| 1023 | TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) { |
| 1024 | Action<int(bool)> a1 = InvokeDummy2(); |
| 1025 | EXPECT_EQ(1, a1.Perform(make_tuple(true))); |
| 1026 | EXPECT_EQ(1, a1.Perform(make_tuple(false))); |
| 1027 | } |
| 1028 | |
| 1029 | // Tests that ACTION() can be used in a namespace. |
| 1030 | namespace action_test { |
| 1031 | ACTION(Sum) { return arg0 + arg1; } |
| 1032 | } // namespace action_test |
| 1033 | |
| 1034 | TEST(ActionMacroTest, WorksInNamespace) { |
| 1035 | Action<int(int, int)> a1 = action_test::Sum(); |
| 1036 | EXPECT_EQ(3, a1.Perform(make_tuple(1, 2))); |
| 1037 | } |
| 1038 | |
| 1039 | // Tests that the same ACTION definition works for mock functions with |
| 1040 | // different argument numbers. |
| 1041 | ACTION(PlusTwo) { return arg0 + 2; } |
| 1042 | |
| 1043 | TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) { |
| 1044 | Action<int(int)> a1 = PlusTwo(); |
| 1045 | EXPECT_EQ(4, a1.Perform(make_tuple(2))); |
| 1046 | |
| 1047 | Action<double(float, void*)> a2 = PlusTwo(); |
| 1048 | int dummy; |
| 1049 | EXPECT_DOUBLE_EQ(6, a2.Perform(make_tuple(4.0f, &dummy))); |
| 1050 | } |
| 1051 | |
| 1052 | // Tests that ACTION_P can define a parameterized action. |
| 1053 | ACTION_P(Plus, n) { return arg0 + n; } |
| 1054 | |
| 1055 | TEST(ActionPMacroTest, DefinesParameterizedAction) { |
| 1056 | Action<int(int m, bool t)> a1 = Plus(9); |
| 1057 | EXPECT_EQ(10, a1.Perform(make_tuple(1, true))); |
| 1058 | } |
| 1059 | |
| 1060 | // Tests that the body of ACTION_P can reference the argument types |
| 1061 | // and the parameter type. |
| 1062 | ACTION_P(TypedPlus, n) { |
| 1063 | arg0_type t1 = arg0; |
| 1064 | n_type t2 = n; |
| 1065 | return t1 + t2; |
| 1066 | } |
| 1067 | |
| 1068 | TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) { |
| 1069 | Action<int(char m, bool t)> a1 = TypedPlus(9); |
| 1070 | EXPECT_EQ(10, a1.Perform(make_tuple(static_cast<char>(1), true))); |
| 1071 | } |
| 1072 | |
| 1073 | // Tests that a parameterized action can be used in any mock function |
| 1074 | // whose type is compatible. |
| 1075 | TEST(ActionPMacroTest, WorksInCompatibleMockFunction) { |
| 1076 | Action<std::string(const std::string& s)> a1 = Plus("tail"); |
| 1077 | const std::string re = "re"; |
| 1078 | EXPECT_EQ("retail", a1.Perform(make_tuple(re))); |
| 1079 | } |
| 1080 | |
| 1081 | // Tests that we can use ACTION*() to define actions overloaded on the |
| 1082 | // number of parameters. |
| 1083 | |
| 1084 | ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; } |
| 1085 | |
| 1086 | ACTION_P(OverloadedAction, default_value) { |
| 1087 | return arg0 ? arg1 : default_value; |
| 1088 | } |
| 1089 | |
| 1090 | ACTION_P2(OverloadedAction, true_value, false_value) { |
| 1091 | return arg0 ? true_value : false_value; |
| 1092 | } |
| 1093 | |
| 1094 | TEST(ActionMacroTest, CanDefineOverloadedActions) { |
| 1095 | typedef Action<const char*(bool, const char*)> MyAction; |
| 1096 | |
| 1097 | const MyAction a1 = OverloadedAction(); |
| 1098 | EXPECT_STREQ("hello", a1.Perform(make_tuple(false, "world"))); |
| 1099 | EXPECT_STREQ("world", a1.Perform(make_tuple(true, "world"))); |
| 1100 | |
| 1101 | const MyAction a2 = OverloadedAction("hi"); |
| 1102 | EXPECT_STREQ("hi", a2.Perform(make_tuple(false, "world"))); |
| 1103 | EXPECT_STREQ("world", a2.Perform(make_tuple(true, "world"))); |
| 1104 | |
| 1105 | const MyAction a3 = OverloadedAction("hi", "you"); |
| 1106 | EXPECT_STREQ("hi", a3.Perform(make_tuple(true, "world"))); |
| 1107 | EXPECT_STREQ("you", a3.Perform(make_tuple(false, "world"))); |
| 1108 | } |
| 1109 | |
| 1110 | // Tests ACTION_Pn where n >= 3. |
| 1111 | |
| 1112 | ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; } |
| 1113 | |
| 1114 | TEST(ActionPnMacroTest, WorksFor3Parameters) { |
| 1115 | Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4); |
| 1116 | EXPECT_DOUBLE_EQ(3123.4, a1.Perform(make_tuple(3000, true))); |
| 1117 | |
| 1118 | Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">"); |
| 1119 | const std::string re = "re"; |
| 1120 | EXPECT_EQ("retail->", a2.Perform(make_tuple(re))); |
| 1121 | } |
| 1122 | |
| 1123 | ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; } |
| 1124 | |
| 1125 | TEST(ActionPnMacroTest, WorksFor4Parameters) { |
| 1126 | Action<int(int)> a1 = Plus(1, 2, 3, 4); |
| 1127 | EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(make_tuple(10))); |
| 1128 | } |
| 1129 | |
| 1130 | ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; } |
| 1131 | |
| 1132 | TEST(ActionPnMacroTest, WorksFor5Parameters) { |
| 1133 | Action<int(int)> a1 = Plus(1, 2, 3, 4, 5); |
| 1134 | EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(make_tuple(10))); |
| 1135 | } |
| 1136 | |
| 1137 | ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) { |
| 1138 | return arg0 + p0 + p1 + p2 + p3 + p4 + p5; |
| 1139 | } |
| 1140 | |
| 1141 | TEST(ActionPnMacroTest, WorksFor6Parameters) { |
| 1142 | Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6); |
| 1143 | EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(make_tuple(10))); |
| 1144 | } |
| 1145 | |
| 1146 | ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) { |
| 1147 | return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6; |
| 1148 | } |
| 1149 | |
| 1150 | TEST(ActionPnMacroTest, WorksFor7Parameters) { |
| 1151 | Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7); |
| 1152 | EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(make_tuple(10))); |
| 1153 | } |
| 1154 | |
| 1155 | ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) { |
| 1156 | return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7; |
| 1157 | } |
| 1158 | |
| 1159 | TEST(ActionPnMacroTest, WorksFor8Parameters) { |
| 1160 | Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8); |
| 1161 | EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, a1.Perform(make_tuple(10))); |
| 1162 | } |
| 1163 | |
| 1164 | ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) { |
| 1165 | return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8; |
| 1166 | } |
| 1167 | |
| 1168 | TEST(ActionPnMacroTest, WorksFor9Parameters) { |
| 1169 | Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 1170 | EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9, a1.Perform(make_tuple(10))); |
| 1171 | } |
| 1172 | |
| 1173 | ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) { |
| 1174 | arg0_type t0 = arg0; |
| 1175 | last_param_type t9 = last_param; |
| 1176 | return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9; |
| 1177 | } |
| 1178 | |
| 1179 | TEST(ActionPnMacroTest, WorksFor10Parameters) { |
| 1180 | Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 1181 | EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, |
| 1182 | a1.Perform(make_tuple(10))); |
| 1183 | } |
| 1184 | |
| 1185 | // Tests that the action body can promote the parameter types. |
| 1186 | |
| 1187 | ACTION_P2(PadArgument, prefix, suffix) { |
| 1188 | // The following lines promote the two parameters to desired types. |
| 1189 | std::string prefix_str(prefix); |
| 1190 | char suffix_char(suffix); |
| 1191 | return prefix_str + arg0 + suffix_char; |
| 1192 | } |
| 1193 | |
| 1194 | TEST(ActionPnMacroTest, SimpleTypePromotion) { |
| 1195 | Action<std::string(const char*)> no_promo = |
| 1196 | PadArgument(std::string("foo"), 'r'); |
| 1197 | Action<std::string(const char*)> promo = |
| 1198 | PadArgument("foo", static_cast<int>('r')); |
| 1199 | EXPECT_EQ("foobar", no_promo.Perform(make_tuple("ba"))); |
| 1200 | EXPECT_EQ("foobar", promo.Perform(make_tuple("ba"))); |
| 1201 | } |
| 1202 | |
| 1203 | // Tests that we can partially restrict parameter types using a |
| 1204 | // straight-forward pattern. |
| 1205 | |
| 1206 | // Defines a generic action that doesn't restrict the types of its |
| 1207 | // parameters. |
| 1208 | ACTION_P3(ConcatImpl, a, b, c) { |
| 1209 | std::stringstream ss; |
| 1210 | ss << a << b << c; |
| 1211 | return ss.str(); |
| 1212 | } |
| 1213 | |
| 1214 | // Next, we try to restrict that either the first parameter is a |
| 1215 | // string, or the second parameter is an int. |
| 1216 | |
| 1217 | // Defines a partially specialized wrapper that restricts the first |
| 1218 | // parameter to std::string. |
| 1219 | template <typename T1, typename T2> |
| 1220 | // ConcatImplActionP3 is the class template ACTION_P3 uses to |
| 1221 | // implement ConcatImpl. We shouldn't change the name as this |
| 1222 | // pattern requires the user to use it directly. |
| 1223 | ConcatImplActionP3<std::string, T1, T2> |
| 1224 | Concat(const std::string& a, T1 b, T2 c) { |
| 1225 | if (true) { |
| 1226 | // This branch verifies that ConcatImpl() can be invoked without |
| 1227 | // explicit template arguments. |
| 1228 | return ConcatImpl(a, b, c); |
| 1229 | } else { |
| 1230 | // This branch verifies that ConcatImpl() can also be invoked with |
| 1231 | // explicit template arguments. It doesn't really need to be |
| 1232 | // executed as this is a compile-time verification. |
| 1233 | return ConcatImpl<std::string, T1, T2>(a, b, c); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | // Defines another partially specialized wrapper that restricts the |
| 1238 | // second parameter to int. |
| 1239 | template <typename T1, typename T2> |
| 1240 | ConcatImplActionP3<T1, int, T2> |
| 1241 | Concat(T1 a, int b, T2 c) { |
| 1242 | return ConcatImpl(a, b, c); |
| 1243 | } |
| 1244 | |
| 1245 | TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) { |
| 1246 | Action<const std::string()> a1 = Concat("Hello", "1", 2); |
| 1247 | EXPECT_EQ("Hello12", a1.Perform(make_tuple())); |
| 1248 | |
| 1249 | a1 = Concat(1, 2, 3); |
| 1250 | EXPECT_EQ("123", a1.Perform(make_tuple())); |
| 1251 | } |
| 1252 | |
| 1253 | // Verifies the type of an ACTION*. |
| 1254 | |
| 1255 | ACTION(DoFoo) {} |
| 1256 | ACTION_P(DoFoo, p) {} |
| 1257 | ACTION_P2(DoFoo, p0, p1) {} |
| 1258 | |
| 1259 | TEST(ActionPnMacroTest, TypesAreCorrect) { |
| 1260 | // DoFoo() must be assignable to a DoFooAction variable. |
| 1261 | DoFooAction a0 = DoFoo(); |
| 1262 | |
| 1263 | // DoFoo(1) must be assignable to a DoFooActionP variable. |
| 1264 | DoFooActionP<int> a1 = DoFoo(1); |
| 1265 | |
| 1266 | // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk |
| 1267 | // variable, and so on. |
| 1268 | DoFooActionP2<int, char> a2 = DoFoo(1, '2'); |
| 1269 | PlusActionP3<int, int, char> a3 = Plus(1, 2, '3'); |
| 1270 | PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4'); |
| 1271 | PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5'); |
| 1272 | PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6'); |
| 1273 | PlusActionP7<int, int, int, int, int, int, char> a7 = |
| 1274 | Plus(1, 2, 3, 4, 5, 6, '7'); |
| 1275 | PlusActionP8<int, int, int, int, int, int, int, char> a8 = |
| 1276 | Plus(1, 2, 3, 4, 5, 6, 7, '8'); |
| 1277 | PlusActionP9<int, int, int, int, int, int, int, int, char> a9 = |
| 1278 | Plus(1, 2, 3, 4, 5, 6, 7, 8, '9'); |
| 1279 | PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 = |
| 1280 | Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); |
| 1281 | } |
| 1282 | |
zhanyong.wan | c069d7f | 2009-02-02 20:51:53 +0000 | [diff] [blame^] | 1283 | // Tests that an ACTION_P*() action can be explicitly instantiated |
| 1284 | // with reference-typed parameters. |
| 1285 | |
| 1286 | ACTION_P(Plus1, x) { return x; } |
| 1287 | ACTION_P2(Plus2, x, y) { return x + y; } |
| 1288 | ACTION_P3(Plus3, x, y, z) { return x + y + z; } |
| 1289 | ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) { |
| 1290 | return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9; |
| 1291 | } |
| 1292 | |
| 1293 | TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) { |
| 1294 | int x = 1, y = 2, z = 3; |
| 1295 | const tuple<> empty = make_tuple(); |
| 1296 | |
| 1297 | Action<int()> a = Plus1<int&>(x); |
| 1298 | EXPECT_EQ(1, a.Perform(empty)); |
| 1299 | |
| 1300 | a = Plus2<const int&, int&>(x, y); |
| 1301 | EXPECT_EQ(3, a.Perform(empty)); |
| 1302 | |
| 1303 | a = Plus3<int&, const int&, int&>(x, y, z); |
| 1304 | EXPECT_EQ(6, a.Perform(empty)); |
| 1305 | |
| 1306 | int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 1307 | a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&, |
| 1308 | int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], |
| 1309 | n[8], n[9]); |
| 1310 | EXPECT_EQ(55, a.Perform(empty)); |
| 1311 | } |
| 1312 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1313 | } // namespace gmock_generated_actions_test |
| 1314 | } // namespace testing |