| OLD | NEW |
| (Empty) |
| 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::ByRef; | |
| 60 using testing::DefaultValue; | |
| 61 using testing::DoDefault; | |
| 62 using testing::IgnoreResult; | |
| 63 using testing::Invoke; | |
| 64 using testing::InvokeWithoutArgs; | |
| 65 using testing::MakePolymorphicAction; | |
| 66 using testing::Ne; | |
| 67 using testing::PolymorphicAction; | |
| 68 using testing::Return; | |
| 69 using testing::ReturnNull; | |
| 70 using testing::ReturnRef; | |
| 71 using testing::SetArgumentPointee; | |
| 72 | |
| 73 #if !GTEST_OS_WINDOWS_MOBILE | |
| 74 using testing::SetErrnoAndReturn; | |
| 75 #endif | |
| 76 | |
| 77 #if GTEST_HAS_PROTOBUF_ | |
| 78 using testing::internal::TestMessage; | |
| 79 #endif // GTEST_HAS_PROTOBUF_ | |
| 80 | |
| 81 // Tests that BuiltInDefaultValue<T*>::Get() returns NULL. | |
| 82 TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) { | |
| 83 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL); | |
| 84 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL); | |
| 85 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL); | |
| 86 } | |
| 87 | |
| 88 // Tests that BuiltInDefaultValue<T*>::Exists() return true. | |
| 89 TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) { | |
| 90 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists()); | |
| 91 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists()); | |
| 92 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists()); | |
| 93 } | |
| 94 | |
| 95 // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a | |
| 96 // built-in numeric type. | |
| 97 TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) { | |
| 98 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get()); | |
| 99 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get()); | |
| 100 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get()); | |
| 101 #if GMOCK_HAS_SIGNED_WCHAR_T_ | |
| 102 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get()); | |
| 103 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get()); | |
| 104 #endif | |
| 105 #if GMOCK_WCHAR_T_IS_NATIVE_ | |
| 106 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get()); | |
| 107 #endif | |
| 108 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT | |
| 109 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT | |
| 110 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT | |
| 111 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get()); | |
| 112 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get()); | |
| 113 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get()); | |
| 114 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT | |
| 115 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT | |
| 116 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT | |
| 117 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get()); | |
| 118 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get()); | |
| 119 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get()); | |
| 120 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get()); | |
| 121 } | |
| 122 | |
| 123 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a | |
| 124 // built-in numeric type. | |
| 125 TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) { | |
| 126 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists()); | |
| 127 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists()); | |
| 128 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists()); | |
| 129 #if GMOCK_HAS_SIGNED_WCHAR_T_ | |
| 130 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists()); | |
| 131 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists()); | |
| 132 #endif | |
| 133 #if GMOCK_WCHAR_T_IS_NATIVE_ | |
| 134 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists()); | |
| 135 #endif | |
| 136 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT | |
| 137 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT | |
| 138 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT | |
| 139 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists()); | |
| 140 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists()); | |
| 141 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists()); | |
| 142 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT | |
| 143 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT | |
| 144 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT | |
| 145 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists()); | |
| 146 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists()); | |
| 147 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists()); | |
| 148 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists()); | |
| 149 } | |
| 150 | |
| 151 // Tests that BuiltInDefaultValue<bool>::Get() returns false. | |
| 152 TEST(BuiltInDefaultValueTest, IsFalseForBool) { | |
| 153 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get()); | |
| 154 } | |
| 155 | |
| 156 // Tests that BuiltInDefaultValue<bool>::Exists() returns true. | |
| 157 TEST(BuiltInDefaultValueTest, BoolExists) { | |
| 158 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists()); | |
| 159 } | |
| 160 | |
| 161 // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a | |
| 162 // string type. | |
| 163 TEST(BuiltInDefaultValueTest, IsEmptyStringForString) { | |
| 164 #if GTEST_HAS_GLOBAL_STRING | |
| 165 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get()); | |
| 166 #endif // GTEST_HAS_GLOBAL_STRING | |
| 167 | |
| 168 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get()); | |
| 169 } | |
| 170 | |
| 171 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a | |
| 172 // string type. | |
| 173 TEST(BuiltInDefaultValueTest, ExistsForString) { | |
| 174 #if GTEST_HAS_GLOBAL_STRING | |
| 175 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists()); | |
| 176 #endif // GTEST_HAS_GLOBAL_STRING | |
| 177 | |
| 178 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists()); | |
| 179 } | |
| 180 | |
| 181 // Tests that BuiltInDefaultValue<const T>::Get() returns the same | |
| 182 // value as BuiltInDefaultValue<T>::Get() does. | |
| 183 TEST(BuiltInDefaultValueTest, WorksForConstTypes) { | |
| 184 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get()); | |
| 185 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get()); | |
| 186 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL); | |
| 187 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get()); | |
| 188 } | |
| 189 | |
| 190 // Tests that BuiltInDefaultValue<T>::Get() aborts the program with | |
| 191 // the correct error message when T is a user-defined type. | |
| 192 struct UserType { | |
| 193 UserType() : value(0) {} | |
| 194 | |
| 195 int value; | |
| 196 }; | |
| 197 | |
| 198 TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) { | |
| 199 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists()); | |
| 200 } | |
| 201 | |
| 202 // Tests that BuiltInDefaultValue<T&>::Get() aborts the program. | |
| 203 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) { | |
| 204 EXPECT_DEATH_IF_SUPPORTED({ | |
| 205 BuiltInDefaultValue<int&>::Get(); | |
| 206 }, ""); | |
| 207 EXPECT_DEATH_IF_SUPPORTED({ | |
| 208 BuiltInDefaultValue<const char&>::Get(); | |
| 209 }, ""); | |
| 210 } | |
| 211 | |
| 212 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) { | |
| 213 EXPECT_DEATH_IF_SUPPORTED({ | |
| 214 BuiltInDefaultValue<UserType>::Get(); | |
| 215 }, ""); | |
| 216 } | |
| 217 | |
| 218 // Tests that DefaultValue<T>::IsSet() is false initially. | |
| 219 TEST(DefaultValueTest, IsInitiallyUnset) { | |
| 220 EXPECT_FALSE(DefaultValue<int>::IsSet()); | |
| 221 EXPECT_FALSE(DefaultValue<const UserType>::IsSet()); | |
| 222 } | |
| 223 | |
| 224 // Tests that DefaultValue<T> can be set and then unset. | |
| 225 TEST(DefaultValueTest, CanBeSetAndUnset) { | |
| 226 EXPECT_TRUE(DefaultValue<int>::Exists()); | |
| 227 EXPECT_FALSE(DefaultValue<const UserType>::Exists()); | |
| 228 | |
| 229 DefaultValue<int>::Set(1); | |
| 230 DefaultValue<const UserType>::Set(UserType()); | |
| 231 | |
| 232 EXPECT_EQ(1, DefaultValue<int>::Get()); | |
| 233 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value); | |
| 234 | |
| 235 EXPECT_TRUE(DefaultValue<int>::Exists()); | |
| 236 EXPECT_TRUE(DefaultValue<const UserType>::Exists()); | |
| 237 | |
| 238 DefaultValue<int>::Clear(); | |
| 239 DefaultValue<const UserType>::Clear(); | |
| 240 | |
| 241 EXPECT_FALSE(DefaultValue<int>::IsSet()); | |
| 242 EXPECT_FALSE(DefaultValue<const UserType>::IsSet()); | |
| 243 | |
| 244 EXPECT_TRUE(DefaultValue<int>::Exists()); | |
| 245 EXPECT_FALSE(DefaultValue<const UserType>::Exists()); | |
| 246 } | |
| 247 | |
| 248 // Tests that DefaultValue<T>::Get() returns the | |
| 249 // BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is | |
| 250 // false. | |
| 251 TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { | |
| 252 EXPECT_FALSE(DefaultValue<int>::IsSet()); | |
| 253 EXPECT_TRUE(DefaultValue<int>::Exists()); | |
| 254 EXPECT_FALSE(DefaultValue<UserType>::IsSet()); | |
| 255 EXPECT_FALSE(DefaultValue<UserType>::Exists()); | |
| 256 | |
| 257 EXPECT_EQ(0, DefaultValue<int>::Get()); | |
| 258 | |
| 259 EXPECT_DEATH_IF_SUPPORTED({ | |
| 260 DefaultValue<UserType>::Get(); | |
| 261 }, ""); | |
| 262 } | |
| 263 | |
| 264 // Tests that DefaultValue<void>::Get() returns void. | |
| 265 TEST(DefaultValueTest, GetWorksForVoid) { | |
| 266 return DefaultValue<void>::Get(); | |
| 267 } | |
| 268 | |
| 269 // Tests using DefaultValue with a reference type. | |
| 270 | |
| 271 // Tests that DefaultValue<T&>::IsSet() is false initially. | |
| 272 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) { | |
| 273 EXPECT_FALSE(DefaultValue<int&>::IsSet()); | |
| 274 EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); | |
| 275 } | |
| 276 | |
| 277 // Tests that DefaultValue<T&>::Exists is false initiallly. | |
| 278 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) { | |
| 279 EXPECT_FALSE(DefaultValue<int&>::Exists()); | |
| 280 EXPECT_FALSE(DefaultValue<UserType&>::Exists()); | |
| 281 } | |
| 282 | |
| 283 // Tests that DefaultValue<T&> can be set and then unset. | |
| 284 TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) { | |
| 285 int n = 1; | |
| 286 DefaultValue<const int&>::Set(n); | |
| 287 UserType u; | |
| 288 DefaultValue<UserType&>::Set(u); | |
| 289 | |
| 290 EXPECT_TRUE(DefaultValue<const int&>::Exists()); | |
| 291 EXPECT_TRUE(DefaultValue<UserType&>::Exists()); | |
| 292 | |
| 293 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get())); | |
| 294 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get())); | |
| 295 | |
| 296 DefaultValue<const int&>::Clear(); | |
| 297 DefaultValue<UserType&>::Clear(); | |
| 298 | |
| 299 EXPECT_FALSE(DefaultValue<const int&>::Exists()); | |
| 300 EXPECT_FALSE(DefaultValue<UserType&>::Exists()); | |
| 301 | |
| 302 EXPECT_FALSE(DefaultValue<const int&>::IsSet()); | |
| 303 EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); | |
| 304 } | |
| 305 | |
| 306 // Tests that DefaultValue<T&>::Get() returns the | |
| 307 // BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is | |
| 308 // false. | |
| 309 TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { | |
| 310 EXPECT_FALSE(DefaultValue<int&>::IsSet()); | |
| 311 EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); | |
| 312 | |
| 313 EXPECT_DEATH_IF_SUPPORTED({ | |
| 314 DefaultValue<int&>::Get(); | |
| 315 }, ""); | |
| 316 EXPECT_DEATH_IF_SUPPORTED({ | |
| 317 DefaultValue<UserType>::Get(); | |
| 318 }, ""); | |
| 319 } | |
| 320 | |
| 321 // Tests that ActionInterface can be implemented by defining the | |
| 322 // Perform method. | |
| 323 | |
| 324 typedef int MyFunction(bool, int); | |
| 325 | |
| 326 class MyActionImpl : public ActionInterface<MyFunction> { | |
| 327 public: | |
| 328 virtual int Perform(const tuple<bool, int>& args) { | |
| 329 return get<0>(args) ? get<1>(args) : 0; | |
| 330 } | |
| 331 }; | |
| 332 | |
| 333 TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) { | |
| 334 MyActionImpl my_action_impl; | |
| 335 | |
| 336 EXPECT_FALSE(my_action_impl.IsDoDefault()); | |
| 337 } | |
| 338 | |
| 339 TEST(ActionInterfaceTest, MakeAction) { | |
| 340 Action<MyFunction> action = MakeAction(new MyActionImpl); | |
| 341 | |
| 342 // When exercising the Perform() method of Action<F>, we must pass | |
| 343 // it a tuple whose size and type are compatible with F's argument | |
| 344 // types. For example, if F is int(), then Perform() takes a | |
| 345 // 0-tuple; if F is void(bool, int), then Perform() takes a | |
| 346 // tuple<bool, int>, and so on. | |
| 347 EXPECT_EQ(5, action.Perform(make_tuple(true, 5))); | |
| 348 } | |
| 349 | |
| 350 // Tests that Action<F> can be contructed from a pointer to | |
| 351 // ActionInterface<F>. | |
| 352 TEST(ActionTest, CanBeConstructedFromActionInterface) { | |
| 353 Action<MyFunction> action(new MyActionImpl); | |
| 354 } | |
| 355 | |
| 356 // Tests that Action<F> delegates actual work to ActionInterface<F>. | |
| 357 TEST(ActionTest, DelegatesWorkToActionInterface) { | |
| 358 const Action<MyFunction> action(new MyActionImpl); | |
| 359 | |
| 360 EXPECT_EQ(5, action.Perform(make_tuple(true, 5))); | |
| 361 EXPECT_EQ(0, action.Perform(make_tuple(false, 1))); | |
| 362 } | |
| 363 | |
| 364 // Tests that Action<F> can be copied. | |
| 365 TEST(ActionTest, IsCopyable) { | |
| 366 Action<MyFunction> a1(new MyActionImpl); | |
| 367 Action<MyFunction> a2(a1); // Tests the copy constructor. | |
| 368 | |
| 369 // a1 should continue to work after being copied from. | |
| 370 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5))); | |
| 371 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1))); | |
| 372 | |
| 373 // a2 should work like the action it was copied from. | |
| 374 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5))); | |
| 375 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1))); | |
| 376 | |
| 377 a2 = a1; // Tests the assignment operator. | |
| 378 | |
| 379 // a1 should continue to work after being copied from. | |
| 380 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5))); | |
| 381 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1))); | |
| 382 | |
| 383 // a2 should work like the action it was copied from. | |
| 384 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5))); | |
| 385 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1))); | |
| 386 } | |
| 387 | |
| 388 // Tests that an Action<From> object can be converted to a | |
| 389 // compatible Action<To> object. | |
| 390 | |
| 391 class IsNotZero : public ActionInterface<bool(int)> { // NOLINT | |
| 392 public: | |
| 393 virtual bool Perform(const tuple<int>& arg) { | |
| 394 return get<0>(arg) != 0; | |
| 395 } | |
| 396 }; | |
| 397 | |
| 398 #if !GTEST_OS_SYMBIAN | |
| 399 // Compiling this test on Nokia's Symbian compiler fails with: | |
| 400 // 'Result' is not a member of class 'testing::internal::Function<int>' | |
| 401 // (point of instantiation: '@unnamed@gmock_actions_test_cc@:: | |
| 402 // ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()') | |
| 403 // with no obvious fix. | |
| 404 TEST(ActionTest, CanBeConvertedToOtherActionType) { | |
| 405 const Action<bool(int)> a1(new IsNotZero); // NOLINT | |
| 406 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT | |
| 407 EXPECT_EQ(1, a2.Perform(make_tuple('a'))); | |
| 408 EXPECT_EQ(0, a2.Perform(make_tuple('\0'))); | |
| 409 } | |
| 410 #endif // !GTEST_OS_SYMBIAN | |
| 411 | |
| 412 // The following two classes are for testing MakePolymorphicAction(). | |
| 413 | |
| 414 // Implements a polymorphic action that returns the second of the | |
| 415 // arguments it receives. | |
| 416 class ReturnSecondArgumentAction { | |
| 417 public: | |
| 418 // We want to verify that MakePolymorphicAction() can work with a | |
| 419 // polymorphic action whose Perform() method template is either | |
| 420 // const or not. This lets us verify the non-const case. | |
| 421 template <typename Result, typename ArgumentTuple> | |
| 422 Result Perform(const ArgumentTuple& args) { return get<1>(args); } | |
| 423 }; | |
| 424 | |
| 425 // Implements a polymorphic action that can be used in a nullary | |
| 426 // function to return 0. | |
| 427 class ReturnZeroFromNullaryFunctionAction { | |
| 428 public: | |
| 429 // For testing that MakePolymorphicAction() works when the | |
| 430 // implementation class' Perform() method template takes only one | |
| 431 // template parameter. | |
| 432 // | |
| 433 // We want to verify that MakePolymorphicAction() can work with a | |
| 434 // polymorphic action whose Perform() method template is either | |
| 435 // const or not. This lets us verify the const case. | |
| 436 template <typename Result> | |
| 437 Result Perform(const tuple<>&) const { return 0; } | |
| 438 }; | |
| 439 | |
| 440 // These functions verify that MakePolymorphicAction() returns a | |
| 441 // PolymorphicAction<T> where T is the argument's type. | |
| 442 | |
| 443 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() { | |
| 444 return MakePolymorphicAction(ReturnSecondArgumentAction()); | |
| 445 } | |
| 446 | |
| 447 PolymorphicAction<ReturnZeroFromNullaryFunctionAction> | |
| 448 ReturnZeroFromNullaryFunction() { | |
| 449 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction()); | |
| 450 } | |
| 451 | |
| 452 // Tests that MakePolymorphicAction() turns a polymorphic action | |
| 453 // implementation class into a polymorphic action. | |
| 454 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) { | |
| 455 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT | |
| 456 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0))); | |
| 457 } | |
| 458 | |
| 459 // Tests that MakePolymorphicAction() works when the implementation | |
| 460 // class' Perform() method template has only one template parameter. | |
| 461 TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) { | |
| 462 Action<int()> a1 = ReturnZeroFromNullaryFunction(); | |
| 463 EXPECT_EQ(0, a1.Perform(make_tuple())); | |
| 464 | |
| 465 Action<void*()> a2 = ReturnZeroFromNullaryFunction(); | |
| 466 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL); | |
| 467 } | |
| 468 | |
| 469 // Tests that Return() works as an action for void-returning | |
| 470 // functions. | |
| 471 TEST(ReturnTest, WorksForVoid) { | |
| 472 const Action<void(int)> ret = Return(); // NOLINT | |
| 473 return ret.Perform(make_tuple(1)); | |
| 474 } | |
| 475 | |
| 476 // Tests that Return(v) returns v. | |
| 477 TEST(ReturnTest, ReturnsGivenValue) { | |
| 478 Action<int()> ret = Return(1); // NOLINT | |
| 479 EXPECT_EQ(1, ret.Perform(make_tuple())); | |
| 480 | |
| 481 ret = Return(-5); | |
| 482 EXPECT_EQ(-5, ret.Perform(make_tuple())); | |
| 483 } | |
| 484 | |
| 485 // Tests that Return("string literal") works. | |
| 486 TEST(ReturnTest, AcceptsStringLiteral) { | |
| 487 Action<const char*()> a1 = Return("Hello"); | |
| 488 EXPECT_STREQ("Hello", a1.Perform(make_tuple())); | |
| 489 | |
| 490 Action<std::string()> a2 = Return("world"); | |
| 491 EXPECT_EQ("world", a2.Perform(make_tuple())); | |
| 492 } | |
| 493 | |
| 494 // Tests that Return(v) is covaraint. | |
| 495 | |
| 496 struct Base { | |
| 497 bool operator==(const Base&) { return true; } | |
| 498 }; | |
| 499 | |
| 500 struct Derived : public Base { | |
| 501 bool operator==(const Derived&) { return true; } | |
| 502 }; | |
| 503 | |
| 504 TEST(ReturnTest, IsCovariant) { | |
| 505 Base base; | |
| 506 Derived derived; | |
| 507 Action<Base*()> ret = Return(&base); | |
| 508 EXPECT_EQ(&base, ret.Perform(make_tuple())); | |
| 509 | |
| 510 ret = Return(&derived); | |
| 511 EXPECT_EQ(&derived, ret.Perform(make_tuple())); | |
| 512 } | |
| 513 | |
| 514 // Tests that the type of the value passed into Return is converted into T | |
| 515 // when the action is cast to Action<T(...)> rather than when the action is | |
| 516 // performed. See comments on testing::internal::ReturnAction in | |
| 517 // gmock-actions.h for more information. | |
| 518 class FromType { | |
| 519 public: | |
| 520 FromType(bool* is_converted) : converted_(is_converted) {} | |
| 521 bool* converted() const { return converted_; } | |
| 522 | |
| 523 private: | |
| 524 bool* const converted_; | |
| 525 | |
| 526 GTEST_DISALLOW_ASSIGN_(FromType); | |
| 527 }; | |
| 528 | |
| 529 class ToType { | |
| 530 public: | |
| 531 ToType(const FromType& x) { *x.converted() = true; } | |
| 532 }; | |
| 533 | |
| 534 TEST(ReturnTest, ConvertsArgumentWhenConverted) { | |
| 535 bool converted = false; | |
| 536 FromType x(&converted); | |
| 537 Action<ToType()> action(Return(x)); | |
| 538 EXPECT_TRUE(converted) << "Return must convert its argument in its own " | |
| 539 << "conversion operator."; | |
| 540 converted = false; | |
| 541 action.Perform(tuple<>()); | |
| 542 EXPECT_FALSE(converted) << "Action must NOT convert its argument " | |
| 543 << "when performed." ; | |
| 544 } | |
| 545 | |
| 546 class DestinationType {}; | |
| 547 | |
| 548 class SourceType { | |
| 549 public: | |
| 550 // Note: a non-const typecast operator. | |
| 551 operator DestinationType() { return DestinationType(); } | |
| 552 }; | |
| 553 | |
| 554 TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) { | |
| 555 SourceType s; | |
| 556 Action<DestinationType()> action(Return(s)); | |
| 557 } | |
| 558 | |
| 559 // Tests that ReturnNull() returns NULL in a pointer-returning function. | |
| 560 TEST(ReturnNullTest, WorksInPointerReturningFunction) { | |
| 561 const Action<int*()> a1 = ReturnNull(); | |
| 562 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL); | |
| 563 | |
| 564 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT | |
| 565 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL); | |
| 566 } | |
| 567 | |
| 568 // Tests that ReturnRef(v) works for reference types. | |
| 569 TEST(ReturnRefTest, WorksForReference) { | |
| 570 const int n = 0; | |
| 571 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT | |
| 572 | |
| 573 EXPECT_EQ(&n, &ret.Perform(make_tuple(true))); | |
| 574 } | |
| 575 | |
| 576 // Tests that ReturnRef(v) is covariant. | |
| 577 TEST(ReturnRefTest, IsCovariant) { | |
| 578 Base base; | |
| 579 Derived derived; | |
| 580 Action<Base&()> a = ReturnRef(base); | |
| 581 EXPECT_EQ(&base, &a.Perform(make_tuple())); | |
| 582 | |
| 583 a = ReturnRef(derived); | |
| 584 EXPECT_EQ(&derived, &a.Perform(make_tuple())); | |
| 585 } | |
| 586 | |
| 587 // Tests that DoDefault() does the default action for the mock method. | |
| 588 | |
| 589 class MyClass {}; | |
| 590 | |
| 591 class MockClass { | |
| 592 public: | |
| 593 MockClass() {} | |
| 594 | |
| 595 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT | |
| 596 MOCK_METHOD0(Foo, MyClass()); | |
| 597 | |
| 598 private: | |
| 599 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass); | |
| 600 }; | |
| 601 | |
| 602 // Tests that DoDefault() returns the built-in default value for the | |
| 603 // return type by default. | |
| 604 TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) { | |
| 605 MockClass mock; | |
| 606 EXPECT_CALL(mock, IntFunc(_)) | |
| 607 .WillOnce(DoDefault()); | |
| 608 EXPECT_EQ(0, mock.IntFunc(true)); | |
| 609 } | |
| 610 | |
| 611 // Tests that DoDefault() aborts the process when there is no built-in | |
| 612 // default value for the return type. | |
| 613 TEST(DoDefaultDeathTest, DiesForUnknowType) { | |
| 614 MockClass mock; | |
| 615 EXPECT_CALL(mock, Foo()) | |
| 616 .WillRepeatedly(DoDefault()); | |
| 617 EXPECT_DEATH_IF_SUPPORTED({ | |
| 618 mock.Foo(); | |
| 619 }, ""); | |
| 620 } | |
| 621 | |
| 622 // Tests that using DoDefault() inside a composite action leads to a | |
| 623 // run-time error. | |
| 624 | |
| 625 void VoidFunc(bool /* flag */) {} | |
| 626 | |
| 627 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) { | |
| 628 MockClass mock; | |
| 629 EXPECT_CALL(mock, IntFunc(_)) | |
| 630 .WillRepeatedly(DoAll(Invoke(VoidFunc), | |
| 631 DoDefault())); | |
| 632 | |
| 633 // Ideally we should verify the error message as well. Sadly, | |
| 634 // EXPECT_DEATH() can only capture stderr, while Google Mock's | |
| 635 // errors are printed on stdout. Therefore we have to settle for | |
| 636 // not verifying the message. | |
| 637 EXPECT_DEATH_IF_SUPPORTED({ | |
| 638 mock.IntFunc(true); | |
| 639 }, ""); | |
| 640 } | |
| 641 | |
| 642 // Tests that DoDefault() returns the default value set by | |
| 643 // DefaultValue<T>::Set() when it's not overriden by an ON_CALL(). | |
| 644 TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) { | |
| 645 DefaultValue<int>::Set(1); | |
| 646 MockClass mock; | |
| 647 EXPECT_CALL(mock, IntFunc(_)) | |
| 648 .WillOnce(DoDefault()); | |
| 649 EXPECT_EQ(1, mock.IntFunc(false)); | |
| 650 DefaultValue<int>::Clear(); | |
| 651 } | |
| 652 | |
| 653 // Tests that DoDefault() does the action specified by ON_CALL(). | |
| 654 TEST(DoDefaultTest, DoesWhatOnCallSpecifies) { | |
| 655 MockClass mock; | |
| 656 ON_CALL(mock, IntFunc(_)) | |
| 657 .WillByDefault(Return(2)); | |
| 658 EXPECT_CALL(mock, IntFunc(_)) | |
| 659 .WillOnce(DoDefault()); | |
| 660 EXPECT_EQ(2, mock.IntFunc(false)); | |
| 661 } | |
| 662 | |
| 663 // Tests that using DoDefault() in ON_CALL() leads to a run-time failure. | |
| 664 TEST(DoDefaultTest, CannotBeUsedInOnCall) { | |
| 665 MockClass mock; | |
| 666 EXPECT_NONFATAL_FAILURE({ // NOLINT | |
| 667 ON_CALL(mock, IntFunc(_)) | |
| 668 .WillByDefault(DoDefault()); | |
| 669 }, "DoDefault() cannot be used in ON_CALL()"); | |
| 670 } | |
| 671 | |
| 672 // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by | |
| 673 // the N-th (0-based) argument to v. | |
| 674 TEST(SetArgumentPointeeTest, SetsTheNthPointee) { | |
| 675 typedef void MyFunction(bool, int*, char*); | |
| 676 Action<MyFunction> a = SetArgumentPointee<1>(2); | |
| 677 | |
| 678 int n = 0; | |
| 679 char ch = '\0'; | |
| 680 a.Perform(make_tuple(true, &n, &ch)); | |
| 681 EXPECT_EQ(2, n); | |
| 682 EXPECT_EQ('\0', ch); | |
| 683 | |
| 684 a = SetArgumentPointee<2>('a'); | |
| 685 n = 0; | |
| 686 ch = '\0'; | |
| 687 a.Perform(make_tuple(true, &n, &ch)); | |
| 688 EXPECT_EQ(0, n); | |
| 689 EXPECT_EQ('a', ch); | |
| 690 } | |
| 691 | |
| 692 #if GTEST_HAS_PROTOBUF_ | |
| 693 | |
| 694 // Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf | |
| 695 // variable pointed to by the N-th (0-based) argument to proto_buffer. | |
| 696 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) { | |
| 697 TestMessage* const msg = new TestMessage; | |
| 698 msg->set_member("yes"); | |
| 699 TestMessage orig_msg; | |
| 700 orig_msg.CopyFrom(*msg); | |
| 701 | |
| 702 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg); | |
| 703 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer | |
| 704 // s.t. the action works even when the original proto_buffer has | |
| 705 // died. We ensure this behavior by deleting msg before using the | |
| 706 // action. | |
| 707 delete msg; | |
| 708 | |
| 709 TestMessage dest; | |
| 710 EXPECT_FALSE(orig_msg.Equals(dest)); | |
| 711 a.Perform(make_tuple(true, &dest)); | |
| 712 EXPECT_TRUE(orig_msg.Equals(dest)); | |
| 713 } | |
| 714 | |
| 715 // Tests that SetArgumentPointee<N>(proto_buffer) sets the | |
| 716 // ::ProtocolMessage variable pointed to by the N-th (0-based) | |
| 717 // argument to proto_buffer. | |
| 718 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) { | |
| 719 TestMessage* const msg = new TestMessage; | |
| 720 msg->set_member("yes"); | |
| 721 TestMessage orig_msg; | |
| 722 orig_msg.CopyFrom(*msg); | |
| 723 | |
| 724 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg); | |
| 725 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer | |
| 726 // s.t. the action works even when the original proto_buffer has | |
| 727 // died. We ensure this behavior by deleting msg before using the | |
| 728 // action. | |
| 729 delete msg; | |
| 730 | |
| 731 TestMessage dest; | |
| 732 ::ProtocolMessage* const dest_base = &dest; | |
| 733 EXPECT_FALSE(orig_msg.Equals(dest)); | |
| 734 a.Perform(make_tuple(true, dest_base)); | |
| 735 EXPECT_TRUE(orig_msg.Equals(dest)); | |
| 736 } | |
| 737 | |
| 738 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2 | |
| 739 // protobuf variable pointed to by the N-th (0-based) argument to | |
| 740 // proto2_buffer. | |
| 741 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) { | |
| 742 using testing::internal::FooMessage; | |
| 743 FooMessage* const msg = new FooMessage; | |
| 744 msg->set_int_field(2); | |
| 745 msg->set_string_field("hi"); | |
| 746 FooMessage orig_msg; | |
| 747 orig_msg.CopyFrom(*msg); | |
| 748 | |
| 749 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg); | |
| 750 // SetArgumentPointee<N>(proto2_buffer) makes a copy of | |
| 751 // proto2_buffer s.t. the action works even when the original | |
| 752 // proto2_buffer has died. We ensure this behavior by deleting msg | |
| 753 // before using the action. | |
| 754 delete msg; | |
| 755 | |
| 756 FooMessage dest; | |
| 757 dest.set_int_field(0); | |
| 758 a.Perform(make_tuple(true, &dest)); | |
| 759 EXPECT_EQ(2, dest.int_field()); | |
| 760 EXPECT_EQ("hi", dest.string_field()); | |
| 761 } | |
| 762 | |
| 763 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the | |
| 764 // proto2::Message variable pointed to by the N-th (0-based) argument | |
| 765 // to proto2_buffer. | |
| 766 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) { | |
| 767 using testing::internal::FooMessage; | |
| 768 FooMessage* const msg = new FooMessage; | |
| 769 msg->set_int_field(2); | |
| 770 msg->set_string_field("hi"); | |
| 771 FooMessage orig_msg; | |
| 772 orig_msg.CopyFrom(*msg); | |
| 773 | |
| 774 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg); | |
| 775 // SetArgumentPointee<N>(proto2_buffer) makes a copy of | |
| 776 // proto2_buffer s.t. the action works even when the original | |
| 777 // proto2_buffer has died. We ensure this behavior by deleting msg | |
| 778 // before using the action. | |
| 779 delete msg; | |
| 780 | |
| 781 FooMessage dest; | |
| 782 dest.set_int_field(0); | |
| 783 ::proto2::Message* const dest_base = &dest; | |
| 784 a.Perform(make_tuple(true, dest_base)); | |
| 785 EXPECT_EQ(2, dest.int_field()); | |
| 786 EXPECT_EQ("hi", dest.string_field()); | |
| 787 } | |
| 788 | |
| 789 #endif // GTEST_HAS_PROTOBUF_ | |
| 790 | |
| 791 // Sample functions and functors for testing Invoke() and etc. | |
| 792 int Nullary() { return 1; } | |
| 793 | |
| 794 class NullaryFunctor { | |
| 795 public: | |
| 796 int operator()() { return 2; } | |
| 797 }; | |
| 798 | |
| 799 bool g_done = false; | |
| 800 void VoidNullary() { g_done = true; } | |
| 801 | |
| 802 class VoidNullaryFunctor { | |
| 803 public: | |
| 804 void operator()() { g_done = true; } | |
| 805 }; | |
| 806 | |
| 807 bool Unary(int x) { return x < 0; } | |
| 808 | |
| 809 const char* Plus1(const char* s) { return s + 1; } | |
| 810 | |
| 811 void VoidUnary(int /* n */) { g_done = true; } | |
| 812 | |
| 813 bool ByConstRef(const std::string& s) { return s == "Hi"; } | |
| 814 | |
| 815 const double g_double = 0; | |
| 816 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; } | |
| 817 | |
| 818 std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT | |
| 819 | |
| 820 struct UnaryFunctor { | |
| 821 int operator()(bool x) { return x ? 1 : -1; } | |
| 822 }; | |
| 823 | |
| 824 const char* Binary(const char* input, short n) { return input + n; } // NOLINT | |
| 825 | |
| 826 void VoidBinary(int, char) { g_done = true; } | |
| 827 | |
| 828 int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT | |
| 829 | |
| 830 void VoidTernary(int, char, bool) { g_done = true; } | |
| 831 | |
| 832 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } | |
| 833 | |
| 834 void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } | |
| 835 | |
| 836 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } | |
| 837 | |
| 838 struct SumOf5Functor { | |
| 839 int operator()(int a, int b, int c, int d, int e) { | |
| 840 return a + b + c + d + e; | |
| 841 } | |
| 842 }; | |
| 843 | |
| 844 int SumOf6(int a, int b, int c, int d, int e, int f) { | |
| 845 return a + b + c + d + e + f; | |
| 846 } | |
| 847 | |
| 848 struct SumOf6Functor { | |
| 849 int operator()(int a, int b, int c, int d, int e, int f) { | |
| 850 return a + b + c + d + e + f; | |
| 851 } | |
| 852 }; | |
| 853 | |
| 854 class Foo { | |
| 855 public: | |
| 856 Foo() : value_(123) {} | |
| 857 | |
| 858 int Nullary() const { return value_; } | |
| 859 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT | |
| 860 std::string Binary(const std::string& str, char c) const { return str + c; } | |
| 861 int Ternary(int x, bool y, char z) { return value_ + x + y*z; } | |
| 862 int SumOf4(int a, int b, int c, int d) const { | |
| 863 return a + b + c + d + value_; | |
| 864 } | |
| 865 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } | |
| 866 int SumOf6(int a, int b, int c, int d, int e, int f) { | |
| 867 return a + b + c + d + e + f; | |
| 868 } | |
| 869 private: | |
| 870 int value_; | |
| 871 }; | |
| 872 | |
| 873 // Tests InvokeWithoutArgs(function). | |
| 874 TEST(InvokeWithoutArgsTest, Function) { | |
| 875 // As an action that takes one argument. | |
| 876 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT | |
| 877 EXPECT_EQ(1, a.Perform(make_tuple(2))); | |
| 878 | |
| 879 // As an action that takes two arguments. | |
| 880 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT | |
| 881 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5))); | |
| 882 | |
| 883 // As an action that returns void. | |
| 884 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT | |
| 885 g_done = false; | |
| 886 a3.Perform(make_tuple(1)); | |
| 887 EXPECT_TRUE(g_done); | |
| 888 } | |
| 889 | |
| 890 // Tests InvokeWithoutArgs(functor). | |
| 891 TEST(InvokeWithoutArgsTest, Functor) { | |
| 892 // As an action that takes no argument. | |
| 893 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT | |
| 894 EXPECT_EQ(2, a.Perform(make_tuple())); | |
| 895 | |
| 896 // As an action that takes three arguments. | |
| 897 Action<int(int, double, char)> a2 = // NOLINT | |
| 898 InvokeWithoutArgs(NullaryFunctor()); | |
| 899 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a'))); | |
| 900 | |
| 901 // As an action that returns void. | |
| 902 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor()); | |
| 903 g_done = false; | |
| 904 a3.Perform(make_tuple()); | |
| 905 EXPECT_TRUE(g_done); | |
| 906 } | |
| 907 | |
| 908 // Tests InvokeWithoutArgs(obj_ptr, method). | |
| 909 TEST(InvokeWithoutArgsTest, Method) { | |
| 910 Foo foo; | |
| 911 Action<int(bool, char)> a = // NOLINT | |
| 912 InvokeWithoutArgs(&foo, &Foo::Nullary); | |
| 913 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a'))); | |
| 914 } | |
| 915 | |
| 916 // Tests using IgnoreResult() on a polymorphic action. | |
| 917 TEST(IgnoreResultTest, PolymorphicAction) { | |
| 918 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT | |
| 919 a.Perform(make_tuple(1)); | |
| 920 } | |
| 921 | |
| 922 // Tests using IgnoreResult() on a monomorphic action. | |
| 923 | |
| 924 int ReturnOne() { | |
| 925 g_done = true; | |
| 926 return 1; | |
| 927 } | |
| 928 | |
| 929 TEST(IgnoreResultTest, MonomorphicAction) { | |
| 930 g_done = false; | |
| 931 Action<void()> a = IgnoreResult(Invoke(ReturnOne)); | |
| 932 a.Perform(make_tuple()); | |
| 933 EXPECT_TRUE(g_done); | |
| 934 } | |
| 935 | |
| 936 // Tests using IgnoreResult() on an action that returns a class type. | |
| 937 | |
| 938 MyClass ReturnMyClass(double /* x */) { | |
| 939 g_done = true; | |
| 940 return MyClass(); | |
| 941 } | |
| 942 | |
| 943 TEST(IgnoreResultTest, ActionReturningClass) { | |
| 944 g_done = false; | |
| 945 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT | |
| 946 a.Perform(make_tuple(2)); | |
| 947 EXPECT_TRUE(g_done); | |
| 948 } | |
| 949 | |
| 950 TEST(AssignTest, Int) { | |
| 951 int x = 0; | |
| 952 Action<void(int)> a = Assign(&x, 5); | |
| 953 a.Perform(make_tuple(0)); | |
| 954 EXPECT_EQ(5, x); | |
| 955 } | |
| 956 | |
| 957 TEST(AssignTest, String) { | |
| 958 ::std::string x; | |
| 959 Action<void(void)> a = Assign(&x, "Hello, world"); | |
| 960 a.Perform(make_tuple()); | |
| 961 EXPECT_EQ("Hello, world", x); | |
| 962 } | |
| 963 | |
| 964 TEST(AssignTest, CompatibleTypes) { | |
| 965 double x = 0; | |
| 966 Action<void(int)> a = Assign(&x, 5); | |
| 967 a.Perform(make_tuple(0)); | |
| 968 EXPECT_DOUBLE_EQ(5, x); | |
| 969 } | |
| 970 | |
| 971 #if !GTEST_OS_WINDOWS_MOBILE | |
| 972 | |
| 973 class SetErrnoAndReturnTest : public testing::Test { | |
| 974 protected: | |
| 975 virtual void SetUp() { errno = 0; } | |
| 976 virtual void TearDown() { errno = 0; } | |
| 977 }; | |
| 978 | |
| 979 TEST_F(SetErrnoAndReturnTest, Int) { | |
| 980 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5); | |
| 981 EXPECT_EQ(-5, a.Perform(make_tuple())); | |
| 982 EXPECT_EQ(ENOTTY, errno); | |
| 983 } | |
| 984 | |
| 985 TEST_F(SetErrnoAndReturnTest, Ptr) { | |
| 986 int x; | |
| 987 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x); | |
| 988 EXPECT_EQ(&x, a.Perform(make_tuple())); | |
| 989 EXPECT_EQ(ENOTTY, errno); | |
| 990 } | |
| 991 | |
| 992 TEST_F(SetErrnoAndReturnTest, CompatibleTypes) { | |
| 993 Action<double()> a = SetErrnoAndReturn(EINVAL, 5); | |
| 994 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple())); | |
| 995 EXPECT_EQ(EINVAL, errno); | |
| 996 } | |
| 997 | |
| 998 #endif // !GTEST_OS_WINDOWS_MOBILE | |
| 999 | |
| 1000 // Tests ByRef(). | |
| 1001 | |
| 1002 // Tests that ReferenceWrapper<T> is copyable. | |
| 1003 TEST(ByRefTest, IsCopyable) { | |
| 1004 const std::string s1 = "Hi"; | |
| 1005 const std::string s2 = "Hello"; | |
| 1006 | |
| 1007 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s
1); | |
| 1008 const std::string& r1 = ref_wrapper; | |
| 1009 EXPECT_EQ(&s1, &r1); | |
| 1010 | |
| 1011 // Assigns a new value to ref_wrapper. | |
| 1012 ref_wrapper = ByRef(s2); | |
| 1013 const std::string& r2 = ref_wrapper; | |
| 1014 EXPECT_EQ(&s2, &r2); | |
| 1015 | |
| 1016 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(
s1); | |
| 1017 // Copies ref_wrapper1 to ref_wrapper. | |
| 1018 ref_wrapper = ref_wrapper1; | |
| 1019 const std::string& r3 = ref_wrapper; | |
| 1020 EXPECT_EQ(&s1, &r3); | |
| 1021 } | |
| 1022 | |
| 1023 // Tests using ByRef() on a const value. | |
| 1024 TEST(ByRefTest, ConstValue) { | |
| 1025 const int n = 0; | |
| 1026 // int& ref = ByRef(n); // This shouldn't compile - we have a | |
| 1027 // negative compilation test to catch it. | |
| 1028 const int& const_ref = ByRef(n); | |
| 1029 EXPECT_EQ(&n, &const_ref); | |
| 1030 } | |
| 1031 | |
| 1032 // Tests using ByRef() on a non-const value. | |
| 1033 TEST(ByRefTest, NonConstValue) { | |
| 1034 int n = 0; | |
| 1035 | |
| 1036 // ByRef(n) can be used as either an int&, | |
| 1037 int& ref = ByRef(n); | |
| 1038 EXPECT_EQ(&n, &ref); | |
| 1039 | |
| 1040 // or a const int&. | |
| 1041 const int& const_ref = ByRef(n); | |
| 1042 EXPECT_EQ(&n, &const_ref); | |
| 1043 } | |
| 1044 | |
| 1045 // Tests explicitly specifying the type when using ByRef(). | |
| 1046 TEST(ByRefTest, ExplicitType) { | |
| 1047 int n = 0; | |
| 1048 const int& r1 = ByRef<const int>(n); | |
| 1049 EXPECT_EQ(&n, &r1); | |
| 1050 | |
| 1051 // ByRef<char>(n); // This shouldn't compile - we have a negative | |
| 1052 // compilation test to catch it. | |
| 1053 | |
| 1054 Derived d; | |
| 1055 Derived& r2 = ByRef<Derived>(d); | |
| 1056 EXPECT_EQ(&d, &r2); | |
| 1057 | |
| 1058 const Derived& r3 = ByRef<const Derived>(d); | |
| 1059 EXPECT_EQ(&d, &r3); | |
| 1060 | |
| 1061 Base& r4 = ByRef<Base>(d); | |
| 1062 EXPECT_EQ(&d, &r4); | |
| 1063 | |
| 1064 const Base& r5 = ByRef<const Base>(d); | |
| 1065 EXPECT_EQ(&d, &r5); | |
| 1066 | |
| 1067 // The following shouldn't compile - we have a negative compilation | |
| 1068 // test for it. | |
| 1069 // | |
| 1070 // Base b; | |
| 1071 // ByRef<Derived>(b); | |
| 1072 } | |
| 1073 | |
| 1074 // Tests that Google Mock prints expression ByRef(x) as a reference to x. | |
| 1075 TEST(ByRefTest, PrintsCorrectly) { | |
| 1076 int n = 42; | |
| 1077 ::std::stringstream expected, actual; | |
| 1078 testing::internal::UniversalPrinter<const int&>::Print(n, &expected); | |
| 1079 testing::internal::UniversalPrint(ByRef(n), &actual); | |
| 1080 EXPECT_EQ(expected.str(), actual.str()); | |
| 1081 } | |
| 1082 | |
| 1083 } // Unnamed namespace | |
| OLD | NEW |