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