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