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