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 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 36 | #include "gmock/gmock-actions.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 37 | #include <algorithm> |
| 38 | #include <iterator> |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame^] | 39 | #include <memory> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 40 | #include <string> |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 41 | #include "gmock/gmock.h" |
| 42 | #include "gmock/internal/gmock-port.h" |
| 43 | #include "gtest/gtest.h" |
| 44 | #include "gtest/gtest-spi.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 45 | |
| 46 | namespace { |
| 47 | |
| 48 | using ::std::tr1::get; |
| 49 | using ::std::tr1::make_tuple; |
| 50 | using ::std::tr1::tuple; |
| 51 | using ::std::tr1::tuple_element; |
| 52 | using testing::internal::BuiltInDefaultValue; |
| 53 | using testing::internal::Int64; |
| 54 | using testing::internal::UInt64; |
| 55 | // This list should be kept sorted. |
| 56 | using testing::_; |
| 57 | using testing::Action; |
| 58 | using testing::ActionInterface; |
| 59 | using testing::Assign; |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 60 | using testing::ByRef; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 61 | using testing::DefaultValue; |
| 62 | using testing::DoDefault; |
| 63 | using testing::IgnoreResult; |
| 64 | using testing::Invoke; |
| 65 | using testing::InvokeWithoutArgs; |
| 66 | using testing::MakePolymorphicAction; |
| 67 | using testing::Ne; |
| 68 | using testing::PolymorphicAction; |
| 69 | using testing::Return; |
| 70 | using testing::ReturnNull; |
| 71 | using testing::ReturnRef; |
zhanyong.wan | e3bd098 | 2010-07-03 00:16:42 +0000 | [diff] [blame] | 72 | using testing::ReturnRefOfCopy; |
zhanyong.wan | 5921483 | 2010-10-05 05:58:51 +0000 | [diff] [blame] | 73 | using testing::SetArgPointee; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 74 | using testing::SetArgumentPointee; |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 75 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 76 | #if !GTEST_OS_WINDOWS_MOBILE |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 77 | using testing::SetErrnoAndReturn; |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 78 | #endif |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 79 | |
zhanyong.wan | 02f7106 | 2010-05-10 17:14:29 +0000 | [diff] [blame] | 80 | #if GTEST_HAS_PROTOBUF_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 81 | using testing::internal::TestMessage; |
zhanyong.wan | 02f7106 | 2010-05-10 17:14:29 +0000 | [diff] [blame] | 82 | #endif // GTEST_HAS_PROTOBUF_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 83 | |
| 84 | // Tests that BuiltInDefaultValue<T*>::Get() returns NULL. |
| 85 | TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) { |
| 86 | EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL); |
| 87 | EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL); |
| 88 | EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL); |
| 89 | } |
| 90 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 91 | // Tests that BuiltInDefaultValue<T*>::Exists() return true. |
| 92 | TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) { |
| 93 | EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists()); |
| 94 | EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists()); |
| 95 | EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists()); |
| 96 | } |
| 97 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 98 | // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a |
| 99 | // built-in numeric type. |
| 100 | TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) { |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 101 | EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 102 | EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get()); |
| 103 | EXPECT_EQ(0, BuiltInDefaultValue<char>::Get()); |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 104 | #if GMOCK_HAS_SIGNED_WCHAR_T_ |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 105 | EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 106 | EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get()); |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 107 | #endif |
| 108 | #if GMOCK_WCHAR_T_IS_NATIVE_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 109 | EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get()); |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 110 | #endif |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 111 | EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 112 | EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT |
| 113 | EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 114 | EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 115 | EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get()); |
| 116 | EXPECT_EQ(0, BuiltInDefaultValue<int>::Get()); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 117 | EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 118 | EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT |
| 119 | EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 120 | EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 121 | EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get()); |
| 122 | EXPECT_EQ(0, BuiltInDefaultValue<float>::Get()); |
| 123 | EXPECT_EQ(0, BuiltInDefaultValue<double>::Get()); |
| 124 | } |
| 125 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 126 | // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a |
| 127 | // built-in numeric type. |
| 128 | TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) { |
| 129 | EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists()); |
| 130 | EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists()); |
| 131 | EXPECT_TRUE(BuiltInDefaultValue<char>::Exists()); |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 132 | #if GMOCK_HAS_SIGNED_WCHAR_T_ |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 133 | EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists()); |
| 134 | EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists()); |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 135 | #endif |
| 136 | #if GMOCK_WCHAR_T_IS_NATIVE_ |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 137 | EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists()); |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 138 | #endif |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 139 | EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT |
| 140 | EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT |
| 141 | EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT |
| 142 | EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists()); |
| 143 | EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists()); |
| 144 | EXPECT_TRUE(BuiltInDefaultValue<int>::Exists()); |
| 145 | EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT |
| 146 | EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT |
| 147 | EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT |
| 148 | EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists()); |
| 149 | EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists()); |
| 150 | EXPECT_TRUE(BuiltInDefaultValue<float>::Exists()); |
| 151 | EXPECT_TRUE(BuiltInDefaultValue<double>::Exists()); |
| 152 | } |
| 153 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 154 | // Tests that BuiltInDefaultValue<bool>::Get() returns false. |
| 155 | TEST(BuiltInDefaultValueTest, IsFalseForBool) { |
| 156 | EXPECT_FALSE(BuiltInDefaultValue<bool>::Get()); |
| 157 | } |
| 158 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 159 | // Tests that BuiltInDefaultValue<bool>::Exists() returns true. |
| 160 | TEST(BuiltInDefaultValueTest, BoolExists) { |
| 161 | EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists()); |
| 162 | } |
| 163 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 164 | // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a |
| 165 | // string type. |
| 166 | TEST(BuiltInDefaultValueTest, IsEmptyStringForString) { |
| 167 | #if GTEST_HAS_GLOBAL_STRING |
| 168 | EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get()); |
| 169 | #endif // GTEST_HAS_GLOBAL_STRING |
| 170 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 171 | EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 172 | } |
| 173 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 174 | // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a |
| 175 | // string type. |
| 176 | TEST(BuiltInDefaultValueTest, ExistsForString) { |
| 177 | #if GTEST_HAS_GLOBAL_STRING |
| 178 | EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists()); |
| 179 | #endif // GTEST_HAS_GLOBAL_STRING |
| 180 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 181 | EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists()); |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 182 | } |
| 183 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 184 | // Tests that BuiltInDefaultValue<const T>::Get() returns the same |
| 185 | // value as BuiltInDefaultValue<T>::Get() does. |
| 186 | TEST(BuiltInDefaultValueTest, WorksForConstTypes) { |
| 187 | EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get()); |
| 188 | EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get()); |
| 189 | EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL); |
| 190 | EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get()); |
| 191 | } |
| 192 | |
| 193 | // Tests that BuiltInDefaultValue<T>::Get() aborts the program with |
| 194 | // the correct error message when T is a user-defined type. |
| 195 | struct UserType { |
| 196 | UserType() : value(0) {} |
| 197 | |
| 198 | int value; |
| 199 | }; |
| 200 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 201 | TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) { |
| 202 | EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists()); |
| 203 | } |
| 204 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 205 | // Tests that BuiltInDefaultValue<T&>::Get() aborts the program. |
| 206 | TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) { |
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<int&>::Get(); |
| 209 | }, ""); |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 210 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 211 | BuiltInDefaultValue<const char&>::Get(); |
| 212 | }, ""); |
| 213 | } |
| 214 | |
| 215 | TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) { |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 216 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 217 | BuiltInDefaultValue<UserType>::Get(); |
| 218 | }, ""); |
| 219 | } |
| 220 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 221 | // Tests that DefaultValue<T>::IsSet() is false initially. |
| 222 | TEST(DefaultValueTest, IsInitiallyUnset) { |
| 223 | EXPECT_FALSE(DefaultValue<int>::IsSet()); |
| 224 | EXPECT_FALSE(DefaultValue<const UserType>::IsSet()); |
| 225 | } |
| 226 | |
| 227 | // Tests that DefaultValue<T> can be set and then unset. |
| 228 | TEST(DefaultValueTest, CanBeSetAndUnset) { |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 229 | EXPECT_TRUE(DefaultValue<int>::Exists()); |
| 230 | EXPECT_FALSE(DefaultValue<const UserType>::Exists()); |
| 231 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 232 | DefaultValue<int>::Set(1); |
| 233 | DefaultValue<const UserType>::Set(UserType()); |
| 234 | |
| 235 | EXPECT_EQ(1, DefaultValue<int>::Get()); |
| 236 | EXPECT_EQ(0, DefaultValue<const UserType>::Get().value); |
| 237 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 238 | EXPECT_TRUE(DefaultValue<int>::Exists()); |
| 239 | EXPECT_TRUE(DefaultValue<const UserType>::Exists()); |
| 240 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 241 | DefaultValue<int>::Clear(); |
| 242 | DefaultValue<const UserType>::Clear(); |
| 243 | |
| 244 | EXPECT_FALSE(DefaultValue<int>::IsSet()); |
| 245 | EXPECT_FALSE(DefaultValue<const UserType>::IsSet()); |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 246 | |
| 247 | EXPECT_TRUE(DefaultValue<int>::Exists()); |
| 248 | EXPECT_FALSE(DefaultValue<const UserType>::Exists()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // Tests that DefaultValue<T>::Get() returns the |
| 252 | // BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is |
| 253 | // false. |
| 254 | TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { |
| 255 | EXPECT_FALSE(DefaultValue<int>::IsSet()); |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 256 | EXPECT_TRUE(DefaultValue<int>::Exists()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 257 | EXPECT_FALSE(DefaultValue<UserType>::IsSet()); |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 258 | EXPECT_FALSE(DefaultValue<UserType>::Exists()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 259 | |
| 260 | EXPECT_EQ(0, DefaultValue<int>::Get()); |
| 261 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 262 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 263 | DefaultValue<UserType>::Get(); |
| 264 | }, ""); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 265 | } |
| 266 | |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame^] | 267 | #if GTEST_LANG_CXX11 |
| 268 | TEST(DefaultValueDeathTest, GetWorksForMoveOnlyIfSet) { |
| 269 | EXPECT_FALSE(DefaultValue<std::unique_ptr<int>>::Exists()); |
| 270 | EXPECT_DEATH_IF_SUPPORTED({ |
| 271 | DefaultValue<std::unique_ptr<int>>::Get(); |
| 272 | }, ""); |
| 273 | DefaultValue<std::unique_ptr<int>>::SetFactory([] { |
| 274 | return std::unique_ptr<int>(new int(42)); |
| 275 | }); |
| 276 | EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists()); |
| 277 | std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get(); |
| 278 | EXPECT_EQ(42, *i); |
| 279 | } |
| 280 | #endif // GTEST_LANG_CXX11 |
| 281 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 282 | // Tests that DefaultValue<void>::Get() returns void. |
| 283 | TEST(DefaultValueTest, GetWorksForVoid) { |
| 284 | return DefaultValue<void>::Get(); |
| 285 | } |
| 286 | |
| 287 | // Tests using DefaultValue with a reference type. |
| 288 | |
| 289 | // Tests that DefaultValue<T&>::IsSet() is false initially. |
| 290 | TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) { |
| 291 | EXPECT_FALSE(DefaultValue<int&>::IsSet()); |
| 292 | EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); |
| 293 | } |
| 294 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 295 | // Tests that DefaultValue<T&>::Exists is false initiallly. |
| 296 | TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) { |
| 297 | EXPECT_FALSE(DefaultValue<int&>::Exists()); |
| 298 | EXPECT_FALSE(DefaultValue<UserType&>::Exists()); |
| 299 | } |
| 300 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 301 | // Tests that DefaultValue<T&> can be set and then unset. |
| 302 | TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) { |
| 303 | int n = 1; |
| 304 | DefaultValue<const int&>::Set(n); |
| 305 | UserType u; |
| 306 | DefaultValue<UserType&>::Set(u); |
| 307 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 308 | EXPECT_TRUE(DefaultValue<const int&>::Exists()); |
| 309 | EXPECT_TRUE(DefaultValue<UserType&>::Exists()); |
| 310 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 311 | EXPECT_EQ(&n, &(DefaultValue<const int&>::Get())); |
| 312 | EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get())); |
| 313 | |
| 314 | DefaultValue<const int&>::Clear(); |
| 315 | DefaultValue<UserType&>::Clear(); |
| 316 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 317 | EXPECT_FALSE(DefaultValue<const int&>::Exists()); |
| 318 | EXPECT_FALSE(DefaultValue<UserType&>::Exists()); |
| 319 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 320 | EXPECT_FALSE(DefaultValue<const int&>::IsSet()); |
| 321 | EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); |
| 322 | } |
| 323 | |
| 324 | // Tests that DefaultValue<T&>::Get() returns the |
| 325 | // BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is |
| 326 | // false. |
| 327 | TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { |
| 328 | EXPECT_FALSE(DefaultValue<int&>::IsSet()); |
| 329 | EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); |
| 330 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 331 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 332 | DefaultValue<int&>::Get(); |
| 333 | }, ""); |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 334 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 335 | DefaultValue<UserType>::Get(); |
| 336 | }, ""); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | // Tests that ActionInterface can be implemented by defining the |
| 340 | // Perform method. |
| 341 | |
zhanyong.wan | a1a98f8 | 2013-03-01 21:28:40 +0000 | [diff] [blame] | 342 | typedef int MyGlobalFunction(bool, int); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 343 | |
zhanyong.wan | a1a98f8 | 2013-03-01 21:28:40 +0000 | [diff] [blame] | 344 | class MyActionImpl : public ActionInterface<MyGlobalFunction> { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 345 | public: |
| 346 | virtual int Perform(const tuple<bool, int>& args) { |
| 347 | return get<0>(args) ? get<1>(args) : 0; |
| 348 | } |
| 349 | }; |
| 350 | |
| 351 | TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) { |
| 352 | MyActionImpl my_action_impl; |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 353 | (void)my_action_impl; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | TEST(ActionInterfaceTest, MakeAction) { |
zhanyong.wan | a1a98f8 | 2013-03-01 21:28:40 +0000 | [diff] [blame] | 357 | Action<MyGlobalFunction> action = MakeAction(new MyActionImpl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 358 | |
| 359 | // When exercising the Perform() method of Action<F>, we must pass |
| 360 | // it a tuple whose size and type are compatible with F's argument |
| 361 | // types. For example, if F is int(), then Perform() takes a |
| 362 | // 0-tuple; if F is void(bool, int), then Perform() takes a |
| 363 | // tuple<bool, int>, and so on. |
| 364 | EXPECT_EQ(5, action.Perform(make_tuple(true, 5))); |
| 365 | } |
| 366 | |
| 367 | // Tests that Action<F> can be contructed from a pointer to |
| 368 | // ActionInterface<F>. |
| 369 | TEST(ActionTest, CanBeConstructedFromActionInterface) { |
zhanyong.wan | a1a98f8 | 2013-03-01 21:28:40 +0000 | [diff] [blame] | 370 | Action<MyGlobalFunction> action(new MyActionImpl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | // Tests that Action<F> delegates actual work to ActionInterface<F>. |
| 374 | TEST(ActionTest, DelegatesWorkToActionInterface) { |
zhanyong.wan | a1a98f8 | 2013-03-01 21:28:40 +0000 | [diff] [blame] | 375 | const Action<MyGlobalFunction> action(new MyActionImpl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 376 | |
| 377 | EXPECT_EQ(5, action.Perform(make_tuple(true, 5))); |
| 378 | EXPECT_EQ(0, action.Perform(make_tuple(false, 1))); |
| 379 | } |
| 380 | |
| 381 | // Tests that Action<F> can be copied. |
| 382 | TEST(ActionTest, IsCopyable) { |
zhanyong.wan | a1a98f8 | 2013-03-01 21:28:40 +0000 | [diff] [blame] | 383 | Action<MyGlobalFunction> a1(new MyActionImpl); |
| 384 | Action<MyGlobalFunction> a2(a1); // Tests the copy constructor. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 385 | |
| 386 | // a1 should continue to work after being copied from. |
| 387 | EXPECT_EQ(5, a1.Perform(make_tuple(true, 5))); |
| 388 | EXPECT_EQ(0, a1.Perform(make_tuple(false, 1))); |
| 389 | |
| 390 | // a2 should work like the action it was copied from. |
| 391 | EXPECT_EQ(5, a2.Perform(make_tuple(true, 5))); |
| 392 | EXPECT_EQ(0, a2.Perform(make_tuple(false, 1))); |
| 393 | |
| 394 | a2 = a1; // Tests the assignment operator. |
| 395 | |
| 396 | // a1 should continue to work after being copied from. |
| 397 | EXPECT_EQ(5, a1.Perform(make_tuple(true, 5))); |
| 398 | EXPECT_EQ(0, a1.Perform(make_tuple(false, 1))); |
| 399 | |
| 400 | // a2 should work like the action it was copied from. |
| 401 | EXPECT_EQ(5, a2.Perform(make_tuple(true, 5))); |
| 402 | EXPECT_EQ(0, a2.Perform(make_tuple(false, 1))); |
| 403 | } |
| 404 | |
| 405 | // Tests that an Action<From> object can be converted to a |
| 406 | // compatible Action<To> object. |
| 407 | |
| 408 | class IsNotZero : public ActionInterface<bool(int)> { // NOLINT |
| 409 | public: |
| 410 | virtual bool Perform(const tuple<int>& arg) { |
| 411 | return get<0>(arg) != 0; |
| 412 | } |
| 413 | }; |
| 414 | |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 415 | #if !GTEST_OS_SYMBIAN |
| 416 | // Compiling this test on Nokia's Symbian compiler fails with: |
| 417 | // 'Result' is not a member of class 'testing::internal::Function<int>' |
| 418 | // (point of instantiation: '@unnamed@gmock_actions_test_cc@:: |
| 419 | // ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()') |
| 420 | // with no obvious fix. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 421 | TEST(ActionTest, CanBeConvertedToOtherActionType) { |
| 422 | const Action<bool(int)> a1(new IsNotZero); // NOLINT |
| 423 | const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT |
| 424 | EXPECT_EQ(1, a2.Perform(make_tuple('a'))); |
| 425 | EXPECT_EQ(0, a2.Perform(make_tuple('\0'))); |
| 426 | } |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 427 | #endif // !GTEST_OS_SYMBIAN |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 428 | |
| 429 | // The following two classes are for testing MakePolymorphicAction(). |
| 430 | |
| 431 | // Implements a polymorphic action that returns the second of the |
| 432 | // arguments it receives. |
| 433 | class ReturnSecondArgumentAction { |
| 434 | public: |
| 435 | // We want to verify that MakePolymorphicAction() can work with a |
| 436 | // polymorphic action whose Perform() method template is either |
| 437 | // const or not. This lets us verify the non-const case. |
| 438 | template <typename Result, typename ArgumentTuple> |
| 439 | Result Perform(const ArgumentTuple& args) { return get<1>(args); } |
| 440 | }; |
| 441 | |
| 442 | // Implements a polymorphic action that can be used in a nullary |
| 443 | // function to return 0. |
| 444 | class ReturnZeroFromNullaryFunctionAction { |
| 445 | public: |
| 446 | // For testing that MakePolymorphicAction() works when the |
| 447 | // implementation class' Perform() method template takes only one |
| 448 | // template parameter. |
| 449 | // |
| 450 | // We want to verify that MakePolymorphicAction() can work with a |
| 451 | // polymorphic action whose Perform() method template is either |
| 452 | // const or not. This lets us verify the const case. |
| 453 | template <typename Result> |
| 454 | Result Perform(const tuple<>&) const { return 0; } |
| 455 | }; |
| 456 | |
| 457 | // These functions verify that MakePolymorphicAction() returns a |
| 458 | // PolymorphicAction<T> where T is the argument's type. |
| 459 | |
| 460 | PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() { |
| 461 | return MakePolymorphicAction(ReturnSecondArgumentAction()); |
| 462 | } |
| 463 | |
| 464 | PolymorphicAction<ReturnZeroFromNullaryFunctionAction> |
| 465 | ReturnZeroFromNullaryFunction() { |
| 466 | return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction()); |
| 467 | } |
| 468 | |
| 469 | // Tests that MakePolymorphicAction() turns a polymorphic action |
| 470 | // implementation class into a polymorphic action. |
| 471 | TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) { |
| 472 | Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT |
| 473 | EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0))); |
| 474 | } |
| 475 | |
| 476 | // Tests that MakePolymorphicAction() works when the implementation |
| 477 | // class' Perform() method template has only one template parameter. |
| 478 | TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) { |
| 479 | Action<int()> a1 = ReturnZeroFromNullaryFunction(); |
| 480 | EXPECT_EQ(0, a1.Perform(make_tuple())); |
| 481 | |
| 482 | Action<void*()> a2 = ReturnZeroFromNullaryFunction(); |
| 483 | EXPECT_TRUE(a2.Perform(make_tuple()) == NULL); |
| 484 | } |
| 485 | |
| 486 | // Tests that Return() works as an action for void-returning |
| 487 | // functions. |
| 488 | TEST(ReturnTest, WorksForVoid) { |
| 489 | const Action<void(int)> ret = Return(); // NOLINT |
| 490 | return ret.Perform(make_tuple(1)); |
| 491 | } |
| 492 | |
| 493 | // Tests that Return(v) returns v. |
| 494 | TEST(ReturnTest, ReturnsGivenValue) { |
| 495 | Action<int()> ret = Return(1); // NOLINT |
| 496 | EXPECT_EQ(1, ret.Perform(make_tuple())); |
| 497 | |
| 498 | ret = Return(-5); |
| 499 | EXPECT_EQ(-5, ret.Perform(make_tuple())); |
| 500 | } |
| 501 | |
| 502 | // Tests that Return("string literal") works. |
| 503 | TEST(ReturnTest, AcceptsStringLiteral) { |
| 504 | Action<const char*()> a1 = Return("Hello"); |
| 505 | EXPECT_STREQ("Hello", a1.Perform(make_tuple())); |
| 506 | |
| 507 | Action<std::string()> a2 = Return("world"); |
| 508 | EXPECT_EQ("world", a2.Perform(make_tuple())); |
| 509 | } |
| 510 | |
| 511 | // Tests that Return(v) is covaraint. |
| 512 | |
| 513 | struct Base { |
| 514 | bool operator==(const Base&) { return true; } |
| 515 | }; |
| 516 | |
| 517 | struct Derived : public Base { |
| 518 | bool operator==(const Derived&) { return true; } |
| 519 | }; |
| 520 | |
| 521 | TEST(ReturnTest, IsCovariant) { |
| 522 | Base base; |
| 523 | Derived derived; |
| 524 | Action<Base*()> ret = Return(&base); |
| 525 | EXPECT_EQ(&base, ret.Perform(make_tuple())); |
| 526 | |
| 527 | ret = Return(&derived); |
| 528 | EXPECT_EQ(&derived, ret.Perform(make_tuple())); |
| 529 | } |
| 530 | |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 531 | // Tests that the type of the value passed into Return is converted into T |
| 532 | // when the action is cast to Action<T(...)> rather than when the action is |
| 533 | // performed. See comments on testing::internal::ReturnAction in |
| 534 | // gmock-actions.h for more information. |
| 535 | class FromType { |
| 536 | public: |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 537 | explicit FromType(bool* is_converted) : converted_(is_converted) {} |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 538 | bool* converted() const { return converted_; } |
| 539 | |
| 540 | private: |
| 541 | bool* const converted_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 542 | |
| 543 | GTEST_DISALLOW_ASSIGN_(FromType); |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 544 | }; |
| 545 | |
| 546 | class ToType { |
| 547 | public: |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 548 | // Must allow implicit conversion due to use in ImplicitCast_<T>. |
| 549 | ToType(const FromType& x) { *x.converted() = true; } // NOLINT |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | TEST(ReturnTest, ConvertsArgumentWhenConverted) { |
| 553 | bool converted = false; |
| 554 | FromType x(&converted); |
| 555 | Action<ToType()> action(Return(x)); |
| 556 | EXPECT_TRUE(converted) << "Return must convert its argument in its own " |
| 557 | << "conversion operator."; |
| 558 | converted = false; |
| 559 | action.Perform(tuple<>()); |
| 560 | EXPECT_FALSE(converted) << "Action must NOT convert its argument " |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 561 | << "when performed."; |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 562 | } |
| 563 | |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 564 | class DestinationType {}; |
| 565 | |
| 566 | class SourceType { |
| 567 | public: |
| 568 | // Note: a non-const typecast operator. |
| 569 | operator DestinationType() { return DestinationType(); } |
| 570 | }; |
| 571 | |
| 572 | TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) { |
| 573 | SourceType s; |
| 574 | Action<DestinationType()> action(Return(s)); |
| 575 | } |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 576 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 577 | // Tests that ReturnNull() returns NULL in a pointer-returning function. |
| 578 | TEST(ReturnNullTest, WorksInPointerReturningFunction) { |
| 579 | const Action<int*()> a1 = ReturnNull(); |
| 580 | EXPECT_TRUE(a1.Perform(make_tuple()) == NULL); |
| 581 | |
| 582 | const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT |
| 583 | EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL); |
| 584 | } |
| 585 | |
| 586 | // Tests that ReturnRef(v) works for reference types. |
| 587 | TEST(ReturnRefTest, WorksForReference) { |
| 588 | const int n = 0; |
| 589 | const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT |
| 590 | |
| 591 | EXPECT_EQ(&n, &ret.Perform(make_tuple(true))); |
| 592 | } |
| 593 | |
| 594 | // Tests that ReturnRef(v) is covariant. |
| 595 | TEST(ReturnRefTest, IsCovariant) { |
| 596 | Base base; |
| 597 | Derived derived; |
| 598 | Action<Base&()> a = ReturnRef(base); |
| 599 | EXPECT_EQ(&base, &a.Perform(make_tuple())); |
| 600 | |
| 601 | a = ReturnRef(derived); |
| 602 | EXPECT_EQ(&derived, &a.Perform(make_tuple())); |
| 603 | } |
| 604 | |
zhanyong.wan | e3bd098 | 2010-07-03 00:16:42 +0000 | [diff] [blame] | 605 | // Tests that ReturnRefOfCopy(v) works for reference types. |
| 606 | TEST(ReturnRefOfCopyTest, WorksForReference) { |
| 607 | int n = 42; |
| 608 | const Action<const int&()> ret = ReturnRefOfCopy(n); |
| 609 | |
| 610 | EXPECT_NE(&n, &ret.Perform(make_tuple())); |
| 611 | EXPECT_EQ(42, ret.Perform(make_tuple())); |
| 612 | |
| 613 | n = 43; |
| 614 | EXPECT_NE(&n, &ret.Perform(make_tuple())); |
| 615 | EXPECT_EQ(42, ret.Perform(make_tuple())); |
| 616 | } |
| 617 | |
| 618 | // Tests that ReturnRefOfCopy(v) is covariant. |
| 619 | TEST(ReturnRefOfCopyTest, IsCovariant) { |
| 620 | Base base; |
| 621 | Derived derived; |
| 622 | Action<Base&()> a = ReturnRefOfCopy(base); |
| 623 | EXPECT_NE(&base, &a.Perform(make_tuple())); |
| 624 | |
| 625 | a = ReturnRefOfCopy(derived); |
| 626 | EXPECT_NE(&derived, &a.Perform(make_tuple())); |
| 627 | } |
| 628 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 629 | // Tests that DoDefault() does the default action for the mock method. |
| 630 | |
| 631 | class MyClass {}; |
| 632 | |
| 633 | class MockClass { |
| 634 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 635 | MockClass() {} |
| 636 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 637 | MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT |
| 638 | MOCK_METHOD0(Foo, MyClass()); |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame^] | 639 | #if GTEST_LANG_CXX11 |
| 640 | MOCK_METHOD0(MakeUnique, std::unique_ptr<int>()); |
| 641 | MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>()); |
| 642 | #endif |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 643 | |
| 644 | private: |
| 645 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 646 | }; |
| 647 | |
| 648 | // Tests that DoDefault() returns the built-in default value for the |
| 649 | // return type by default. |
| 650 | TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) { |
| 651 | MockClass mock; |
| 652 | EXPECT_CALL(mock, IntFunc(_)) |
| 653 | .WillOnce(DoDefault()); |
| 654 | EXPECT_EQ(0, mock.IntFunc(true)); |
| 655 | } |
| 656 | |
zhanyong.wan | edd4ab4 | 2013-02-28 22:58:51 +0000 | [diff] [blame] | 657 | // Tests that DoDefault() throws (when exceptions are enabled) or aborts |
| 658 | // the process when there is no built-in default value for the return type. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 659 | TEST(DoDefaultDeathTest, DiesForUnknowType) { |
| 660 | MockClass mock; |
| 661 | EXPECT_CALL(mock, Foo()) |
| 662 | .WillRepeatedly(DoDefault()); |
zhanyong.wan | edd4ab4 | 2013-02-28 22:58:51 +0000 | [diff] [blame] | 663 | #if GTEST_HAS_EXCEPTIONS |
| 664 | EXPECT_ANY_THROW(mock.Foo()); |
| 665 | #else |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 666 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 667 | mock.Foo(); |
| 668 | }, ""); |
zhanyong.wan | edd4ab4 | 2013-02-28 22:58:51 +0000 | [diff] [blame] | 669 | #endif |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | // Tests that using DoDefault() inside a composite action leads to a |
| 673 | // run-time error. |
| 674 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 675 | void VoidFunc(bool /* flag */) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 676 | |
| 677 | TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) { |
| 678 | MockClass mock; |
| 679 | EXPECT_CALL(mock, IntFunc(_)) |
| 680 | .WillRepeatedly(DoAll(Invoke(VoidFunc), |
| 681 | DoDefault())); |
| 682 | |
| 683 | // Ideally we should verify the error message as well. Sadly, |
| 684 | // EXPECT_DEATH() can only capture stderr, while Google Mock's |
| 685 | // errors are printed on stdout. Therefore we have to settle for |
| 686 | // not verifying the message. |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 687 | EXPECT_DEATH_IF_SUPPORTED({ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 688 | mock.IntFunc(true); |
| 689 | }, ""); |
| 690 | } |
| 691 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 692 | // Tests that DoDefault() returns the default value set by |
| 693 | // DefaultValue<T>::Set() when it's not overriden by an ON_CALL(). |
| 694 | TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) { |
| 695 | DefaultValue<int>::Set(1); |
| 696 | MockClass mock; |
| 697 | EXPECT_CALL(mock, IntFunc(_)) |
| 698 | .WillOnce(DoDefault()); |
| 699 | EXPECT_EQ(1, mock.IntFunc(false)); |
| 700 | DefaultValue<int>::Clear(); |
| 701 | } |
| 702 | |
| 703 | // Tests that DoDefault() does the action specified by ON_CALL(). |
| 704 | TEST(DoDefaultTest, DoesWhatOnCallSpecifies) { |
| 705 | MockClass mock; |
| 706 | ON_CALL(mock, IntFunc(_)) |
| 707 | .WillByDefault(Return(2)); |
| 708 | EXPECT_CALL(mock, IntFunc(_)) |
| 709 | .WillOnce(DoDefault()); |
| 710 | EXPECT_EQ(2, mock.IntFunc(false)); |
| 711 | } |
| 712 | |
| 713 | // Tests that using DoDefault() in ON_CALL() leads to a run-time failure. |
| 714 | TEST(DoDefaultTest, CannotBeUsedInOnCall) { |
| 715 | MockClass mock; |
| 716 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 717 | ON_CALL(mock, IntFunc(_)) |
| 718 | .WillByDefault(DoDefault()); |
| 719 | }, "DoDefault() cannot be used in ON_CALL()"); |
| 720 | } |
| 721 | |
zhanyong.wan | 5921483 | 2010-10-05 05:58:51 +0000 | [diff] [blame] | 722 | // Tests that SetArgPointee<N>(v) sets the variable pointed to by |
| 723 | // the N-th (0-based) argument to v. |
| 724 | TEST(SetArgPointeeTest, SetsTheNthPointee) { |
| 725 | typedef void MyFunction(bool, int*, char*); |
| 726 | Action<MyFunction> a = SetArgPointee<1>(2); |
| 727 | |
| 728 | int n = 0; |
| 729 | char ch = '\0'; |
| 730 | a.Perform(make_tuple(true, &n, &ch)); |
| 731 | EXPECT_EQ(2, n); |
| 732 | EXPECT_EQ('\0', ch); |
| 733 | |
| 734 | a = SetArgPointee<2>('a'); |
| 735 | n = 0; |
| 736 | ch = '\0'; |
| 737 | a.Perform(make_tuple(true, &n, &ch)); |
| 738 | EXPECT_EQ(0, n); |
| 739 | EXPECT_EQ('a', ch); |
| 740 | } |
| 741 | |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 742 | #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN) |
zhanyong.wan | a684b5a | 2010-12-02 23:30:50 +0000 | [diff] [blame] | 743 | // Tests that SetArgPointee<N>() accepts a string literal. |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 744 | // GCC prior to v4.0 and the Symbian compiler do not support this. |
zhanyong.wan | a684b5a | 2010-12-02 23:30:50 +0000 | [diff] [blame] | 745 | TEST(SetArgPointeeTest, AcceptsStringLiteral) { |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 746 | typedef void MyFunction(std::string*, const char**); |
| 747 | Action<MyFunction> a = SetArgPointee<0>("hi"); |
zhanyong.wan | a684b5a | 2010-12-02 23:30:50 +0000 | [diff] [blame] | 748 | std::string str; |
| 749 | const char* ptr = NULL; |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 750 | a.Perform(make_tuple(&str, &ptr)); |
zhanyong.wan | a684b5a | 2010-12-02 23:30:50 +0000 | [diff] [blame] | 751 | EXPECT_EQ("hi", str); |
| 752 | EXPECT_TRUE(ptr == NULL); |
| 753 | |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 754 | a = SetArgPointee<1>("world"); |
zhanyong.wan | a684b5a | 2010-12-02 23:30:50 +0000 | [diff] [blame] | 755 | str = ""; |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 756 | a.Perform(make_tuple(&str, &ptr)); |
zhanyong.wan | a684b5a | 2010-12-02 23:30:50 +0000 | [diff] [blame] | 757 | EXPECT_EQ("", str); |
| 758 | EXPECT_STREQ("world", ptr); |
| 759 | } |
| 760 | |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 761 | TEST(SetArgPointeeTest, AcceptsWideStringLiteral) { |
| 762 | typedef void MyFunction(const wchar_t**); |
| 763 | Action<MyFunction> a = SetArgPointee<0>(L"world"); |
| 764 | const wchar_t* ptr = NULL; |
| 765 | a.Perform(make_tuple(&ptr)); |
| 766 | EXPECT_STREQ(L"world", ptr); |
| 767 | |
| 768 | # if GTEST_HAS_STD_WSTRING |
| 769 | |
| 770 | typedef void MyStringFunction(std::wstring*); |
| 771 | Action<MyStringFunction> a2 = SetArgPointee<0>(L"world"); |
| 772 | std::wstring str = L""; |
| 773 | a2.Perform(make_tuple(&str)); |
| 774 | EXPECT_EQ(L"world", str); |
| 775 | |
| 776 | # endif |
| 777 | } |
| 778 | #endif |
| 779 | |
zhanyong.wan | a684b5a | 2010-12-02 23:30:50 +0000 | [diff] [blame] | 780 | // Tests that SetArgPointee<N>() accepts a char pointer. |
| 781 | TEST(SetArgPointeeTest, AcceptsCharPointer) { |
| 782 | typedef void MyFunction(bool, std::string*, const char**); |
| 783 | const char* const hi = "hi"; |
| 784 | Action<MyFunction> a = SetArgPointee<1>(hi); |
| 785 | std::string str; |
| 786 | const char* ptr = NULL; |
| 787 | a.Perform(make_tuple(true, &str, &ptr)); |
| 788 | EXPECT_EQ("hi", str); |
| 789 | EXPECT_TRUE(ptr == NULL); |
| 790 | |
| 791 | char world_array[] = "world"; |
| 792 | char* const world = world_array; |
| 793 | a = SetArgPointee<2>(world); |
| 794 | str = ""; |
| 795 | a.Perform(make_tuple(true, &str, &ptr)); |
| 796 | EXPECT_EQ("", str); |
| 797 | EXPECT_EQ(world, ptr); |
| 798 | } |
| 799 | |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 800 | TEST(SetArgPointeeTest, AcceptsWideCharPointer) { |
| 801 | typedef void MyFunction(bool, const wchar_t**); |
| 802 | const wchar_t* const hi = L"hi"; |
| 803 | Action<MyFunction> a = SetArgPointee<1>(hi); |
| 804 | const wchar_t* ptr = NULL; |
| 805 | a.Perform(make_tuple(true, &ptr)); |
| 806 | EXPECT_EQ(hi, ptr); |
| 807 | |
| 808 | # if GTEST_HAS_STD_WSTRING |
| 809 | |
| 810 | typedef void MyStringFunction(bool, std::wstring*); |
| 811 | wchar_t world_array[] = L"world"; |
| 812 | wchar_t* const world = world_array; |
| 813 | Action<MyStringFunction> a2 = SetArgPointee<1>(world); |
| 814 | std::wstring str; |
| 815 | a2.Perform(make_tuple(true, &str)); |
| 816 | EXPECT_EQ(world_array, str); |
| 817 | # endif |
| 818 | } |
| 819 | |
zhanyong.wan | 5921483 | 2010-10-05 05:58:51 +0000 | [diff] [blame] | 820 | #if GTEST_HAS_PROTOBUF_ |
| 821 | |
| 822 | // Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf |
| 823 | // variable pointed to by the N-th (0-based) argument to proto_buffer. |
| 824 | TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) { |
| 825 | TestMessage* const msg = new TestMessage; |
| 826 | msg->set_member("yes"); |
| 827 | TestMessage orig_msg; |
| 828 | orig_msg.CopyFrom(*msg); |
| 829 | |
| 830 | Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg); |
| 831 | // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer |
| 832 | // s.t. the action works even when the original proto_buffer has |
| 833 | // died. We ensure this behavior by deleting msg before using the |
| 834 | // action. |
| 835 | delete msg; |
| 836 | |
| 837 | TestMessage dest; |
| 838 | EXPECT_FALSE(orig_msg.Equals(dest)); |
| 839 | a.Perform(make_tuple(true, &dest)); |
| 840 | EXPECT_TRUE(orig_msg.Equals(dest)); |
| 841 | } |
| 842 | |
| 843 | // Tests that SetArgPointee<N>(proto_buffer) sets the |
| 844 | // ::ProtocolMessage variable pointed to by the N-th (0-based) |
| 845 | // argument to proto_buffer. |
| 846 | TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) { |
| 847 | TestMessage* const msg = new TestMessage; |
| 848 | msg->set_member("yes"); |
| 849 | TestMessage orig_msg; |
| 850 | orig_msg.CopyFrom(*msg); |
| 851 | |
| 852 | Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg); |
| 853 | // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer |
| 854 | // s.t. the action works even when the original proto_buffer has |
| 855 | // died. We ensure this behavior by deleting msg before using the |
| 856 | // action. |
| 857 | delete msg; |
| 858 | |
| 859 | TestMessage dest; |
| 860 | ::ProtocolMessage* const dest_base = &dest; |
| 861 | EXPECT_FALSE(orig_msg.Equals(dest)); |
| 862 | a.Perform(make_tuple(true, dest_base)); |
| 863 | EXPECT_TRUE(orig_msg.Equals(dest)); |
| 864 | } |
| 865 | |
| 866 | // Tests that SetArgPointee<N>(proto2_buffer) sets the v2 |
| 867 | // protobuf variable pointed to by the N-th (0-based) argument to |
| 868 | // proto2_buffer. |
| 869 | TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) { |
| 870 | using testing::internal::FooMessage; |
| 871 | FooMessage* const msg = new FooMessage; |
| 872 | msg->set_int_field(2); |
| 873 | msg->set_string_field("hi"); |
| 874 | FooMessage orig_msg; |
| 875 | orig_msg.CopyFrom(*msg); |
| 876 | |
| 877 | Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg); |
| 878 | // SetArgPointee<N>(proto2_buffer) makes a copy of |
| 879 | // proto2_buffer s.t. the action works even when the original |
| 880 | // proto2_buffer has died. We ensure this behavior by deleting msg |
| 881 | // before using the action. |
| 882 | delete msg; |
| 883 | |
| 884 | FooMessage dest; |
| 885 | dest.set_int_field(0); |
| 886 | a.Perform(make_tuple(true, &dest)); |
| 887 | EXPECT_EQ(2, dest.int_field()); |
| 888 | EXPECT_EQ("hi", dest.string_field()); |
| 889 | } |
| 890 | |
| 891 | // Tests that SetArgPointee<N>(proto2_buffer) sets the |
| 892 | // proto2::Message variable pointed to by the N-th (0-based) argument |
| 893 | // to proto2_buffer. |
| 894 | TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) { |
| 895 | using testing::internal::FooMessage; |
| 896 | FooMessage* const msg = new FooMessage; |
| 897 | msg->set_int_field(2); |
| 898 | msg->set_string_field("hi"); |
| 899 | FooMessage orig_msg; |
| 900 | orig_msg.CopyFrom(*msg); |
| 901 | |
| 902 | Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg); |
| 903 | // SetArgPointee<N>(proto2_buffer) makes a copy of |
| 904 | // proto2_buffer s.t. the action works even when the original |
| 905 | // proto2_buffer has died. We ensure this behavior by deleting msg |
| 906 | // before using the action. |
| 907 | delete msg; |
| 908 | |
| 909 | FooMessage dest; |
| 910 | dest.set_int_field(0); |
| 911 | ::proto2::Message* const dest_base = &dest; |
| 912 | a.Perform(make_tuple(true, dest_base)); |
| 913 | EXPECT_EQ(2, dest.int_field()); |
| 914 | EXPECT_EQ("hi", dest.string_field()); |
| 915 | } |
| 916 | |
| 917 | #endif // GTEST_HAS_PROTOBUF_ |
| 918 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 919 | // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by |
| 920 | // the N-th (0-based) argument to v. |
| 921 | TEST(SetArgumentPointeeTest, SetsTheNthPointee) { |
| 922 | typedef void MyFunction(bool, int*, char*); |
| 923 | Action<MyFunction> a = SetArgumentPointee<1>(2); |
| 924 | |
| 925 | int n = 0; |
| 926 | char ch = '\0'; |
| 927 | a.Perform(make_tuple(true, &n, &ch)); |
| 928 | EXPECT_EQ(2, n); |
| 929 | EXPECT_EQ('\0', ch); |
| 930 | |
| 931 | a = SetArgumentPointee<2>('a'); |
| 932 | n = 0; |
| 933 | ch = '\0'; |
| 934 | a.Perform(make_tuple(true, &n, &ch)); |
| 935 | EXPECT_EQ(0, n); |
| 936 | EXPECT_EQ('a', ch); |
| 937 | } |
| 938 | |
zhanyong.wan | 02f7106 | 2010-05-10 17:14:29 +0000 | [diff] [blame] | 939 | #if GTEST_HAS_PROTOBUF_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 940 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 941 | // Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf |
| 942 | // variable pointed to by the N-th (0-based) argument to proto_buffer. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 943 | TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 944 | TestMessage* const msg = new TestMessage; |
| 945 | msg->set_member("yes"); |
| 946 | TestMessage orig_msg; |
| 947 | orig_msg.CopyFrom(*msg); |
| 948 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 949 | Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 950 | // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer |
| 951 | // s.t. the action works even when the original proto_buffer has |
| 952 | // died. We ensure this behavior by deleting msg before using the |
| 953 | // action. |
| 954 | delete msg; |
| 955 | |
| 956 | TestMessage dest; |
| 957 | EXPECT_FALSE(orig_msg.Equals(dest)); |
| 958 | a.Perform(make_tuple(true, &dest)); |
| 959 | EXPECT_TRUE(orig_msg.Equals(dest)); |
| 960 | } |
| 961 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 962 | // Tests that SetArgumentPointee<N>(proto_buffer) sets the |
| 963 | // ::ProtocolMessage variable pointed to by the N-th (0-based) |
| 964 | // argument to proto_buffer. |
| 965 | TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) { |
| 966 | TestMessage* const msg = new TestMessage; |
| 967 | msg->set_member("yes"); |
| 968 | TestMessage orig_msg; |
| 969 | orig_msg.CopyFrom(*msg); |
| 970 | |
| 971 | Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg); |
| 972 | // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer |
| 973 | // s.t. the action works even when the original proto_buffer has |
| 974 | // died. We ensure this behavior by deleting msg before using the |
| 975 | // action. |
| 976 | delete msg; |
| 977 | |
| 978 | TestMessage dest; |
| 979 | ::ProtocolMessage* const dest_base = &dest; |
| 980 | EXPECT_FALSE(orig_msg.Equals(dest)); |
| 981 | a.Perform(make_tuple(true, dest_base)); |
| 982 | EXPECT_TRUE(orig_msg.Equals(dest)); |
| 983 | } |
| 984 | |
| 985 | // Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2 |
| 986 | // protobuf variable pointed to by the N-th (0-based) argument to |
| 987 | // proto2_buffer. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 988 | TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) { |
| 989 | using testing::internal::FooMessage; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 990 | FooMessage* const msg = new FooMessage; |
| 991 | msg->set_int_field(2); |
| 992 | msg->set_string_field("hi"); |
| 993 | FooMessage orig_msg; |
| 994 | orig_msg.CopyFrom(*msg); |
| 995 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 996 | Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 997 | // SetArgumentPointee<N>(proto2_buffer) makes a copy of |
| 998 | // proto2_buffer s.t. the action works even when the original |
| 999 | // proto2_buffer has died. We ensure this behavior by deleting msg |
| 1000 | // before using the action. |
| 1001 | delete msg; |
| 1002 | |
| 1003 | FooMessage dest; |
| 1004 | dest.set_int_field(0); |
| 1005 | a.Perform(make_tuple(true, &dest)); |
| 1006 | EXPECT_EQ(2, dest.int_field()); |
| 1007 | EXPECT_EQ("hi", dest.string_field()); |
| 1008 | } |
| 1009 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 1010 | // Tests that SetArgumentPointee<N>(proto2_buffer) sets the |
| 1011 | // proto2::Message variable pointed to by the N-th (0-based) argument |
| 1012 | // to proto2_buffer. |
| 1013 | TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) { |
| 1014 | using testing::internal::FooMessage; |
| 1015 | FooMessage* const msg = new FooMessage; |
| 1016 | msg->set_int_field(2); |
| 1017 | msg->set_string_field("hi"); |
| 1018 | FooMessage orig_msg; |
| 1019 | orig_msg.CopyFrom(*msg); |
| 1020 | |
| 1021 | Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg); |
| 1022 | // SetArgumentPointee<N>(proto2_buffer) makes a copy of |
| 1023 | // proto2_buffer s.t. the action works even when the original |
| 1024 | // proto2_buffer has died. We ensure this behavior by deleting msg |
| 1025 | // before using the action. |
| 1026 | delete msg; |
| 1027 | |
| 1028 | FooMessage dest; |
| 1029 | dest.set_int_field(0); |
| 1030 | ::proto2::Message* const dest_base = &dest; |
| 1031 | a.Perform(make_tuple(true, dest_base)); |
| 1032 | EXPECT_EQ(2, dest.int_field()); |
| 1033 | EXPECT_EQ("hi", dest.string_field()); |
| 1034 | } |
| 1035 | |
zhanyong.wan | 02f7106 | 2010-05-10 17:14:29 +0000 | [diff] [blame] | 1036 | #endif // GTEST_HAS_PROTOBUF_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1037 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1038 | // Sample functions and functors for testing Invoke() and etc. |
| 1039 | int Nullary() { return 1; } |
| 1040 | |
| 1041 | class NullaryFunctor { |
| 1042 | public: |
| 1043 | int operator()() { return 2; } |
| 1044 | }; |
| 1045 | |
| 1046 | bool g_done = false; |
| 1047 | void VoidNullary() { g_done = true; } |
| 1048 | |
| 1049 | class VoidNullaryFunctor { |
| 1050 | public: |
| 1051 | void operator()() { g_done = true; } |
| 1052 | }; |
| 1053 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1054 | class Foo { |
| 1055 | public: |
| 1056 | Foo() : value_(123) {} |
| 1057 | |
| 1058 | int Nullary() const { return value_; } |
zhanyong.wan | 29be923 | 2013-03-01 06:53:35 +0000 | [diff] [blame] | 1059 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1060 | private: |
| 1061 | int value_; |
| 1062 | }; |
| 1063 | |
| 1064 | // Tests InvokeWithoutArgs(function). |
| 1065 | TEST(InvokeWithoutArgsTest, Function) { |
| 1066 | // As an action that takes one argument. |
| 1067 | Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT |
| 1068 | EXPECT_EQ(1, a.Perform(make_tuple(2))); |
| 1069 | |
| 1070 | // As an action that takes two arguments. |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1071 | Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1072 | EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5))); |
| 1073 | |
| 1074 | // As an action that returns void. |
| 1075 | Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT |
| 1076 | g_done = false; |
| 1077 | a3.Perform(make_tuple(1)); |
| 1078 | EXPECT_TRUE(g_done); |
| 1079 | } |
| 1080 | |
| 1081 | // Tests InvokeWithoutArgs(functor). |
| 1082 | TEST(InvokeWithoutArgsTest, Functor) { |
| 1083 | // As an action that takes no argument. |
| 1084 | Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT |
| 1085 | EXPECT_EQ(2, a.Perform(make_tuple())); |
| 1086 | |
| 1087 | // As an action that takes three arguments. |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1088 | Action<int(int, double, char)> a2 = // NOLINT |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1089 | InvokeWithoutArgs(NullaryFunctor()); |
| 1090 | EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a'))); |
| 1091 | |
| 1092 | // As an action that returns void. |
| 1093 | Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor()); |
| 1094 | g_done = false; |
| 1095 | a3.Perform(make_tuple()); |
| 1096 | EXPECT_TRUE(g_done); |
| 1097 | } |
| 1098 | |
| 1099 | // Tests InvokeWithoutArgs(obj_ptr, method). |
| 1100 | TEST(InvokeWithoutArgsTest, Method) { |
| 1101 | Foo foo; |
| 1102 | Action<int(bool, char)> a = // NOLINT |
| 1103 | InvokeWithoutArgs(&foo, &Foo::Nullary); |
| 1104 | EXPECT_EQ(123, a.Perform(make_tuple(true, 'a'))); |
| 1105 | } |
| 1106 | |
| 1107 | // Tests using IgnoreResult() on a polymorphic action. |
| 1108 | TEST(IgnoreResultTest, PolymorphicAction) { |
| 1109 | Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT |
| 1110 | a.Perform(make_tuple(1)); |
| 1111 | } |
| 1112 | |
| 1113 | // Tests using IgnoreResult() on a monomorphic action. |
| 1114 | |
| 1115 | int ReturnOne() { |
| 1116 | g_done = true; |
| 1117 | return 1; |
| 1118 | } |
| 1119 | |
| 1120 | TEST(IgnoreResultTest, MonomorphicAction) { |
| 1121 | g_done = false; |
| 1122 | Action<void()> a = IgnoreResult(Invoke(ReturnOne)); |
| 1123 | a.Perform(make_tuple()); |
| 1124 | EXPECT_TRUE(g_done); |
| 1125 | } |
| 1126 | |
| 1127 | // Tests using IgnoreResult() on an action that returns a class type. |
| 1128 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1129 | MyClass ReturnMyClass(double /* x */) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1130 | g_done = true; |
| 1131 | return MyClass(); |
| 1132 | } |
| 1133 | |
| 1134 | TEST(IgnoreResultTest, ActionReturningClass) { |
| 1135 | g_done = false; |
| 1136 | Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT |
| 1137 | a.Perform(make_tuple(2)); |
| 1138 | EXPECT_TRUE(g_done); |
| 1139 | } |
| 1140 | |
| 1141 | TEST(AssignTest, Int) { |
| 1142 | int x = 0; |
| 1143 | Action<void(int)> a = Assign(&x, 5); |
| 1144 | a.Perform(make_tuple(0)); |
| 1145 | EXPECT_EQ(5, x); |
| 1146 | } |
| 1147 | |
| 1148 | TEST(AssignTest, String) { |
| 1149 | ::std::string x; |
| 1150 | Action<void(void)> a = Assign(&x, "Hello, world"); |
| 1151 | a.Perform(make_tuple()); |
| 1152 | EXPECT_EQ("Hello, world", x); |
| 1153 | } |
| 1154 | |
| 1155 | TEST(AssignTest, CompatibleTypes) { |
| 1156 | double x = 0; |
| 1157 | Action<void(int)> a = Assign(&x, 5); |
| 1158 | a.Perform(make_tuple(0)); |
| 1159 | EXPECT_DOUBLE_EQ(5, x); |
| 1160 | } |
| 1161 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 1162 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 1163 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1164 | class SetErrnoAndReturnTest : public testing::Test { |
| 1165 | protected: |
| 1166 | virtual void SetUp() { errno = 0; } |
| 1167 | virtual void TearDown() { errno = 0; } |
| 1168 | }; |
| 1169 | |
| 1170 | TEST_F(SetErrnoAndReturnTest, Int) { |
| 1171 | Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5); |
| 1172 | EXPECT_EQ(-5, a.Perform(make_tuple())); |
| 1173 | EXPECT_EQ(ENOTTY, errno); |
| 1174 | } |
| 1175 | |
| 1176 | TEST_F(SetErrnoAndReturnTest, Ptr) { |
| 1177 | int x; |
| 1178 | Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x); |
| 1179 | EXPECT_EQ(&x, a.Perform(make_tuple())); |
| 1180 | EXPECT_EQ(ENOTTY, errno); |
| 1181 | } |
| 1182 | |
| 1183 | TEST_F(SetErrnoAndReturnTest, CompatibleTypes) { |
| 1184 | Action<double()> a = SetErrnoAndReturn(EINVAL, 5); |
| 1185 | EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple())); |
| 1186 | EXPECT_EQ(EINVAL, errno); |
| 1187 | } |
| 1188 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 1189 | #endif // !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 1190 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 1191 | // Tests ByRef(). |
| 1192 | |
| 1193 | // Tests that ReferenceWrapper<T> is copyable. |
| 1194 | TEST(ByRefTest, IsCopyable) { |
| 1195 | const std::string s1 = "Hi"; |
| 1196 | const std::string s2 = "Hello"; |
| 1197 | |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 1198 | ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = |
| 1199 | ByRef(s1); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 1200 | const std::string& r1 = ref_wrapper; |
| 1201 | EXPECT_EQ(&s1, &r1); |
| 1202 | |
| 1203 | // Assigns a new value to ref_wrapper. |
| 1204 | ref_wrapper = ByRef(s2); |
| 1205 | const std::string& r2 = ref_wrapper; |
| 1206 | EXPECT_EQ(&s2, &r2); |
| 1207 | |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 1208 | ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = |
| 1209 | ByRef(s1); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 1210 | // Copies ref_wrapper1 to ref_wrapper. |
| 1211 | ref_wrapper = ref_wrapper1; |
| 1212 | const std::string& r3 = ref_wrapper; |
| 1213 | EXPECT_EQ(&s1, &r3); |
| 1214 | } |
| 1215 | |
| 1216 | // Tests using ByRef() on a const value. |
| 1217 | TEST(ByRefTest, ConstValue) { |
| 1218 | const int n = 0; |
| 1219 | // int& ref = ByRef(n); // This shouldn't compile - we have a |
| 1220 | // negative compilation test to catch it. |
| 1221 | const int& const_ref = ByRef(n); |
| 1222 | EXPECT_EQ(&n, &const_ref); |
| 1223 | } |
| 1224 | |
| 1225 | // Tests using ByRef() on a non-const value. |
| 1226 | TEST(ByRefTest, NonConstValue) { |
| 1227 | int n = 0; |
| 1228 | |
| 1229 | // ByRef(n) can be used as either an int&, |
| 1230 | int& ref = ByRef(n); |
| 1231 | EXPECT_EQ(&n, &ref); |
| 1232 | |
| 1233 | // or a const int&. |
| 1234 | const int& const_ref = ByRef(n); |
| 1235 | EXPECT_EQ(&n, &const_ref); |
| 1236 | } |
| 1237 | |
| 1238 | // Tests explicitly specifying the type when using ByRef(). |
| 1239 | TEST(ByRefTest, ExplicitType) { |
| 1240 | int n = 0; |
| 1241 | const int& r1 = ByRef<const int>(n); |
| 1242 | EXPECT_EQ(&n, &r1); |
| 1243 | |
| 1244 | // ByRef<char>(n); // This shouldn't compile - we have a negative |
| 1245 | // compilation test to catch it. |
| 1246 | |
| 1247 | Derived d; |
| 1248 | Derived& r2 = ByRef<Derived>(d); |
| 1249 | EXPECT_EQ(&d, &r2); |
| 1250 | |
| 1251 | const Derived& r3 = ByRef<const Derived>(d); |
| 1252 | EXPECT_EQ(&d, &r3); |
| 1253 | |
| 1254 | Base& r4 = ByRef<Base>(d); |
| 1255 | EXPECT_EQ(&d, &r4); |
| 1256 | |
| 1257 | const Base& r5 = ByRef<const Base>(d); |
| 1258 | EXPECT_EQ(&d, &r5); |
| 1259 | |
| 1260 | // The following shouldn't compile - we have a negative compilation |
| 1261 | // test for it. |
| 1262 | // |
| 1263 | // Base b; |
| 1264 | // ByRef<Derived>(b); |
| 1265 | } |
| 1266 | |
| 1267 | // Tests that Google Mock prints expression ByRef(x) as a reference to x. |
| 1268 | TEST(ByRefTest, PrintsCorrectly) { |
| 1269 | int n = 42; |
| 1270 | ::std::stringstream expected, actual; |
| 1271 | testing::internal::UniversalPrinter<const int&>::Print(n, &expected); |
| 1272 | testing::internal::UniversalPrint(ByRef(n), &actual); |
| 1273 | EXPECT_EQ(expected.str(), actual.str()); |
| 1274 | } |
| 1275 | |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame^] | 1276 | #if GTEST_LANG_CXX11 |
| 1277 | |
| 1278 | std::unique_ptr<int> UniquePtrSource() { |
| 1279 | return std::unique_ptr<int>(new int(19)); |
| 1280 | } |
| 1281 | |
| 1282 | std::vector<std::unique_ptr<int>> VectorUniquePtrSource() { |
| 1283 | std::vector<std::unique_ptr<int>> out; |
| 1284 | out.emplace_back(new int(7)); |
| 1285 | return out; |
| 1286 | } |
| 1287 | |
| 1288 | TEST(MockMethodTest, CanReturnMoveOnlyValue) { |
| 1289 | MockClass mock; |
| 1290 | |
| 1291 | // Check default value |
| 1292 | DefaultValue<std::unique_ptr<int>>::SetFactory([] { |
| 1293 | return std::unique_ptr<int>(new int(42)); |
| 1294 | }); |
| 1295 | EXPECT_EQ(42, *mock.MakeUnique()); |
| 1296 | |
| 1297 | EXPECT_CALL(mock, MakeUnique()) |
| 1298 | .WillRepeatedly(Invoke(UniquePtrSource)); |
| 1299 | EXPECT_CALL(mock, MakeVectorUnique()) |
| 1300 | .WillRepeatedly(Invoke(VectorUniquePtrSource)); |
| 1301 | std::unique_ptr<int> result1 = mock.MakeUnique(); |
| 1302 | EXPECT_EQ(19, *result1); |
| 1303 | std::unique_ptr<int> result2 = mock.MakeUnique(); |
| 1304 | EXPECT_EQ(19, *result2); |
| 1305 | EXPECT_NE(result1, result2); |
| 1306 | |
| 1307 | std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique(); |
| 1308 | EXPECT_EQ(1, vresult.size()); |
| 1309 | EXPECT_NE(nullptr, vresult[0]); |
| 1310 | EXPECT_EQ(7, *vresult[0]); |
| 1311 | } |
| 1312 | |
| 1313 | #endif // GTEST_LANG_CXX11 |
| 1314 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1315 | } // Unnamed namespace |