zhanyong.wan | a18423e | 2009-07-22 23:58:19 +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 in gmock-more-actions.h. |
| 35 | |
| 36 | #include <gmock/gmock-more-actions.h> |
| 37 | |
| 38 | #include <functional> |
| 39 | #include <sstream> |
| 40 | #include <string> |
| 41 | #include <gmock/gmock.h> |
| 42 | #include <gtest/gtest.h> |
| 43 | |
| 44 | namespace testing { |
| 45 | namespace gmock_more_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::DeleteArg; |
| 57 | using testing::Invoke; |
| 58 | using testing::Return; |
| 59 | using testing::ReturnArg; |
| 60 | using testing::SaveArg; |
| 61 | using testing::SetArgReferee; |
| 62 | using testing::SetArgumentPointee; |
| 63 | using testing::StaticAssertTypeEq; |
| 64 | using testing::Unused; |
| 65 | using testing::WithArg; |
| 66 | using testing::WithoutArgs; |
| 67 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 68 | // For suppressing compiler warnings on conversion possibly losing precision. |
| 69 | inline short Short(short n) { return n; } // NOLINT |
| 70 | inline char Char(char ch) { return ch; } |
| 71 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 72 | // Sample functions and functors for testing Invoke() and etc. |
| 73 | int Nullary() { return 1; } |
| 74 | |
| 75 | class NullaryFunctor { |
| 76 | public: |
| 77 | int operator()() { return 2; } |
| 78 | }; |
| 79 | |
| 80 | bool g_done = false; |
| 81 | void VoidNullary() { g_done = true; } |
| 82 | |
| 83 | class VoidNullaryFunctor { |
| 84 | public: |
| 85 | void operator()() { g_done = true; } |
| 86 | }; |
| 87 | |
| 88 | bool Unary(int x) { return x < 0; } |
| 89 | |
| 90 | const char* Plus1(const char* s) { return s + 1; } |
| 91 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 92 | void VoidUnary(int /* n */) { g_done = true; } |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 93 | |
| 94 | bool ByConstRef(const string& s) { return s == "Hi"; } |
| 95 | |
| 96 | const double g_double = 0; |
| 97 | bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; } |
| 98 | |
| 99 | string ByNonConstRef(string& s) { return s += "+"; } // NOLINT |
| 100 | |
| 101 | struct UnaryFunctor { |
| 102 | int operator()(bool x) { return x ? 1 : -1; } |
| 103 | }; |
| 104 | |
| 105 | const char* Binary(const char* input, short n) { return input + n; } // NOLINT |
| 106 | |
| 107 | void VoidBinary(int, char) { g_done = true; } |
| 108 | |
| 109 | int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT |
| 110 | |
| 111 | void VoidTernary(int, char, bool) { g_done = true; } |
| 112 | |
| 113 | int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } |
| 114 | |
| 115 | int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; } |
| 116 | |
| 117 | void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } |
| 118 | |
| 119 | string Concat4(const char* s1, const char* s2, const char* s3, |
| 120 | const char* s4) { |
| 121 | return string(s1) + s2 + s3 + s4; |
| 122 | } |
| 123 | |
| 124 | int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } |
| 125 | |
| 126 | struct SumOf5Functor { |
| 127 | int operator()(int a, int b, int c, int d, int e) { |
| 128 | return a + b + c + d + e; |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | string Concat5(const char* s1, const char* s2, const char* s3, |
| 133 | const char* s4, const char* s5) { |
| 134 | return string(s1) + s2 + s3 + s4 + s5; |
| 135 | } |
| 136 | |
| 137 | int SumOf6(int a, int b, int c, int d, int e, int f) { |
| 138 | return a + b + c + d + e + f; |
| 139 | } |
| 140 | |
| 141 | struct SumOf6Functor { |
| 142 | int operator()(int a, int b, int c, int d, int e, int f) { |
| 143 | return a + b + c + d + e + f; |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | string Concat6(const char* s1, const char* s2, const char* s3, |
| 148 | const char* s4, const char* s5, const char* s6) { |
| 149 | return string(s1) + s2 + s3 + s4 + s5 + s6; |
| 150 | } |
| 151 | |
| 152 | string Concat7(const char* s1, const char* s2, const char* s3, |
| 153 | const char* s4, const char* s5, const char* s6, |
| 154 | const char* s7) { |
| 155 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; |
| 156 | } |
| 157 | |
| 158 | string Concat8(const char* s1, const char* s2, const char* s3, |
| 159 | const char* s4, const char* s5, const char* s6, |
| 160 | const char* s7, const char* s8) { |
| 161 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; |
| 162 | } |
| 163 | |
| 164 | string Concat9(const char* s1, const char* s2, const char* s3, |
| 165 | const char* s4, const char* s5, const char* s6, |
| 166 | const char* s7, const char* s8, const char* s9) { |
| 167 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; |
| 168 | } |
| 169 | |
| 170 | string Concat10(const char* s1, const char* s2, const char* s3, |
| 171 | const char* s4, const char* s5, const char* s6, |
| 172 | const char* s7, const char* s8, const char* s9, |
| 173 | const char* s10) { |
| 174 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; |
| 175 | } |
| 176 | |
| 177 | class Foo { |
| 178 | public: |
| 179 | Foo() : value_(123) {} |
| 180 | |
| 181 | int Nullary() const { return value_; } |
| 182 | |
| 183 | short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT |
| 184 | |
| 185 | string Binary(const string& str, char c) const { return str + c; } |
| 186 | |
| 187 | int Ternary(int x, bool y, char z) { return value_ + x + y*z; } |
| 188 | |
| 189 | int SumOf4(int a, int b, int c, int d) const { |
| 190 | return a + b + c + d + value_; |
| 191 | } |
| 192 | |
| 193 | int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; } |
| 194 | |
| 195 | int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } |
| 196 | |
| 197 | int SumOf6(int a, int b, int c, int d, int e, int f) { |
| 198 | return a + b + c + d + e + f; |
| 199 | } |
| 200 | |
| 201 | string Concat7(const char* s1, const char* s2, const char* s3, |
| 202 | const char* s4, const char* s5, const char* s6, |
| 203 | const char* s7) { |
| 204 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; |
| 205 | } |
| 206 | |
| 207 | string Concat8(const char* s1, const char* s2, const char* s3, |
| 208 | const char* s4, const char* s5, const char* s6, |
| 209 | const char* s7, const char* s8) { |
| 210 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; |
| 211 | } |
| 212 | |
| 213 | string Concat9(const char* s1, const char* s2, const char* s3, |
| 214 | const char* s4, const char* s5, const char* s6, |
| 215 | const char* s7, const char* s8, const char* s9) { |
| 216 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; |
| 217 | } |
| 218 | |
| 219 | string Concat10(const char* s1, const char* s2, const char* s3, |
| 220 | const char* s4, const char* s5, const char* s6, |
| 221 | const char* s7, const char* s8, const char* s9, |
| 222 | const char* s10) { |
| 223 | return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; |
| 224 | } |
| 225 | private: |
| 226 | int value_; |
| 227 | }; |
| 228 | |
| 229 | // Tests using Invoke() with a nullary function. |
| 230 | TEST(InvokeTest, Nullary) { |
| 231 | Action<int()> a = Invoke(Nullary); // NOLINT |
| 232 | EXPECT_EQ(1, a.Perform(make_tuple())); |
| 233 | } |
| 234 | |
| 235 | // Tests using Invoke() with a unary function. |
| 236 | TEST(InvokeTest, Unary) { |
| 237 | Action<bool(int)> a = Invoke(Unary); // NOLINT |
| 238 | EXPECT_FALSE(a.Perform(make_tuple(1))); |
| 239 | EXPECT_TRUE(a.Perform(make_tuple(-1))); |
| 240 | } |
| 241 | |
| 242 | // Tests using Invoke() with a binary function. |
| 243 | TEST(InvokeTest, Binary) { |
| 244 | Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT |
| 245 | const char* p = "Hello"; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 246 | EXPECT_EQ(p + 2, a.Perform(make_tuple(p, Short(2)))); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Tests using Invoke() with a ternary function. |
| 250 | TEST(InvokeTest, Ternary) { |
| 251 | Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 252 | EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', Short(3)))); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Tests using Invoke() with a 4-argument function. |
| 256 | TEST(InvokeTest, FunctionThatTakes4Arguments) { |
| 257 | Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT |
| 258 | EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4))); |
| 259 | } |
| 260 | |
| 261 | // Tests using Invoke() with a 5-argument function. |
| 262 | TEST(InvokeTest, FunctionThatTakes5Arguments) { |
| 263 | Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT |
| 264 | EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); |
| 265 | } |
| 266 | |
| 267 | // Tests using Invoke() with a 6-argument function. |
| 268 | TEST(InvokeTest, FunctionThatTakes6Arguments) { |
| 269 | Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT |
| 270 | EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); |
| 271 | } |
| 272 | |
| 273 | // A helper that turns the type of a C-string literal from const |
| 274 | // char[N] to const char*. |
| 275 | inline const char* CharPtr(const char* s) { return s; } |
| 276 | |
| 277 | // Tests using Invoke() with a 7-argument function. |
| 278 | TEST(InvokeTest, FunctionThatTakes7Arguments) { |
| 279 | Action<string(const char*, const char*, const char*, const char*, |
| 280 | const char*, const char*, const char*)> a = |
| 281 | Invoke(Concat7); |
| 282 | EXPECT_EQ("1234567", |
| 283 | a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 284 | CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 285 | CharPtr("7")))); |
| 286 | } |
| 287 | |
| 288 | // Tests using Invoke() with a 8-argument function. |
| 289 | TEST(InvokeTest, FunctionThatTakes8Arguments) { |
| 290 | Action<string(const char*, const char*, const char*, const char*, |
| 291 | const char*, const char*, const char*, const char*)> a = |
| 292 | Invoke(Concat8); |
| 293 | EXPECT_EQ("12345678", |
| 294 | a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 295 | CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 296 | CharPtr("7"), CharPtr("8")))); |
| 297 | } |
| 298 | |
| 299 | // Tests using Invoke() with a 9-argument function. |
| 300 | TEST(InvokeTest, FunctionThatTakes9Arguments) { |
| 301 | Action<string(const char*, const char*, const char*, const char*, |
| 302 | const char*, const char*, const char*, const char*, |
| 303 | const char*)> a = Invoke(Concat9); |
| 304 | EXPECT_EQ("123456789", |
| 305 | a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 306 | CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 307 | CharPtr("7"), CharPtr("8"), CharPtr("9")))); |
| 308 | } |
| 309 | |
| 310 | // Tests using Invoke() with a 10-argument function. |
| 311 | TEST(InvokeTest, FunctionThatTakes10Arguments) { |
| 312 | Action<string(const char*, const char*, const char*, const char*, |
| 313 | const char*, const char*, const char*, const char*, |
| 314 | const char*, const char*)> a = Invoke(Concat10); |
| 315 | EXPECT_EQ("1234567890", |
| 316 | a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 317 | CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 318 | CharPtr("7"), CharPtr("8"), CharPtr("9"), |
| 319 | CharPtr("0")))); |
| 320 | } |
| 321 | |
| 322 | // Tests using Invoke() with functions with parameters declared as Unused. |
| 323 | TEST(InvokeTest, FunctionWithUnusedParameters) { |
| 324 | Action<int(int, int, double, const string&)> a1 = |
| 325 | Invoke(SumOfFirst2); |
| 326 | EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, CharPtr("hi")))); |
| 327 | |
| 328 | Action<int(int, int, bool, int*)> a2 = |
| 329 | Invoke(SumOfFirst2); |
| 330 | EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL)))); |
| 331 | } |
| 332 | |
| 333 | // Tests using Invoke() with methods with parameters declared as Unused. |
| 334 | TEST(InvokeTest, MethodWithUnusedParameters) { |
| 335 | Foo foo; |
| 336 | Action<int(string, bool, int, int)> a1 = |
| 337 | Invoke(&foo, &Foo::SumOfLast2); |
| 338 | EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2))); |
| 339 | |
| 340 | Action<int(char, double, int, int)> a2 = |
| 341 | Invoke(&foo, &Foo::SumOfLast2); |
| 342 | EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3))); |
| 343 | } |
| 344 | |
| 345 | // Tests using Invoke() with a functor. |
| 346 | TEST(InvokeTest, Functor) { |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 347 | Action<long(long, int)> a = Invoke(plus<long>()); // NOLINT |
| 348 | EXPECT_EQ(3L, a.Perform(make_tuple(1, 2))); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // Tests using Invoke(f) as an action of a compatible type. |
| 352 | TEST(InvokeTest, FunctionWithCompatibleType) { |
| 353 | Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 354 | EXPECT_EQ(4321, a.Perform(make_tuple(4000, Short(300), Char(20), true))); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 355 | } |
| 356 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 357 | // Tests using Invoke() with an object pointer and a method pointer. |
| 358 | |
| 359 | // Tests using Invoke() with a nullary method. |
| 360 | TEST(InvokeMethodTest, Nullary) { |
| 361 | Foo foo; |
| 362 | Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT |
| 363 | EXPECT_EQ(123, a.Perform(make_tuple())); |
| 364 | } |
| 365 | |
| 366 | // Tests using Invoke() with a unary method. |
| 367 | TEST(InvokeMethodTest, Unary) { |
| 368 | Foo foo; |
| 369 | Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT |
| 370 | EXPECT_EQ(4123, a.Perform(make_tuple(4000))); |
| 371 | } |
| 372 | |
| 373 | // Tests using Invoke() with a binary method. |
| 374 | TEST(InvokeMethodTest, Binary) { |
| 375 | Foo foo; |
| 376 | Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary); |
| 377 | string s("Hell"); |
| 378 | EXPECT_EQ("Hello", a.Perform(make_tuple(s, 'o'))); |
| 379 | } |
| 380 | |
| 381 | // Tests using Invoke() with a ternary method. |
| 382 | TEST(InvokeMethodTest, Ternary) { |
| 383 | Foo foo; |
| 384 | Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 385 | EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, Char(1)))); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // Tests using Invoke() with a 4-argument method. |
| 389 | TEST(InvokeMethodTest, MethodThatTakes4Arguments) { |
| 390 | Foo foo; |
| 391 | Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT |
| 392 | EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4))); |
| 393 | } |
| 394 | |
| 395 | // Tests using Invoke() with a 5-argument method. |
| 396 | TEST(InvokeMethodTest, MethodThatTakes5Arguments) { |
| 397 | Foo foo; |
| 398 | Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT |
| 399 | EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); |
| 400 | } |
| 401 | |
| 402 | // Tests using Invoke() with a 6-argument method. |
| 403 | TEST(InvokeMethodTest, MethodThatTakes6Arguments) { |
| 404 | Foo foo; |
| 405 | Action<int(int, int, int, int, int, int)> a = // NOLINT |
| 406 | Invoke(&foo, &Foo::SumOf6); |
| 407 | EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); |
| 408 | } |
| 409 | |
| 410 | // Tests using Invoke() with a 7-argument method. |
| 411 | TEST(InvokeMethodTest, MethodThatTakes7Arguments) { |
| 412 | Foo foo; |
| 413 | Action<string(const char*, const char*, const char*, const char*, |
| 414 | const char*, const char*, const char*)> a = |
| 415 | Invoke(&foo, &Foo::Concat7); |
| 416 | EXPECT_EQ("1234567", |
| 417 | a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 418 | CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 419 | CharPtr("7")))); |
| 420 | } |
| 421 | |
| 422 | // Tests using Invoke() with a 8-argument method. |
| 423 | TEST(InvokeMethodTest, MethodThatTakes8Arguments) { |
| 424 | Foo foo; |
| 425 | Action<string(const char*, const char*, const char*, const char*, |
| 426 | const char*, const char*, const char*, const char*)> a = |
| 427 | Invoke(&foo, &Foo::Concat8); |
| 428 | EXPECT_EQ("12345678", |
| 429 | a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 430 | CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 431 | CharPtr("7"), CharPtr("8")))); |
| 432 | } |
| 433 | |
| 434 | // Tests using Invoke() with a 9-argument method. |
| 435 | TEST(InvokeMethodTest, MethodThatTakes9Arguments) { |
| 436 | Foo foo; |
| 437 | Action<string(const char*, const char*, const char*, const char*, |
| 438 | const char*, const char*, const char*, const char*, |
| 439 | const char*)> a = Invoke(&foo, &Foo::Concat9); |
| 440 | EXPECT_EQ("123456789", |
| 441 | a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 442 | CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 443 | CharPtr("7"), CharPtr("8"), CharPtr("9")))); |
| 444 | } |
| 445 | |
| 446 | // Tests using Invoke() with a 10-argument method. |
| 447 | TEST(InvokeMethodTest, MethodThatTakes10Arguments) { |
| 448 | Foo foo; |
| 449 | Action<string(const char*, const char*, const char*, const char*, |
| 450 | const char*, const char*, const char*, const char*, |
| 451 | const char*, const char*)> a = Invoke(&foo, &Foo::Concat10); |
| 452 | EXPECT_EQ("1234567890", |
| 453 | a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), |
| 454 | CharPtr("4"), CharPtr("5"), CharPtr("6"), |
| 455 | CharPtr("7"), CharPtr("8"), CharPtr("9"), |
| 456 | CharPtr("0")))); |
| 457 | } |
| 458 | |
| 459 | // Tests using Invoke(f) as an action of a compatible type. |
| 460 | TEST(InvokeMethodTest, MethodWithCompatibleType) { |
| 461 | Foo foo; |
| 462 | Action<long(int, short, char, bool)> a = // NOLINT |
| 463 | Invoke(&foo, &Foo::SumOf4); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 464 | EXPECT_EQ(4444, a.Perform(make_tuple(4000, Short(300), Char(20), true))); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 465 | } |
| 466 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 467 | // Tests using WithoutArgs with an action that takes no argument. |
| 468 | TEST(WithoutArgsTest, NoArg) { |
| 469 | Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT |
| 470 | EXPECT_EQ(1, a.Perform(make_tuple(2))); |
| 471 | } |
| 472 | |
| 473 | // Tests using WithArg with an action that takes 1 argument. |
| 474 | TEST(WithArgTest, OneArg) { |
| 475 | Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT |
| 476 | EXPECT_TRUE(b.Perform(make_tuple(1.5, -1))); |
| 477 | EXPECT_FALSE(b.Perform(make_tuple(1.5, 1))); |
| 478 | } |
| 479 | |
| 480 | TEST(ReturnArgActionTest, WorksForOneArgIntArg0) { |
| 481 | const Action<int(int)> a = ReturnArg<0>(); |
| 482 | EXPECT_EQ(5, a.Perform(make_tuple(5))); |
| 483 | } |
| 484 | |
| 485 | TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) { |
| 486 | const Action<bool(bool, bool, bool)> a = ReturnArg<0>(); |
| 487 | EXPECT_TRUE(a.Perform(make_tuple(true, false, false))); |
| 488 | } |
| 489 | |
| 490 | TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) { |
| 491 | const Action<string(int, int, string, int)> a = ReturnArg<2>(); |
| 492 | EXPECT_EQ("seven", a.Perform(make_tuple(5, 6, string("seven"), 8))); |
| 493 | } |
| 494 | |
| 495 | TEST(SaveArgActionTest, WorksForSameType) { |
| 496 | int result = 0; |
| 497 | const Action<void(int n)> a1 = SaveArg<0>(&result); |
| 498 | a1.Perform(make_tuple(5)); |
| 499 | EXPECT_EQ(5, result); |
| 500 | } |
| 501 | |
| 502 | TEST(SaveArgActionTest, WorksForCompatibleType) { |
| 503 | int result = 0; |
| 504 | const Action<void(bool, char)> a1 = SaveArg<1>(&result); |
| 505 | a1.Perform(make_tuple(true, 'a')); |
| 506 | EXPECT_EQ('a', result); |
| 507 | } |
| 508 | |
| 509 | TEST(SetArgRefereeActionTest, WorksForSameType) { |
| 510 | int value = 0; |
| 511 | const Action<void(int&)> a1 = SetArgReferee<0>(1); |
| 512 | a1.Perform(tuple<int&>(value)); |
| 513 | EXPECT_EQ(1, value); |
| 514 | } |
| 515 | |
| 516 | TEST(SetArgRefereeActionTest, WorksForCompatibleType) { |
| 517 | int value = 0; |
| 518 | const Action<void(int, int&)> a1 = SetArgReferee<1>('a'); |
| 519 | a1.Perform(tuple<int, int&>(0, value)); |
| 520 | EXPECT_EQ('a', value); |
| 521 | } |
| 522 | |
| 523 | TEST(SetArgRefereeActionTest, WorksWithExtraArguments) { |
| 524 | int value = 0; |
| 525 | const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a'); |
| 526 | a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi")); |
| 527 | EXPECT_EQ('a', value); |
| 528 | } |
| 529 | |
| 530 | // A class that can be used to verify that its destructor is called: it will set |
| 531 | // the bool provided to the constructor to true when destroyed. |
| 532 | class DeletionTester { |
| 533 | public: |
| 534 | explicit DeletionTester(bool* is_deleted) |
| 535 | : is_deleted_(is_deleted) { |
| 536 | // Make sure the bit is set to false. |
| 537 | *is_deleted_ = false; |
| 538 | } |
| 539 | |
| 540 | ~DeletionTester() { |
| 541 | *is_deleted_ = true; |
| 542 | } |
| 543 | |
| 544 | private: |
| 545 | bool* is_deleted_; |
| 546 | }; |
| 547 | |
| 548 | TEST(DeleteArgActionTest, OneArg) { |
| 549 | bool is_deleted = false; |
| 550 | DeletionTester* t = new DeletionTester(&is_deleted); |
| 551 | const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT |
| 552 | EXPECT_FALSE(is_deleted); |
| 553 | a1.Perform(make_tuple(t)); |
| 554 | EXPECT_TRUE(is_deleted); |
| 555 | } |
| 556 | |
| 557 | TEST(DeleteArgActionTest, TenArgs) { |
| 558 | bool is_deleted = false; |
| 559 | DeletionTester* t = new DeletionTester(&is_deleted); |
| 560 | const Action<void(bool, int, int, const char*, bool, |
| 561 | int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>(); |
| 562 | EXPECT_FALSE(is_deleted); |
| 563 | a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t)); |
| 564 | EXPECT_TRUE(is_deleted); |
| 565 | } |
| 566 | |
| 567 | #if GTEST_HAS_EXCEPTIONS |
| 568 | |
| 569 | TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) { |
| 570 | const Action<void(int n)> a = Throw('a'); |
| 571 | EXPECT_THROW(a.Perform(make_tuple(0)), char); |
| 572 | } |
| 573 | |
| 574 | class MyException {}; |
| 575 | |
| 576 | TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) { |
| 577 | const Action<double(char ch)> a = Throw(MyException()); |
| 578 | EXPECT_THROW(a.Perform(make_tuple('0')), MyException); |
| 579 | } |
| 580 | |
| 581 | TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) { |
| 582 | const Action<double()> a = Throw(MyException()); |
| 583 | EXPECT_THROW(a.Perform(make_tuple()), MyException); |
| 584 | } |
| 585 | |
| 586 | #endif // GTEST_HAS_EXCEPTIONS |
| 587 | |
| 588 | // Tests that SetArrayArgument<N>(first, last) sets the elements of the array |
| 589 | // pointed to by the N-th (0-based) argument to values in range [first, last). |
| 590 | TEST(SetArrayArgumentTest, SetsTheNthArray) { |
| 591 | typedef void MyFunction(bool, int*, char*); |
| 592 | int numbers[] = { 1, 2, 3 }; |
| 593 | Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3); |
| 594 | |
| 595 | int n[4] = {}; |
| 596 | int* pn = n; |
| 597 | char ch[4] = {}; |
| 598 | char* pch = ch; |
| 599 | a.Perform(make_tuple(true, pn, pch)); |
| 600 | EXPECT_EQ(1, n[0]); |
| 601 | EXPECT_EQ(2, n[1]); |
| 602 | EXPECT_EQ(3, n[2]); |
| 603 | EXPECT_EQ(0, n[3]); |
| 604 | EXPECT_EQ('\0', ch[0]); |
| 605 | EXPECT_EQ('\0', ch[1]); |
| 606 | EXPECT_EQ('\0', ch[2]); |
| 607 | EXPECT_EQ('\0', ch[3]); |
| 608 | |
| 609 | // Tests first and last are iterators. |
| 610 | std::string letters = "abc"; |
| 611 | a = SetArrayArgument<2>(letters.begin(), letters.end()); |
| 612 | std::fill_n(n, 4, 0); |
| 613 | std::fill_n(ch, 4, '\0'); |
| 614 | a.Perform(make_tuple(true, pn, pch)); |
| 615 | EXPECT_EQ(0, n[0]); |
| 616 | EXPECT_EQ(0, n[1]); |
| 617 | EXPECT_EQ(0, n[2]); |
| 618 | EXPECT_EQ(0, n[3]); |
| 619 | EXPECT_EQ('a', ch[0]); |
| 620 | EXPECT_EQ('b', ch[1]); |
| 621 | EXPECT_EQ('c', ch[2]); |
| 622 | EXPECT_EQ('\0', ch[3]); |
| 623 | } |
| 624 | |
| 625 | // Tests SetArrayArgument<N>(first, last) where first == last. |
| 626 | TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) { |
| 627 | typedef void MyFunction(bool, int*); |
| 628 | int numbers[] = { 1, 2, 3 }; |
| 629 | Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers); |
| 630 | |
| 631 | int n[4] = {}; |
| 632 | int* pn = n; |
| 633 | a.Perform(make_tuple(true, pn)); |
| 634 | EXPECT_EQ(0, n[0]); |
| 635 | EXPECT_EQ(0, n[1]); |
| 636 | EXPECT_EQ(0, n[2]); |
| 637 | EXPECT_EQ(0, n[3]); |
| 638 | } |
| 639 | |
| 640 | // Tests SetArrayArgument<N>(first, last) where *first is convertible |
| 641 | // (but not equal) to the argument type. |
| 642 | TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) { |
| 643 | typedef void MyFunction(bool, char*); |
| 644 | int codes[] = { 97, 98, 99 }; |
| 645 | Action<MyFunction> a = SetArrayArgument<1>(codes, codes + 3); |
| 646 | |
| 647 | char ch[4] = {}; |
| 648 | char* pch = ch; |
| 649 | a.Perform(make_tuple(true, pch)); |
| 650 | EXPECT_EQ('a', ch[0]); |
| 651 | EXPECT_EQ('b', ch[1]); |
| 652 | EXPECT_EQ('c', ch[2]); |
| 653 | EXPECT_EQ('\0', ch[3]); |
| 654 | } |
| 655 | |
| 656 | // Test SetArrayArgument<N>(first, last) with iterator as argument. |
| 657 | TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) { |
| 658 | typedef void MyFunction(bool, std::back_insert_iterator<std::string>); |
| 659 | std::string letters = "abc"; |
| 660 | Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end()); |
| 661 | |
| 662 | std::string s; |
| 663 | a.Perform(make_tuple(true, back_inserter(s))); |
| 664 | EXPECT_EQ(letters, s); |
| 665 | } |
| 666 | |
| 667 | } // namespace gmock_generated_actions_test |
| 668 | } // namespace testing |