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. |
| 35 | |
| 36 | #include <gmock/gmock-actions.h> |
| 37 | #include <algorithm> |
| 38 | #include <iterator> |
| 39 | #include <string> |
| 40 | #include <gmock/gmock.h> |
| 41 | #include <gmock/internal/gmock-port.h> |
| 42 | #include <gtest/gtest.h> |
| 43 | #include <gtest/gtest-spi.h> |
| 44 | |
| 45 | namespace { |
| 46 | |
| 47 | using ::std::tr1::get; |
| 48 | using ::std::tr1::make_tuple; |
| 49 | using ::std::tr1::tuple; |
| 50 | using ::std::tr1::tuple_element; |
| 51 | using testing::internal::BuiltInDefaultValue; |
| 52 | using testing::internal::Int64; |
| 53 | using testing::internal::UInt64; |
| 54 | // This list should be kept sorted. |
| 55 | using testing::_; |
| 56 | using testing::Action; |
| 57 | using testing::ActionInterface; |
| 58 | using testing::Assign; |
| 59 | using testing::DefaultValue; |
| 60 | using testing::DoDefault; |
| 61 | using testing::IgnoreResult; |
| 62 | using testing::Invoke; |
| 63 | using testing::InvokeWithoutArgs; |
| 64 | using testing::MakePolymorphicAction; |
| 65 | using testing::Ne; |
| 66 | using testing::PolymorphicAction; |
| 67 | using testing::Return; |
| 68 | using testing::ReturnNull; |
| 69 | using testing::ReturnRef; |
| 70 | using testing::SetArgumentPointee; |
| 71 | using testing::SetArrayArgument; |
| 72 | using testing::SetErrnoAndReturn; |
| 73 | |
| 74 | #if GMOCK_HAS_PROTOBUF_ |
| 75 | using testing::internal::TestMessage; |
| 76 | #endif // GMOCK_HAS_PROTOBUF_ |
| 77 | |
| 78 | // Tests that BuiltInDefaultValue<T*>::Get() returns NULL. |
| 79 | TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) { |
| 80 | EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL); |
| 81 | EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL); |
| 82 | EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL); |
| 83 | } |
| 84 | |
| 85 | // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a |
| 86 | // built-in numeric type. |
| 87 | TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) { |
| 88 | EXPECT_EQ(0, BuiltInDefaultValue<unsigned char>::Get()); |
| 89 | EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get()); |
| 90 | EXPECT_EQ(0, BuiltInDefaultValue<char>::Get()); |
| 91 | #ifndef GTEST_OS_WINDOWS |
| 92 | EXPECT_EQ(0, BuiltInDefaultValue<unsigned wchar_t>::Get()); |
| 93 | EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get()); |
| 94 | #endif // GTEST_OS_WINDOWS |
| 95 | EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get()); |
| 96 | EXPECT_EQ(0, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT |
| 97 | EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT |
| 98 | EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT |
| 99 | EXPECT_EQ(0, BuiltInDefaultValue<unsigned int>::Get()); |
| 100 | EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get()); |
| 101 | EXPECT_EQ(0, BuiltInDefaultValue<int>::Get()); |
| 102 | EXPECT_EQ(0, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT |
| 103 | EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT |
| 104 | EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT |
| 105 | EXPECT_EQ(0, BuiltInDefaultValue<UInt64>::Get()); |
| 106 | EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get()); |
| 107 | EXPECT_EQ(0, BuiltInDefaultValue<float>::Get()); |
| 108 | EXPECT_EQ(0, BuiltInDefaultValue<double>::Get()); |
| 109 | } |
| 110 | |
| 111 | // Tests that BuiltInDefaultValue<bool>::Get() returns false. |
| 112 | TEST(BuiltInDefaultValueTest, IsFalseForBool) { |
| 113 | EXPECT_FALSE(BuiltInDefaultValue<bool>::Get()); |
| 114 | } |
| 115 | |
| 116 | // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a |
| 117 | // string type. |
| 118 | TEST(BuiltInDefaultValueTest, IsEmptyStringForString) { |
| 119 | #if GTEST_HAS_GLOBAL_STRING |
| 120 | EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get()); |
| 121 | #endif // GTEST_HAS_GLOBAL_STRING |
| 122 | |
| 123 | #if GTEST_HAS_STD_STRING |
| 124 | EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get()); |
| 125 | #endif // GTEST_HAS_STD_STRING |
| 126 | } |
| 127 | |
| 128 | // Tests that BuiltInDefaultValue<const T>::Get() returns the same |
| 129 | // value as BuiltInDefaultValue<T>::Get() does. |
| 130 | TEST(BuiltInDefaultValueTest, WorksForConstTypes) { |
| 131 | EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get()); |
| 132 | EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get()); |
| 133 | EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL); |
| 134 | EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get()); |
| 135 | } |
| 136 | |
| 137 | // Tests that BuiltInDefaultValue<T>::Get() aborts the program with |
| 138 | // the correct error message when T is a user-defined type. |
| 139 | struct UserType { |
| 140 | UserType() : value(0) {} |
| 141 | |
| 142 | int value; |
| 143 | }; |
| 144 | |
| 145 | #ifdef GTEST_HAS_DEATH_TEST |
| 146 | |
| 147 | // Tests that BuiltInDefaultValue<T&>::Get() aborts the program. |
| 148 | TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) { |
| 149 | EXPECT_DEATH({ // NOLINT |
| 150 | BuiltInDefaultValue<int&>::Get(); |
| 151 | }, ""); |
| 152 | EXPECT_DEATH({ // NOLINT |
| 153 | BuiltInDefaultValue<const char&>::Get(); |
| 154 | }, ""); |
| 155 | } |
| 156 | |
| 157 | TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) { |
| 158 | EXPECT_DEATH({ // NOLINT |
| 159 | BuiltInDefaultValue<UserType>::Get(); |
| 160 | }, ""); |
| 161 | } |
| 162 | |
| 163 | #endif // GTEST_HAS_DEATH_TEST |
| 164 | |
| 165 | // Tests that DefaultValue<T>::IsSet() is false initially. |
| 166 | TEST(DefaultValueTest, IsInitiallyUnset) { |
| 167 | EXPECT_FALSE(DefaultValue<int>::IsSet()); |
| 168 | EXPECT_FALSE(DefaultValue<const UserType>::IsSet()); |
| 169 | } |
| 170 | |
| 171 | // Tests that DefaultValue<T> can be set and then unset. |
| 172 | TEST(DefaultValueTest, CanBeSetAndUnset) { |
| 173 | DefaultValue<int>::Set(1); |
| 174 | DefaultValue<const UserType>::Set(UserType()); |
| 175 | |
| 176 | EXPECT_EQ(1, DefaultValue<int>::Get()); |
| 177 | EXPECT_EQ(0, DefaultValue<const UserType>::Get().value); |
| 178 | |
| 179 | DefaultValue<int>::Clear(); |
| 180 | DefaultValue<const UserType>::Clear(); |
| 181 | |
| 182 | EXPECT_FALSE(DefaultValue<int>::IsSet()); |
| 183 | EXPECT_FALSE(DefaultValue<const UserType>::IsSet()); |
| 184 | } |
| 185 | |
| 186 | // Tests that DefaultValue<T>::Get() returns the |
| 187 | // BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is |
| 188 | // false. |
| 189 | TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { |
| 190 | EXPECT_FALSE(DefaultValue<int>::IsSet()); |
| 191 | EXPECT_FALSE(DefaultValue<UserType>::IsSet()); |
| 192 | |
| 193 | EXPECT_EQ(0, DefaultValue<int>::Get()); |
| 194 | |
| 195 | #ifdef GTEST_HAS_DEATH_TEST |
| 196 | EXPECT_DEATH({ // NOLINT |
| 197 | DefaultValue<UserType>::Get(); |
| 198 | }, ""); |
| 199 | #endif // GTEST_HAS_DEATH_TEST |
| 200 | } |
| 201 | |
| 202 | // Tests that DefaultValue<void>::Get() returns void. |
| 203 | TEST(DefaultValueTest, GetWorksForVoid) { |
| 204 | return DefaultValue<void>::Get(); |
| 205 | } |
| 206 | |
| 207 | // Tests using DefaultValue with a reference type. |
| 208 | |
| 209 | // Tests that DefaultValue<T&>::IsSet() is false initially. |
| 210 | TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) { |
| 211 | EXPECT_FALSE(DefaultValue<int&>::IsSet()); |
| 212 | EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); |
| 213 | } |
| 214 | |
| 215 | // Tests that DefaultValue<T&> can be set and then unset. |
| 216 | TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) { |
| 217 | int n = 1; |
| 218 | DefaultValue<const int&>::Set(n); |
| 219 | UserType u; |
| 220 | DefaultValue<UserType&>::Set(u); |
| 221 | |
| 222 | EXPECT_EQ(&n, &(DefaultValue<const int&>::Get())); |
| 223 | EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get())); |
| 224 | |
| 225 | DefaultValue<const int&>::Clear(); |
| 226 | DefaultValue<UserType&>::Clear(); |
| 227 | |
| 228 | EXPECT_FALSE(DefaultValue<const int&>::IsSet()); |
| 229 | EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); |
| 230 | } |
| 231 | |
| 232 | // Tests that DefaultValue<T&>::Get() returns the |
| 233 | // BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is |
| 234 | // false. |
| 235 | TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { |
| 236 | EXPECT_FALSE(DefaultValue<int&>::IsSet()); |
| 237 | EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); |
| 238 | |
| 239 | #ifdef GTEST_HAS_DEATH_TEST |
| 240 | EXPECT_DEATH({ // NOLINT |
| 241 | DefaultValue<int&>::Get(); |
| 242 | }, ""); |
| 243 | EXPECT_DEATH({ // NOLINT |
| 244 | DefaultValue<UserType>::Get(); |
| 245 | }, ""); |
| 246 | #endif // GTEST_HAS_DEATH_TEST |
| 247 | } |
| 248 | |
| 249 | // Tests that ActionInterface can be implemented by defining the |
| 250 | // Perform method. |
| 251 | |
| 252 | typedef int MyFunction(bool, int); |
| 253 | |
| 254 | class MyActionImpl : public ActionInterface<MyFunction> { |
| 255 | public: |
| 256 | virtual int Perform(const tuple<bool, int>& args) { |
| 257 | return get<0>(args) ? get<1>(args) : 0; |
| 258 | } |
| 259 | }; |
| 260 | |
| 261 | TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) { |
| 262 | MyActionImpl my_action_impl; |
| 263 | |
| 264 | EXPECT_FALSE(my_action_impl.IsDoDefault()); |
| 265 | } |
| 266 | |
| 267 | TEST(ActionInterfaceTest, MakeAction) { |
| 268 | Action<MyFunction> action = MakeAction(new MyActionImpl); |
| 269 | |
| 270 | // When exercising the Perform() method of Action<F>, we must pass |
| 271 | // it a tuple whose size and type are compatible with F's argument |
| 272 | // types. For example, if F is int(), then Perform() takes a |
| 273 | // 0-tuple; if F is void(bool, int), then Perform() takes a |
| 274 | // tuple<bool, int>, and so on. |
| 275 | EXPECT_EQ(5, action.Perform(make_tuple(true, 5))); |
| 276 | } |
| 277 | |
| 278 | // Tests that Action<F> can be contructed from a pointer to |
| 279 | // ActionInterface<F>. |
| 280 | TEST(ActionTest, CanBeConstructedFromActionInterface) { |
| 281 | Action<MyFunction> action(new MyActionImpl); |
| 282 | } |
| 283 | |
| 284 | // Tests that Action<F> delegates actual work to ActionInterface<F>. |
| 285 | TEST(ActionTest, DelegatesWorkToActionInterface) { |
| 286 | const Action<MyFunction> action(new MyActionImpl); |
| 287 | |
| 288 | EXPECT_EQ(5, action.Perform(make_tuple(true, 5))); |
| 289 | EXPECT_EQ(0, action.Perform(make_tuple(false, 1))); |
| 290 | } |
| 291 | |
| 292 | // Tests that Action<F> can be copied. |
| 293 | TEST(ActionTest, IsCopyable) { |
| 294 | Action<MyFunction> a1(new MyActionImpl); |
| 295 | Action<MyFunction> a2(a1); // Tests the copy constructor. |
| 296 | |
| 297 | // a1 should continue to work after being copied from. |
| 298 | EXPECT_EQ(5, a1.Perform(make_tuple(true, 5))); |
| 299 | EXPECT_EQ(0, a1.Perform(make_tuple(false, 1))); |
| 300 | |
| 301 | // a2 should work like the action it was copied from. |
| 302 | EXPECT_EQ(5, a2.Perform(make_tuple(true, 5))); |
| 303 | EXPECT_EQ(0, a2.Perform(make_tuple(false, 1))); |
| 304 | |
| 305 | a2 = a1; // Tests the assignment operator. |
| 306 | |
| 307 | // a1 should continue to work after being copied from. |
| 308 | EXPECT_EQ(5, a1.Perform(make_tuple(true, 5))); |
| 309 | EXPECT_EQ(0, a1.Perform(make_tuple(false, 1))); |
| 310 | |
| 311 | // a2 should work like the action it was copied from. |
| 312 | EXPECT_EQ(5, a2.Perform(make_tuple(true, 5))); |
| 313 | EXPECT_EQ(0, a2.Perform(make_tuple(false, 1))); |
| 314 | } |
| 315 | |
| 316 | // Tests that an Action<From> object can be converted to a |
| 317 | // compatible Action<To> object. |
| 318 | |
| 319 | class IsNotZero : public ActionInterface<bool(int)> { // NOLINT |
| 320 | public: |
| 321 | virtual bool Perform(const tuple<int>& arg) { |
| 322 | return get<0>(arg) != 0; |
| 323 | } |
| 324 | }; |
| 325 | |
| 326 | TEST(ActionTest, CanBeConvertedToOtherActionType) { |
| 327 | const Action<bool(int)> a1(new IsNotZero); // NOLINT |
| 328 | const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT |
| 329 | EXPECT_EQ(1, a2.Perform(make_tuple('a'))); |
| 330 | EXPECT_EQ(0, a2.Perform(make_tuple('\0'))); |
| 331 | } |
| 332 | |
| 333 | // The following two classes are for testing MakePolymorphicAction(). |
| 334 | |
| 335 | // Implements a polymorphic action that returns the second of the |
| 336 | // arguments it receives. |
| 337 | class ReturnSecondArgumentAction { |
| 338 | public: |
| 339 | // We want to verify that MakePolymorphicAction() can work with a |
| 340 | // polymorphic action whose Perform() method template is either |
| 341 | // const or not. This lets us verify the non-const case. |
| 342 | template <typename Result, typename ArgumentTuple> |
| 343 | Result Perform(const ArgumentTuple& args) { return get<1>(args); } |
| 344 | }; |
| 345 | |
| 346 | // Implements a polymorphic action that can be used in a nullary |
| 347 | // function to return 0. |
| 348 | class ReturnZeroFromNullaryFunctionAction { |
| 349 | public: |
| 350 | // For testing that MakePolymorphicAction() works when the |
| 351 | // implementation class' Perform() method template takes only one |
| 352 | // template parameter. |
| 353 | // |
| 354 | // We want to verify that MakePolymorphicAction() can work with a |
| 355 | // polymorphic action whose Perform() method template is either |
| 356 | // const or not. This lets us verify the const case. |
| 357 | template <typename Result> |
| 358 | Result Perform(const tuple<>&) const { return 0; } |
| 359 | }; |
| 360 | |
| 361 | // These functions verify that MakePolymorphicAction() returns a |
| 362 | // PolymorphicAction<T> where T is the argument's type. |
| 363 | |
| 364 | PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() { |
| 365 | return MakePolymorphicAction(ReturnSecondArgumentAction()); |
| 366 | } |
| 367 | |
| 368 | PolymorphicAction<ReturnZeroFromNullaryFunctionAction> |
| 369 | ReturnZeroFromNullaryFunction() { |
| 370 | return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction()); |
| 371 | } |
| 372 | |
| 373 | // Tests that MakePolymorphicAction() turns a polymorphic action |
| 374 | // implementation class into a polymorphic action. |
| 375 | TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) { |
| 376 | Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT |
| 377 | EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0))); |
| 378 | } |
| 379 | |
| 380 | // Tests that MakePolymorphicAction() works when the implementation |
| 381 | // class' Perform() method template has only one template parameter. |
| 382 | TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) { |
| 383 | Action<int()> a1 = ReturnZeroFromNullaryFunction(); |
| 384 | EXPECT_EQ(0, a1.Perform(make_tuple())); |
| 385 | |
| 386 | Action<void*()> a2 = ReturnZeroFromNullaryFunction(); |
| 387 | EXPECT_TRUE(a2.Perform(make_tuple()) == NULL); |
| 388 | } |
| 389 | |
| 390 | // Tests that Return() works as an action for void-returning |
| 391 | // functions. |
| 392 | TEST(ReturnTest, WorksForVoid) { |
| 393 | const Action<void(int)> ret = Return(); // NOLINT |
| 394 | return ret.Perform(make_tuple(1)); |
| 395 | } |
| 396 | |
| 397 | // Tests that Return(v) returns v. |
| 398 | TEST(ReturnTest, ReturnsGivenValue) { |
| 399 | Action<int()> ret = Return(1); // NOLINT |
| 400 | EXPECT_EQ(1, ret.Perform(make_tuple())); |
| 401 | |
| 402 | ret = Return(-5); |
| 403 | EXPECT_EQ(-5, ret.Perform(make_tuple())); |
| 404 | } |
| 405 | |
| 406 | // Tests that Return("string literal") works. |
| 407 | TEST(ReturnTest, AcceptsStringLiteral) { |
| 408 | Action<const char*()> a1 = Return("Hello"); |
| 409 | EXPECT_STREQ("Hello", a1.Perform(make_tuple())); |
| 410 | |
| 411 | Action<std::string()> a2 = Return("world"); |
| 412 | EXPECT_EQ("world", a2.Perform(make_tuple())); |
| 413 | } |
| 414 | |
| 415 | // Tests that Return(v) is covaraint. |
| 416 | |
| 417 | struct Base { |
| 418 | bool operator==(const Base&) { return true; } |
| 419 | }; |
| 420 | |
| 421 | struct Derived : public Base { |
| 422 | bool operator==(const Derived&) { return true; } |
| 423 | }; |
| 424 | |
| 425 | TEST(ReturnTest, IsCovariant) { |
| 426 | Base base; |
| 427 | Derived derived; |
| 428 | Action<Base*()> ret = Return(&base); |
| 429 | EXPECT_EQ(&base, ret.Perform(make_tuple())); |
| 430 | |
| 431 | ret = Return(&derived); |
| 432 | EXPECT_EQ(&derived, ret.Perform(make_tuple())); |
| 433 | } |
| 434 | |
| 435 | // Tests that ReturnNull() returns NULL in a pointer-returning function. |
| 436 | TEST(ReturnNullTest, WorksInPointerReturningFunction) { |
| 437 | const Action<int*()> a1 = ReturnNull(); |
| 438 | EXPECT_TRUE(a1.Perform(make_tuple()) == NULL); |
| 439 | |
| 440 | const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT |
| 441 | EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL); |
| 442 | } |
| 443 | |
| 444 | // Tests that ReturnRef(v) works for reference types. |
| 445 | TEST(ReturnRefTest, WorksForReference) { |
| 446 | const int n = 0; |
| 447 | const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT |
| 448 | |
| 449 | EXPECT_EQ(&n, &ret.Perform(make_tuple(true))); |
| 450 | } |
| 451 | |
| 452 | // Tests that ReturnRef(v) is covariant. |
| 453 | TEST(ReturnRefTest, IsCovariant) { |
| 454 | Base base; |
| 455 | Derived derived; |
| 456 | Action<Base&()> a = ReturnRef(base); |
| 457 | EXPECT_EQ(&base, &a.Perform(make_tuple())); |
| 458 | |
| 459 | a = ReturnRef(derived); |
| 460 | EXPECT_EQ(&derived, &a.Perform(make_tuple())); |
| 461 | } |
| 462 | |
| 463 | // Tests that DoDefault() does the default action for the mock method. |
| 464 | |
| 465 | class MyClass {}; |
| 466 | |
| 467 | class MockClass { |
| 468 | public: |
| 469 | MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT |
| 470 | MOCK_METHOD0(Foo, MyClass()); |
| 471 | }; |
| 472 | |
| 473 | // Tests that DoDefault() returns the built-in default value for the |
| 474 | // return type by default. |
| 475 | TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) { |
| 476 | MockClass mock; |
| 477 | EXPECT_CALL(mock, IntFunc(_)) |
| 478 | .WillOnce(DoDefault()); |
| 479 | EXPECT_EQ(0, mock.IntFunc(true)); |
| 480 | } |
| 481 | |
| 482 | #ifdef GTEST_HAS_DEATH_TEST |
| 483 | |
| 484 | // Tests that DoDefault() aborts the process when there is no built-in |
| 485 | // default value for the return type. |
| 486 | TEST(DoDefaultDeathTest, DiesForUnknowType) { |
| 487 | MockClass mock; |
| 488 | EXPECT_CALL(mock, Foo()) |
| 489 | .WillRepeatedly(DoDefault()); |
| 490 | EXPECT_DEATH({ // NOLINT |
| 491 | mock.Foo(); |
| 492 | }, ""); |
| 493 | } |
| 494 | |
| 495 | // Tests that using DoDefault() inside a composite action leads to a |
| 496 | // run-time error. |
| 497 | |
| 498 | void VoidFunc(bool flag) {} |
| 499 | |
| 500 | TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) { |
| 501 | MockClass mock; |
| 502 | EXPECT_CALL(mock, IntFunc(_)) |
| 503 | .WillRepeatedly(DoAll(Invoke(VoidFunc), |
| 504 | DoDefault())); |
| 505 | |
| 506 | // Ideally we should verify the error message as well. Sadly, |
| 507 | // EXPECT_DEATH() can only capture stderr, while Google Mock's |
| 508 | // errors are printed on stdout. Therefore we have to settle for |
| 509 | // not verifying the message. |
| 510 | EXPECT_DEATH({ // NOLINT |
| 511 | mock.IntFunc(true); |
| 512 | }, ""); |
| 513 | } |
| 514 | |
| 515 | #endif // GTEST_HAS_DEATH_TEST |
| 516 | |
| 517 | // Tests that DoDefault() returns the default value set by |
| 518 | // DefaultValue<T>::Set() when it's not overriden by an ON_CALL(). |
| 519 | TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) { |
| 520 | DefaultValue<int>::Set(1); |
| 521 | MockClass mock; |
| 522 | EXPECT_CALL(mock, IntFunc(_)) |
| 523 | .WillOnce(DoDefault()); |
| 524 | EXPECT_EQ(1, mock.IntFunc(false)); |
| 525 | DefaultValue<int>::Clear(); |
| 526 | } |
| 527 | |
| 528 | // Tests that DoDefault() does the action specified by ON_CALL(). |
| 529 | TEST(DoDefaultTest, DoesWhatOnCallSpecifies) { |
| 530 | MockClass mock; |
| 531 | ON_CALL(mock, IntFunc(_)) |
| 532 | .WillByDefault(Return(2)); |
| 533 | EXPECT_CALL(mock, IntFunc(_)) |
| 534 | .WillOnce(DoDefault()); |
| 535 | EXPECT_EQ(2, mock.IntFunc(false)); |
| 536 | } |
| 537 | |
| 538 | // Tests that using DoDefault() in ON_CALL() leads to a run-time failure. |
| 539 | TEST(DoDefaultTest, CannotBeUsedInOnCall) { |
| 540 | MockClass mock; |
| 541 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 542 | ON_CALL(mock, IntFunc(_)) |
| 543 | .WillByDefault(DoDefault()); |
| 544 | }, "DoDefault() cannot be used in ON_CALL()"); |
| 545 | } |
| 546 | |
| 547 | // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by |
| 548 | // the N-th (0-based) argument to v. |
| 549 | TEST(SetArgumentPointeeTest, SetsTheNthPointee) { |
| 550 | typedef void MyFunction(bool, int*, char*); |
| 551 | Action<MyFunction> a = SetArgumentPointee<1>(2); |
| 552 | |
| 553 | int n = 0; |
| 554 | char ch = '\0'; |
| 555 | a.Perform(make_tuple(true, &n, &ch)); |
| 556 | EXPECT_EQ(2, n); |
| 557 | EXPECT_EQ('\0', ch); |
| 558 | |
| 559 | a = SetArgumentPointee<2>('a'); |
| 560 | n = 0; |
| 561 | ch = '\0'; |
| 562 | a.Perform(make_tuple(true, &n, &ch)); |
| 563 | EXPECT_EQ(0, n); |
| 564 | EXPECT_EQ('a', ch); |
| 565 | } |
| 566 | |
| 567 | #if GMOCK_HAS_PROTOBUF_ |
| 568 | |
| 569 | // Tests that SetArgumentPointee<N>(proto_buffer) sets the variable |
| 570 | // pointed to by the N-th (0-based) argument to proto_buffer. |
| 571 | TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) { |
| 572 | typedef void MyFunction(bool, TestMessage*); |
| 573 | TestMessage* const msg = new TestMessage; |
| 574 | msg->set_member("yes"); |
| 575 | TestMessage orig_msg; |
| 576 | orig_msg.CopyFrom(*msg); |
| 577 | |
| 578 | Action<MyFunction> a = SetArgumentPointee<1>(*msg); |
| 579 | // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer |
| 580 | // s.t. the action works even when the original proto_buffer has |
| 581 | // died. We ensure this behavior by deleting msg before using the |
| 582 | // action. |
| 583 | delete msg; |
| 584 | |
| 585 | TestMessage dest; |
| 586 | EXPECT_FALSE(orig_msg.Equals(dest)); |
| 587 | a.Perform(make_tuple(true, &dest)); |
| 588 | EXPECT_TRUE(orig_msg.Equals(dest)); |
| 589 | } |
| 590 | |
| 591 | // Tests that SetArgumentPointee<N>(proto2_buffer) sets the variable |
| 592 | // pointed to by the N-th (0-based) argument to proto2_buffer. |
| 593 | TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) { |
| 594 | using testing::internal::FooMessage; |
| 595 | typedef void MyFunction(bool, FooMessage*); |
| 596 | FooMessage* const msg = new FooMessage; |
| 597 | msg->set_int_field(2); |
| 598 | msg->set_string_field("hi"); |
| 599 | FooMessage orig_msg; |
| 600 | orig_msg.CopyFrom(*msg); |
| 601 | |
| 602 | Action<MyFunction> a = SetArgumentPointee<1>(*msg); |
| 603 | // SetArgumentPointee<N>(proto2_buffer) makes a copy of |
| 604 | // proto2_buffer s.t. the action works even when the original |
| 605 | // proto2_buffer has died. We ensure this behavior by deleting msg |
| 606 | // before using the action. |
| 607 | delete msg; |
| 608 | |
| 609 | FooMessage dest; |
| 610 | dest.set_int_field(0); |
| 611 | a.Perform(make_tuple(true, &dest)); |
| 612 | EXPECT_EQ(2, dest.int_field()); |
| 613 | EXPECT_EQ("hi", dest.string_field()); |
| 614 | } |
| 615 | |
| 616 | #endif // GMOCK_HAS_PROTOBUF_ |
| 617 | |
| 618 | // Tests that SetArrayArgument<N>(first, last) sets the elements of the array |
| 619 | // pointed to by the N-th (0-based) argument to values in range [first, last). |
| 620 | TEST(SetArrayArgumentTest, SetsTheNthArray) { |
| 621 | typedef void MyFunction(bool, int*, char*); |
| 622 | int numbers[] = { 1, 2, 3 }; |
| 623 | Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3); |
| 624 | |
| 625 | int n[4] = {}; |
| 626 | int* pn = n; |
| 627 | char ch[4] = {}; |
| 628 | char* pch = ch; |
| 629 | a.Perform(make_tuple(true, pn, pch)); |
| 630 | EXPECT_EQ(1, n[0]); |
| 631 | EXPECT_EQ(2, n[1]); |
| 632 | EXPECT_EQ(3, n[2]); |
| 633 | EXPECT_EQ(0, n[3]); |
| 634 | EXPECT_EQ('\0', ch[0]); |
| 635 | EXPECT_EQ('\0', ch[1]); |
| 636 | EXPECT_EQ('\0', ch[2]); |
| 637 | EXPECT_EQ('\0', ch[3]); |
| 638 | |
| 639 | // Tests first and last are iterators. |
| 640 | std::string letters = "abc"; |
| 641 | a = SetArrayArgument<2>(letters.begin(), letters.end()); |
| 642 | std::fill_n(n, 4, 0); |
| 643 | std::fill_n(ch, 4, '\0'); |
| 644 | a.Perform(make_tuple(true, pn, pch)); |
| 645 | EXPECT_EQ(0, n[0]); |
| 646 | EXPECT_EQ(0, n[1]); |
| 647 | EXPECT_EQ(0, n[2]); |
| 648 | EXPECT_EQ(0, n[3]); |
| 649 | EXPECT_EQ('a', ch[0]); |
| 650 | EXPECT_EQ('b', ch[1]); |
| 651 | EXPECT_EQ('c', ch[2]); |
| 652 | EXPECT_EQ('\0', ch[3]); |
| 653 | } |
| 654 | |
| 655 | // Tests SetArrayArgument<N>(first, last) where first == last. |
| 656 | TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) { |
| 657 | typedef void MyFunction(bool, int*); |
| 658 | int numbers[] = { 1, 2, 3 }; |
| 659 | Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers); |
| 660 | |
| 661 | int n[4] = {}; |
| 662 | int* pn = n; |
| 663 | a.Perform(make_tuple(true, pn)); |
| 664 | EXPECT_EQ(0, n[0]); |
| 665 | EXPECT_EQ(0, n[1]); |
| 666 | EXPECT_EQ(0, n[2]); |
| 667 | EXPECT_EQ(0, n[3]); |
| 668 | } |
| 669 | |
| 670 | // Tests SetArrayArgument<N>(first, last) where *first is convertible |
| 671 | // (but not equal) to the argument type. |
| 672 | TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) { |
| 673 | typedef void MyFunction(bool, char*); |
| 674 | int codes[] = { 97, 98, 99 }; |
| 675 | Action<MyFunction> a = SetArrayArgument<1>(codes, codes + 3); |
| 676 | |
| 677 | char ch[4] = {}; |
| 678 | char* pch = ch; |
| 679 | a.Perform(make_tuple(true, pch)); |
| 680 | EXPECT_EQ('a', ch[0]); |
| 681 | EXPECT_EQ('b', ch[1]); |
| 682 | EXPECT_EQ('c', ch[2]); |
| 683 | EXPECT_EQ('\0', ch[3]); |
| 684 | } |
| 685 | |
| 686 | // Test SetArrayArgument<N>(first, last) with iterator as argument. |
| 687 | TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) { |
| 688 | typedef void MyFunction(bool, std::back_insert_iterator<std::string>); |
| 689 | std::string letters = "abc"; |
| 690 | Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end()); |
| 691 | |
| 692 | std::string s; |
| 693 | a.Perform(make_tuple(true, back_inserter(s))); |
| 694 | EXPECT_EQ(letters, s); |
| 695 | } |
| 696 | |
| 697 | // Sample functions and functors for testing Invoke() and etc. |
| 698 | int Nullary() { return 1; } |
| 699 | |
| 700 | class NullaryFunctor { |
| 701 | public: |
| 702 | int operator()() { return 2; } |
| 703 | }; |
| 704 | |
| 705 | bool g_done = false; |
| 706 | void VoidNullary() { g_done = true; } |
| 707 | |
| 708 | class VoidNullaryFunctor { |
| 709 | public: |
| 710 | void operator()() { g_done = true; } |
| 711 | }; |
| 712 | |
| 713 | bool Unary(int x) { return x < 0; } |
| 714 | |
| 715 | const char* Plus1(const char* s) { return s + 1; } |
| 716 | |
| 717 | void VoidUnary(int n) { g_done = true; } |
| 718 | |
| 719 | bool ByConstRef(const std::string& s) { return s == "Hi"; } |
| 720 | |
| 721 | const double g_double = 0; |
| 722 | bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; } |
| 723 | |
| 724 | std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT |
| 725 | |
| 726 | struct UnaryFunctor { |
| 727 | int operator()(bool x) { return x ? 1 : -1; } |
| 728 | }; |
| 729 | |
| 730 | const char* Binary(const char* input, short n) { return input + n; } // NOLINT |
| 731 | |
| 732 | void VoidBinary(int, char) { g_done = true; } |
| 733 | |
| 734 | int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT |
| 735 | |
| 736 | void VoidTernary(int, char, bool) { g_done = true; } |
| 737 | |
| 738 | int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } |
| 739 | |
| 740 | void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } |
| 741 | |
| 742 | int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } |
| 743 | |
| 744 | struct SumOf5Functor { |
| 745 | int operator()(int a, int b, int c, int d, int e) { |
| 746 | return a + b + c + d + e; |
| 747 | } |
| 748 | }; |
| 749 | |
| 750 | int SumOf6(int a, int b, int c, int d, int e, int f) { |
| 751 | return a + b + c + d + e + f; |
| 752 | } |
| 753 | |
| 754 | struct SumOf6Functor { |
| 755 | int operator()(int a, int b, int c, int d, int e, int f) { |
| 756 | return a + b + c + d + e + f; |
| 757 | } |
| 758 | }; |
| 759 | |
| 760 | class Foo { |
| 761 | public: |
| 762 | Foo() : value_(123) {} |
| 763 | |
| 764 | int Nullary() const { return value_; } |
| 765 | short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT |
| 766 | std::string Binary(const std::string& str, char c) const { return str + c; } |
| 767 | int Ternary(int x, bool y, char z) { return value_ + x + y*z; } |
| 768 | int SumOf4(int a, int b, int c, int d) const { |
| 769 | return a + b + c + d + value_; |
| 770 | } |
| 771 | int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } |
| 772 | int SumOf6(int a, int b, int c, int d, int e, int f) { |
| 773 | return a + b + c + d + e + f; |
| 774 | } |
| 775 | private: |
| 776 | int value_; |
| 777 | }; |
| 778 | |
| 779 | // Tests InvokeWithoutArgs(function). |
| 780 | TEST(InvokeWithoutArgsTest, Function) { |
| 781 | // As an action that takes one argument. |
| 782 | Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT |
| 783 | EXPECT_EQ(1, a.Perform(make_tuple(2))); |
| 784 | |
| 785 | // As an action that takes two arguments. |
| 786 | Action<short(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT |
| 787 | EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5))); |
| 788 | |
| 789 | // As an action that returns void. |
| 790 | Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT |
| 791 | g_done = false; |
| 792 | a3.Perform(make_tuple(1)); |
| 793 | EXPECT_TRUE(g_done); |
| 794 | } |
| 795 | |
| 796 | // Tests InvokeWithoutArgs(functor). |
| 797 | TEST(InvokeWithoutArgsTest, Functor) { |
| 798 | // As an action that takes no argument. |
| 799 | Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT |
| 800 | EXPECT_EQ(2, a.Perform(make_tuple())); |
| 801 | |
| 802 | // As an action that takes three arguments. |
| 803 | Action<short(int, double, char)> a2 = // NOLINT |
| 804 | InvokeWithoutArgs(NullaryFunctor()); |
| 805 | EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a'))); |
| 806 | |
| 807 | // As an action that returns void. |
| 808 | Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor()); |
| 809 | g_done = false; |
| 810 | a3.Perform(make_tuple()); |
| 811 | EXPECT_TRUE(g_done); |
| 812 | } |
| 813 | |
| 814 | // Tests InvokeWithoutArgs(obj_ptr, method). |
| 815 | TEST(InvokeWithoutArgsTest, Method) { |
| 816 | Foo foo; |
| 817 | Action<int(bool, char)> a = // NOLINT |
| 818 | InvokeWithoutArgs(&foo, &Foo::Nullary); |
| 819 | EXPECT_EQ(123, a.Perform(make_tuple(true, 'a'))); |
| 820 | } |
| 821 | |
| 822 | // Tests using IgnoreResult() on a polymorphic action. |
| 823 | TEST(IgnoreResultTest, PolymorphicAction) { |
| 824 | Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT |
| 825 | a.Perform(make_tuple(1)); |
| 826 | } |
| 827 | |
| 828 | // Tests using IgnoreResult() on a monomorphic action. |
| 829 | |
| 830 | int ReturnOne() { |
| 831 | g_done = true; |
| 832 | return 1; |
| 833 | } |
| 834 | |
| 835 | TEST(IgnoreResultTest, MonomorphicAction) { |
| 836 | g_done = false; |
| 837 | Action<void()> a = IgnoreResult(Invoke(ReturnOne)); |
| 838 | a.Perform(make_tuple()); |
| 839 | EXPECT_TRUE(g_done); |
| 840 | } |
| 841 | |
| 842 | // Tests using IgnoreResult() on an action that returns a class type. |
| 843 | |
| 844 | MyClass ReturnMyClass(double x) { |
| 845 | g_done = true; |
| 846 | return MyClass(); |
| 847 | } |
| 848 | |
| 849 | TEST(IgnoreResultTest, ActionReturningClass) { |
| 850 | g_done = false; |
| 851 | Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT |
| 852 | a.Perform(make_tuple(2)); |
| 853 | EXPECT_TRUE(g_done); |
| 854 | } |
| 855 | |
| 856 | TEST(AssignTest, Int) { |
| 857 | int x = 0; |
| 858 | Action<void(int)> a = Assign(&x, 5); |
| 859 | a.Perform(make_tuple(0)); |
| 860 | EXPECT_EQ(5, x); |
| 861 | } |
| 862 | |
| 863 | TEST(AssignTest, String) { |
| 864 | ::std::string x; |
| 865 | Action<void(void)> a = Assign(&x, "Hello, world"); |
| 866 | a.Perform(make_tuple()); |
| 867 | EXPECT_EQ("Hello, world", x); |
| 868 | } |
| 869 | |
| 870 | TEST(AssignTest, CompatibleTypes) { |
| 871 | double x = 0; |
| 872 | Action<void(int)> a = Assign(&x, 5); |
| 873 | a.Perform(make_tuple(0)); |
| 874 | EXPECT_DOUBLE_EQ(5, x); |
| 875 | } |
| 876 | |
| 877 | class SetErrnoAndReturnTest : public testing::Test { |
| 878 | protected: |
| 879 | virtual void SetUp() { errno = 0; } |
| 880 | virtual void TearDown() { errno = 0; } |
| 881 | }; |
| 882 | |
| 883 | TEST_F(SetErrnoAndReturnTest, Int) { |
| 884 | Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5); |
| 885 | EXPECT_EQ(-5, a.Perform(make_tuple())); |
| 886 | EXPECT_EQ(ENOTTY, errno); |
| 887 | } |
| 888 | |
| 889 | TEST_F(SetErrnoAndReturnTest, Ptr) { |
| 890 | int x; |
| 891 | Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x); |
| 892 | EXPECT_EQ(&x, a.Perform(make_tuple())); |
| 893 | EXPECT_EQ(ENOTTY, errno); |
| 894 | } |
| 895 | |
| 896 | TEST_F(SetErrnoAndReturnTest, CompatibleTypes) { |
| 897 | Action<double()> a = SetErrnoAndReturn(EINVAL, 5); |
| 898 | EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple())); |
| 899 | EXPECT_EQ(EINVAL, errno); |
| 900 | } |
| 901 | |
| 902 | } // Unnamed namespace |