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