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