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