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; |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 59 | using testing::ByRef; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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; |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 72 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame^] | 73 | #if !GTEST_OS_WINDOWS_MOBILE |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 74 | using testing::SetErrnoAndReturn; |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame^] | 75 | #endif |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 76 | |
| 77 | #if GMOCK_HAS_PROTOBUF_ |
| 78 | using testing::internal::TestMessage; |
| 79 | #endif // GMOCK_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 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 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 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 95 | // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a |
| 96 | // built-in numeric type. |
| 97 | TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) { |
| 98 | EXPECT_EQ(0, BuiltInDefaultValue<unsigned char>::Get()); |
| 99 | EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get()); |
| 100 | EXPECT_EQ(0, BuiltInDefaultValue<char>::Get()); |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 101 | #if !GTEST_OS_WINDOWS |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 102 | EXPECT_EQ(0, BuiltInDefaultValue<unsigned wchar_t>::Get()); |
| 103 | EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get()); |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 104 | #endif // !GTEST_OS_WINDOWS |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 105 | EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get()); |
| 106 | EXPECT_EQ(0, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT |
| 107 | EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT |
| 108 | EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT |
| 109 | EXPECT_EQ(0, BuiltInDefaultValue<unsigned int>::Get()); |
| 110 | EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get()); |
| 111 | EXPECT_EQ(0, BuiltInDefaultValue<int>::Get()); |
| 112 | EXPECT_EQ(0, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT |
| 113 | EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT |
| 114 | EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT |
| 115 | EXPECT_EQ(0, BuiltInDefaultValue<UInt64>::Get()); |
| 116 | EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get()); |
| 117 | EXPECT_EQ(0, BuiltInDefaultValue<float>::Get()); |
| 118 | EXPECT_EQ(0, BuiltInDefaultValue<double>::Get()); |
| 119 | } |
| 120 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 121 | // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a |
| 122 | // built-in numeric type. |
| 123 | TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) { |
| 124 | EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists()); |
| 125 | EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists()); |
| 126 | EXPECT_TRUE(BuiltInDefaultValue<char>::Exists()); |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 127 | #if !GTEST_OS_WINDOWS |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 128 | EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists()); |
| 129 | EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists()); |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 130 | #endif // !GTEST_OS_WINDOWS |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 131 | EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists()); |
| 132 | EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT |
| 133 | EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT |
| 134 | EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT |
| 135 | EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists()); |
| 136 | EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists()); |
| 137 | EXPECT_TRUE(BuiltInDefaultValue<int>::Exists()); |
| 138 | EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT |
| 139 | EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT |
| 140 | EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT |
| 141 | EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists()); |
| 142 | EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists()); |
| 143 | EXPECT_TRUE(BuiltInDefaultValue<float>::Exists()); |
| 144 | EXPECT_TRUE(BuiltInDefaultValue<double>::Exists()); |
| 145 | } |
| 146 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 147 | // Tests that BuiltInDefaultValue<bool>::Get() returns false. |
| 148 | TEST(BuiltInDefaultValueTest, IsFalseForBool) { |
| 149 | EXPECT_FALSE(BuiltInDefaultValue<bool>::Get()); |
| 150 | } |
| 151 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 152 | // Tests that BuiltInDefaultValue<bool>::Exists() returns true. |
| 153 | TEST(BuiltInDefaultValueTest, BoolExists) { |
| 154 | EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists()); |
| 155 | } |
| 156 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 157 | // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a |
| 158 | // string type. |
| 159 | TEST(BuiltInDefaultValueTest, IsEmptyStringForString) { |
| 160 | #if GTEST_HAS_GLOBAL_STRING |
| 161 | EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get()); |
| 162 | #endif // GTEST_HAS_GLOBAL_STRING |
| 163 | |
| 164 | #if GTEST_HAS_STD_STRING |
| 165 | EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get()); |
| 166 | #endif // GTEST_HAS_STD_STRING |
| 167 | } |
| 168 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 169 | // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a |
| 170 | // string type. |
| 171 | TEST(BuiltInDefaultValueTest, ExistsForString) { |
| 172 | #if GTEST_HAS_GLOBAL_STRING |
| 173 | EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists()); |
| 174 | #endif // GTEST_HAS_GLOBAL_STRING |
| 175 | |
| 176 | #if GTEST_HAS_STD_STRING |
| 177 | EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists()); |
| 178 | #endif // GTEST_HAS_STD_STRING |
| 179 | } |
| 180 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 198 | TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) { |
| 199 | EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists()); |
| 200 | } |
| 201 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 202 | // Tests that BuiltInDefaultValue<T&>::Get() aborts the program. |
| 203 | TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) { |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 204 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 205 | BuiltInDefaultValue<int&>::Get(); |
| 206 | }, ""); |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 207 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 208 | BuiltInDefaultValue<const char&>::Get(); |
| 209 | }, ""); |
| 210 | } |
| 211 | |
| 212 | TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) { |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 213 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 214 | BuiltInDefaultValue<UserType>::Get(); |
| 215 | }, ""); |
| 216 | } |
| 217 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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) { |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 226 | EXPECT_TRUE(DefaultValue<int>::Exists()); |
| 227 | EXPECT_FALSE(DefaultValue<const UserType>::Exists()); |
| 228 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 235 | EXPECT_TRUE(DefaultValue<int>::Exists()); |
| 236 | EXPECT_TRUE(DefaultValue<const UserType>::Exists()); |
| 237 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 238 | DefaultValue<int>::Clear(); |
| 239 | DefaultValue<const UserType>::Clear(); |
| 240 | |
| 241 | EXPECT_FALSE(DefaultValue<int>::IsSet()); |
| 242 | EXPECT_FALSE(DefaultValue<const UserType>::IsSet()); |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 243 | |
| 244 | EXPECT_TRUE(DefaultValue<int>::Exists()); |
| 245 | EXPECT_FALSE(DefaultValue<const UserType>::Exists()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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()); |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 253 | EXPECT_TRUE(DefaultValue<int>::Exists()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 254 | EXPECT_FALSE(DefaultValue<UserType>::IsSet()); |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 255 | EXPECT_FALSE(DefaultValue<UserType>::Exists()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 256 | |
| 257 | EXPECT_EQ(0, DefaultValue<int>::Get()); |
| 258 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 259 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 260 | DefaultValue<UserType>::Get(); |
| 261 | }, ""); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 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 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 290 | EXPECT_TRUE(DefaultValue<const int&>::Exists()); |
| 291 | EXPECT_TRUE(DefaultValue<UserType&>::Exists()); |
| 292 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 299 | EXPECT_FALSE(DefaultValue<const int&>::Exists()); |
| 300 | EXPECT_FALSE(DefaultValue<UserType&>::Exists()); |
| 301 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 313 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 314 | DefaultValue<int&>::Get(); |
| 315 | }, ""); |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 316 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 317 | DefaultValue<UserType>::Get(); |
| 318 | }, ""); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | TEST(ActionTest, CanBeConvertedToOtherActionType) { |
| 399 | const Action<bool(int)> a1(new IsNotZero); // NOLINT |
| 400 | const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT |
| 401 | EXPECT_EQ(1, a2.Perform(make_tuple('a'))); |
| 402 | EXPECT_EQ(0, a2.Perform(make_tuple('\0'))); |
| 403 | } |
| 404 | |
| 405 | // The following two classes are for testing MakePolymorphicAction(). |
| 406 | |
| 407 | // Implements a polymorphic action that returns the second of the |
| 408 | // arguments it receives. |
| 409 | class ReturnSecondArgumentAction { |
| 410 | public: |
| 411 | // We want to verify that MakePolymorphicAction() can work with a |
| 412 | // polymorphic action whose Perform() method template is either |
| 413 | // const or not. This lets us verify the non-const case. |
| 414 | template <typename Result, typename ArgumentTuple> |
| 415 | Result Perform(const ArgumentTuple& args) { return get<1>(args); } |
| 416 | }; |
| 417 | |
| 418 | // Implements a polymorphic action that can be used in a nullary |
| 419 | // function to return 0. |
| 420 | class ReturnZeroFromNullaryFunctionAction { |
| 421 | public: |
| 422 | // For testing that MakePolymorphicAction() works when the |
| 423 | // implementation class' Perform() method template takes only one |
| 424 | // template parameter. |
| 425 | // |
| 426 | // We want to verify that MakePolymorphicAction() can work with a |
| 427 | // polymorphic action whose Perform() method template is either |
| 428 | // const or not. This lets us verify the const case. |
| 429 | template <typename Result> |
| 430 | Result Perform(const tuple<>&) const { return 0; } |
| 431 | }; |
| 432 | |
| 433 | // These functions verify that MakePolymorphicAction() returns a |
| 434 | // PolymorphicAction<T> where T is the argument's type. |
| 435 | |
| 436 | PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() { |
| 437 | return MakePolymorphicAction(ReturnSecondArgumentAction()); |
| 438 | } |
| 439 | |
| 440 | PolymorphicAction<ReturnZeroFromNullaryFunctionAction> |
| 441 | ReturnZeroFromNullaryFunction() { |
| 442 | return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction()); |
| 443 | } |
| 444 | |
| 445 | // Tests that MakePolymorphicAction() turns a polymorphic action |
| 446 | // implementation class into a polymorphic action. |
| 447 | TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) { |
| 448 | Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT |
| 449 | EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0))); |
| 450 | } |
| 451 | |
| 452 | // Tests that MakePolymorphicAction() works when the implementation |
| 453 | // class' Perform() method template has only one template parameter. |
| 454 | TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) { |
| 455 | Action<int()> a1 = ReturnZeroFromNullaryFunction(); |
| 456 | EXPECT_EQ(0, a1.Perform(make_tuple())); |
| 457 | |
| 458 | Action<void*()> a2 = ReturnZeroFromNullaryFunction(); |
| 459 | EXPECT_TRUE(a2.Perform(make_tuple()) == NULL); |
| 460 | } |
| 461 | |
| 462 | // Tests that Return() works as an action for void-returning |
| 463 | // functions. |
| 464 | TEST(ReturnTest, WorksForVoid) { |
| 465 | const Action<void(int)> ret = Return(); // NOLINT |
| 466 | return ret.Perform(make_tuple(1)); |
| 467 | } |
| 468 | |
| 469 | // Tests that Return(v) returns v. |
| 470 | TEST(ReturnTest, ReturnsGivenValue) { |
| 471 | Action<int()> ret = Return(1); // NOLINT |
| 472 | EXPECT_EQ(1, ret.Perform(make_tuple())); |
| 473 | |
| 474 | ret = Return(-5); |
| 475 | EXPECT_EQ(-5, ret.Perform(make_tuple())); |
| 476 | } |
| 477 | |
| 478 | // Tests that Return("string literal") works. |
| 479 | TEST(ReturnTest, AcceptsStringLiteral) { |
| 480 | Action<const char*()> a1 = Return("Hello"); |
| 481 | EXPECT_STREQ("Hello", a1.Perform(make_tuple())); |
| 482 | |
| 483 | Action<std::string()> a2 = Return("world"); |
| 484 | EXPECT_EQ("world", a2.Perform(make_tuple())); |
| 485 | } |
| 486 | |
| 487 | // Tests that Return(v) is covaraint. |
| 488 | |
| 489 | struct Base { |
| 490 | bool operator==(const Base&) { return true; } |
| 491 | }; |
| 492 | |
| 493 | struct Derived : public Base { |
| 494 | bool operator==(const Derived&) { return true; } |
| 495 | }; |
| 496 | |
| 497 | TEST(ReturnTest, IsCovariant) { |
| 498 | Base base; |
| 499 | Derived derived; |
| 500 | Action<Base*()> ret = Return(&base); |
| 501 | EXPECT_EQ(&base, ret.Perform(make_tuple())); |
| 502 | |
| 503 | ret = Return(&derived); |
| 504 | EXPECT_EQ(&derived, ret.Perform(make_tuple())); |
| 505 | } |
| 506 | |
| 507 | // Tests that ReturnNull() returns NULL in a pointer-returning function. |
| 508 | TEST(ReturnNullTest, WorksInPointerReturningFunction) { |
| 509 | const Action<int*()> a1 = ReturnNull(); |
| 510 | EXPECT_TRUE(a1.Perform(make_tuple()) == NULL); |
| 511 | |
| 512 | const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT |
| 513 | EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL); |
| 514 | } |
| 515 | |
| 516 | // Tests that ReturnRef(v) works for reference types. |
| 517 | TEST(ReturnRefTest, WorksForReference) { |
| 518 | const int n = 0; |
| 519 | const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT |
| 520 | |
| 521 | EXPECT_EQ(&n, &ret.Perform(make_tuple(true))); |
| 522 | } |
| 523 | |
| 524 | // Tests that ReturnRef(v) is covariant. |
| 525 | TEST(ReturnRefTest, IsCovariant) { |
| 526 | Base base; |
| 527 | Derived derived; |
| 528 | Action<Base&()> a = ReturnRef(base); |
| 529 | EXPECT_EQ(&base, &a.Perform(make_tuple())); |
| 530 | |
| 531 | a = ReturnRef(derived); |
| 532 | EXPECT_EQ(&derived, &a.Perform(make_tuple())); |
| 533 | } |
| 534 | |
| 535 | // Tests that DoDefault() does the default action for the mock method. |
| 536 | |
| 537 | class MyClass {}; |
| 538 | |
| 539 | class MockClass { |
| 540 | public: |
| 541 | MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT |
| 542 | MOCK_METHOD0(Foo, MyClass()); |
| 543 | }; |
| 544 | |
| 545 | // Tests that DoDefault() returns the built-in default value for the |
| 546 | // return type by default. |
| 547 | TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) { |
| 548 | MockClass mock; |
| 549 | EXPECT_CALL(mock, IntFunc(_)) |
| 550 | .WillOnce(DoDefault()); |
| 551 | EXPECT_EQ(0, mock.IntFunc(true)); |
| 552 | } |
| 553 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 554 | // Tests that DoDefault() aborts the process when there is no built-in |
| 555 | // default value for the return type. |
| 556 | TEST(DoDefaultDeathTest, DiesForUnknowType) { |
| 557 | MockClass mock; |
| 558 | EXPECT_CALL(mock, Foo()) |
| 559 | .WillRepeatedly(DoDefault()); |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 560 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 561 | mock.Foo(); |
| 562 | }, ""); |
| 563 | } |
| 564 | |
| 565 | // Tests that using DoDefault() inside a composite action leads to a |
| 566 | // run-time error. |
| 567 | |
| 568 | void VoidFunc(bool flag) {} |
| 569 | |
| 570 | TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) { |
| 571 | MockClass mock; |
| 572 | EXPECT_CALL(mock, IntFunc(_)) |
| 573 | .WillRepeatedly(DoAll(Invoke(VoidFunc), |
| 574 | DoDefault())); |
| 575 | |
| 576 | // Ideally we should verify the error message as well. Sadly, |
| 577 | // EXPECT_DEATH() can only capture stderr, while Google Mock's |
| 578 | // errors are printed on stdout. Therefore we have to settle for |
| 579 | // not verifying the message. |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 580 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 581 | mock.IntFunc(true); |
| 582 | }, ""); |
| 583 | } |
| 584 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 585 | // Tests that DoDefault() returns the default value set by |
| 586 | // DefaultValue<T>::Set() when it's not overriden by an ON_CALL(). |
| 587 | TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) { |
| 588 | DefaultValue<int>::Set(1); |
| 589 | MockClass mock; |
| 590 | EXPECT_CALL(mock, IntFunc(_)) |
| 591 | .WillOnce(DoDefault()); |
| 592 | EXPECT_EQ(1, mock.IntFunc(false)); |
| 593 | DefaultValue<int>::Clear(); |
| 594 | } |
| 595 | |
| 596 | // Tests that DoDefault() does the action specified by ON_CALL(). |
| 597 | TEST(DoDefaultTest, DoesWhatOnCallSpecifies) { |
| 598 | MockClass mock; |
| 599 | ON_CALL(mock, IntFunc(_)) |
| 600 | .WillByDefault(Return(2)); |
| 601 | EXPECT_CALL(mock, IntFunc(_)) |
| 602 | .WillOnce(DoDefault()); |
| 603 | EXPECT_EQ(2, mock.IntFunc(false)); |
| 604 | } |
| 605 | |
| 606 | // Tests that using DoDefault() in ON_CALL() leads to a run-time failure. |
| 607 | TEST(DoDefaultTest, CannotBeUsedInOnCall) { |
| 608 | MockClass mock; |
| 609 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 610 | ON_CALL(mock, IntFunc(_)) |
| 611 | .WillByDefault(DoDefault()); |
| 612 | }, "DoDefault() cannot be used in ON_CALL()"); |
| 613 | } |
| 614 | |
| 615 | // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by |
| 616 | // the N-th (0-based) argument to v. |
| 617 | TEST(SetArgumentPointeeTest, SetsTheNthPointee) { |
| 618 | typedef void MyFunction(bool, int*, char*); |
| 619 | Action<MyFunction> a = SetArgumentPointee<1>(2); |
| 620 | |
| 621 | int n = 0; |
| 622 | char ch = '\0'; |
| 623 | a.Perform(make_tuple(true, &n, &ch)); |
| 624 | EXPECT_EQ(2, n); |
| 625 | EXPECT_EQ('\0', ch); |
| 626 | |
| 627 | a = SetArgumentPointee<2>('a'); |
| 628 | n = 0; |
| 629 | ch = '\0'; |
| 630 | a.Perform(make_tuple(true, &n, &ch)); |
| 631 | EXPECT_EQ(0, n); |
| 632 | EXPECT_EQ('a', ch); |
| 633 | } |
| 634 | |
| 635 | #if GMOCK_HAS_PROTOBUF_ |
| 636 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 637 | // Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf |
| 638 | // variable pointed to by the N-th (0-based) argument to proto_buffer. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 639 | TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 640 | TestMessage* const msg = new TestMessage; |
| 641 | msg->set_member("yes"); |
| 642 | TestMessage orig_msg; |
| 643 | orig_msg.CopyFrom(*msg); |
| 644 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 645 | Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 646 | // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer |
| 647 | // s.t. the action works even when the original proto_buffer has |
| 648 | // died. We ensure this behavior by deleting msg before using the |
| 649 | // action. |
| 650 | delete msg; |
| 651 | |
| 652 | TestMessage dest; |
| 653 | EXPECT_FALSE(orig_msg.Equals(dest)); |
| 654 | a.Perform(make_tuple(true, &dest)); |
| 655 | EXPECT_TRUE(orig_msg.Equals(dest)); |
| 656 | } |
| 657 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 658 | // Tests that SetArgumentPointee<N>(proto_buffer) sets the |
| 659 | // ::ProtocolMessage variable pointed to by the N-th (0-based) |
| 660 | // argument to proto_buffer. |
| 661 | TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) { |
| 662 | TestMessage* const msg = new TestMessage; |
| 663 | msg->set_member("yes"); |
| 664 | TestMessage orig_msg; |
| 665 | orig_msg.CopyFrom(*msg); |
| 666 | |
| 667 | Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg); |
| 668 | // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer |
| 669 | // s.t. the action works even when the original proto_buffer has |
| 670 | // died. We ensure this behavior by deleting msg before using the |
| 671 | // action. |
| 672 | delete msg; |
| 673 | |
| 674 | TestMessage dest; |
| 675 | ::ProtocolMessage* const dest_base = &dest; |
| 676 | EXPECT_FALSE(orig_msg.Equals(dest)); |
| 677 | a.Perform(make_tuple(true, dest_base)); |
| 678 | EXPECT_TRUE(orig_msg.Equals(dest)); |
| 679 | } |
| 680 | |
| 681 | // Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2 |
| 682 | // protobuf variable pointed to by the N-th (0-based) argument to |
| 683 | // proto2_buffer. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 684 | TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) { |
| 685 | using testing::internal::FooMessage; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 686 | FooMessage* const msg = new FooMessage; |
| 687 | msg->set_int_field(2); |
| 688 | msg->set_string_field("hi"); |
| 689 | FooMessage orig_msg; |
| 690 | orig_msg.CopyFrom(*msg); |
| 691 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 692 | Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 693 | // SetArgumentPointee<N>(proto2_buffer) makes a copy of |
| 694 | // proto2_buffer s.t. the action works even when the original |
| 695 | // proto2_buffer has died. We ensure this behavior by deleting msg |
| 696 | // before using the action. |
| 697 | delete msg; |
| 698 | |
| 699 | FooMessage dest; |
| 700 | dest.set_int_field(0); |
| 701 | a.Perform(make_tuple(true, &dest)); |
| 702 | EXPECT_EQ(2, dest.int_field()); |
| 703 | EXPECT_EQ("hi", dest.string_field()); |
| 704 | } |
| 705 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 706 | // Tests that SetArgumentPointee<N>(proto2_buffer) sets the |
| 707 | // proto2::Message variable pointed to by the N-th (0-based) argument |
| 708 | // to proto2_buffer. |
| 709 | TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) { |
| 710 | using testing::internal::FooMessage; |
| 711 | FooMessage* const msg = new FooMessage; |
| 712 | msg->set_int_field(2); |
| 713 | msg->set_string_field("hi"); |
| 714 | FooMessage orig_msg; |
| 715 | orig_msg.CopyFrom(*msg); |
| 716 | |
| 717 | Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg); |
| 718 | // SetArgumentPointee<N>(proto2_buffer) makes a copy of |
| 719 | // proto2_buffer s.t. the action works even when the original |
| 720 | // proto2_buffer has died. We ensure this behavior by deleting msg |
| 721 | // before using the action. |
| 722 | delete msg; |
| 723 | |
| 724 | FooMessage dest; |
| 725 | dest.set_int_field(0); |
| 726 | ::proto2::Message* const dest_base = &dest; |
| 727 | a.Perform(make_tuple(true, dest_base)); |
| 728 | EXPECT_EQ(2, dest.int_field()); |
| 729 | EXPECT_EQ("hi", dest.string_field()); |
| 730 | } |
| 731 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 732 | #endif // GMOCK_HAS_PROTOBUF_ |
| 733 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 734 | // Sample functions and functors for testing Invoke() and etc. |
| 735 | int Nullary() { return 1; } |
| 736 | |
| 737 | class NullaryFunctor { |
| 738 | public: |
| 739 | int operator()() { return 2; } |
| 740 | }; |
| 741 | |
| 742 | bool g_done = false; |
| 743 | void VoidNullary() { g_done = true; } |
| 744 | |
| 745 | class VoidNullaryFunctor { |
| 746 | public: |
| 747 | void operator()() { g_done = true; } |
| 748 | }; |
| 749 | |
| 750 | bool Unary(int x) { return x < 0; } |
| 751 | |
| 752 | const char* Plus1(const char* s) { return s + 1; } |
| 753 | |
| 754 | void VoidUnary(int n) { g_done = true; } |
| 755 | |
| 756 | bool ByConstRef(const std::string& s) { return s == "Hi"; } |
| 757 | |
| 758 | const double g_double = 0; |
| 759 | bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; } |
| 760 | |
| 761 | std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT |
| 762 | |
| 763 | struct UnaryFunctor { |
| 764 | int operator()(bool x) { return x ? 1 : -1; } |
| 765 | }; |
| 766 | |
| 767 | const char* Binary(const char* input, short n) { return input + n; } // NOLINT |
| 768 | |
| 769 | void VoidBinary(int, char) { g_done = true; } |
| 770 | |
| 771 | int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT |
| 772 | |
| 773 | void VoidTernary(int, char, bool) { g_done = true; } |
| 774 | |
| 775 | int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } |
| 776 | |
| 777 | void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } |
| 778 | |
| 779 | int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } |
| 780 | |
| 781 | struct SumOf5Functor { |
| 782 | int operator()(int a, int b, int c, int d, int e) { |
| 783 | return a + b + c + d + e; |
| 784 | } |
| 785 | }; |
| 786 | |
| 787 | int SumOf6(int a, int b, int c, int d, int e, int f) { |
| 788 | return a + b + c + d + e + f; |
| 789 | } |
| 790 | |
| 791 | struct SumOf6Functor { |
| 792 | int operator()(int a, int b, int c, int d, int e, int f) { |
| 793 | return a + b + c + d + e + f; |
| 794 | } |
| 795 | }; |
| 796 | |
| 797 | class Foo { |
| 798 | public: |
| 799 | Foo() : value_(123) {} |
| 800 | |
| 801 | int Nullary() const { return value_; } |
| 802 | short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT |
| 803 | std::string Binary(const std::string& str, char c) const { return str + c; } |
| 804 | int Ternary(int x, bool y, char z) { return value_ + x + y*z; } |
| 805 | int SumOf4(int a, int b, int c, int d) const { |
| 806 | return a + b + c + d + value_; |
| 807 | } |
| 808 | int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } |
| 809 | int SumOf6(int a, int b, int c, int d, int e, int f) { |
| 810 | return a + b + c + d + e + f; |
| 811 | } |
| 812 | private: |
| 813 | int value_; |
| 814 | }; |
| 815 | |
| 816 | // Tests InvokeWithoutArgs(function). |
| 817 | TEST(InvokeWithoutArgsTest, Function) { |
| 818 | // As an action that takes one argument. |
| 819 | Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT |
| 820 | EXPECT_EQ(1, a.Perform(make_tuple(2))); |
| 821 | |
| 822 | // As an action that takes two arguments. |
| 823 | Action<short(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT |
| 824 | EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5))); |
| 825 | |
| 826 | // As an action that returns void. |
| 827 | Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT |
| 828 | g_done = false; |
| 829 | a3.Perform(make_tuple(1)); |
| 830 | EXPECT_TRUE(g_done); |
| 831 | } |
| 832 | |
| 833 | // Tests InvokeWithoutArgs(functor). |
| 834 | TEST(InvokeWithoutArgsTest, Functor) { |
| 835 | // As an action that takes no argument. |
| 836 | Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT |
| 837 | EXPECT_EQ(2, a.Perform(make_tuple())); |
| 838 | |
| 839 | // As an action that takes three arguments. |
| 840 | Action<short(int, double, char)> a2 = // NOLINT |
| 841 | InvokeWithoutArgs(NullaryFunctor()); |
| 842 | EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a'))); |
| 843 | |
| 844 | // As an action that returns void. |
| 845 | Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor()); |
| 846 | g_done = false; |
| 847 | a3.Perform(make_tuple()); |
| 848 | EXPECT_TRUE(g_done); |
| 849 | } |
| 850 | |
| 851 | // Tests InvokeWithoutArgs(obj_ptr, method). |
| 852 | TEST(InvokeWithoutArgsTest, Method) { |
| 853 | Foo foo; |
| 854 | Action<int(bool, char)> a = // NOLINT |
| 855 | InvokeWithoutArgs(&foo, &Foo::Nullary); |
| 856 | EXPECT_EQ(123, a.Perform(make_tuple(true, 'a'))); |
| 857 | } |
| 858 | |
| 859 | // Tests using IgnoreResult() on a polymorphic action. |
| 860 | TEST(IgnoreResultTest, PolymorphicAction) { |
| 861 | Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT |
| 862 | a.Perform(make_tuple(1)); |
| 863 | } |
| 864 | |
| 865 | // Tests using IgnoreResult() on a monomorphic action. |
| 866 | |
| 867 | int ReturnOne() { |
| 868 | g_done = true; |
| 869 | return 1; |
| 870 | } |
| 871 | |
| 872 | TEST(IgnoreResultTest, MonomorphicAction) { |
| 873 | g_done = false; |
| 874 | Action<void()> a = IgnoreResult(Invoke(ReturnOne)); |
| 875 | a.Perform(make_tuple()); |
| 876 | EXPECT_TRUE(g_done); |
| 877 | } |
| 878 | |
| 879 | // Tests using IgnoreResult() on an action that returns a class type. |
| 880 | |
| 881 | MyClass ReturnMyClass(double x) { |
| 882 | g_done = true; |
| 883 | return MyClass(); |
| 884 | } |
| 885 | |
| 886 | TEST(IgnoreResultTest, ActionReturningClass) { |
| 887 | g_done = false; |
| 888 | Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT |
| 889 | a.Perform(make_tuple(2)); |
| 890 | EXPECT_TRUE(g_done); |
| 891 | } |
| 892 | |
| 893 | TEST(AssignTest, Int) { |
| 894 | int x = 0; |
| 895 | Action<void(int)> a = Assign(&x, 5); |
| 896 | a.Perform(make_tuple(0)); |
| 897 | EXPECT_EQ(5, x); |
| 898 | } |
| 899 | |
| 900 | TEST(AssignTest, String) { |
| 901 | ::std::string x; |
| 902 | Action<void(void)> a = Assign(&x, "Hello, world"); |
| 903 | a.Perform(make_tuple()); |
| 904 | EXPECT_EQ("Hello, world", x); |
| 905 | } |
| 906 | |
| 907 | TEST(AssignTest, CompatibleTypes) { |
| 908 | double x = 0; |
| 909 | Action<void(int)> a = Assign(&x, 5); |
| 910 | a.Perform(make_tuple(0)); |
| 911 | EXPECT_DOUBLE_EQ(5, x); |
| 912 | } |
| 913 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame^] | 914 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 915 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 916 | class SetErrnoAndReturnTest : public testing::Test { |
| 917 | protected: |
| 918 | virtual void SetUp() { errno = 0; } |
| 919 | virtual void TearDown() { errno = 0; } |
| 920 | }; |
| 921 | |
| 922 | TEST_F(SetErrnoAndReturnTest, Int) { |
| 923 | Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5); |
| 924 | EXPECT_EQ(-5, a.Perform(make_tuple())); |
| 925 | EXPECT_EQ(ENOTTY, errno); |
| 926 | } |
| 927 | |
| 928 | TEST_F(SetErrnoAndReturnTest, Ptr) { |
| 929 | int x; |
| 930 | Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x); |
| 931 | EXPECT_EQ(&x, a.Perform(make_tuple())); |
| 932 | EXPECT_EQ(ENOTTY, errno); |
| 933 | } |
| 934 | |
| 935 | TEST_F(SetErrnoAndReturnTest, CompatibleTypes) { |
| 936 | Action<double()> a = SetErrnoAndReturn(EINVAL, 5); |
| 937 | EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple())); |
| 938 | EXPECT_EQ(EINVAL, errno); |
| 939 | } |
| 940 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame^] | 941 | #endif // !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 942 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 943 | // Tests ByRef(). |
| 944 | |
| 945 | // Tests that ReferenceWrapper<T> is copyable. |
| 946 | TEST(ByRefTest, IsCopyable) { |
| 947 | const std::string s1 = "Hi"; |
| 948 | const std::string s2 = "Hello"; |
| 949 | |
| 950 | ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s1); |
| 951 | const std::string& r1 = ref_wrapper; |
| 952 | EXPECT_EQ(&s1, &r1); |
| 953 | |
| 954 | // Assigns a new value to ref_wrapper. |
| 955 | ref_wrapper = ByRef(s2); |
| 956 | const std::string& r2 = ref_wrapper; |
| 957 | EXPECT_EQ(&s2, &r2); |
| 958 | |
| 959 | ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1); |
| 960 | // Copies ref_wrapper1 to ref_wrapper. |
| 961 | ref_wrapper = ref_wrapper1; |
| 962 | const std::string& r3 = ref_wrapper; |
| 963 | EXPECT_EQ(&s1, &r3); |
| 964 | } |
| 965 | |
| 966 | // Tests using ByRef() on a const value. |
| 967 | TEST(ByRefTest, ConstValue) { |
| 968 | const int n = 0; |
| 969 | // int& ref = ByRef(n); // This shouldn't compile - we have a |
| 970 | // negative compilation test to catch it. |
| 971 | const int& const_ref = ByRef(n); |
| 972 | EXPECT_EQ(&n, &const_ref); |
| 973 | } |
| 974 | |
| 975 | // Tests using ByRef() on a non-const value. |
| 976 | TEST(ByRefTest, NonConstValue) { |
| 977 | int n = 0; |
| 978 | |
| 979 | // ByRef(n) can be used as either an int&, |
| 980 | int& ref = ByRef(n); |
| 981 | EXPECT_EQ(&n, &ref); |
| 982 | |
| 983 | // or a const int&. |
| 984 | const int& const_ref = ByRef(n); |
| 985 | EXPECT_EQ(&n, &const_ref); |
| 986 | } |
| 987 | |
| 988 | // Tests explicitly specifying the type when using ByRef(). |
| 989 | TEST(ByRefTest, ExplicitType) { |
| 990 | int n = 0; |
| 991 | const int& r1 = ByRef<const int>(n); |
| 992 | EXPECT_EQ(&n, &r1); |
| 993 | |
| 994 | // ByRef<char>(n); // This shouldn't compile - we have a negative |
| 995 | // compilation test to catch it. |
| 996 | |
| 997 | Derived d; |
| 998 | Derived& r2 = ByRef<Derived>(d); |
| 999 | EXPECT_EQ(&d, &r2); |
| 1000 | |
| 1001 | const Derived& r3 = ByRef<const Derived>(d); |
| 1002 | EXPECT_EQ(&d, &r3); |
| 1003 | |
| 1004 | Base& r4 = ByRef<Base>(d); |
| 1005 | EXPECT_EQ(&d, &r4); |
| 1006 | |
| 1007 | const Base& r5 = ByRef<const Base>(d); |
| 1008 | EXPECT_EQ(&d, &r5); |
| 1009 | |
| 1010 | // The following shouldn't compile - we have a negative compilation |
| 1011 | // test for it. |
| 1012 | // |
| 1013 | // Base b; |
| 1014 | // ByRef<Derived>(b); |
| 1015 | } |
| 1016 | |
| 1017 | // Tests that Google Mock prints expression ByRef(x) as a reference to x. |
| 1018 | TEST(ByRefTest, PrintsCorrectly) { |
| 1019 | int n = 42; |
| 1020 | ::std::stringstream expected, actual; |
| 1021 | testing::internal::UniversalPrinter<const int&>::Print(n, &expected); |
| 1022 | testing::internal::UniversalPrint(ByRef(n), &actual); |
| 1023 | EXPECT_EQ(expected.str(), actual.str()); |
| 1024 | } |
| 1025 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1026 | } // Unnamed namespace |