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