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