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