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