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