1b89a7cc2SEnji Cooper // Copyright 2007, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper // * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper // * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper // * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper
30b89a7cc2SEnji Cooper // Google Mock - a framework for writing C++ mock classes.
31b89a7cc2SEnji Cooper //
32b89a7cc2SEnji Cooper // This file tests the built-in actions.
33b89a7cc2SEnji Cooper
34b89a7cc2SEnji Cooper #include "gmock/gmock-actions.h"
35*28f6c2f2SEnji Cooper
36b89a7cc2SEnji Cooper #include <algorithm>
37*28f6c2f2SEnji Cooper #include <functional>
38b89a7cc2SEnji Cooper #include <iterator>
39b89a7cc2SEnji Cooper #include <memory>
40*28f6c2f2SEnji Cooper #include <sstream>
41b89a7cc2SEnji Cooper #include <string>
42*28f6c2f2SEnji Cooper #include <tuple>
43*28f6c2f2SEnji Cooper #include <type_traits>
44*28f6c2f2SEnji Cooper #include <utility>
45*28f6c2f2SEnji Cooper #include <vector>
46*28f6c2f2SEnji Cooper
47b89a7cc2SEnji Cooper #include "gmock/gmock.h"
48b89a7cc2SEnji Cooper #include "gmock/internal/gmock-port.h"
49b89a7cc2SEnji Cooper #include "gtest/gtest-spi.h"
50*28f6c2f2SEnji Cooper #include "gtest/gtest.h"
51*28f6c2f2SEnji Cooper #include "gtest/internal/gtest-port.h"
52b89a7cc2SEnji Cooper
53*28f6c2f2SEnji Cooper // Silence C4100 (unreferenced formal parameter) and C4503 (decorated name
54*28f6c2f2SEnji Cooper // length exceeded) for MSVC.
55*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100 4503)
56*28f6c2f2SEnji Cooper #if defined(_MSC_VER) && (_MSC_VER == 1900)
57*28f6c2f2SEnji Cooper // and silence C4800 (C4800: 'int *const ': forcing value
58*28f6c2f2SEnji Cooper // to bool 'true' or 'false') for MSVC 15
59*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800)
60*28f6c2f2SEnji Cooper #endif
61*28f6c2f2SEnji Cooper
62*28f6c2f2SEnji Cooper namespace testing {
63b89a7cc2SEnji Cooper namespace {
64b89a7cc2SEnji Cooper
65*28f6c2f2SEnji Cooper using ::testing::internal::BuiltInDefaultValue;
66b89a7cc2SEnji Cooper
TEST(TypeTraits,Negation)67*28f6c2f2SEnji Cooper TEST(TypeTraits, Negation) {
68*28f6c2f2SEnji Cooper // Direct use with std types.
69*28f6c2f2SEnji Cooper static_assert(std::is_base_of<std::false_type,
70*28f6c2f2SEnji Cooper internal::negation<std::true_type>>::value,
71*28f6c2f2SEnji Cooper "");
72*28f6c2f2SEnji Cooper
73*28f6c2f2SEnji Cooper static_assert(std::is_base_of<std::true_type,
74*28f6c2f2SEnji Cooper internal::negation<std::false_type>>::value,
75*28f6c2f2SEnji Cooper "");
76*28f6c2f2SEnji Cooper
77*28f6c2f2SEnji Cooper // With other types that fit the requirement of a value member that is
78*28f6c2f2SEnji Cooper // convertible to bool.
79*28f6c2f2SEnji Cooper static_assert(std::is_base_of<
80*28f6c2f2SEnji Cooper std::true_type,
81*28f6c2f2SEnji Cooper internal::negation<std::integral_constant<int, 0>>>::value,
82*28f6c2f2SEnji Cooper "");
83*28f6c2f2SEnji Cooper
84*28f6c2f2SEnji Cooper static_assert(std::is_base_of<
85*28f6c2f2SEnji Cooper std::false_type,
86*28f6c2f2SEnji Cooper internal::negation<std::integral_constant<int, 1>>>::value,
87*28f6c2f2SEnji Cooper "");
88*28f6c2f2SEnji Cooper
89*28f6c2f2SEnji Cooper static_assert(std::is_base_of<
90*28f6c2f2SEnji Cooper std::false_type,
91*28f6c2f2SEnji Cooper internal::negation<std::integral_constant<int, -1>>>::value,
92*28f6c2f2SEnji Cooper "");
93*28f6c2f2SEnji Cooper }
94*28f6c2f2SEnji Cooper
95*28f6c2f2SEnji Cooper // Weird false/true types that aren't actually bool constants (but should still
96*28f6c2f2SEnji Cooper // be legal according to [meta.logical] because `bool(T::value)` is valid), are
97*28f6c2f2SEnji Cooper // distinct from std::false_type and std::true_type, and are distinct from other
98*28f6c2f2SEnji Cooper // instantiations of the same template.
99*28f6c2f2SEnji Cooper //
100*28f6c2f2SEnji Cooper // These let us check finicky details mandated by the standard like
101*28f6c2f2SEnji Cooper // "std::conjunction should evaluate to a type that inherits from the first
102*28f6c2f2SEnji Cooper // false-y input".
103*28f6c2f2SEnji Cooper template <int>
104*28f6c2f2SEnji Cooper struct MyFalse : std::integral_constant<int, 0> {};
105*28f6c2f2SEnji Cooper
106*28f6c2f2SEnji Cooper template <int>
107*28f6c2f2SEnji Cooper struct MyTrue : std::integral_constant<int, -1> {};
108*28f6c2f2SEnji Cooper
TEST(TypeTraits,Conjunction)109*28f6c2f2SEnji Cooper TEST(TypeTraits, Conjunction) {
110*28f6c2f2SEnji Cooper // Base case: always true.
111*28f6c2f2SEnji Cooper static_assert(std::is_base_of<std::true_type, internal::conjunction<>>::value,
112*28f6c2f2SEnji Cooper "");
113*28f6c2f2SEnji Cooper
114*28f6c2f2SEnji Cooper // One predicate: inherits from that predicate, regardless of value.
115*28f6c2f2SEnji Cooper static_assert(
116*28f6c2f2SEnji Cooper std::is_base_of<MyFalse<0>, internal::conjunction<MyFalse<0>>>::value,
117*28f6c2f2SEnji Cooper "");
118*28f6c2f2SEnji Cooper
119*28f6c2f2SEnji Cooper static_assert(
120*28f6c2f2SEnji Cooper std::is_base_of<MyTrue<0>, internal::conjunction<MyTrue<0>>>::value, "");
121*28f6c2f2SEnji Cooper
122*28f6c2f2SEnji Cooper // Multiple predicates, with at least one false: inherits from that one.
123*28f6c2f2SEnji Cooper static_assert(
124*28f6c2f2SEnji Cooper std::is_base_of<MyFalse<1>, internal::conjunction<MyTrue<0>, MyFalse<1>,
125*28f6c2f2SEnji Cooper MyTrue<2>>>::value,
126*28f6c2f2SEnji Cooper "");
127*28f6c2f2SEnji Cooper
128*28f6c2f2SEnji Cooper static_assert(
129*28f6c2f2SEnji Cooper std::is_base_of<MyFalse<1>, internal::conjunction<MyTrue<0>, MyFalse<1>,
130*28f6c2f2SEnji Cooper MyFalse<2>>>::value,
131*28f6c2f2SEnji Cooper "");
132*28f6c2f2SEnji Cooper
133*28f6c2f2SEnji Cooper // Short circuiting: in the case above, additional predicates need not even
134*28f6c2f2SEnji Cooper // define a value member.
135*28f6c2f2SEnji Cooper struct Empty {};
136*28f6c2f2SEnji Cooper static_assert(
137*28f6c2f2SEnji Cooper std::is_base_of<MyFalse<1>, internal::conjunction<MyTrue<0>, MyFalse<1>,
138*28f6c2f2SEnji Cooper Empty>>::value,
139*28f6c2f2SEnji Cooper "");
140*28f6c2f2SEnji Cooper
141*28f6c2f2SEnji Cooper // All predicates true: inherits from the last.
142*28f6c2f2SEnji Cooper static_assert(
143*28f6c2f2SEnji Cooper std::is_base_of<MyTrue<2>, internal::conjunction<MyTrue<0>, MyTrue<1>,
144*28f6c2f2SEnji Cooper MyTrue<2>>>::value,
145*28f6c2f2SEnji Cooper "");
146*28f6c2f2SEnji Cooper }
147*28f6c2f2SEnji Cooper
TEST(TypeTraits,Disjunction)148*28f6c2f2SEnji Cooper TEST(TypeTraits, Disjunction) {
149*28f6c2f2SEnji Cooper // Base case: always false.
150*28f6c2f2SEnji Cooper static_assert(
151*28f6c2f2SEnji Cooper std::is_base_of<std::false_type, internal::disjunction<>>::value, "");
152*28f6c2f2SEnji Cooper
153*28f6c2f2SEnji Cooper // One predicate: inherits from that predicate, regardless of value.
154*28f6c2f2SEnji Cooper static_assert(
155*28f6c2f2SEnji Cooper std::is_base_of<MyFalse<0>, internal::disjunction<MyFalse<0>>>::value,
156*28f6c2f2SEnji Cooper "");
157*28f6c2f2SEnji Cooper
158*28f6c2f2SEnji Cooper static_assert(
159*28f6c2f2SEnji Cooper std::is_base_of<MyTrue<0>, internal::disjunction<MyTrue<0>>>::value, "");
160*28f6c2f2SEnji Cooper
161*28f6c2f2SEnji Cooper // Multiple predicates, with at least one true: inherits from that one.
162*28f6c2f2SEnji Cooper static_assert(
163*28f6c2f2SEnji Cooper std::is_base_of<MyTrue<1>, internal::disjunction<MyFalse<0>, MyTrue<1>,
164*28f6c2f2SEnji Cooper MyFalse<2>>>::value,
165*28f6c2f2SEnji Cooper "");
166*28f6c2f2SEnji Cooper
167*28f6c2f2SEnji Cooper static_assert(
168*28f6c2f2SEnji Cooper std::is_base_of<MyTrue<1>, internal::disjunction<MyFalse<0>, MyTrue<1>,
169*28f6c2f2SEnji Cooper MyTrue<2>>>::value,
170*28f6c2f2SEnji Cooper "");
171*28f6c2f2SEnji Cooper
172*28f6c2f2SEnji Cooper // Short circuiting: in the case above, additional predicates need not even
173*28f6c2f2SEnji Cooper // define a value member.
174*28f6c2f2SEnji Cooper struct Empty {};
175*28f6c2f2SEnji Cooper static_assert(
176*28f6c2f2SEnji Cooper std::is_base_of<MyTrue<1>, internal::disjunction<MyFalse<0>, MyTrue<1>,
177*28f6c2f2SEnji Cooper Empty>>::value,
178*28f6c2f2SEnji Cooper "");
179*28f6c2f2SEnji Cooper
180*28f6c2f2SEnji Cooper // All predicates false: inherits from the last.
181*28f6c2f2SEnji Cooper static_assert(
182*28f6c2f2SEnji Cooper std::is_base_of<MyFalse<2>, internal::disjunction<MyFalse<0>, MyFalse<1>,
183*28f6c2f2SEnji Cooper MyFalse<2>>>::value,
184*28f6c2f2SEnji Cooper "");
185*28f6c2f2SEnji Cooper }
186*28f6c2f2SEnji Cooper
TEST(TypeTraits,IsInvocableRV)187*28f6c2f2SEnji Cooper TEST(TypeTraits, IsInvocableRV) {
188*28f6c2f2SEnji Cooper struct C {
189*28f6c2f2SEnji Cooper int operator()() const { return 0; }
190*28f6c2f2SEnji Cooper void operator()(int) & {}
191*28f6c2f2SEnji Cooper std::string operator()(int) && { return ""; };
192*28f6c2f2SEnji Cooper };
193*28f6c2f2SEnji Cooper
194*28f6c2f2SEnji Cooper // The first overload is callable for const and non-const rvalues and lvalues.
195*28f6c2f2SEnji Cooper // It can be used to obtain an int, cv void, or anything int is convertible
196*28f6c2f2SEnji Cooper // to.
197*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<int, C>::value, "");
198*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<int, C&>::value, "");
199*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<int, const C>::value, "");
200*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<int, const C&>::value, "");
201*28f6c2f2SEnji Cooper
202*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<void, C>::value, "");
203*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<const volatile void, C>::value, "");
204*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<char, C>::value, "");
205*28f6c2f2SEnji Cooper
206*28f6c2f2SEnji Cooper // It's possible to provide an int. If it's given to an lvalue, the result is
207*28f6c2f2SEnji Cooper // void. Otherwise it is std::string (which is also treated as allowed for a
208*28f6c2f2SEnji Cooper // void result type).
209*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<void, C&, int>::value, "");
210*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<int, C&, int>::value, "");
211*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<std::string, C&, int>::value, "");
212*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<void, const C&, int>::value, "");
213*28f6c2f2SEnji Cooper
214*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<std::string, C, int>::value, "");
215*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<void, C, int>::value, "");
216*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<int, C, int>::value, "");
217*28f6c2f2SEnji Cooper
218*28f6c2f2SEnji Cooper // It's not possible to provide other arguments.
219*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<void, C, std::string>::value, "");
220*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<void, C, int, int>::value, "");
221*28f6c2f2SEnji Cooper
222*28f6c2f2SEnji Cooper // In C++17 and above, where it's guaranteed that functions can return
223*28f6c2f2SEnji Cooper // non-moveable objects, everything should work fine for non-moveable rsult
224*28f6c2f2SEnji Cooper // types too.
225*28f6c2f2SEnji Cooper #if defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \
226*28f6c2f2SEnji Cooper GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
227*28f6c2f2SEnji Cooper {
228*28f6c2f2SEnji Cooper struct NonMoveable {
229*28f6c2f2SEnji Cooper NonMoveable() = default;
230*28f6c2f2SEnji Cooper NonMoveable(NonMoveable&&) = delete;
231*28f6c2f2SEnji Cooper };
232*28f6c2f2SEnji Cooper
233*28f6c2f2SEnji Cooper static_assert(!std::is_move_constructible_v<NonMoveable>);
234*28f6c2f2SEnji Cooper
235*28f6c2f2SEnji Cooper struct Callable {
236*28f6c2f2SEnji Cooper NonMoveable operator()() { return NonMoveable(); }
237*28f6c2f2SEnji Cooper };
238*28f6c2f2SEnji Cooper
239*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<NonMoveable, Callable>::value);
240*28f6c2f2SEnji Cooper static_assert(internal::is_callable_r<void, Callable>::value);
241*28f6c2f2SEnji Cooper static_assert(
242*28f6c2f2SEnji Cooper internal::is_callable_r<const volatile void, Callable>::value);
243*28f6c2f2SEnji Cooper
244*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<int, Callable>::value);
245*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<NonMoveable, Callable, int>::value);
246*28f6c2f2SEnji Cooper }
247*28f6c2f2SEnji Cooper #endif // C++17 and above
248*28f6c2f2SEnji Cooper
249*28f6c2f2SEnji Cooper // Nothing should choke when we try to call other arguments besides directly
250*28f6c2f2SEnji Cooper // callable objects, but they should not show up as callable.
251*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<void, int>::value, "");
252*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<void, void (C::*)()>::value, "");
253*28f6c2f2SEnji Cooper static_assert(!internal::is_callable_r<void, void (C::*)(), C*>::value, "");
254*28f6c2f2SEnji Cooper }
255b89a7cc2SEnji Cooper
256b89a7cc2SEnji Cooper // Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
TEST(BuiltInDefaultValueTest,IsNullForPointerTypes)257b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
258*28f6c2f2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == nullptr);
259*28f6c2f2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == nullptr);
260*28f6c2f2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == nullptr);
261b89a7cc2SEnji Cooper }
262b89a7cc2SEnji Cooper
263b89a7cc2SEnji Cooper // Tests that BuiltInDefaultValue<T*>::Exists() return true.
TEST(BuiltInDefaultValueTest,ExistsForPointerTypes)264b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
265b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
266b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
267b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
268b89a7cc2SEnji Cooper }
269b89a7cc2SEnji Cooper
270b89a7cc2SEnji Cooper // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
271b89a7cc2SEnji Cooper // built-in numeric type.
TEST(BuiltInDefaultValueTest,IsZeroForNumericTypes)272b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
273b89a7cc2SEnji Cooper EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
274b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
275b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
276b89a7cc2SEnji Cooper #if GMOCK_WCHAR_T_IS_NATIVE_
277b89a7cc2SEnji Cooper #if !defined(__WCHAR_UNSIGNED__)
278b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
279b89a7cc2SEnji Cooper #else
280b89a7cc2SEnji Cooper EXPECT_EQ(0U, BuiltInDefaultValue<wchar_t>::Get());
281b89a7cc2SEnji Cooper #endif
282b89a7cc2SEnji Cooper #endif
283b89a7cc2SEnji Cooper EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
284b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
285b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
286b89a7cc2SEnji Cooper EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
287b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
288b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
289b89a7cc2SEnji Cooper EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
290b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
291b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
292*28f6c2f2SEnji Cooper EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long long>::Get()); // NOLINT
293*28f6c2f2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<signed long long>::Get()); // NOLINT
294*28f6c2f2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<long long>::Get()); // NOLINT
295b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
296b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
297b89a7cc2SEnji Cooper }
298b89a7cc2SEnji Cooper
299b89a7cc2SEnji Cooper // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
300b89a7cc2SEnji Cooper // built-in numeric type.
TEST(BuiltInDefaultValueTest,ExistsForNumericTypes)301b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
302b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
303b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
304b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
305b89a7cc2SEnji Cooper #if GMOCK_WCHAR_T_IS_NATIVE_
306b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
307b89a7cc2SEnji Cooper #endif
308b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
309b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
310b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
311b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
312b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
313b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
314b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
315b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
316b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
317*28f6c2f2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<unsigned long long>::Exists()); // NOLINT
318*28f6c2f2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<signed long long>::Exists()); // NOLINT
319*28f6c2f2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<long long>::Exists()); // NOLINT
320b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
321b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
322b89a7cc2SEnji Cooper }
323b89a7cc2SEnji Cooper
324b89a7cc2SEnji Cooper // Tests that BuiltInDefaultValue<bool>::Get() returns false.
TEST(BuiltInDefaultValueTest,IsFalseForBool)325b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, IsFalseForBool) {
326b89a7cc2SEnji Cooper EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
327b89a7cc2SEnji Cooper }
328b89a7cc2SEnji Cooper
329b89a7cc2SEnji Cooper // Tests that BuiltInDefaultValue<bool>::Exists() returns true.
TEST(BuiltInDefaultValueTest,BoolExists)330b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, BoolExists) {
331b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
332b89a7cc2SEnji Cooper }
333b89a7cc2SEnji Cooper
334b89a7cc2SEnji Cooper // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
335b89a7cc2SEnji Cooper // string type.
TEST(BuiltInDefaultValueTest,IsEmptyStringForString)336b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
337b89a7cc2SEnji Cooper EXPECT_EQ("", BuiltInDefaultValue<::std::string>::Get());
338b89a7cc2SEnji Cooper }
339b89a7cc2SEnji Cooper
340b89a7cc2SEnji Cooper // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
341b89a7cc2SEnji Cooper // string type.
TEST(BuiltInDefaultValueTest,ExistsForString)342b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, ExistsForString) {
343b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<::std::string>::Exists());
344b89a7cc2SEnji Cooper }
345b89a7cc2SEnji Cooper
346b89a7cc2SEnji Cooper // Tests that BuiltInDefaultValue<const T>::Get() returns the same
347b89a7cc2SEnji Cooper // value as BuiltInDefaultValue<T>::Get() does.
TEST(BuiltInDefaultValueTest,WorksForConstTypes)348b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
349b89a7cc2SEnji Cooper EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
350b89a7cc2SEnji Cooper EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
351*28f6c2f2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == nullptr);
352b89a7cc2SEnji Cooper EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
353b89a7cc2SEnji Cooper }
354b89a7cc2SEnji Cooper
355b89a7cc2SEnji Cooper // A type that's default constructible.
356b89a7cc2SEnji Cooper class MyDefaultConstructible {
357b89a7cc2SEnji Cooper public:
MyDefaultConstructible()358b89a7cc2SEnji Cooper MyDefaultConstructible() : value_(42) {}
359b89a7cc2SEnji Cooper
value() const360b89a7cc2SEnji Cooper int value() const { return value_; }
361b89a7cc2SEnji Cooper
362b89a7cc2SEnji Cooper private:
363b89a7cc2SEnji Cooper int value_;
364b89a7cc2SEnji Cooper };
365b89a7cc2SEnji Cooper
366b89a7cc2SEnji Cooper // A type that's not default constructible.
367b89a7cc2SEnji Cooper class MyNonDefaultConstructible {
368b89a7cc2SEnji Cooper public:
369b89a7cc2SEnji Cooper // Does not have a default ctor.
MyNonDefaultConstructible(int a_value)370b89a7cc2SEnji Cooper explicit MyNonDefaultConstructible(int a_value) : value_(a_value) {}
371b89a7cc2SEnji Cooper
value() const372b89a7cc2SEnji Cooper int value() const { return value_; }
373b89a7cc2SEnji Cooper
374b89a7cc2SEnji Cooper private:
375b89a7cc2SEnji Cooper int value_;
376b89a7cc2SEnji Cooper };
377b89a7cc2SEnji Cooper
TEST(BuiltInDefaultValueTest,ExistsForDefaultConstructibleType)378b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) {
379b89a7cc2SEnji Cooper EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists());
380b89a7cc2SEnji Cooper }
381b89a7cc2SEnji Cooper
TEST(BuiltInDefaultValueTest,IsDefaultConstructedForDefaultConstructibleType)382b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) {
383b89a7cc2SEnji Cooper EXPECT_EQ(42, BuiltInDefaultValue<MyDefaultConstructible>::Get().value());
384b89a7cc2SEnji Cooper }
385b89a7cc2SEnji Cooper
TEST(BuiltInDefaultValueTest,DoesNotExistForNonDefaultConstructibleType)386b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) {
387b89a7cc2SEnji Cooper EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists());
388b89a7cc2SEnji Cooper }
389b89a7cc2SEnji Cooper
390b89a7cc2SEnji Cooper // Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
TEST(BuiltInDefaultValueDeathTest,IsUndefinedForReferences)391b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
392*28f6c2f2SEnji Cooper EXPECT_DEATH_IF_SUPPORTED({ BuiltInDefaultValue<int&>::Get(); }, "");
393*28f6c2f2SEnji Cooper EXPECT_DEATH_IF_SUPPORTED({ BuiltInDefaultValue<const char&>::Get(); }, "");
394b89a7cc2SEnji Cooper }
395b89a7cc2SEnji Cooper
TEST(BuiltInDefaultValueDeathTest,IsUndefinedForNonDefaultConstructibleType)396b89a7cc2SEnji Cooper TEST(BuiltInDefaultValueDeathTest, IsUndefinedForNonDefaultConstructibleType) {
397*28f6c2f2SEnji Cooper EXPECT_DEATH_IF_SUPPORTED(
398*28f6c2f2SEnji Cooper { BuiltInDefaultValue<MyNonDefaultConstructible>::Get(); }, "");
399b89a7cc2SEnji Cooper }
400b89a7cc2SEnji Cooper
401b89a7cc2SEnji Cooper // Tests that DefaultValue<T>::IsSet() is false initially.
TEST(DefaultValueTest,IsInitiallyUnset)402b89a7cc2SEnji Cooper TEST(DefaultValueTest, IsInitiallyUnset) {
403b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<int>::IsSet());
404b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<MyDefaultConstructible>::IsSet());
405b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
406b89a7cc2SEnji Cooper }
407b89a7cc2SEnji Cooper
408b89a7cc2SEnji Cooper // Tests that DefaultValue<T> can be set and then unset.
TEST(DefaultValueTest,CanBeSetAndUnset)409b89a7cc2SEnji Cooper TEST(DefaultValueTest, CanBeSetAndUnset) {
410b89a7cc2SEnji Cooper EXPECT_TRUE(DefaultValue<int>::Exists());
411b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
412b89a7cc2SEnji Cooper
413b89a7cc2SEnji Cooper DefaultValue<int>::Set(1);
414b89a7cc2SEnji Cooper DefaultValue<const MyNonDefaultConstructible>::Set(
415b89a7cc2SEnji Cooper MyNonDefaultConstructible(42));
416b89a7cc2SEnji Cooper
417b89a7cc2SEnji Cooper EXPECT_EQ(1, DefaultValue<int>::Get());
418b89a7cc2SEnji Cooper EXPECT_EQ(42, DefaultValue<const MyNonDefaultConstructible>::Get().value());
419b89a7cc2SEnji Cooper
420b89a7cc2SEnji Cooper EXPECT_TRUE(DefaultValue<int>::Exists());
421b89a7cc2SEnji Cooper EXPECT_TRUE(DefaultValue<const MyNonDefaultConstructible>::Exists());
422b89a7cc2SEnji Cooper
423b89a7cc2SEnji Cooper DefaultValue<int>::Clear();
424b89a7cc2SEnji Cooper DefaultValue<const MyNonDefaultConstructible>::Clear();
425b89a7cc2SEnji Cooper
426b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<int>::IsSet());
427b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
428b89a7cc2SEnji Cooper
429b89a7cc2SEnji Cooper EXPECT_TRUE(DefaultValue<int>::Exists());
430b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
431b89a7cc2SEnji Cooper }
432b89a7cc2SEnji Cooper
433b89a7cc2SEnji Cooper // Tests that DefaultValue<T>::Get() returns the
434b89a7cc2SEnji Cooper // BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
435b89a7cc2SEnji Cooper // false.
TEST(DefaultValueDeathTest,GetReturnsBuiltInDefaultValueWhenUnset)436b89a7cc2SEnji Cooper TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
437b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<int>::IsSet());
438b89a7cc2SEnji Cooper EXPECT_TRUE(DefaultValue<int>::Exists());
439b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::IsSet());
440b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::Exists());
441b89a7cc2SEnji Cooper
442b89a7cc2SEnji Cooper EXPECT_EQ(0, DefaultValue<int>::Get());
443b89a7cc2SEnji Cooper
444*28f6c2f2SEnji Cooper EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<MyNonDefaultConstructible>::Get(); },
445*28f6c2f2SEnji Cooper "");
446b89a7cc2SEnji Cooper }
447b89a7cc2SEnji Cooper
TEST(DefaultValueTest,GetWorksForMoveOnlyIfSet)448b89a7cc2SEnji Cooper TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
449b89a7cc2SEnji Cooper EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
450*28f6c2f2SEnji Cooper EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() == nullptr);
451*28f6c2f2SEnji Cooper DefaultValue<std::unique_ptr<int>>::SetFactory(
452*28f6c2f2SEnji Cooper [] { return std::make_unique<int>(42); });
453b89a7cc2SEnji Cooper EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
454b89a7cc2SEnji Cooper std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
455b89a7cc2SEnji Cooper EXPECT_EQ(42, *i);
456b89a7cc2SEnji Cooper }
457b89a7cc2SEnji Cooper
458b89a7cc2SEnji Cooper // Tests that DefaultValue<void>::Get() returns void.
TEST(DefaultValueTest,GetWorksForVoid)459*28f6c2f2SEnji Cooper TEST(DefaultValueTest, GetWorksForVoid) { return DefaultValue<void>::Get(); }
460b89a7cc2SEnji Cooper
461b89a7cc2SEnji Cooper // Tests using DefaultValue with a reference type.
462b89a7cc2SEnji Cooper
463b89a7cc2SEnji Cooper // Tests that DefaultValue<T&>::IsSet() is false initially.
TEST(DefaultValueOfReferenceTest,IsInitiallyUnset)464b89a7cc2SEnji Cooper TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
465b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<int&>::IsSet());
466b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::IsSet());
467b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
468b89a7cc2SEnji Cooper }
469b89a7cc2SEnji Cooper
470*28f6c2f2SEnji Cooper // Tests that DefaultValue<T&>::Exists is false initially.
TEST(DefaultValueOfReferenceTest,IsInitiallyNotExisting)471b89a7cc2SEnji Cooper TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
472b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<int&>::Exists());
473b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::Exists());
474b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
475b89a7cc2SEnji Cooper }
476b89a7cc2SEnji Cooper
477b89a7cc2SEnji Cooper // Tests that DefaultValue<T&> can be set and then unset.
TEST(DefaultValueOfReferenceTest,CanBeSetAndUnset)478b89a7cc2SEnji Cooper TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
479b89a7cc2SEnji Cooper int n = 1;
480b89a7cc2SEnji Cooper DefaultValue<const int&>::Set(n);
481b89a7cc2SEnji Cooper MyNonDefaultConstructible x(42);
482b89a7cc2SEnji Cooper DefaultValue<MyNonDefaultConstructible&>::Set(x);
483b89a7cc2SEnji Cooper
484b89a7cc2SEnji Cooper EXPECT_TRUE(DefaultValue<const int&>::Exists());
485b89a7cc2SEnji Cooper EXPECT_TRUE(DefaultValue<MyNonDefaultConstructible&>::Exists());
486b89a7cc2SEnji Cooper
487b89a7cc2SEnji Cooper EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
488b89a7cc2SEnji Cooper EXPECT_EQ(&x, &(DefaultValue<MyNonDefaultConstructible&>::Get()));
489b89a7cc2SEnji Cooper
490b89a7cc2SEnji Cooper DefaultValue<const int&>::Clear();
491b89a7cc2SEnji Cooper DefaultValue<MyNonDefaultConstructible&>::Clear();
492b89a7cc2SEnji Cooper
493b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<const int&>::Exists());
494b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
495b89a7cc2SEnji Cooper
496b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<const int&>::IsSet());
497b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
498b89a7cc2SEnji Cooper }
499b89a7cc2SEnji Cooper
500b89a7cc2SEnji Cooper // Tests that DefaultValue<T&>::Get() returns the
501b89a7cc2SEnji Cooper // BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
502b89a7cc2SEnji Cooper // false.
TEST(DefaultValueOfReferenceDeathTest,GetReturnsBuiltInDefaultValueWhenUnset)503b89a7cc2SEnji Cooper TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
504b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<int&>::IsSet());
505b89a7cc2SEnji Cooper EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
506b89a7cc2SEnji Cooper
507*28f6c2f2SEnji Cooper EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<int&>::Get(); }, "");
508*28f6c2f2SEnji Cooper EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<MyNonDefaultConstructible>::Get(); },
509*28f6c2f2SEnji Cooper "");
510b89a7cc2SEnji Cooper }
511b89a7cc2SEnji Cooper
512b89a7cc2SEnji Cooper // Tests that ActionInterface can be implemented by defining the
513b89a7cc2SEnji Cooper // Perform method.
514b89a7cc2SEnji Cooper
515b89a7cc2SEnji Cooper typedef int MyGlobalFunction(bool, int);
516b89a7cc2SEnji Cooper
517b89a7cc2SEnji Cooper class MyActionImpl : public ActionInterface<MyGlobalFunction> {
518b89a7cc2SEnji Cooper public:
Perform(const std::tuple<bool,int> & args)519*28f6c2f2SEnji Cooper int Perform(const std::tuple<bool, int>& args) override {
520*28f6c2f2SEnji Cooper return std::get<0>(args) ? std::get<1>(args) : 0;
521b89a7cc2SEnji Cooper }
522b89a7cc2SEnji Cooper };
523b89a7cc2SEnji Cooper
TEST(ActionInterfaceTest,CanBeImplementedByDefiningPerform)524b89a7cc2SEnji Cooper TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
525b89a7cc2SEnji Cooper MyActionImpl my_action_impl;
526b89a7cc2SEnji Cooper (void)my_action_impl;
527b89a7cc2SEnji Cooper }
528b89a7cc2SEnji Cooper
TEST(ActionInterfaceTest,MakeAction)529b89a7cc2SEnji Cooper TEST(ActionInterfaceTest, MakeAction) {
530b89a7cc2SEnji Cooper Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
531b89a7cc2SEnji Cooper
532b89a7cc2SEnji Cooper // When exercising the Perform() method of Action<F>, we must pass
533b89a7cc2SEnji Cooper // it a tuple whose size and type are compatible with F's argument
534b89a7cc2SEnji Cooper // types. For example, if F is int(), then Perform() takes a
535b89a7cc2SEnji Cooper // 0-tuple; if F is void(bool, int), then Perform() takes a
536*28f6c2f2SEnji Cooper // std::tuple<bool, int>, and so on.
537*28f6c2f2SEnji Cooper EXPECT_EQ(5, action.Perform(std::make_tuple(true, 5)));
538b89a7cc2SEnji Cooper }
539b89a7cc2SEnji Cooper
540*28f6c2f2SEnji Cooper // Tests that Action<F> can be constructed from a pointer to
541b89a7cc2SEnji Cooper // ActionInterface<F>.
TEST(ActionTest,CanBeConstructedFromActionInterface)542b89a7cc2SEnji Cooper TEST(ActionTest, CanBeConstructedFromActionInterface) {
543b89a7cc2SEnji Cooper Action<MyGlobalFunction> action(new MyActionImpl);
544b89a7cc2SEnji Cooper }
545b89a7cc2SEnji Cooper
546b89a7cc2SEnji Cooper // Tests that Action<F> delegates actual work to ActionInterface<F>.
TEST(ActionTest,DelegatesWorkToActionInterface)547b89a7cc2SEnji Cooper TEST(ActionTest, DelegatesWorkToActionInterface) {
548b89a7cc2SEnji Cooper const Action<MyGlobalFunction> action(new MyActionImpl);
549b89a7cc2SEnji Cooper
550*28f6c2f2SEnji Cooper EXPECT_EQ(5, action.Perform(std::make_tuple(true, 5)));
551*28f6c2f2SEnji Cooper EXPECT_EQ(0, action.Perform(std::make_tuple(false, 1)));
552b89a7cc2SEnji Cooper }
553b89a7cc2SEnji Cooper
554b89a7cc2SEnji Cooper // Tests that Action<F> can be copied.
TEST(ActionTest,IsCopyable)555b89a7cc2SEnji Cooper TEST(ActionTest, IsCopyable) {
556b89a7cc2SEnji Cooper Action<MyGlobalFunction> a1(new MyActionImpl);
557b89a7cc2SEnji Cooper Action<MyGlobalFunction> a2(a1); // Tests the copy constructor.
558b89a7cc2SEnji Cooper
559b89a7cc2SEnji Cooper // a1 should continue to work after being copied from.
560*28f6c2f2SEnji Cooper EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
561*28f6c2f2SEnji Cooper EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 1)));
562b89a7cc2SEnji Cooper
563b89a7cc2SEnji Cooper // a2 should work like the action it was copied from.
564*28f6c2f2SEnji Cooper EXPECT_EQ(5, a2.Perform(std::make_tuple(true, 5)));
565*28f6c2f2SEnji Cooper EXPECT_EQ(0, a2.Perform(std::make_tuple(false, 1)));
566b89a7cc2SEnji Cooper
567b89a7cc2SEnji Cooper a2 = a1; // Tests the assignment operator.
568b89a7cc2SEnji Cooper
569b89a7cc2SEnji Cooper // a1 should continue to work after being copied from.
570*28f6c2f2SEnji Cooper EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
571*28f6c2f2SEnji Cooper EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 1)));
572b89a7cc2SEnji Cooper
573b89a7cc2SEnji Cooper // a2 should work like the action it was copied from.
574*28f6c2f2SEnji Cooper EXPECT_EQ(5, a2.Perform(std::make_tuple(true, 5)));
575*28f6c2f2SEnji Cooper EXPECT_EQ(0, a2.Perform(std::make_tuple(false, 1)));
576b89a7cc2SEnji Cooper }
577b89a7cc2SEnji Cooper
578b89a7cc2SEnji Cooper // Tests that an Action<From> object can be converted to a
579b89a7cc2SEnji Cooper // compatible Action<To> object.
580b89a7cc2SEnji Cooper
581b89a7cc2SEnji Cooper class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
582b89a7cc2SEnji Cooper public:
Perform(const std::tuple<int> & arg)583*28f6c2f2SEnji Cooper bool Perform(const std::tuple<int>& arg) override {
584*28f6c2f2SEnji Cooper return std::get<0>(arg) != 0;
585b89a7cc2SEnji Cooper }
586b89a7cc2SEnji Cooper };
587b89a7cc2SEnji Cooper
TEST(ActionTest,CanBeConvertedToOtherActionType)588b89a7cc2SEnji Cooper TEST(ActionTest, CanBeConvertedToOtherActionType) {
589b89a7cc2SEnji Cooper const Action<bool(int)> a1(new IsNotZero); // NOLINT
590b89a7cc2SEnji Cooper const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
591*28f6c2f2SEnji Cooper EXPECT_EQ(1, a2.Perform(std::make_tuple('a')));
592*28f6c2f2SEnji Cooper EXPECT_EQ(0, a2.Perform(std::make_tuple('\0')));
593b89a7cc2SEnji Cooper }
594b89a7cc2SEnji Cooper
595b89a7cc2SEnji Cooper // The following two classes are for testing MakePolymorphicAction().
596b89a7cc2SEnji Cooper
597b89a7cc2SEnji Cooper // Implements a polymorphic action that returns the second of the
598b89a7cc2SEnji Cooper // arguments it receives.
599b89a7cc2SEnji Cooper class ReturnSecondArgumentAction {
600b89a7cc2SEnji Cooper public:
601b89a7cc2SEnji Cooper // We want to verify that MakePolymorphicAction() can work with a
602b89a7cc2SEnji Cooper // polymorphic action whose Perform() method template is either
603b89a7cc2SEnji Cooper // const or not. This lets us verify the non-const case.
604b89a7cc2SEnji Cooper template <typename Result, typename ArgumentTuple>
Perform(const ArgumentTuple & args)605*28f6c2f2SEnji Cooper Result Perform(const ArgumentTuple& args) {
606*28f6c2f2SEnji Cooper return std::get<1>(args);
607*28f6c2f2SEnji Cooper }
608b89a7cc2SEnji Cooper };
609b89a7cc2SEnji Cooper
610b89a7cc2SEnji Cooper // Implements a polymorphic action that can be used in a nullary
611b89a7cc2SEnji Cooper // function to return 0.
612b89a7cc2SEnji Cooper class ReturnZeroFromNullaryFunctionAction {
613b89a7cc2SEnji Cooper public:
614b89a7cc2SEnji Cooper // For testing that MakePolymorphicAction() works when the
615b89a7cc2SEnji Cooper // implementation class' Perform() method template takes only one
616b89a7cc2SEnji Cooper // template parameter.
617b89a7cc2SEnji Cooper //
618b89a7cc2SEnji Cooper // We want to verify that MakePolymorphicAction() can work with a
619b89a7cc2SEnji Cooper // polymorphic action whose Perform() method template is either
620b89a7cc2SEnji Cooper // const or not. This lets us verify the const case.
621b89a7cc2SEnji Cooper template <typename Result>
Perform(const std::tuple<> &) const622*28f6c2f2SEnji Cooper Result Perform(const std::tuple<>&) const {
623*28f6c2f2SEnji Cooper return 0;
624*28f6c2f2SEnji Cooper }
625b89a7cc2SEnji Cooper };
626b89a7cc2SEnji Cooper
627b89a7cc2SEnji Cooper // These functions verify that MakePolymorphicAction() returns a
628b89a7cc2SEnji Cooper // PolymorphicAction<T> where T is the argument's type.
629b89a7cc2SEnji Cooper
ReturnSecondArgument()630b89a7cc2SEnji Cooper PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
631b89a7cc2SEnji Cooper return MakePolymorphicAction(ReturnSecondArgumentAction());
632b89a7cc2SEnji Cooper }
633b89a7cc2SEnji Cooper
634b89a7cc2SEnji Cooper PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
ReturnZeroFromNullaryFunction()635b89a7cc2SEnji Cooper ReturnZeroFromNullaryFunction() {
636b89a7cc2SEnji Cooper return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
637b89a7cc2SEnji Cooper }
638b89a7cc2SEnji Cooper
639b89a7cc2SEnji Cooper // Tests that MakePolymorphicAction() turns a polymorphic action
640b89a7cc2SEnji Cooper // implementation class into a polymorphic action.
TEST(MakePolymorphicActionTest,ConstructsActionFromImpl)641b89a7cc2SEnji Cooper TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
642b89a7cc2SEnji Cooper Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
643*28f6c2f2SEnji Cooper EXPECT_EQ(5, a1.Perform(std::make_tuple(false, 5, 2.0)));
644b89a7cc2SEnji Cooper }
645b89a7cc2SEnji Cooper
646b89a7cc2SEnji Cooper // Tests that MakePolymorphicAction() works when the implementation
647b89a7cc2SEnji Cooper // class' Perform() method template has only one template parameter.
TEST(MakePolymorphicActionTest,WorksWhenPerformHasOneTemplateParameter)648b89a7cc2SEnji Cooper TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
649b89a7cc2SEnji Cooper Action<int()> a1 = ReturnZeroFromNullaryFunction();
650*28f6c2f2SEnji Cooper EXPECT_EQ(0, a1.Perform(std::make_tuple()));
651b89a7cc2SEnji Cooper
652b89a7cc2SEnji Cooper Action<void*()> a2 = ReturnZeroFromNullaryFunction();
653*28f6c2f2SEnji Cooper EXPECT_TRUE(a2.Perform(std::make_tuple()) == nullptr);
654b89a7cc2SEnji Cooper }
655b89a7cc2SEnji Cooper
656b89a7cc2SEnji Cooper // Tests that Return() works as an action for void-returning
657b89a7cc2SEnji Cooper // functions.
TEST(ReturnTest,WorksForVoid)658b89a7cc2SEnji Cooper TEST(ReturnTest, WorksForVoid) {
659b89a7cc2SEnji Cooper const Action<void(int)> ret = Return(); // NOLINT
660*28f6c2f2SEnji Cooper return ret.Perform(std::make_tuple(1));
661b89a7cc2SEnji Cooper }
662b89a7cc2SEnji Cooper
663b89a7cc2SEnji Cooper // Tests that Return(v) returns v.
TEST(ReturnTest,ReturnsGivenValue)664b89a7cc2SEnji Cooper TEST(ReturnTest, ReturnsGivenValue) {
665b89a7cc2SEnji Cooper Action<int()> ret = Return(1); // NOLINT
666*28f6c2f2SEnji Cooper EXPECT_EQ(1, ret.Perform(std::make_tuple()));
667b89a7cc2SEnji Cooper
668b89a7cc2SEnji Cooper ret = Return(-5);
669*28f6c2f2SEnji Cooper EXPECT_EQ(-5, ret.Perform(std::make_tuple()));
670b89a7cc2SEnji Cooper }
671b89a7cc2SEnji Cooper
672b89a7cc2SEnji Cooper // Tests that Return("string literal") works.
TEST(ReturnTest,AcceptsStringLiteral)673b89a7cc2SEnji Cooper TEST(ReturnTest, AcceptsStringLiteral) {
674b89a7cc2SEnji Cooper Action<const char*()> a1 = Return("Hello");
675*28f6c2f2SEnji Cooper EXPECT_STREQ("Hello", a1.Perform(std::make_tuple()));
676b89a7cc2SEnji Cooper
677b89a7cc2SEnji Cooper Action<std::string()> a2 = Return("world");
678*28f6c2f2SEnji Cooper EXPECT_EQ("world", a2.Perform(std::make_tuple()));
679b89a7cc2SEnji Cooper }
680b89a7cc2SEnji Cooper
681*28f6c2f2SEnji Cooper // Return(x) should work fine when the mock function's return type is a
682*28f6c2f2SEnji Cooper // reference-like wrapper for decltype(x), as when x is a std::string and the
683*28f6c2f2SEnji Cooper // mock function returns std::string_view.
TEST(ReturnTest,SupportsReferenceLikeReturnType)684*28f6c2f2SEnji Cooper TEST(ReturnTest, SupportsReferenceLikeReturnType) {
685*28f6c2f2SEnji Cooper // A reference wrapper for std::vector<int>, implicitly convertible from it.
686*28f6c2f2SEnji Cooper struct Result {
687*28f6c2f2SEnji Cooper const std::vector<int>* v;
688*28f6c2f2SEnji Cooper Result(const std::vector<int>& vec) : v(&vec) {} // NOLINT
689b89a7cc2SEnji Cooper };
690b89a7cc2SEnji Cooper
691*28f6c2f2SEnji Cooper // Set up an action for a mock function that returns the reference wrapper
692*28f6c2f2SEnji Cooper // type, initializing it with an actual vector.
693*28f6c2f2SEnji Cooper //
694*28f6c2f2SEnji Cooper // The returned wrapper should be initialized with a copy of that vector
695*28f6c2f2SEnji Cooper // that's embedded within the action itself (which should stay alive as long
696*28f6c2f2SEnji Cooper // as the mock object is alive), rather than e.g. a reference to the temporary
697*28f6c2f2SEnji Cooper // we feed to Return. This should work fine both for WillOnce and
698*28f6c2f2SEnji Cooper // WillRepeatedly.
699*28f6c2f2SEnji Cooper MockFunction<Result()> mock;
700*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
701*28f6c2f2SEnji Cooper .WillOnce(Return(std::vector<int>{17, 19, 23}))
702*28f6c2f2SEnji Cooper .WillRepeatedly(Return(std::vector<int>{29, 31, 37}));
703b89a7cc2SEnji Cooper
704*28f6c2f2SEnji Cooper EXPECT_THAT(mock.AsStdFunction()(),
705*28f6c2f2SEnji Cooper Field(&Result::v, Pointee(ElementsAre(17, 19, 23))));
706*28f6c2f2SEnji Cooper
707*28f6c2f2SEnji Cooper EXPECT_THAT(mock.AsStdFunction()(),
708*28f6c2f2SEnji Cooper Field(&Result::v, Pointee(ElementsAre(29, 31, 37))));
709b89a7cc2SEnji Cooper }
710b89a7cc2SEnji Cooper
TEST(ReturnTest,PrefersConversionOperator)711*28f6c2f2SEnji Cooper TEST(ReturnTest, PrefersConversionOperator) {
712*28f6c2f2SEnji Cooper // Define types In and Out such that:
713*28f6c2f2SEnji Cooper //
714*28f6c2f2SEnji Cooper // * In is implicitly convertible to Out.
715*28f6c2f2SEnji Cooper // * Out also has an explicit constructor from In.
716*28f6c2f2SEnji Cooper //
717*28f6c2f2SEnji Cooper struct In;
718*28f6c2f2SEnji Cooper struct Out {
719*28f6c2f2SEnji Cooper int x;
720*28f6c2f2SEnji Cooper
721*28f6c2f2SEnji Cooper explicit Out(const int val) : x(val) {}
722*28f6c2f2SEnji Cooper explicit Out(const In&) : x(0) {}
723*28f6c2f2SEnji Cooper };
724*28f6c2f2SEnji Cooper
725*28f6c2f2SEnji Cooper struct In {
726*28f6c2f2SEnji Cooper operator Out() const { return Out{19}; } // NOLINT
727*28f6c2f2SEnji Cooper };
728*28f6c2f2SEnji Cooper
729*28f6c2f2SEnji Cooper // Assumption check: the C++ language rules are such that a function that
730*28f6c2f2SEnji Cooper // returns Out which uses In a return statement will use the implicit
731*28f6c2f2SEnji Cooper // conversion path rather than the explicit constructor.
732*28f6c2f2SEnji Cooper EXPECT_THAT([]() -> Out { return In(); }(), Field(&Out::x, 19));
733*28f6c2f2SEnji Cooper
734*28f6c2f2SEnji Cooper // Return should work the same way: if the mock function's return type is Out
735*28f6c2f2SEnji Cooper // and we feed Return an In value, then the Out should be created through the
736*28f6c2f2SEnji Cooper // implicit conversion path rather than the explicit constructor.
737*28f6c2f2SEnji Cooper MockFunction<Out()> mock;
738*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(Return(In()));
739*28f6c2f2SEnji Cooper EXPECT_THAT(mock.AsStdFunction()(), Field(&Out::x, 19));
740*28f6c2f2SEnji Cooper }
741*28f6c2f2SEnji Cooper
742*28f6c2f2SEnji Cooper // It should be possible to use Return(R) with a mock function result type U
743*28f6c2f2SEnji Cooper // that is convertible from const R& but *not* R (such as
744*28f6c2f2SEnji Cooper // std::reference_wrapper). This should work for both WillOnce and
745*28f6c2f2SEnji Cooper // WillRepeatedly.
TEST(ReturnTest,ConversionRequiresConstLvalueReference)746*28f6c2f2SEnji Cooper TEST(ReturnTest, ConversionRequiresConstLvalueReference) {
747*28f6c2f2SEnji Cooper using R = int;
748*28f6c2f2SEnji Cooper using U = std::reference_wrapper<const int>;
749*28f6c2f2SEnji Cooper
750*28f6c2f2SEnji Cooper static_assert(std::is_convertible<const R&, U>::value, "");
751*28f6c2f2SEnji Cooper static_assert(!std::is_convertible<R, U>::value, "");
752*28f6c2f2SEnji Cooper
753*28f6c2f2SEnji Cooper MockFunction<U()> mock;
754*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(Return(17)).WillRepeatedly(Return(19));
755*28f6c2f2SEnji Cooper
756*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()());
757*28f6c2f2SEnji Cooper EXPECT_EQ(19, mock.AsStdFunction()());
758*28f6c2f2SEnji Cooper }
759*28f6c2f2SEnji Cooper
760*28f6c2f2SEnji Cooper // Return(x) should not be usable with a mock function result type that's
761*28f6c2f2SEnji Cooper // implicitly convertible from decltype(x) but requires a non-const lvalue
762*28f6c2f2SEnji Cooper // reference to the input. It doesn't make sense for the conversion operator to
763*28f6c2f2SEnji Cooper // modify the input.
TEST(ReturnTest,ConversionRequiresMutableLvalueReference)764*28f6c2f2SEnji Cooper TEST(ReturnTest, ConversionRequiresMutableLvalueReference) {
765*28f6c2f2SEnji Cooper // Set up a type that is implicitly convertible from std::string&, but not
766*28f6c2f2SEnji Cooper // std::string&& or `const std::string&`.
767*28f6c2f2SEnji Cooper //
768*28f6c2f2SEnji Cooper // Avoid asserting about conversion from std::string on MSVC, which seems to
769*28f6c2f2SEnji Cooper // implement std::is_convertible incorrectly in this case.
770*28f6c2f2SEnji Cooper struct S {
771*28f6c2f2SEnji Cooper S(std::string&) {} // NOLINT
772*28f6c2f2SEnji Cooper };
773*28f6c2f2SEnji Cooper
774*28f6c2f2SEnji Cooper static_assert(std::is_convertible<std::string&, S>::value, "");
775*28f6c2f2SEnji Cooper #ifndef _MSC_VER
776*28f6c2f2SEnji Cooper static_assert(!std::is_convertible<std::string&&, S>::value, "");
777*28f6c2f2SEnji Cooper #endif
778*28f6c2f2SEnji Cooper static_assert(!std::is_convertible<const std::string&, S>::value, "");
779*28f6c2f2SEnji Cooper
780*28f6c2f2SEnji Cooper // It shouldn't be possible to use the result of Return(std::string) in a
781*28f6c2f2SEnji Cooper // context where an S is needed.
782*28f6c2f2SEnji Cooper //
783*28f6c2f2SEnji Cooper // Here too we disable the assertion for MSVC, since its incorrect
784*28f6c2f2SEnji Cooper // implementation of is_convertible causes our SFINAE to be wrong.
785*28f6c2f2SEnji Cooper using RA = decltype(Return(std::string()));
786*28f6c2f2SEnji Cooper
787*28f6c2f2SEnji Cooper static_assert(!std::is_convertible<RA, Action<S()>>::value, "");
788*28f6c2f2SEnji Cooper #ifndef _MSC_VER
789*28f6c2f2SEnji Cooper static_assert(!std::is_convertible<RA, OnceAction<S()>>::value, "");
790*28f6c2f2SEnji Cooper #endif
791*28f6c2f2SEnji Cooper }
792*28f6c2f2SEnji Cooper
TEST(ReturnTest,MoveOnlyResultType)793*28f6c2f2SEnji Cooper TEST(ReturnTest, MoveOnlyResultType) {
794*28f6c2f2SEnji Cooper // Return should support move-only result types when used with WillOnce.
795*28f6c2f2SEnji Cooper {
796*28f6c2f2SEnji Cooper MockFunction<std::unique_ptr<int>()> mock;
797*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
798*28f6c2f2SEnji Cooper // NOLINTNEXTLINE
799*28f6c2f2SEnji Cooper .WillOnce(Return(std::unique_ptr<int>(new int(17))));
800*28f6c2f2SEnji Cooper
801*28f6c2f2SEnji Cooper EXPECT_THAT(mock.AsStdFunction()(), Pointee(17));
802*28f6c2f2SEnji Cooper }
803*28f6c2f2SEnji Cooper
804*28f6c2f2SEnji Cooper // The result of Return should not be convertible to Action (so it can't be
805*28f6c2f2SEnji Cooper // used with WillRepeatedly).
806*28f6c2f2SEnji Cooper static_assert(!std::is_convertible<decltype(Return(std::unique_ptr<int>())),
807*28f6c2f2SEnji Cooper Action<std::unique_ptr<int>()>>::value,
808*28f6c2f2SEnji Cooper "");
809*28f6c2f2SEnji Cooper }
810*28f6c2f2SEnji Cooper
811*28f6c2f2SEnji Cooper // Tests that Return(v) is covariant.
812b89a7cc2SEnji Cooper
813b89a7cc2SEnji Cooper struct Base {
operator ==testing::__anon10e2b6f40111::Base814b89a7cc2SEnji Cooper bool operator==(const Base&) { return true; }
815b89a7cc2SEnji Cooper };
816b89a7cc2SEnji Cooper
817b89a7cc2SEnji Cooper struct Derived : public Base {
operator ==testing::__anon10e2b6f40111::Derived818b89a7cc2SEnji Cooper bool operator==(const Derived&) { return true; }
819b89a7cc2SEnji Cooper };
820b89a7cc2SEnji Cooper
TEST(ReturnTest,IsCovariant)821b89a7cc2SEnji Cooper TEST(ReturnTest, IsCovariant) {
822b89a7cc2SEnji Cooper Base base;
823b89a7cc2SEnji Cooper Derived derived;
824b89a7cc2SEnji Cooper Action<Base*()> ret = Return(&base);
825*28f6c2f2SEnji Cooper EXPECT_EQ(&base, ret.Perform(std::make_tuple()));
826b89a7cc2SEnji Cooper
827b89a7cc2SEnji Cooper ret = Return(&derived);
828*28f6c2f2SEnji Cooper EXPECT_EQ(&derived, ret.Perform(std::make_tuple()));
829b89a7cc2SEnji Cooper }
830b89a7cc2SEnji Cooper
831b89a7cc2SEnji Cooper // Tests that the type of the value passed into Return is converted into T
832b89a7cc2SEnji Cooper // when the action is cast to Action<T(...)> rather than when the action is
833b89a7cc2SEnji Cooper // performed. See comments on testing::internal::ReturnAction in
834b89a7cc2SEnji Cooper // gmock-actions.h for more information.
835b89a7cc2SEnji Cooper class FromType {
836b89a7cc2SEnji Cooper public:
FromType(bool * is_converted)837b89a7cc2SEnji Cooper explicit FromType(bool* is_converted) : converted_(is_converted) {}
converted() const838b89a7cc2SEnji Cooper bool* converted() const { return converted_; }
839b89a7cc2SEnji Cooper
840b89a7cc2SEnji Cooper private:
841b89a7cc2SEnji Cooper bool* const converted_;
842b89a7cc2SEnji Cooper };
843b89a7cc2SEnji Cooper
844b89a7cc2SEnji Cooper class ToType {
845b89a7cc2SEnji Cooper public:
846b89a7cc2SEnji Cooper // Must allow implicit conversion due to use in ImplicitCast_<T>.
ToType(const FromType & x)847b89a7cc2SEnji Cooper ToType(const FromType& x) { *x.converted() = true; } // NOLINT
848b89a7cc2SEnji Cooper };
849b89a7cc2SEnji Cooper
TEST(ReturnTest,ConvertsArgumentWhenConverted)850b89a7cc2SEnji Cooper TEST(ReturnTest, ConvertsArgumentWhenConverted) {
851b89a7cc2SEnji Cooper bool converted = false;
852b89a7cc2SEnji Cooper FromType x(&converted);
853b89a7cc2SEnji Cooper Action<ToType()> action(Return(x));
854b89a7cc2SEnji Cooper EXPECT_TRUE(converted) << "Return must convert its argument in its own "
855b89a7cc2SEnji Cooper << "conversion operator.";
856b89a7cc2SEnji Cooper converted = false;
857*28f6c2f2SEnji Cooper action.Perform(std::tuple<>());
858b89a7cc2SEnji Cooper EXPECT_FALSE(converted) << "Action must NOT convert its argument "
859b89a7cc2SEnji Cooper << "when performed.";
860b89a7cc2SEnji Cooper }
861b89a7cc2SEnji Cooper
862b89a7cc2SEnji Cooper // Tests that ReturnNull() returns NULL in a pointer-returning function.
TEST(ReturnNullTest,WorksInPointerReturningFunction)863b89a7cc2SEnji Cooper TEST(ReturnNullTest, WorksInPointerReturningFunction) {
864b89a7cc2SEnji Cooper const Action<int*()> a1 = ReturnNull();
865*28f6c2f2SEnji Cooper EXPECT_TRUE(a1.Perform(std::make_tuple()) == nullptr);
866b89a7cc2SEnji Cooper
867b89a7cc2SEnji Cooper const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
868*28f6c2f2SEnji Cooper EXPECT_TRUE(a2.Perform(std::make_tuple(true)) == nullptr);
869b89a7cc2SEnji Cooper }
870b89a7cc2SEnji Cooper
871b89a7cc2SEnji Cooper // Tests that ReturnNull() returns NULL for shared_ptr and unique_ptr returning
872b89a7cc2SEnji Cooper // functions.
TEST(ReturnNullTest,WorksInSmartPointerReturningFunction)873b89a7cc2SEnji Cooper TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
874b89a7cc2SEnji Cooper const Action<std::unique_ptr<const int>()> a1 = ReturnNull();
875*28f6c2f2SEnji Cooper EXPECT_TRUE(a1.Perform(std::make_tuple()) == nullptr);
876b89a7cc2SEnji Cooper
877b89a7cc2SEnji Cooper const Action<std::shared_ptr<int>(std::string)> a2 = ReturnNull();
878*28f6c2f2SEnji Cooper EXPECT_TRUE(a2.Perform(std::make_tuple("foo")) == nullptr);
879b89a7cc2SEnji Cooper }
880b89a7cc2SEnji Cooper
881b89a7cc2SEnji Cooper // Tests that ReturnRef(v) works for reference types.
TEST(ReturnRefTest,WorksForReference)882b89a7cc2SEnji Cooper TEST(ReturnRefTest, WorksForReference) {
883b89a7cc2SEnji Cooper const int n = 0;
884b89a7cc2SEnji Cooper const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
885b89a7cc2SEnji Cooper
886*28f6c2f2SEnji Cooper EXPECT_EQ(&n, &ret.Perform(std::make_tuple(true)));
887b89a7cc2SEnji Cooper }
888b89a7cc2SEnji Cooper
889b89a7cc2SEnji Cooper // Tests that ReturnRef(v) is covariant.
TEST(ReturnRefTest,IsCovariant)890b89a7cc2SEnji Cooper TEST(ReturnRefTest, IsCovariant) {
891b89a7cc2SEnji Cooper Base base;
892b89a7cc2SEnji Cooper Derived derived;
893b89a7cc2SEnji Cooper Action<Base&()> a = ReturnRef(base);
894*28f6c2f2SEnji Cooper EXPECT_EQ(&base, &a.Perform(std::make_tuple()));
895b89a7cc2SEnji Cooper
896b89a7cc2SEnji Cooper a = ReturnRef(derived);
897*28f6c2f2SEnji Cooper EXPECT_EQ(&derived, &a.Perform(std::make_tuple()));
898*28f6c2f2SEnji Cooper }
899*28f6c2f2SEnji Cooper
900*28f6c2f2SEnji Cooper template <typename T, typename = decltype(ReturnRef(std::declval<T&&>()))>
CanCallReturnRef(T &&)901*28f6c2f2SEnji Cooper bool CanCallReturnRef(T&&) {
902*28f6c2f2SEnji Cooper return true;
903*28f6c2f2SEnji Cooper }
CanCallReturnRef(Unused)904*28f6c2f2SEnji Cooper bool CanCallReturnRef(Unused) { return false; }
905*28f6c2f2SEnji Cooper
906*28f6c2f2SEnji Cooper // Tests that ReturnRef(v) is working with non-temporaries (T&)
TEST(ReturnRefTest,WorksForNonTemporary)907*28f6c2f2SEnji Cooper TEST(ReturnRefTest, WorksForNonTemporary) {
908*28f6c2f2SEnji Cooper int scalar_value = 123;
909*28f6c2f2SEnji Cooper EXPECT_TRUE(CanCallReturnRef(scalar_value));
910*28f6c2f2SEnji Cooper
911*28f6c2f2SEnji Cooper std::string non_scalar_value("ABC");
912*28f6c2f2SEnji Cooper EXPECT_TRUE(CanCallReturnRef(non_scalar_value));
913*28f6c2f2SEnji Cooper
914*28f6c2f2SEnji Cooper const int const_scalar_value{321};
915*28f6c2f2SEnji Cooper EXPECT_TRUE(CanCallReturnRef(const_scalar_value));
916*28f6c2f2SEnji Cooper
917*28f6c2f2SEnji Cooper const std::string const_non_scalar_value("CBA");
918*28f6c2f2SEnji Cooper EXPECT_TRUE(CanCallReturnRef(const_non_scalar_value));
919*28f6c2f2SEnji Cooper }
920*28f6c2f2SEnji Cooper
921*28f6c2f2SEnji Cooper // Tests that ReturnRef(v) is not working with temporaries (T&&)
TEST(ReturnRefTest,DoesNotWorkForTemporary)922*28f6c2f2SEnji Cooper TEST(ReturnRefTest, DoesNotWorkForTemporary) {
923*28f6c2f2SEnji Cooper auto scalar_value = []() -> int { return 123; };
924*28f6c2f2SEnji Cooper EXPECT_FALSE(CanCallReturnRef(scalar_value()));
925*28f6c2f2SEnji Cooper
926*28f6c2f2SEnji Cooper auto non_scalar_value = []() -> std::string { return "ABC"; };
927*28f6c2f2SEnji Cooper EXPECT_FALSE(CanCallReturnRef(non_scalar_value()));
928*28f6c2f2SEnji Cooper
929*28f6c2f2SEnji Cooper // cannot use here callable returning "const scalar type",
930*28f6c2f2SEnji Cooper // because such const for scalar return type is ignored
931*28f6c2f2SEnji Cooper EXPECT_FALSE(CanCallReturnRef(static_cast<const int>(321)));
932*28f6c2f2SEnji Cooper
933*28f6c2f2SEnji Cooper auto const_non_scalar_value = []() -> const std::string { return "CBA"; };
934*28f6c2f2SEnji Cooper EXPECT_FALSE(CanCallReturnRef(const_non_scalar_value()));
935b89a7cc2SEnji Cooper }
936b89a7cc2SEnji Cooper
937b89a7cc2SEnji Cooper // Tests that ReturnRefOfCopy(v) works for reference types.
TEST(ReturnRefOfCopyTest,WorksForReference)938b89a7cc2SEnji Cooper TEST(ReturnRefOfCopyTest, WorksForReference) {
939b89a7cc2SEnji Cooper int n = 42;
940b89a7cc2SEnji Cooper const Action<const int&()> ret = ReturnRefOfCopy(n);
941b89a7cc2SEnji Cooper
942*28f6c2f2SEnji Cooper EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
943*28f6c2f2SEnji Cooper EXPECT_EQ(42, ret.Perform(std::make_tuple()));
944b89a7cc2SEnji Cooper
945b89a7cc2SEnji Cooper n = 43;
946*28f6c2f2SEnji Cooper EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
947*28f6c2f2SEnji Cooper EXPECT_EQ(42, ret.Perform(std::make_tuple()));
948b89a7cc2SEnji Cooper }
949b89a7cc2SEnji Cooper
950b89a7cc2SEnji Cooper // Tests that ReturnRefOfCopy(v) is covariant.
TEST(ReturnRefOfCopyTest,IsCovariant)951b89a7cc2SEnji Cooper TEST(ReturnRefOfCopyTest, IsCovariant) {
952b89a7cc2SEnji Cooper Base base;
953b89a7cc2SEnji Cooper Derived derived;
954b89a7cc2SEnji Cooper Action<Base&()> a = ReturnRefOfCopy(base);
955*28f6c2f2SEnji Cooper EXPECT_NE(&base, &a.Perform(std::make_tuple()));
956b89a7cc2SEnji Cooper
957b89a7cc2SEnji Cooper a = ReturnRefOfCopy(derived);
958*28f6c2f2SEnji Cooper EXPECT_NE(&derived, &a.Perform(std::make_tuple()));
959*28f6c2f2SEnji Cooper }
960*28f6c2f2SEnji Cooper
961*28f6c2f2SEnji Cooper // Tests that ReturnRoundRobin(v) works with initializer lists
TEST(ReturnRoundRobinTest,WorksForInitList)962*28f6c2f2SEnji Cooper TEST(ReturnRoundRobinTest, WorksForInitList) {
963*28f6c2f2SEnji Cooper Action<int()> ret = ReturnRoundRobin({1, 2, 3});
964*28f6c2f2SEnji Cooper
965*28f6c2f2SEnji Cooper EXPECT_EQ(1, ret.Perform(std::make_tuple()));
966*28f6c2f2SEnji Cooper EXPECT_EQ(2, ret.Perform(std::make_tuple()));
967*28f6c2f2SEnji Cooper EXPECT_EQ(3, ret.Perform(std::make_tuple()));
968*28f6c2f2SEnji Cooper EXPECT_EQ(1, ret.Perform(std::make_tuple()));
969*28f6c2f2SEnji Cooper EXPECT_EQ(2, ret.Perform(std::make_tuple()));
970*28f6c2f2SEnji Cooper EXPECT_EQ(3, ret.Perform(std::make_tuple()));
971*28f6c2f2SEnji Cooper }
972*28f6c2f2SEnji Cooper
973*28f6c2f2SEnji Cooper // Tests that ReturnRoundRobin(v) works with vectors
TEST(ReturnRoundRobinTest,WorksForVector)974*28f6c2f2SEnji Cooper TEST(ReturnRoundRobinTest, WorksForVector) {
975*28f6c2f2SEnji Cooper std::vector<double> v = {4.4, 5.5, 6.6};
976*28f6c2f2SEnji Cooper Action<double()> ret = ReturnRoundRobin(v);
977*28f6c2f2SEnji Cooper
978*28f6c2f2SEnji Cooper EXPECT_EQ(4.4, ret.Perform(std::make_tuple()));
979*28f6c2f2SEnji Cooper EXPECT_EQ(5.5, ret.Perform(std::make_tuple()));
980*28f6c2f2SEnji Cooper EXPECT_EQ(6.6, ret.Perform(std::make_tuple()));
981*28f6c2f2SEnji Cooper EXPECT_EQ(4.4, ret.Perform(std::make_tuple()));
982*28f6c2f2SEnji Cooper EXPECT_EQ(5.5, ret.Perform(std::make_tuple()));
983*28f6c2f2SEnji Cooper EXPECT_EQ(6.6, ret.Perform(std::make_tuple()));
984b89a7cc2SEnji Cooper }
985b89a7cc2SEnji Cooper
986b89a7cc2SEnji Cooper // Tests that DoDefault() does the default action for the mock method.
987b89a7cc2SEnji Cooper
988b89a7cc2SEnji Cooper class MockClass {
989b89a7cc2SEnji Cooper public:
990*28f6c2f2SEnji Cooper MockClass() = default;
991b89a7cc2SEnji Cooper
992b89a7cc2SEnji Cooper MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
993b89a7cc2SEnji Cooper MOCK_METHOD0(Foo, MyNonDefaultConstructible());
994b89a7cc2SEnji Cooper MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
995b89a7cc2SEnji Cooper MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>());
996b89a7cc2SEnji Cooper MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
997b89a7cc2SEnji Cooper MOCK_METHOD1(TakeUnique, int(std::unique_ptr<int>));
998b89a7cc2SEnji Cooper MOCK_METHOD2(TakeUnique,
999b89a7cc2SEnji Cooper int(const std::unique_ptr<int>&, std::unique_ptr<int>));
1000b89a7cc2SEnji Cooper
1001b89a7cc2SEnji Cooper private:
1002*28f6c2f2SEnji Cooper MockClass(const MockClass&) = delete;
1003*28f6c2f2SEnji Cooper MockClass& operator=(const MockClass&) = delete;
1004b89a7cc2SEnji Cooper };
1005b89a7cc2SEnji Cooper
1006b89a7cc2SEnji Cooper // Tests that DoDefault() returns the built-in default value for the
1007b89a7cc2SEnji Cooper // return type by default.
TEST(DoDefaultTest,ReturnsBuiltInDefaultValueByDefault)1008b89a7cc2SEnji Cooper TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
1009b89a7cc2SEnji Cooper MockClass mock;
1010*28f6c2f2SEnji Cooper EXPECT_CALL(mock, IntFunc(_)).WillOnce(DoDefault());
1011b89a7cc2SEnji Cooper EXPECT_EQ(0, mock.IntFunc(true));
1012b89a7cc2SEnji Cooper }
1013b89a7cc2SEnji Cooper
1014b89a7cc2SEnji Cooper // Tests that DoDefault() throws (when exceptions are enabled) or aborts
1015b89a7cc2SEnji Cooper // the process when there is no built-in default value for the return type.
TEST(DoDefaultDeathTest,DiesForUnknowType)1016b89a7cc2SEnji Cooper TEST(DoDefaultDeathTest, DiesForUnknowType) {
1017b89a7cc2SEnji Cooper MockClass mock;
1018*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Foo()).WillRepeatedly(DoDefault());
1019b89a7cc2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
1020b89a7cc2SEnji Cooper EXPECT_ANY_THROW(mock.Foo());
1021b89a7cc2SEnji Cooper #else
1022*28f6c2f2SEnji Cooper EXPECT_DEATH_IF_SUPPORTED({ mock.Foo(); }, "");
1023b89a7cc2SEnji Cooper #endif
1024b89a7cc2SEnji Cooper }
1025b89a7cc2SEnji Cooper
1026b89a7cc2SEnji Cooper // Tests that using DoDefault() inside a composite action leads to a
1027b89a7cc2SEnji Cooper // run-time error.
1028b89a7cc2SEnji Cooper
VoidFunc(bool)1029b89a7cc2SEnji Cooper void VoidFunc(bool /* flag */) {}
1030b89a7cc2SEnji Cooper
TEST(DoDefaultDeathTest,DiesIfUsedInCompositeAction)1031b89a7cc2SEnji Cooper TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
1032b89a7cc2SEnji Cooper MockClass mock;
1033b89a7cc2SEnji Cooper EXPECT_CALL(mock, IntFunc(_))
1034*28f6c2f2SEnji Cooper .WillRepeatedly(DoAll(Invoke(VoidFunc), DoDefault()));
1035b89a7cc2SEnji Cooper
1036b89a7cc2SEnji Cooper // Ideally we should verify the error message as well. Sadly,
1037b89a7cc2SEnji Cooper // EXPECT_DEATH() can only capture stderr, while Google Mock's
1038b89a7cc2SEnji Cooper // errors are printed on stdout. Therefore we have to settle for
1039b89a7cc2SEnji Cooper // not verifying the message.
1040*28f6c2f2SEnji Cooper EXPECT_DEATH_IF_SUPPORTED({ mock.IntFunc(true); }, "");
1041b89a7cc2SEnji Cooper }
1042b89a7cc2SEnji Cooper
1043b89a7cc2SEnji Cooper // Tests that DoDefault() returns the default value set by
1044*28f6c2f2SEnji Cooper // DefaultValue<T>::Set() when it's not overridden by an ON_CALL().
TEST(DoDefaultTest,ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne)1045b89a7cc2SEnji Cooper TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
1046b89a7cc2SEnji Cooper DefaultValue<int>::Set(1);
1047b89a7cc2SEnji Cooper MockClass mock;
1048*28f6c2f2SEnji Cooper EXPECT_CALL(mock, IntFunc(_)).WillOnce(DoDefault());
1049b89a7cc2SEnji Cooper EXPECT_EQ(1, mock.IntFunc(false));
1050b89a7cc2SEnji Cooper DefaultValue<int>::Clear();
1051b89a7cc2SEnji Cooper }
1052b89a7cc2SEnji Cooper
1053b89a7cc2SEnji Cooper // Tests that DoDefault() does the action specified by ON_CALL().
TEST(DoDefaultTest,DoesWhatOnCallSpecifies)1054b89a7cc2SEnji Cooper TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
1055b89a7cc2SEnji Cooper MockClass mock;
1056*28f6c2f2SEnji Cooper ON_CALL(mock, IntFunc(_)).WillByDefault(Return(2));
1057*28f6c2f2SEnji Cooper EXPECT_CALL(mock, IntFunc(_)).WillOnce(DoDefault());
1058b89a7cc2SEnji Cooper EXPECT_EQ(2, mock.IntFunc(false));
1059b89a7cc2SEnji Cooper }
1060b89a7cc2SEnji Cooper
1061b89a7cc2SEnji Cooper // Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
TEST(DoDefaultTest,CannotBeUsedInOnCall)1062b89a7cc2SEnji Cooper TEST(DoDefaultTest, CannotBeUsedInOnCall) {
1063b89a7cc2SEnji Cooper MockClass mock;
1064*28f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
1065*28f6c2f2SEnji Cooper { // NOLINT
1066*28f6c2f2SEnji Cooper ON_CALL(mock, IntFunc(_)).WillByDefault(DoDefault());
1067*28f6c2f2SEnji Cooper },
1068*28f6c2f2SEnji Cooper "DoDefault() cannot be used in ON_CALL()");
1069b89a7cc2SEnji Cooper }
1070b89a7cc2SEnji Cooper
1071b89a7cc2SEnji Cooper // Tests that SetArgPointee<N>(v) sets the variable pointed to by
1072b89a7cc2SEnji Cooper // the N-th (0-based) argument to v.
TEST(SetArgPointeeTest,SetsTheNthPointee)1073b89a7cc2SEnji Cooper TEST(SetArgPointeeTest, SetsTheNthPointee) {
1074b89a7cc2SEnji Cooper typedef void MyFunction(bool, int*, char*);
1075b89a7cc2SEnji Cooper Action<MyFunction> a = SetArgPointee<1>(2);
1076b89a7cc2SEnji Cooper
1077b89a7cc2SEnji Cooper int n = 0;
1078b89a7cc2SEnji Cooper char ch = '\0';
1079*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(true, &n, &ch));
1080b89a7cc2SEnji Cooper EXPECT_EQ(2, n);
1081b89a7cc2SEnji Cooper EXPECT_EQ('\0', ch);
1082b89a7cc2SEnji Cooper
1083b89a7cc2SEnji Cooper a = SetArgPointee<2>('a');
1084b89a7cc2SEnji Cooper n = 0;
1085b89a7cc2SEnji Cooper ch = '\0';
1086*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(true, &n, &ch));
1087b89a7cc2SEnji Cooper EXPECT_EQ(0, n);
1088b89a7cc2SEnji Cooper EXPECT_EQ('a', ch);
1089b89a7cc2SEnji Cooper }
1090b89a7cc2SEnji Cooper
1091b89a7cc2SEnji Cooper // Tests that SetArgPointee<N>() accepts a string literal.
TEST(SetArgPointeeTest,AcceptsStringLiteral)1092b89a7cc2SEnji Cooper TEST(SetArgPointeeTest, AcceptsStringLiteral) {
1093b89a7cc2SEnji Cooper typedef void MyFunction(std::string*, const char**);
1094b89a7cc2SEnji Cooper Action<MyFunction> a = SetArgPointee<0>("hi");
1095b89a7cc2SEnji Cooper std::string str;
1096*28f6c2f2SEnji Cooper const char* ptr = nullptr;
1097*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(&str, &ptr));
1098b89a7cc2SEnji Cooper EXPECT_EQ("hi", str);
1099*28f6c2f2SEnji Cooper EXPECT_TRUE(ptr == nullptr);
1100b89a7cc2SEnji Cooper
1101b89a7cc2SEnji Cooper a = SetArgPointee<1>("world");
1102b89a7cc2SEnji Cooper str = "";
1103*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(&str, &ptr));
1104b89a7cc2SEnji Cooper EXPECT_EQ("", str);
1105b89a7cc2SEnji Cooper EXPECT_STREQ("world", ptr);
1106b89a7cc2SEnji Cooper }
1107b89a7cc2SEnji Cooper
TEST(SetArgPointeeTest,AcceptsWideStringLiteral)1108b89a7cc2SEnji Cooper TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
1109b89a7cc2SEnji Cooper typedef void MyFunction(const wchar_t**);
1110b89a7cc2SEnji Cooper Action<MyFunction> a = SetArgPointee<0>(L"world");
1111*28f6c2f2SEnji Cooper const wchar_t* ptr = nullptr;
1112*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(&ptr));
1113b89a7cc2SEnji Cooper EXPECT_STREQ(L"world", ptr);
1114b89a7cc2SEnji Cooper
1115b89a7cc2SEnji Cooper #if GTEST_HAS_STD_WSTRING
1116b89a7cc2SEnji Cooper
1117b89a7cc2SEnji Cooper typedef void MyStringFunction(std::wstring*);
1118b89a7cc2SEnji Cooper Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
1119b89a7cc2SEnji Cooper std::wstring str = L"";
1120*28f6c2f2SEnji Cooper a2.Perform(std::make_tuple(&str));
1121b89a7cc2SEnji Cooper EXPECT_EQ(L"world", str);
1122b89a7cc2SEnji Cooper
1123b89a7cc2SEnji Cooper #endif
1124b89a7cc2SEnji Cooper }
1125b89a7cc2SEnji Cooper
1126b89a7cc2SEnji Cooper // Tests that SetArgPointee<N>() accepts a char pointer.
TEST(SetArgPointeeTest,AcceptsCharPointer)1127b89a7cc2SEnji Cooper TEST(SetArgPointeeTest, AcceptsCharPointer) {
1128b89a7cc2SEnji Cooper typedef void MyFunction(bool, std::string*, const char**);
1129b89a7cc2SEnji Cooper const char* const hi = "hi";
1130b89a7cc2SEnji Cooper Action<MyFunction> a = SetArgPointee<1>(hi);
1131b89a7cc2SEnji Cooper std::string str;
1132*28f6c2f2SEnji Cooper const char* ptr = nullptr;
1133*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(true, &str, &ptr));
1134b89a7cc2SEnji Cooper EXPECT_EQ("hi", str);
1135*28f6c2f2SEnji Cooper EXPECT_TRUE(ptr == nullptr);
1136b89a7cc2SEnji Cooper
1137b89a7cc2SEnji Cooper char world_array[] = "world";
1138b89a7cc2SEnji Cooper char* const world = world_array;
1139b89a7cc2SEnji Cooper a = SetArgPointee<2>(world);
1140b89a7cc2SEnji Cooper str = "";
1141*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(true, &str, &ptr));
1142b89a7cc2SEnji Cooper EXPECT_EQ("", str);
1143b89a7cc2SEnji Cooper EXPECT_EQ(world, ptr);
1144b89a7cc2SEnji Cooper }
1145b89a7cc2SEnji Cooper
TEST(SetArgPointeeTest,AcceptsWideCharPointer)1146b89a7cc2SEnji Cooper TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
1147b89a7cc2SEnji Cooper typedef void MyFunction(bool, const wchar_t**);
1148b89a7cc2SEnji Cooper const wchar_t* const hi = L"hi";
1149b89a7cc2SEnji Cooper Action<MyFunction> a = SetArgPointee<1>(hi);
1150*28f6c2f2SEnji Cooper const wchar_t* ptr = nullptr;
1151*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(true, &ptr));
1152b89a7cc2SEnji Cooper EXPECT_EQ(hi, ptr);
1153b89a7cc2SEnji Cooper
1154b89a7cc2SEnji Cooper #if GTEST_HAS_STD_WSTRING
1155b89a7cc2SEnji Cooper
1156b89a7cc2SEnji Cooper typedef void MyStringFunction(bool, std::wstring*);
1157b89a7cc2SEnji Cooper wchar_t world_array[] = L"world";
1158b89a7cc2SEnji Cooper wchar_t* const world = world_array;
1159b89a7cc2SEnji Cooper Action<MyStringFunction> a2 = SetArgPointee<1>(world);
1160b89a7cc2SEnji Cooper std::wstring str;
1161*28f6c2f2SEnji Cooper a2.Perform(std::make_tuple(true, &str));
1162b89a7cc2SEnji Cooper EXPECT_EQ(world_array, str);
1163b89a7cc2SEnji Cooper #endif
1164b89a7cc2SEnji Cooper }
1165b89a7cc2SEnji Cooper
1166b89a7cc2SEnji Cooper // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
1167b89a7cc2SEnji Cooper // the N-th (0-based) argument to v.
TEST(SetArgumentPointeeTest,SetsTheNthPointee)1168b89a7cc2SEnji Cooper TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
1169b89a7cc2SEnji Cooper typedef void MyFunction(bool, int*, char*);
1170b89a7cc2SEnji Cooper Action<MyFunction> a = SetArgumentPointee<1>(2);
1171b89a7cc2SEnji Cooper
1172b89a7cc2SEnji Cooper int n = 0;
1173b89a7cc2SEnji Cooper char ch = '\0';
1174*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(true, &n, &ch));
1175b89a7cc2SEnji Cooper EXPECT_EQ(2, n);
1176b89a7cc2SEnji Cooper EXPECT_EQ('\0', ch);
1177b89a7cc2SEnji Cooper
1178b89a7cc2SEnji Cooper a = SetArgumentPointee<2>('a');
1179b89a7cc2SEnji Cooper n = 0;
1180b89a7cc2SEnji Cooper ch = '\0';
1181*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(true, &n, &ch));
1182b89a7cc2SEnji Cooper EXPECT_EQ(0, n);
1183b89a7cc2SEnji Cooper EXPECT_EQ('a', ch);
1184b89a7cc2SEnji Cooper }
1185b89a7cc2SEnji Cooper
1186b89a7cc2SEnji Cooper // Sample functions and functors for testing Invoke() and etc.
Nullary()1187b89a7cc2SEnji Cooper int Nullary() { return 1; }
1188b89a7cc2SEnji Cooper
1189b89a7cc2SEnji Cooper class NullaryFunctor {
1190b89a7cc2SEnji Cooper public:
operator ()()1191b89a7cc2SEnji Cooper int operator()() { return 2; }
1192b89a7cc2SEnji Cooper };
1193b89a7cc2SEnji Cooper
1194b89a7cc2SEnji Cooper bool g_done = false;
VoidNullary()1195b89a7cc2SEnji Cooper void VoidNullary() { g_done = true; }
1196b89a7cc2SEnji Cooper
1197b89a7cc2SEnji Cooper class VoidNullaryFunctor {
1198b89a7cc2SEnji Cooper public:
operator ()()1199b89a7cc2SEnji Cooper void operator()() { g_done = true; }
1200b89a7cc2SEnji Cooper };
1201b89a7cc2SEnji Cooper
Short(short n)1202*28f6c2f2SEnji Cooper short Short(short n) { return n; } // NOLINT
Char(char ch)1203*28f6c2f2SEnji Cooper char Char(char ch) { return ch; }
1204*28f6c2f2SEnji Cooper
CharPtr(const char * s)1205*28f6c2f2SEnji Cooper const char* CharPtr(const char* s) { return s; }
1206*28f6c2f2SEnji Cooper
Unary(int x)1207*28f6c2f2SEnji Cooper bool Unary(int x) { return x < 0; }
1208*28f6c2f2SEnji Cooper
Binary(const char * input,short n)1209*28f6c2f2SEnji Cooper const char* Binary(const char* input, short n) { return input + n; } // NOLINT
1210*28f6c2f2SEnji Cooper
VoidBinary(int,char)1211*28f6c2f2SEnji Cooper void VoidBinary(int, char) { g_done = true; }
1212*28f6c2f2SEnji Cooper
Ternary(int x,char y,short z)1213*28f6c2f2SEnji Cooper int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
1214*28f6c2f2SEnji Cooper
SumOf4(int a,int b,int c,int d)1215*28f6c2f2SEnji Cooper int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
1216*28f6c2f2SEnji Cooper
1217b89a7cc2SEnji Cooper class Foo {
1218b89a7cc2SEnji Cooper public:
Foo()1219b89a7cc2SEnji Cooper Foo() : value_(123) {}
1220b89a7cc2SEnji Cooper
Nullary() const1221b89a7cc2SEnji Cooper int Nullary() const { return value_; }
1222b89a7cc2SEnji Cooper
1223b89a7cc2SEnji Cooper private:
1224b89a7cc2SEnji Cooper int value_;
1225b89a7cc2SEnji Cooper };
1226b89a7cc2SEnji Cooper
1227b89a7cc2SEnji Cooper // Tests InvokeWithoutArgs(function).
TEST(InvokeWithoutArgsTest,Function)1228b89a7cc2SEnji Cooper TEST(InvokeWithoutArgsTest, Function) {
1229b89a7cc2SEnji Cooper // As an action that takes one argument.
1230b89a7cc2SEnji Cooper Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
1231*28f6c2f2SEnji Cooper EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
1232b89a7cc2SEnji Cooper
1233b89a7cc2SEnji Cooper // As an action that takes two arguments.
1234b89a7cc2SEnji Cooper Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
1235*28f6c2f2SEnji Cooper EXPECT_EQ(1, a2.Perform(std::make_tuple(2, 3.5)));
1236b89a7cc2SEnji Cooper
1237b89a7cc2SEnji Cooper // As an action that returns void.
1238b89a7cc2SEnji Cooper Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
1239b89a7cc2SEnji Cooper g_done = false;
1240*28f6c2f2SEnji Cooper a3.Perform(std::make_tuple(1));
1241b89a7cc2SEnji Cooper EXPECT_TRUE(g_done);
1242b89a7cc2SEnji Cooper }
1243b89a7cc2SEnji Cooper
1244b89a7cc2SEnji Cooper // Tests InvokeWithoutArgs(functor).
TEST(InvokeWithoutArgsTest,Functor)1245b89a7cc2SEnji Cooper TEST(InvokeWithoutArgsTest, Functor) {
1246b89a7cc2SEnji Cooper // As an action that takes no argument.
1247b89a7cc2SEnji Cooper Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
1248*28f6c2f2SEnji Cooper EXPECT_EQ(2, a.Perform(std::make_tuple()));
1249b89a7cc2SEnji Cooper
1250b89a7cc2SEnji Cooper // As an action that takes three arguments.
1251b89a7cc2SEnji Cooper Action<int(int, double, char)> a2 = // NOLINT
1252b89a7cc2SEnji Cooper InvokeWithoutArgs(NullaryFunctor());
1253*28f6c2f2SEnji Cooper EXPECT_EQ(2, a2.Perform(std::make_tuple(3, 3.5, 'a')));
1254b89a7cc2SEnji Cooper
1255b89a7cc2SEnji Cooper // As an action that returns void.
1256b89a7cc2SEnji Cooper Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
1257b89a7cc2SEnji Cooper g_done = false;
1258*28f6c2f2SEnji Cooper a3.Perform(std::make_tuple());
1259b89a7cc2SEnji Cooper EXPECT_TRUE(g_done);
1260b89a7cc2SEnji Cooper }
1261b89a7cc2SEnji Cooper
1262b89a7cc2SEnji Cooper // Tests InvokeWithoutArgs(obj_ptr, method).
TEST(InvokeWithoutArgsTest,Method)1263b89a7cc2SEnji Cooper TEST(InvokeWithoutArgsTest, Method) {
1264b89a7cc2SEnji Cooper Foo foo;
1265b89a7cc2SEnji Cooper Action<int(bool, char)> a = // NOLINT
1266b89a7cc2SEnji Cooper InvokeWithoutArgs(&foo, &Foo::Nullary);
1267*28f6c2f2SEnji Cooper EXPECT_EQ(123, a.Perform(std::make_tuple(true, 'a')));
1268b89a7cc2SEnji Cooper }
1269b89a7cc2SEnji Cooper
1270b89a7cc2SEnji Cooper // Tests using IgnoreResult() on a polymorphic action.
TEST(IgnoreResultTest,PolymorphicAction)1271b89a7cc2SEnji Cooper TEST(IgnoreResultTest, PolymorphicAction) {
1272b89a7cc2SEnji Cooper Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
1273*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(1));
1274b89a7cc2SEnji Cooper }
1275b89a7cc2SEnji Cooper
1276b89a7cc2SEnji Cooper // Tests using IgnoreResult() on a monomorphic action.
1277b89a7cc2SEnji Cooper
ReturnOne()1278b89a7cc2SEnji Cooper int ReturnOne() {
1279b89a7cc2SEnji Cooper g_done = true;
1280b89a7cc2SEnji Cooper return 1;
1281b89a7cc2SEnji Cooper }
1282b89a7cc2SEnji Cooper
TEST(IgnoreResultTest,MonomorphicAction)1283b89a7cc2SEnji Cooper TEST(IgnoreResultTest, MonomorphicAction) {
1284b89a7cc2SEnji Cooper g_done = false;
1285b89a7cc2SEnji Cooper Action<void()> a = IgnoreResult(Invoke(ReturnOne));
1286*28f6c2f2SEnji Cooper a.Perform(std::make_tuple());
1287b89a7cc2SEnji Cooper EXPECT_TRUE(g_done);
1288b89a7cc2SEnji Cooper }
1289b89a7cc2SEnji Cooper
1290b89a7cc2SEnji Cooper // Tests using IgnoreResult() on an action that returns a class type.
1291b89a7cc2SEnji Cooper
ReturnMyNonDefaultConstructible(double)1292b89a7cc2SEnji Cooper MyNonDefaultConstructible ReturnMyNonDefaultConstructible(double /* x */) {
1293b89a7cc2SEnji Cooper g_done = true;
1294b89a7cc2SEnji Cooper return MyNonDefaultConstructible(42);
1295b89a7cc2SEnji Cooper }
1296b89a7cc2SEnji Cooper
TEST(IgnoreResultTest,ActionReturningClass)1297b89a7cc2SEnji Cooper TEST(IgnoreResultTest, ActionReturningClass) {
1298b89a7cc2SEnji Cooper g_done = false;
1299b89a7cc2SEnji Cooper Action<void(int)> a =
1300b89a7cc2SEnji Cooper IgnoreResult(Invoke(ReturnMyNonDefaultConstructible)); // NOLINT
1301*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(2));
1302b89a7cc2SEnji Cooper EXPECT_TRUE(g_done);
1303b89a7cc2SEnji Cooper }
1304b89a7cc2SEnji Cooper
TEST(AssignTest,Int)1305b89a7cc2SEnji Cooper TEST(AssignTest, Int) {
1306b89a7cc2SEnji Cooper int x = 0;
1307b89a7cc2SEnji Cooper Action<void(int)> a = Assign(&x, 5);
1308*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(0));
1309b89a7cc2SEnji Cooper EXPECT_EQ(5, x);
1310b89a7cc2SEnji Cooper }
1311b89a7cc2SEnji Cooper
TEST(AssignTest,String)1312b89a7cc2SEnji Cooper TEST(AssignTest, String) {
1313b89a7cc2SEnji Cooper ::std::string x;
1314b89a7cc2SEnji Cooper Action<void(void)> a = Assign(&x, "Hello, world");
1315*28f6c2f2SEnji Cooper a.Perform(std::make_tuple());
1316b89a7cc2SEnji Cooper EXPECT_EQ("Hello, world", x);
1317b89a7cc2SEnji Cooper }
1318b89a7cc2SEnji Cooper
TEST(AssignTest,CompatibleTypes)1319b89a7cc2SEnji Cooper TEST(AssignTest, CompatibleTypes) {
1320b89a7cc2SEnji Cooper double x = 0;
1321b89a7cc2SEnji Cooper Action<void(int)> a = Assign(&x, 5);
1322*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(0));
1323b89a7cc2SEnji Cooper EXPECT_DOUBLE_EQ(5, x);
1324b89a7cc2SEnji Cooper }
1325b89a7cc2SEnji Cooper
1326*28f6c2f2SEnji Cooper // DoAll should support &&-qualified actions when used with WillOnce.
TEST(DoAll,SupportsRefQualifiedActions)1327*28f6c2f2SEnji Cooper TEST(DoAll, SupportsRefQualifiedActions) {
1328*28f6c2f2SEnji Cooper struct InitialAction {
1329*28f6c2f2SEnji Cooper void operator()(const int arg) && { EXPECT_EQ(17, arg); }
1330*28f6c2f2SEnji Cooper };
1331*28f6c2f2SEnji Cooper
1332*28f6c2f2SEnji Cooper struct FinalAction {
1333*28f6c2f2SEnji Cooper int operator()() && { return 19; }
1334*28f6c2f2SEnji Cooper };
1335*28f6c2f2SEnji Cooper
1336*28f6c2f2SEnji Cooper MockFunction<int(int)> mock;
1337*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(DoAll(InitialAction{}, FinalAction{}));
1338*28f6c2f2SEnji Cooper EXPECT_EQ(19, mock.AsStdFunction()(17));
1339*28f6c2f2SEnji Cooper }
1340*28f6c2f2SEnji Cooper
1341*28f6c2f2SEnji Cooper // DoAll should never provide rvalue references to the initial actions. If the
1342*28f6c2f2SEnji Cooper // mock action itself accepts an rvalue reference or a non-scalar object by
1343*28f6c2f2SEnji Cooper // value then the final action should receive an rvalue reference, but initial
1344*28f6c2f2SEnji Cooper // actions should receive only lvalue references.
TEST(DoAll,ProvidesLvalueReferencesToInitialActions)1345*28f6c2f2SEnji Cooper TEST(DoAll, ProvidesLvalueReferencesToInitialActions) {
1346*28f6c2f2SEnji Cooper struct Obj {};
1347*28f6c2f2SEnji Cooper
1348*28f6c2f2SEnji Cooper // Mock action accepts by value: the initial action should be fed a const
1349*28f6c2f2SEnji Cooper // lvalue reference, and the final action an rvalue reference.
1350*28f6c2f2SEnji Cooper {
1351*28f6c2f2SEnji Cooper struct InitialAction {
1352*28f6c2f2SEnji Cooper void operator()(Obj&) const { FAIL() << "Unexpected call"; }
1353*28f6c2f2SEnji Cooper void operator()(const Obj&) const {}
1354*28f6c2f2SEnji Cooper void operator()(Obj&&) const { FAIL() << "Unexpected call"; }
1355*28f6c2f2SEnji Cooper void operator()(const Obj&&) const { FAIL() << "Unexpected call"; }
1356*28f6c2f2SEnji Cooper };
1357*28f6c2f2SEnji Cooper
1358*28f6c2f2SEnji Cooper MockFunction<void(Obj)> mock;
1359*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
1360*28f6c2f2SEnji Cooper .WillOnce(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {}))
1361*28f6c2f2SEnji Cooper .WillRepeatedly(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {}));
1362*28f6c2f2SEnji Cooper
1363*28f6c2f2SEnji Cooper mock.AsStdFunction()(Obj{});
1364*28f6c2f2SEnji Cooper mock.AsStdFunction()(Obj{});
1365*28f6c2f2SEnji Cooper }
1366*28f6c2f2SEnji Cooper
1367*28f6c2f2SEnji Cooper // Mock action accepts by const lvalue reference: both actions should receive
1368*28f6c2f2SEnji Cooper // a const lvalue reference.
1369*28f6c2f2SEnji Cooper {
1370*28f6c2f2SEnji Cooper struct InitialAction {
1371*28f6c2f2SEnji Cooper void operator()(Obj&) const { FAIL() << "Unexpected call"; }
1372*28f6c2f2SEnji Cooper void operator()(const Obj&) const {}
1373*28f6c2f2SEnji Cooper void operator()(Obj&&) const { FAIL() << "Unexpected call"; }
1374*28f6c2f2SEnji Cooper void operator()(const Obj&&) const { FAIL() << "Unexpected call"; }
1375*28f6c2f2SEnji Cooper };
1376*28f6c2f2SEnji Cooper
1377*28f6c2f2SEnji Cooper MockFunction<void(const Obj&)> mock;
1378*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
1379*28f6c2f2SEnji Cooper .WillOnce(DoAll(InitialAction{}, InitialAction{}, [](const Obj&) {}))
1380*28f6c2f2SEnji Cooper .WillRepeatedly(
1381*28f6c2f2SEnji Cooper DoAll(InitialAction{}, InitialAction{}, [](const Obj&) {}));
1382*28f6c2f2SEnji Cooper
1383*28f6c2f2SEnji Cooper mock.AsStdFunction()(Obj{});
1384*28f6c2f2SEnji Cooper mock.AsStdFunction()(Obj{});
1385*28f6c2f2SEnji Cooper }
1386*28f6c2f2SEnji Cooper
1387*28f6c2f2SEnji Cooper // Mock action accepts by non-const lvalue reference: both actions should get
1388*28f6c2f2SEnji Cooper // a non-const lvalue reference if they want them.
1389*28f6c2f2SEnji Cooper {
1390*28f6c2f2SEnji Cooper struct InitialAction {
1391*28f6c2f2SEnji Cooper void operator()(Obj&) const {}
1392*28f6c2f2SEnji Cooper void operator()(Obj&&) const { FAIL() << "Unexpected call"; }
1393*28f6c2f2SEnji Cooper };
1394*28f6c2f2SEnji Cooper
1395*28f6c2f2SEnji Cooper MockFunction<void(Obj&)> mock;
1396*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
1397*28f6c2f2SEnji Cooper .WillOnce(DoAll(InitialAction{}, InitialAction{}, [](Obj&) {}))
1398*28f6c2f2SEnji Cooper .WillRepeatedly(DoAll(InitialAction{}, InitialAction{}, [](Obj&) {}));
1399*28f6c2f2SEnji Cooper
1400*28f6c2f2SEnji Cooper Obj obj;
1401*28f6c2f2SEnji Cooper mock.AsStdFunction()(obj);
1402*28f6c2f2SEnji Cooper mock.AsStdFunction()(obj);
1403*28f6c2f2SEnji Cooper }
1404*28f6c2f2SEnji Cooper
1405*28f6c2f2SEnji Cooper // Mock action accepts by rvalue reference: the initial actions should receive
1406*28f6c2f2SEnji Cooper // a non-const lvalue reference if it wants it, and the final action an rvalue
1407*28f6c2f2SEnji Cooper // reference.
1408*28f6c2f2SEnji Cooper {
1409*28f6c2f2SEnji Cooper struct InitialAction {
1410*28f6c2f2SEnji Cooper void operator()(Obj&) const {}
1411*28f6c2f2SEnji Cooper void operator()(Obj&&) const { FAIL() << "Unexpected call"; }
1412*28f6c2f2SEnji Cooper };
1413*28f6c2f2SEnji Cooper
1414*28f6c2f2SEnji Cooper MockFunction<void(Obj&&)> mock;
1415*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
1416*28f6c2f2SEnji Cooper .WillOnce(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {}))
1417*28f6c2f2SEnji Cooper .WillRepeatedly(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {}));
1418*28f6c2f2SEnji Cooper
1419*28f6c2f2SEnji Cooper mock.AsStdFunction()(Obj{});
1420*28f6c2f2SEnji Cooper mock.AsStdFunction()(Obj{});
1421*28f6c2f2SEnji Cooper }
1422*28f6c2f2SEnji Cooper
1423*28f6c2f2SEnji Cooper // &&-qualified initial actions should also be allowed with WillOnce.
1424*28f6c2f2SEnji Cooper {
1425*28f6c2f2SEnji Cooper struct InitialAction {
1426*28f6c2f2SEnji Cooper void operator()(Obj&) && {}
1427*28f6c2f2SEnji Cooper };
1428*28f6c2f2SEnji Cooper
1429*28f6c2f2SEnji Cooper MockFunction<void(Obj&)> mock;
1430*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
1431*28f6c2f2SEnji Cooper .WillOnce(DoAll(InitialAction{}, InitialAction{}, [](Obj&) {}));
1432*28f6c2f2SEnji Cooper
1433*28f6c2f2SEnji Cooper Obj obj;
1434*28f6c2f2SEnji Cooper mock.AsStdFunction()(obj);
1435*28f6c2f2SEnji Cooper }
1436*28f6c2f2SEnji Cooper
1437*28f6c2f2SEnji Cooper {
1438*28f6c2f2SEnji Cooper struct InitialAction {
1439*28f6c2f2SEnji Cooper void operator()(Obj&) && {}
1440*28f6c2f2SEnji Cooper };
1441*28f6c2f2SEnji Cooper
1442*28f6c2f2SEnji Cooper MockFunction<void(Obj&&)> mock;
1443*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
1444*28f6c2f2SEnji Cooper .WillOnce(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {}));
1445*28f6c2f2SEnji Cooper
1446*28f6c2f2SEnji Cooper mock.AsStdFunction()(Obj{});
1447*28f6c2f2SEnji Cooper }
1448*28f6c2f2SEnji Cooper }
1449*28f6c2f2SEnji Cooper
1450*28f6c2f2SEnji Cooper // DoAll should support being used with type-erased Action objects, both through
1451*28f6c2f2SEnji Cooper // WillOnce and WillRepeatedly.
TEST(DoAll,SupportsTypeErasedActions)1452*28f6c2f2SEnji Cooper TEST(DoAll, SupportsTypeErasedActions) {
1453*28f6c2f2SEnji Cooper // With only type-erased actions.
1454*28f6c2f2SEnji Cooper const Action<void()> initial_action = [] {};
1455*28f6c2f2SEnji Cooper const Action<int()> final_action = [] { return 17; };
1456*28f6c2f2SEnji Cooper
1457*28f6c2f2SEnji Cooper MockFunction<int()> mock;
1458*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
1459*28f6c2f2SEnji Cooper .WillOnce(DoAll(initial_action, initial_action, final_action))
1460*28f6c2f2SEnji Cooper .WillRepeatedly(DoAll(initial_action, initial_action, final_action));
1461*28f6c2f2SEnji Cooper
1462*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()());
1463*28f6c2f2SEnji Cooper
1464*28f6c2f2SEnji Cooper // With &&-qualified and move-only final action.
1465*28f6c2f2SEnji Cooper {
1466*28f6c2f2SEnji Cooper struct FinalAction {
1467*28f6c2f2SEnji Cooper FinalAction() = default;
1468*28f6c2f2SEnji Cooper FinalAction(FinalAction&&) = default;
1469*28f6c2f2SEnji Cooper
1470*28f6c2f2SEnji Cooper int operator()() && { return 17; }
1471*28f6c2f2SEnji Cooper };
1472*28f6c2f2SEnji Cooper
1473*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
1474*28f6c2f2SEnji Cooper .WillOnce(DoAll(initial_action, initial_action, FinalAction{}));
1475*28f6c2f2SEnji Cooper
1476*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()());
1477*28f6c2f2SEnji Cooper }
1478*28f6c2f2SEnji Cooper }
1479*28f6c2f2SEnji Cooper
1480*28f6c2f2SEnji Cooper // Tests using WithArgs and with an action that takes 1 argument.
TEST(WithArgsTest,OneArg)1481*28f6c2f2SEnji Cooper TEST(WithArgsTest, OneArg) {
1482*28f6c2f2SEnji Cooper Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT
1483*28f6c2f2SEnji Cooper EXPECT_TRUE(a.Perform(std::make_tuple(1.5, -1)));
1484*28f6c2f2SEnji Cooper EXPECT_FALSE(a.Perform(std::make_tuple(1.5, 1)));
1485*28f6c2f2SEnji Cooper }
1486*28f6c2f2SEnji Cooper
1487*28f6c2f2SEnji Cooper // Tests using WithArgs with an action that takes 2 arguments.
TEST(WithArgsTest,TwoArgs)1488*28f6c2f2SEnji Cooper TEST(WithArgsTest, TwoArgs) {
1489*28f6c2f2SEnji Cooper Action<const char*(const char* s, double x, short n)> a = // NOLINT
1490*28f6c2f2SEnji Cooper WithArgs<0, 2>(Invoke(Binary));
1491*28f6c2f2SEnji Cooper const char s[] = "Hello";
1492*28f6c2f2SEnji Cooper EXPECT_EQ(s + 2, a.Perform(std::make_tuple(CharPtr(s), 0.5, Short(2))));
1493*28f6c2f2SEnji Cooper }
1494*28f6c2f2SEnji Cooper
1495*28f6c2f2SEnji Cooper struct ConcatAll {
operator ()testing::__anon10e2b6f40111::ConcatAll1496*28f6c2f2SEnji Cooper std::string operator()() const { return {}; }
1497*28f6c2f2SEnji Cooper template <typename... I>
operator ()testing::__anon10e2b6f40111::ConcatAll1498*28f6c2f2SEnji Cooper std::string operator()(const char* a, I... i) const {
1499*28f6c2f2SEnji Cooper return a + ConcatAll()(i...);
1500*28f6c2f2SEnji Cooper }
1501*28f6c2f2SEnji Cooper };
1502*28f6c2f2SEnji Cooper
1503*28f6c2f2SEnji Cooper // Tests using WithArgs with an action that takes 10 arguments.
TEST(WithArgsTest,TenArgs)1504*28f6c2f2SEnji Cooper TEST(WithArgsTest, TenArgs) {
1505*28f6c2f2SEnji Cooper Action<std::string(const char*, const char*, const char*, const char*)> a =
1506*28f6c2f2SEnji Cooper WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(ConcatAll{}));
1507*28f6c2f2SEnji Cooper EXPECT_EQ("0123210123",
1508*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
1509*28f6c2f2SEnji Cooper CharPtr("3"))));
1510*28f6c2f2SEnji Cooper }
1511*28f6c2f2SEnji Cooper
1512*28f6c2f2SEnji Cooper // Tests using WithArgs with an action that is not Invoke().
1513*28f6c2f2SEnji Cooper class SubtractAction : public ActionInterface<int(int, int)> {
1514*28f6c2f2SEnji Cooper public:
Perform(const std::tuple<int,int> & args)1515*28f6c2f2SEnji Cooper int Perform(const std::tuple<int, int>& args) override {
1516*28f6c2f2SEnji Cooper return std::get<0>(args) - std::get<1>(args);
1517*28f6c2f2SEnji Cooper }
1518*28f6c2f2SEnji Cooper };
1519*28f6c2f2SEnji Cooper
TEST(WithArgsTest,NonInvokeAction)1520*28f6c2f2SEnji Cooper TEST(WithArgsTest, NonInvokeAction) {
1521*28f6c2f2SEnji Cooper Action<int(const std::string&, int, int)> a =
1522*28f6c2f2SEnji Cooper WithArgs<2, 1>(MakeAction(new SubtractAction));
1523*28f6c2f2SEnji Cooper std::tuple<std::string, int, int> dummy =
1524*28f6c2f2SEnji Cooper std::make_tuple(std::string("hi"), 2, 10);
1525*28f6c2f2SEnji Cooper EXPECT_EQ(8, a.Perform(dummy));
1526*28f6c2f2SEnji Cooper }
1527*28f6c2f2SEnji Cooper
1528*28f6c2f2SEnji Cooper // Tests using WithArgs to pass all original arguments in the original order.
TEST(WithArgsTest,Identity)1529*28f6c2f2SEnji Cooper TEST(WithArgsTest, Identity) {
1530*28f6c2f2SEnji Cooper Action<int(int x, char y, short z)> a = // NOLINT
1531*28f6c2f2SEnji Cooper WithArgs<0, 1, 2>(Invoke(Ternary));
1532*28f6c2f2SEnji Cooper EXPECT_EQ(123, a.Perform(std::make_tuple(100, Char(20), Short(3))));
1533*28f6c2f2SEnji Cooper }
1534*28f6c2f2SEnji Cooper
1535*28f6c2f2SEnji Cooper // Tests using WithArgs with repeated arguments.
TEST(WithArgsTest,RepeatedArguments)1536*28f6c2f2SEnji Cooper TEST(WithArgsTest, RepeatedArguments) {
1537*28f6c2f2SEnji Cooper Action<int(bool, int m, int n)> a = // NOLINT
1538*28f6c2f2SEnji Cooper WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
1539*28f6c2f2SEnji Cooper EXPECT_EQ(4, a.Perform(std::make_tuple(false, 1, 10)));
1540*28f6c2f2SEnji Cooper }
1541*28f6c2f2SEnji Cooper
1542*28f6c2f2SEnji Cooper // Tests using WithArgs with reversed argument order.
TEST(WithArgsTest,ReversedArgumentOrder)1543*28f6c2f2SEnji Cooper TEST(WithArgsTest, ReversedArgumentOrder) {
1544*28f6c2f2SEnji Cooper Action<const char*(short n, const char* input)> a = // NOLINT
1545*28f6c2f2SEnji Cooper WithArgs<1, 0>(Invoke(Binary));
1546*28f6c2f2SEnji Cooper const char s[] = "Hello";
1547*28f6c2f2SEnji Cooper EXPECT_EQ(s + 2, a.Perform(std::make_tuple(Short(2), CharPtr(s))));
1548*28f6c2f2SEnji Cooper }
1549*28f6c2f2SEnji Cooper
1550*28f6c2f2SEnji Cooper // Tests using WithArgs with compatible, but not identical, argument types.
TEST(WithArgsTest,ArgsOfCompatibleTypes)1551*28f6c2f2SEnji Cooper TEST(WithArgsTest, ArgsOfCompatibleTypes) {
1552*28f6c2f2SEnji Cooper Action<long(short x, char y, double z, char c)> a = // NOLINT
1553*28f6c2f2SEnji Cooper WithArgs<0, 1, 3>(Invoke(Ternary));
1554*28f6c2f2SEnji Cooper EXPECT_EQ(123,
1555*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(Short(100), Char(20), 5.6, Char(3))));
1556*28f6c2f2SEnji Cooper }
1557*28f6c2f2SEnji Cooper
1558*28f6c2f2SEnji Cooper // Tests using WithArgs with an action that returns void.
TEST(WithArgsTest,VoidAction)1559*28f6c2f2SEnji Cooper TEST(WithArgsTest, VoidAction) {
1560*28f6c2f2SEnji Cooper Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
1561*28f6c2f2SEnji Cooper g_done = false;
1562*28f6c2f2SEnji Cooper a.Perform(std::make_tuple(1.5, 'a', 3));
1563*28f6c2f2SEnji Cooper EXPECT_TRUE(g_done);
1564*28f6c2f2SEnji Cooper }
1565*28f6c2f2SEnji Cooper
TEST(WithArgsTest,ReturnReference)1566*28f6c2f2SEnji Cooper TEST(WithArgsTest, ReturnReference) {
1567*28f6c2f2SEnji Cooper Action<int&(int&, void*)> aa = WithArgs<0>([](int& a) -> int& { return a; });
1568*28f6c2f2SEnji Cooper int i = 0;
1569*28f6c2f2SEnji Cooper const int& res = aa.Perform(std::forward_as_tuple(i, nullptr));
1570*28f6c2f2SEnji Cooper EXPECT_EQ(&i, &res);
1571*28f6c2f2SEnji Cooper }
1572*28f6c2f2SEnji Cooper
TEST(WithArgsTest,InnerActionWithConversion)1573*28f6c2f2SEnji Cooper TEST(WithArgsTest, InnerActionWithConversion) {
1574*28f6c2f2SEnji Cooper Action<Derived*()> inner = [] { return nullptr; };
1575*28f6c2f2SEnji Cooper
1576*28f6c2f2SEnji Cooper MockFunction<Base*(double)> mock;
1577*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
1578*28f6c2f2SEnji Cooper .WillOnce(WithoutArgs(inner))
1579*28f6c2f2SEnji Cooper .WillRepeatedly(WithoutArgs(inner));
1580*28f6c2f2SEnji Cooper
1581*28f6c2f2SEnji Cooper EXPECT_EQ(nullptr, mock.AsStdFunction()(1.1));
1582*28f6c2f2SEnji Cooper EXPECT_EQ(nullptr, mock.AsStdFunction()(1.1));
1583*28f6c2f2SEnji Cooper }
1584*28f6c2f2SEnji Cooper
1585*28f6c2f2SEnji Cooper // It should be possible to use an &&-qualified inner action as long as the
1586*28f6c2f2SEnji Cooper // whole shebang is used as an rvalue with WillOnce.
TEST(WithArgsTest,RefQualifiedInnerAction)1587*28f6c2f2SEnji Cooper TEST(WithArgsTest, RefQualifiedInnerAction) {
1588*28f6c2f2SEnji Cooper struct SomeAction {
1589*28f6c2f2SEnji Cooper int operator()(const int arg) && {
1590*28f6c2f2SEnji Cooper EXPECT_EQ(17, arg);
1591*28f6c2f2SEnji Cooper return 19;
1592*28f6c2f2SEnji Cooper }
1593*28f6c2f2SEnji Cooper };
1594*28f6c2f2SEnji Cooper
1595*28f6c2f2SEnji Cooper MockFunction<int(int, int)> mock;
1596*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(WithArg<1>(SomeAction{}));
1597*28f6c2f2SEnji Cooper EXPECT_EQ(19, mock.AsStdFunction()(0, 17));
1598*28f6c2f2SEnji Cooper }
1599*28f6c2f2SEnji Cooper
1600*28f6c2f2SEnji Cooper #ifndef GTEST_OS_WINDOWS_MOBILE
1601b89a7cc2SEnji Cooper
1602b89a7cc2SEnji Cooper class SetErrnoAndReturnTest : public testing::Test {
1603b89a7cc2SEnji Cooper protected:
SetUp()1604*28f6c2f2SEnji Cooper void SetUp() override { errno = 0; }
TearDown()1605*28f6c2f2SEnji Cooper void TearDown() override { errno = 0; }
1606b89a7cc2SEnji Cooper };
1607b89a7cc2SEnji Cooper
TEST_F(SetErrnoAndReturnTest,Int)1608b89a7cc2SEnji Cooper TEST_F(SetErrnoAndReturnTest, Int) {
1609b89a7cc2SEnji Cooper Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1610*28f6c2f2SEnji Cooper EXPECT_EQ(-5, a.Perform(std::make_tuple()));
1611b89a7cc2SEnji Cooper EXPECT_EQ(ENOTTY, errno);
1612b89a7cc2SEnji Cooper }
1613b89a7cc2SEnji Cooper
TEST_F(SetErrnoAndReturnTest,Ptr)1614b89a7cc2SEnji Cooper TEST_F(SetErrnoAndReturnTest, Ptr) {
1615b89a7cc2SEnji Cooper int x;
1616b89a7cc2SEnji Cooper Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1617*28f6c2f2SEnji Cooper EXPECT_EQ(&x, a.Perform(std::make_tuple()));
1618b89a7cc2SEnji Cooper EXPECT_EQ(ENOTTY, errno);
1619b89a7cc2SEnji Cooper }
1620b89a7cc2SEnji Cooper
TEST_F(SetErrnoAndReturnTest,CompatibleTypes)1621b89a7cc2SEnji Cooper TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1622b89a7cc2SEnji Cooper Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1623*28f6c2f2SEnji Cooper EXPECT_DOUBLE_EQ(5.0, a.Perform(std::make_tuple()));
1624b89a7cc2SEnji Cooper EXPECT_EQ(EINVAL, errno);
1625b89a7cc2SEnji Cooper }
1626b89a7cc2SEnji Cooper
1627b89a7cc2SEnji Cooper #endif // !GTEST_OS_WINDOWS_MOBILE
1628b89a7cc2SEnji Cooper
1629b89a7cc2SEnji Cooper // Tests ByRef().
1630b89a7cc2SEnji Cooper
1631*28f6c2f2SEnji Cooper // Tests that the result of ByRef() is copyable.
TEST(ByRefTest,IsCopyable)1632b89a7cc2SEnji Cooper TEST(ByRefTest, IsCopyable) {
1633b89a7cc2SEnji Cooper const std::string s1 = "Hi";
1634b89a7cc2SEnji Cooper const std::string s2 = "Hello";
1635b89a7cc2SEnji Cooper
1636*28f6c2f2SEnji Cooper auto ref_wrapper = ByRef(s1);
1637b89a7cc2SEnji Cooper const std::string& r1 = ref_wrapper;
1638b89a7cc2SEnji Cooper EXPECT_EQ(&s1, &r1);
1639b89a7cc2SEnji Cooper
1640b89a7cc2SEnji Cooper // Assigns a new value to ref_wrapper.
1641b89a7cc2SEnji Cooper ref_wrapper = ByRef(s2);
1642b89a7cc2SEnji Cooper const std::string& r2 = ref_wrapper;
1643b89a7cc2SEnji Cooper EXPECT_EQ(&s2, &r2);
1644b89a7cc2SEnji Cooper
1645*28f6c2f2SEnji Cooper auto ref_wrapper1 = ByRef(s1);
1646b89a7cc2SEnji Cooper // Copies ref_wrapper1 to ref_wrapper.
1647b89a7cc2SEnji Cooper ref_wrapper = ref_wrapper1;
1648b89a7cc2SEnji Cooper const std::string& r3 = ref_wrapper;
1649b89a7cc2SEnji Cooper EXPECT_EQ(&s1, &r3);
1650b89a7cc2SEnji Cooper }
1651b89a7cc2SEnji Cooper
1652b89a7cc2SEnji Cooper // Tests using ByRef() on a const value.
TEST(ByRefTest,ConstValue)1653b89a7cc2SEnji Cooper TEST(ByRefTest, ConstValue) {
1654b89a7cc2SEnji Cooper const int n = 0;
1655b89a7cc2SEnji Cooper // int& ref = ByRef(n); // This shouldn't compile - we have a
1656b89a7cc2SEnji Cooper // negative compilation test to catch it.
1657b89a7cc2SEnji Cooper const int& const_ref = ByRef(n);
1658b89a7cc2SEnji Cooper EXPECT_EQ(&n, &const_ref);
1659b89a7cc2SEnji Cooper }
1660b89a7cc2SEnji Cooper
1661b89a7cc2SEnji Cooper // Tests using ByRef() on a non-const value.
TEST(ByRefTest,NonConstValue)1662b89a7cc2SEnji Cooper TEST(ByRefTest, NonConstValue) {
1663b89a7cc2SEnji Cooper int n = 0;
1664b89a7cc2SEnji Cooper
1665b89a7cc2SEnji Cooper // ByRef(n) can be used as either an int&,
1666b89a7cc2SEnji Cooper int& ref = ByRef(n);
1667b89a7cc2SEnji Cooper EXPECT_EQ(&n, &ref);
1668b89a7cc2SEnji Cooper
1669b89a7cc2SEnji Cooper // or a const int&.
1670b89a7cc2SEnji Cooper const int& const_ref = ByRef(n);
1671b89a7cc2SEnji Cooper EXPECT_EQ(&n, &const_ref);
1672b89a7cc2SEnji Cooper }
1673b89a7cc2SEnji Cooper
1674b89a7cc2SEnji Cooper // Tests explicitly specifying the type when using ByRef().
TEST(ByRefTest,ExplicitType)1675b89a7cc2SEnji Cooper TEST(ByRefTest, ExplicitType) {
1676b89a7cc2SEnji Cooper int n = 0;
1677b89a7cc2SEnji Cooper const int& r1 = ByRef<const int>(n);
1678b89a7cc2SEnji Cooper EXPECT_EQ(&n, &r1);
1679b89a7cc2SEnji Cooper
1680b89a7cc2SEnji Cooper // ByRef<char>(n); // This shouldn't compile - we have a negative
1681b89a7cc2SEnji Cooper // compilation test to catch it.
1682b89a7cc2SEnji Cooper
1683b89a7cc2SEnji Cooper Derived d;
1684b89a7cc2SEnji Cooper Derived& r2 = ByRef<Derived>(d);
1685b89a7cc2SEnji Cooper EXPECT_EQ(&d, &r2);
1686b89a7cc2SEnji Cooper
1687b89a7cc2SEnji Cooper const Derived& r3 = ByRef<const Derived>(d);
1688b89a7cc2SEnji Cooper EXPECT_EQ(&d, &r3);
1689b89a7cc2SEnji Cooper
1690b89a7cc2SEnji Cooper Base& r4 = ByRef<Base>(d);
1691b89a7cc2SEnji Cooper EXPECT_EQ(&d, &r4);
1692b89a7cc2SEnji Cooper
1693b89a7cc2SEnji Cooper const Base& r5 = ByRef<const Base>(d);
1694b89a7cc2SEnji Cooper EXPECT_EQ(&d, &r5);
1695b89a7cc2SEnji Cooper
1696b89a7cc2SEnji Cooper // The following shouldn't compile - we have a negative compilation
1697b89a7cc2SEnji Cooper // test for it.
1698b89a7cc2SEnji Cooper //
1699b89a7cc2SEnji Cooper // Base b;
1700b89a7cc2SEnji Cooper // ByRef<Derived>(b);
1701b89a7cc2SEnji Cooper }
1702b89a7cc2SEnji Cooper
1703b89a7cc2SEnji Cooper // Tests that Google Mock prints expression ByRef(x) as a reference to x.
TEST(ByRefTest,PrintsCorrectly)1704b89a7cc2SEnji Cooper TEST(ByRefTest, PrintsCorrectly) {
1705b89a7cc2SEnji Cooper int n = 42;
1706b89a7cc2SEnji Cooper ::std::stringstream expected, actual;
1707b89a7cc2SEnji Cooper testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1708b89a7cc2SEnji Cooper testing::internal::UniversalPrint(ByRef(n), &actual);
1709b89a7cc2SEnji Cooper EXPECT_EQ(expected.str(), actual.str());
1710b89a7cc2SEnji Cooper }
1711b89a7cc2SEnji Cooper
1712*28f6c2f2SEnji Cooper struct UnaryConstructorClass {
UnaryConstructorClasstesting::__anon10e2b6f40111::UnaryConstructorClass1713*28f6c2f2SEnji Cooper explicit UnaryConstructorClass(int v) : value(v) {}
1714*28f6c2f2SEnji Cooper int value;
1715*28f6c2f2SEnji Cooper };
1716b89a7cc2SEnji Cooper
1717*28f6c2f2SEnji Cooper // Tests using ReturnNew() with a unary constructor.
TEST(ReturnNewTest,Unary)1718*28f6c2f2SEnji Cooper TEST(ReturnNewTest, Unary) {
1719*28f6c2f2SEnji Cooper Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
1720*28f6c2f2SEnji Cooper UnaryConstructorClass* c = a.Perform(std::make_tuple());
1721*28f6c2f2SEnji Cooper EXPECT_EQ(4000, c->value);
1722*28f6c2f2SEnji Cooper delete c;
1723b89a7cc2SEnji Cooper }
1724b89a7cc2SEnji Cooper
TEST(ReturnNewTest,UnaryWorksWhenMockMethodHasArgs)1725*28f6c2f2SEnji Cooper TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
1726*28f6c2f2SEnji Cooper Action<UnaryConstructorClass*(bool, int)> a =
1727*28f6c2f2SEnji Cooper ReturnNew<UnaryConstructorClass>(4000);
1728*28f6c2f2SEnji Cooper UnaryConstructorClass* c = a.Perform(std::make_tuple(false, 5));
1729*28f6c2f2SEnji Cooper EXPECT_EQ(4000, c->value);
1730*28f6c2f2SEnji Cooper delete c;
1731*28f6c2f2SEnji Cooper }
1732*28f6c2f2SEnji Cooper
TEST(ReturnNewTest,UnaryWorksWhenMockMethodReturnsPointerToConst)1733*28f6c2f2SEnji Cooper TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
1734*28f6c2f2SEnji Cooper Action<const UnaryConstructorClass*()> a =
1735*28f6c2f2SEnji Cooper ReturnNew<UnaryConstructorClass>(4000);
1736*28f6c2f2SEnji Cooper const UnaryConstructorClass* c = a.Perform(std::make_tuple());
1737*28f6c2f2SEnji Cooper EXPECT_EQ(4000, c->value);
1738*28f6c2f2SEnji Cooper delete c;
1739*28f6c2f2SEnji Cooper }
1740*28f6c2f2SEnji Cooper
1741*28f6c2f2SEnji Cooper class TenArgConstructorClass {
1742*28f6c2f2SEnji Cooper public:
TenArgConstructorClass(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int a10)1743*28f6c2f2SEnji Cooper TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5, int a6, int a7,
1744*28f6c2f2SEnji Cooper int a8, int a9, int a10)
1745*28f6c2f2SEnji Cooper : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {}
1746*28f6c2f2SEnji Cooper int value_;
1747*28f6c2f2SEnji Cooper };
1748*28f6c2f2SEnji Cooper
1749*28f6c2f2SEnji Cooper // Tests using ReturnNew() with a 10-argument constructor.
TEST(ReturnNewTest,ConstructorThatTakes10Arguments)1750*28f6c2f2SEnji Cooper TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
1751*28f6c2f2SEnji Cooper Action<TenArgConstructorClass*()> a = ReturnNew<TenArgConstructorClass>(
1752*28f6c2f2SEnji Cooper 1000000000, 200000000, 30000000, 4000000, 500000, 60000, 7000, 800, 90,
1753*28f6c2f2SEnji Cooper 0);
1754*28f6c2f2SEnji Cooper TenArgConstructorClass* c = a.Perform(std::make_tuple());
1755*28f6c2f2SEnji Cooper EXPECT_EQ(1234567890, c->value_);
1756*28f6c2f2SEnji Cooper delete c;
1757*28f6c2f2SEnji Cooper }
1758*28f6c2f2SEnji Cooper
UniquePtrSource()1759*28f6c2f2SEnji Cooper std::unique_ptr<int> UniquePtrSource() { return std::make_unique<int>(19); }
1760*28f6c2f2SEnji Cooper
VectorUniquePtrSource()1761b89a7cc2SEnji Cooper std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1762b89a7cc2SEnji Cooper std::vector<std::unique_ptr<int>> out;
1763b89a7cc2SEnji Cooper out.emplace_back(new int(7));
1764b89a7cc2SEnji Cooper return out;
1765b89a7cc2SEnji Cooper }
1766b89a7cc2SEnji Cooper
TEST(MockMethodTest,CanReturnMoveOnlyValue_Return)1767b89a7cc2SEnji Cooper TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1768b89a7cc2SEnji Cooper MockClass mock;
1769b89a7cc2SEnji Cooper std::unique_ptr<int> i(new int(19));
1770b89a7cc2SEnji Cooper EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
1771b89a7cc2SEnji Cooper EXPECT_CALL(mock, MakeVectorUnique())
1772b89a7cc2SEnji Cooper .WillOnce(Return(ByMove(VectorUniquePtrSource())));
1773b89a7cc2SEnji Cooper Derived* d = new Derived;
1774b89a7cc2SEnji Cooper EXPECT_CALL(mock, MakeUniqueBase())
1775b89a7cc2SEnji Cooper .WillOnce(Return(ByMove(std::unique_ptr<Derived>(d))));
1776b89a7cc2SEnji Cooper
1777b89a7cc2SEnji Cooper std::unique_ptr<int> result1 = mock.MakeUnique();
1778b89a7cc2SEnji Cooper EXPECT_EQ(19, *result1);
1779b89a7cc2SEnji Cooper
1780b89a7cc2SEnji Cooper std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1781b89a7cc2SEnji Cooper EXPECT_EQ(1u, vresult.size());
1782b89a7cc2SEnji Cooper EXPECT_NE(nullptr, vresult[0]);
1783b89a7cc2SEnji Cooper EXPECT_EQ(7, *vresult[0]);
1784b89a7cc2SEnji Cooper
1785b89a7cc2SEnji Cooper std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1786b89a7cc2SEnji Cooper EXPECT_EQ(d, result2.get());
1787b89a7cc2SEnji Cooper }
1788b89a7cc2SEnji Cooper
TEST(MockMethodTest,CanReturnMoveOnlyValue_DoAllReturn)1789b89a7cc2SEnji Cooper TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1790b89a7cc2SEnji Cooper testing::MockFunction<void()> mock_function;
1791b89a7cc2SEnji Cooper MockClass mock;
1792b89a7cc2SEnji Cooper std::unique_ptr<int> i(new int(19));
1793b89a7cc2SEnji Cooper EXPECT_CALL(mock_function, Call());
1794*28f6c2f2SEnji Cooper EXPECT_CALL(mock, MakeUnique())
1795*28f6c2f2SEnji Cooper .WillOnce(DoAll(InvokeWithoutArgs(&mock_function,
1796*28f6c2f2SEnji Cooper &testing::MockFunction<void()>::Call),
1797b89a7cc2SEnji Cooper Return(ByMove(std::move(i)))));
1798b89a7cc2SEnji Cooper
1799b89a7cc2SEnji Cooper std::unique_ptr<int> result1 = mock.MakeUnique();
1800b89a7cc2SEnji Cooper EXPECT_EQ(19, *result1);
1801b89a7cc2SEnji Cooper }
1802b89a7cc2SEnji Cooper
TEST(MockMethodTest,CanReturnMoveOnlyValue_Invoke)1803b89a7cc2SEnji Cooper TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
1804b89a7cc2SEnji Cooper MockClass mock;
1805b89a7cc2SEnji Cooper
1806b89a7cc2SEnji Cooper // Check default value
1807*28f6c2f2SEnji Cooper DefaultValue<std::unique_ptr<int>>::SetFactory(
1808*28f6c2f2SEnji Cooper [] { return std::make_unique<int>(42); });
1809b89a7cc2SEnji Cooper EXPECT_EQ(42, *mock.MakeUnique());
1810b89a7cc2SEnji Cooper
1811b89a7cc2SEnji Cooper EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
1812b89a7cc2SEnji Cooper EXPECT_CALL(mock, MakeVectorUnique())
1813b89a7cc2SEnji Cooper .WillRepeatedly(Invoke(VectorUniquePtrSource));
1814b89a7cc2SEnji Cooper std::unique_ptr<int> result1 = mock.MakeUnique();
1815b89a7cc2SEnji Cooper EXPECT_EQ(19, *result1);
1816b89a7cc2SEnji Cooper std::unique_ptr<int> result2 = mock.MakeUnique();
1817b89a7cc2SEnji Cooper EXPECT_EQ(19, *result2);
1818b89a7cc2SEnji Cooper EXPECT_NE(result1, result2);
1819b89a7cc2SEnji Cooper
1820b89a7cc2SEnji Cooper std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1821b89a7cc2SEnji Cooper EXPECT_EQ(1u, vresult.size());
1822b89a7cc2SEnji Cooper EXPECT_NE(nullptr, vresult[0]);
1823b89a7cc2SEnji Cooper EXPECT_EQ(7, *vresult[0]);
1824b89a7cc2SEnji Cooper }
1825b89a7cc2SEnji Cooper
TEST(MockMethodTest,CanTakeMoveOnlyValue)1826b89a7cc2SEnji Cooper TEST(MockMethodTest, CanTakeMoveOnlyValue) {
1827b89a7cc2SEnji Cooper MockClass mock;
1828*28f6c2f2SEnji Cooper auto make = [](int i) { return std::make_unique<int>(i); };
1829b89a7cc2SEnji Cooper
1830b89a7cc2SEnji Cooper EXPECT_CALL(mock, TakeUnique(_)).WillRepeatedly([](std::unique_ptr<int> i) {
1831b89a7cc2SEnji Cooper return *i;
1832b89a7cc2SEnji Cooper });
1833b89a7cc2SEnji Cooper // DoAll() does not compile, since it would move from its arguments twice.
1834b89a7cc2SEnji Cooper // EXPECT_CALL(mock, TakeUnique(_, _))
1835b89a7cc2SEnji Cooper // .WillRepeatedly(DoAll(Invoke([](std::unique_ptr<int> j) {}),
1836b89a7cc2SEnji Cooper // Return(1)));
1837b89a7cc2SEnji Cooper EXPECT_CALL(mock, TakeUnique(testing::Pointee(7)))
1838b89a7cc2SEnji Cooper .WillOnce(Return(-7))
1839b89a7cc2SEnji Cooper .RetiresOnSaturation();
1840b89a7cc2SEnji Cooper EXPECT_CALL(mock, TakeUnique(testing::IsNull()))
1841b89a7cc2SEnji Cooper .WillOnce(Return(-1))
1842b89a7cc2SEnji Cooper .RetiresOnSaturation();
1843b89a7cc2SEnji Cooper
1844b89a7cc2SEnji Cooper EXPECT_EQ(5, mock.TakeUnique(make(5)));
1845b89a7cc2SEnji Cooper EXPECT_EQ(-7, mock.TakeUnique(make(7)));
1846b89a7cc2SEnji Cooper EXPECT_EQ(7, mock.TakeUnique(make(7)));
1847b89a7cc2SEnji Cooper EXPECT_EQ(7, mock.TakeUnique(make(7)));
1848b89a7cc2SEnji Cooper EXPECT_EQ(-1, mock.TakeUnique({}));
1849b89a7cc2SEnji Cooper
1850b89a7cc2SEnji Cooper // Some arguments are moved, some passed by reference.
1851b89a7cc2SEnji Cooper auto lvalue = make(6);
1852b89a7cc2SEnji Cooper EXPECT_CALL(mock, TakeUnique(_, _))
1853b89a7cc2SEnji Cooper .WillOnce([](const std::unique_ptr<int>& i, std::unique_ptr<int> j) {
1854b89a7cc2SEnji Cooper return *i * *j;
1855b89a7cc2SEnji Cooper });
1856b89a7cc2SEnji Cooper EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7)));
1857b89a7cc2SEnji Cooper
1858b89a7cc2SEnji Cooper // The unique_ptr can be saved by the action.
1859b89a7cc2SEnji Cooper std::unique_ptr<int> saved;
1860b89a7cc2SEnji Cooper EXPECT_CALL(mock, TakeUnique(_)).WillOnce([&saved](std::unique_ptr<int> i) {
1861b89a7cc2SEnji Cooper saved = std::move(i);
1862b89a7cc2SEnji Cooper return 0;
1863b89a7cc2SEnji Cooper });
1864b89a7cc2SEnji Cooper EXPECT_EQ(0, mock.TakeUnique(make(42)));
1865b89a7cc2SEnji Cooper EXPECT_EQ(42, *saved);
1866b89a7cc2SEnji Cooper }
1867b89a7cc2SEnji Cooper
1868*28f6c2f2SEnji Cooper // It should be possible to use callables with an &&-qualified call operator
1869*28f6c2f2SEnji Cooper // with WillOnce, since they will be called only once. This allows actions to
1870*28f6c2f2SEnji Cooper // contain and manipulate move-only types.
TEST(MockMethodTest,ActionHasRvalueRefQualifiedCallOperator)1871*28f6c2f2SEnji Cooper TEST(MockMethodTest, ActionHasRvalueRefQualifiedCallOperator) {
1872*28f6c2f2SEnji Cooper struct Return17 {
1873*28f6c2f2SEnji Cooper int operator()() && { return 17; }
1874*28f6c2f2SEnji Cooper };
1875b89a7cc2SEnji Cooper
1876*28f6c2f2SEnji Cooper // Action is directly compatible with mocked function type.
1877*28f6c2f2SEnji Cooper {
1878*28f6c2f2SEnji Cooper MockFunction<int()> mock;
1879*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(Return17());
1880*28f6c2f2SEnji Cooper
1881*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()());
1882*28f6c2f2SEnji Cooper }
1883*28f6c2f2SEnji Cooper
1884*28f6c2f2SEnji Cooper // Action doesn't want mocked function arguments.
1885*28f6c2f2SEnji Cooper {
1886*28f6c2f2SEnji Cooper MockFunction<int(int)> mock;
1887*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(Return17());
1888*28f6c2f2SEnji Cooper
1889*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()(0));
1890*28f6c2f2SEnji Cooper }
1891*28f6c2f2SEnji Cooper }
1892*28f6c2f2SEnji Cooper
1893*28f6c2f2SEnji Cooper // Edge case: if an action has both a const-qualified and an &&-qualified call
1894*28f6c2f2SEnji Cooper // operator, there should be no "ambiguous call" errors. The &&-qualified
1895*28f6c2f2SEnji Cooper // operator should be used by WillOnce (since it doesn't need to retain the
1896*28f6c2f2SEnji Cooper // action beyond one call), and the const-qualified one by WillRepeatedly.
TEST(MockMethodTest,ActionHasMultipleCallOperators)1897*28f6c2f2SEnji Cooper TEST(MockMethodTest, ActionHasMultipleCallOperators) {
1898*28f6c2f2SEnji Cooper struct ReturnInt {
1899*28f6c2f2SEnji Cooper int operator()() && { return 17; }
1900*28f6c2f2SEnji Cooper int operator()() const& { return 19; }
1901*28f6c2f2SEnji Cooper };
1902*28f6c2f2SEnji Cooper
1903*28f6c2f2SEnji Cooper // Directly compatible with mocked function type.
1904*28f6c2f2SEnji Cooper {
1905*28f6c2f2SEnji Cooper MockFunction<int()> mock;
1906*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(ReturnInt()).WillRepeatedly(ReturnInt());
1907*28f6c2f2SEnji Cooper
1908*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()());
1909*28f6c2f2SEnji Cooper EXPECT_EQ(19, mock.AsStdFunction()());
1910*28f6c2f2SEnji Cooper EXPECT_EQ(19, mock.AsStdFunction()());
1911*28f6c2f2SEnji Cooper }
1912*28f6c2f2SEnji Cooper
1913*28f6c2f2SEnji Cooper // Ignores function arguments.
1914*28f6c2f2SEnji Cooper {
1915*28f6c2f2SEnji Cooper MockFunction<int(int)> mock;
1916*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(ReturnInt()).WillRepeatedly(ReturnInt());
1917*28f6c2f2SEnji Cooper
1918*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()(0));
1919*28f6c2f2SEnji Cooper EXPECT_EQ(19, mock.AsStdFunction()(0));
1920*28f6c2f2SEnji Cooper EXPECT_EQ(19, mock.AsStdFunction()(0));
1921*28f6c2f2SEnji Cooper }
1922*28f6c2f2SEnji Cooper }
1923*28f6c2f2SEnji Cooper
1924*28f6c2f2SEnji Cooper // WillOnce should have no problem coping with a move-only action, whether it is
1925*28f6c2f2SEnji Cooper // &&-qualified or not.
TEST(MockMethodTest,MoveOnlyAction)1926*28f6c2f2SEnji Cooper TEST(MockMethodTest, MoveOnlyAction) {
1927*28f6c2f2SEnji Cooper // &&-qualified
1928*28f6c2f2SEnji Cooper {
1929*28f6c2f2SEnji Cooper struct Return17 {
1930*28f6c2f2SEnji Cooper Return17() = default;
1931*28f6c2f2SEnji Cooper Return17(Return17&&) = default;
1932*28f6c2f2SEnji Cooper
1933*28f6c2f2SEnji Cooper Return17(const Return17&) = delete;
1934*28f6c2f2SEnji Cooper Return17 operator=(const Return17&) = delete;
1935*28f6c2f2SEnji Cooper
1936*28f6c2f2SEnji Cooper int operator()() && { return 17; }
1937*28f6c2f2SEnji Cooper };
1938*28f6c2f2SEnji Cooper
1939*28f6c2f2SEnji Cooper MockFunction<int()> mock;
1940*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(Return17());
1941*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()());
1942*28f6c2f2SEnji Cooper }
1943*28f6c2f2SEnji Cooper
1944*28f6c2f2SEnji Cooper // Not &&-qualified
1945*28f6c2f2SEnji Cooper {
1946*28f6c2f2SEnji Cooper struct Return17 {
1947*28f6c2f2SEnji Cooper Return17() = default;
1948*28f6c2f2SEnji Cooper Return17(Return17&&) = default;
1949*28f6c2f2SEnji Cooper
1950*28f6c2f2SEnji Cooper Return17(const Return17&) = delete;
1951*28f6c2f2SEnji Cooper Return17 operator=(const Return17&) = delete;
1952*28f6c2f2SEnji Cooper
1953*28f6c2f2SEnji Cooper int operator()() const { return 17; }
1954*28f6c2f2SEnji Cooper };
1955*28f6c2f2SEnji Cooper
1956*28f6c2f2SEnji Cooper MockFunction<int()> mock;
1957*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(Return17());
1958*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()());
1959*28f6c2f2SEnji Cooper }
1960*28f6c2f2SEnji Cooper }
1961*28f6c2f2SEnji Cooper
1962*28f6c2f2SEnji Cooper // It should be possible to use an action that returns a value with a mock
1963*28f6c2f2SEnji Cooper // function that doesn't, both through WillOnce and WillRepeatedly.
TEST(MockMethodTest,ActionReturnsIgnoredValue)1964*28f6c2f2SEnji Cooper TEST(MockMethodTest, ActionReturnsIgnoredValue) {
1965*28f6c2f2SEnji Cooper struct ReturnInt {
1966*28f6c2f2SEnji Cooper int operator()() const { return 0; }
1967*28f6c2f2SEnji Cooper };
1968*28f6c2f2SEnji Cooper
1969*28f6c2f2SEnji Cooper MockFunction<void()> mock;
1970*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(ReturnInt()).WillRepeatedly(ReturnInt());
1971*28f6c2f2SEnji Cooper
1972*28f6c2f2SEnji Cooper mock.AsStdFunction()();
1973*28f6c2f2SEnji Cooper mock.AsStdFunction()();
1974*28f6c2f2SEnji Cooper }
1975*28f6c2f2SEnji Cooper
1976*28f6c2f2SEnji Cooper // Despite the fanciness around move-only actions and so on, it should still be
1977*28f6c2f2SEnji Cooper // possible to hand an lvalue reference to a copyable action to WillOnce.
TEST(MockMethodTest,WillOnceCanAcceptLvalueReference)1978*28f6c2f2SEnji Cooper TEST(MockMethodTest, WillOnceCanAcceptLvalueReference) {
1979*28f6c2f2SEnji Cooper MockFunction<int()> mock;
1980*28f6c2f2SEnji Cooper
1981*28f6c2f2SEnji Cooper const auto action = [] { return 17; };
1982*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call).WillOnce(action);
1983*28f6c2f2SEnji Cooper
1984*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()());
1985*28f6c2f2SEnji Cooper }
1986*28f6c2f2SEnji Cooper
1987*28f6c2f2SEnji Cooper // A callable that doesn't use SFINAE to restrict its call operator's overload
1988*28f6c2f2SEnji Cooper // set, but is still picky about which arguments it will accept.
1989*28f6c2f2SEnji Cooper struct StaticAssertSingleArgument {
1990*28f6c2f2SEnji Cooper template <typename... Args>
CheckArgstesting::__anon10e2b6f40111::StaticAssertSingleArgument1991*28f6c2f2SEnji Cooper static constexpr bool CheckArgs() {
1992*28f6c2f2SEnji Cooper static_assert(sizeof...(Args) == 1, "");
1993*28f6c2f2SEnji Cooper return true;
1994*28f6c2f2SEnji Cooper }
1995*28f6c2f2SEnji Cooper
1996*28f6c2f2SEnji Cooper template <typename... Args, bool = CheckArgs<Args...>()>
operator ()testing::__anon10e2b6f40111::StaticAssertSingleArgument1997*28f6c2f2SEnji Cooper int operator()(Args...) const {
1998*28f6c2f2SEnji Cooper return 17;
1999*28f6c2f2SEnji Cooper }
2000*28f6c2f2SEnji Cooper };
2001*28f6c2f2SEnji Cooper
2002*28f6c2f2SEnji Cooper // WillOnce and WillRepeatedly should both work fine with naïve implementations
2003*28f6c2f2SEnji Cooper // of actions that don't use SFINAE to limit the overload set for their call
2004*28f6c2f2SEnji Cooper // operator. If they are compatible with the actual mocked signature, we
2005*28f6c2f2SEnji Cooper // shouldn't probe them with no arguments and trip a static_assert.
TEST(MockMethodTest,ActionSwallowsAllArguments)2006*28f6c2f2SEnji Cooper TEST(MockMethodTest, ActionSwallowsAllArguments) {
2007*28f6c2f2SEnji Cooper MockFunction<int(int)> mock;
2008*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
2009*28f6c2f2SEnji Cooper .WillOnce(StaticAssertSingleArgument{})
2010*28f6c2f2SEnji Cooper .WillRepeatedly(StaticAssertSingleArgument{});
2011*28f6c2f2SEnji Cooper
2012*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()(0));
2013*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()(0));
2014*28f6c2f2SEnji Cooper }
2015*28f6c2f2SEnji Cooper
2016*28f6c2f2SEnji Cooper struct ActionWithTemplatedConversionOperators {
2017*28f6c2f2SEnji Cooper template <typename... Args>
operator OnceAction<int(Args...)>testing::__anon10e2b6f40111::ActionWithTemplatedConversionOperators2018*28f6c2f2SEnji Cooper operator OnceAction<int(Args...)>() && { // NOLINT
2019*28f6c2f2SEnji Cooper return [] { return 17; };
2020*28f6c2f2SEnji Cooper }
2021*28f6c2f2SEnji Cooper
2022*28f6c2f2SEnji Cooper template <typename... Args>
operator Action<int(Args...)>testing::__anon10e2b6f40111::ActionWithTemplatedConversionOperators2023*28f6c2f2SEnji Cooper operator Action<int(Args...)>() const { // NOLINT
2024*28f6c2f2SEnji Cooper return [] { return 19; };
2025*28f6c2f2SEnji Cooper }
2026*28f6c2f2SEnji Cooper };
2027*28f6c2f2SEnji Cooper
2028*28f6c2f2SEnji Cooper // It should be fine to hand both WillOnce and WillRepeatedly a function that
2029*28f6c2f2SEnji Cooper // defines templated conversion operators to OnceAction and Action. WillOnce
2030*28f6c2f2SEnji Cooper // should prefer the OnceAction version.
TEST(MockMethodTest,ActionHasTemplatedConversionOperators)2031*28f6c2f2SEnji Cooper TEST(MockMethodTest, ActionHasTemplatedConversionOperators) {
2032*28f6c2f2SEnji Cooper MockFunction<int()> mock;
2033*28f6c2f2SEnji Cooper EXPECT_CALL(mock, Call)
2034*28f6c2f2SEnji Cooper .WillOnce(ActionWithTemplatedConversionOperators{})
2035*28f6c2f2SEnji Cooper .WillRepeatedly(ActionWithTemplatedConversionOperators{});
2036*28f6c2f2SEnji Cooper
2037*28f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()());
2038*28f6c2f2SEnji Cooper EXPECT_EQ(19, mock.AsStdFunction()());
2039*28f6c2f2SEnji Cooper }
2040*28f6c2f2SEnji Cooper
2041b89a7cc2SEnji Cooper // Tests for std::function based action.
2042b89a7cc2SEnji Cooper
Add(int val,int & ref,int * ptr)2043b89a7cc2SEnji Cooper int Add(int val, int& ref, int* ptr) { // NOLINT
2044b89a7cc2SEnji Cooper int result = val + ref + *ptr;
2045b89a7cc2SEnji Cooper ref = 42;
2046b89a7cc2SEnji Cooper *ptr = 43;
2047b89a7cc2SEnji Cooper return result;
2048b89a7cc2SEnji Cooper }
2049b89a7cc2SEnji Cooper
Deref(std::unique_ptr<int> ptr)2050b89a7cc2SEnji Cooper int Deref(std::unique_ptr<int> ptr) { return *ptr; }
2051b89a7cc2SEnji Cooper
2052b89a7cc2SEnji Cooper struct Double {
2053b89a7cc2SEnji Cooper template <typename T>
operator ()testing::__anon10e2b6f40111::Double2054*28f6c2f2SEnji Cooper T operator()(T t) {
2055*28f6c2f2SEnji Cooper return 2 * t;
2056*28f6c2f2SEnji Cooper }
2057b89a7cc2SEnji Cooper };
2058b89a7cc2SEnji Cooper
UniqueInt(int i)2059*28f6c2f2SEnji Cooper std::unique_ptr<int> UniqueInt(int i) { return std::make_unique<int>(i); }
2060b89a7cc2SEnji Cooper
TEST(FunctorActionTest,ActionFromFunction)2061b89a7cc2SEnji Cooper TEST(FunctorActionTest, ActionFromFunction) {
2062b89a7cc2SEnji Cooper Action<int(int, int&, int*)> a = &Add;
2063b89a7cc2SEnji Cooper int x = 1, y = 2, z = 3;
2064b89a7cc2SEnji Cooper EXPECT_EQ(6, a.Perform(std::forward_as_tuple(x, y, &z)));
2065b89a7cc2SEnji Cooper EXPECT_EQ(42, y);
2066b89a7cc2SEnji Cooper EXPECT_EQ(43, z);
2067b89a7cc2SEnji Cooper
2068b89a7cc2SEnji Cooper Action<int(std::unique_ptr<int>)> a1 = &Deref;
2069b89a7cc2SEnji Cooper EXPECT_EQ(7, a1.Perform(std::make_tuple(UniqueInt(7))));
2070b89a7cc2SEnji Cooper }
2071b89a7cc2SEnji Cooper
TEST(FunctorActionTest,ActionFromLambda)2072b89a7cc2SEnji Cooper TEST(FunctorActionTest, ActionFromLambda) {
2073b89a7cc2SEnji Cooper Action<int(bool, int)> a1 = [](bool b, int i) { return b ? i : 0; };
2074*28f6c2f2SEnji Cooper EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
2075*28f6c2f2SEnji Cooper EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 5)));
2076b89a7cc2SEnji Cooper
2077b89a7cc2SEnji Cooper std::unique_ptr<int> saved;
2078b89a7cc2SEnji Cooper Action<void(std::unique_ptr<int>)> a2 = [&saved](std::unique_ptr<int> p) {
2079b89a7cc2SEnji Cooper saved = std::move(p);
2080b89a7cc2SEnji Cooper };
2081*28f6c2f2SEnji Cooper a2.Perform(std::make_tuple(UniqueInt(5)));
2082b89a7cc2SEnji Cooper EXPECT_EQ(5, *saved);
2083b89a7cc2SEnji Cooper }
2084b89a7cc2SEnji Cooper
TEST(FunctorActionTest,PolymorphicFunctor)2085b89a7cc2SEnji Cooper TEST(FunctorActionTest, PolymorphicFunctor) {
2086b89a7cc2SEnji Cooper Action<int(int)> ai = Double();
2087*28f6c2f2SEnji Cooper EXPECT_EQ(2, ai.Perform(std::make_tuple(1)));
2088b89a7cc2SEnji Cooper Action<double(double)> ad = Double(); // Double? Double double!
2089*28f6c2f2SEnji Cooper EXPECT_EQ(3.0, ad.Perform(std::make_tuple(1.5)));
2090b89a7cc2SEnji Cooper }
2091b89a7cc2SEnji Cooper
TEST(FunctorActionTest,TypeConversion)2092b89a7cc2SEnji Cooper TEST(FunctorActionTest, TypeConversion) {
2093b89a7cc2SEnji Cooper // Numeric promotions are allowed.
2094b89a7cc2SEnji Cooper const Action<bool(int)> a1 = [](int i) { return i > 1; };
2095b89a7cc2SEnji Cooper const Action<int(bool)> a2 = Action<int(bool)>(a1);
2096*28f6c2f2SEnji Cooper EXPECT_EQ(1, a1.Perform(std::make_tuple(42)));
2097*28f6c2f2SEnji Cooper EXPECT_EQ(0, a2.Perform(std::make_tuple(42)));
2098b89a7cc2SEnji Cooper
2099b89a7cc2SEnji Cooper // Implicit constructors are allowed.
2100b89a7cc2SEnji Cooper const Action<bool(std::string)> s1 = [](std::string s) { return !s.empty(); };
2101b89a7cc2SEnji Cooper const Action<int(const char*)> s2 = Action<int(const char*)>(s1);
2102*28f6c2f2SEnji Cooper EXPECT_EQ(0, s2.Perform(std::make_tuple("")));
2103*28f6c2f2SEnji Cooper EXPECT_EQ(1, s2.Perform(std::make_tuple("hello")));
2104b89a7cc2SEnji Cooper
2105b89a7cc2SEnji Cooper // Also between the lambda and the action itself.
2106*28f6c2f2SEnji Cooper const Action<bool(std::string)> x1 = [](Unused) { return 42; };
2107*28f6c2f2SEnji Cooper const Action<bool(std::string)> x2 = [] { return 42; };
2108*28f6c2f2SEnji Cooper EXPECT_TRUE(x1.Perform(std::make_tuple("hello")));
2109*28f6c2f2SEnji Cooper EXPECT_TRUE(x2.Perform(std::make_tuple("hello")));
2110*28f6c2f2SEnji Cooper
2111*28f6c2f2SEnji Cooper // Ensure decay occurs where required.
2112*28f6c2f2SEnji Cooper std::function<int()> f = [] { return 7; };
2113*28f6c2f2SEnji Cooper Action<int(int)> d = f;
2114*28f6c2f2SEnji Cooper f = nullptr;
2115*28f6c2f2SEnji Cooper EXPECT_EQ(7, d.Perform(std::make_tuple(1)));
2116*28f6c2f2SEnji Cooper
2117*28f6c2f2SEnji Cooper // Ensure creation of an empty action succeeds.
2118*28f6c2f2SEnji Cooper Action<void(int)>(nullptr);
2119b89a7cc2SEnji Cooper }
2120b89a7cc2SEnji Cooper
TEST(FunctorActionTest,UnusedArguments)2121b89a7cc2SEnji Cooper TEST(FunctorActionTest, UnusedArguments) {
2122b89a7cc2SEnji Cooper // Verify that users can ignore uninteresting arguments.
2123*28f6c2f2SEnji Cooper Action<int(int, double y, double z)> a = [](int i, Unused, Unused) {
2124*28f6c2f2SEnji Cooper return 2 * i;
2125*28f6c2f2SEnji Cooper };
2126*28f6c2f2SEnji Cooper std::tuple<int, double, double> dummy = std::make_tuple(3, 7.3, 9.44);
2127b89a7cc2SEnji Cooper EXPECT_EQ(6, a.Perform(dummy));
2128b89a7cc2SEnji Cooper }
2129b89a7cc2SEnji Cooper
2130b89a7cc2SEnji Cooper // Test that basic built-in actions work with move-only arguments.
TEST(MoveOnlyArgumentsTest,ReturningActions)2131b89a7cc2SEnji Cooper TEST(MoveOnlyArgumentsTest, ReturningActions) {
2132b89a7cc2SEnji Cooper Action<int(std::unique_ptr<int>)> a = Return(1);
2133*28f6c2f2SEnji Cooper EXPECT_EQ(1, a.Perform(std::make_tuple(nullptr)));
2134b89a7cc2SEnji Cooper
2135b89a7cc2SEnji Cooper a = testing::WithoutArgs([]() { return 7; });
2136*28f6c2f2SEnji Cooper EXPECT_EQ(7, a.Perform(std::make_tuple(nullptr)));
2137b89a7cc2SEnji Cooper
2138b89a7cc2SEnji Cooper Action<void(std::unique_ptr<int>, int*)> a2 = testing::SetArgPointee<1>(3);
2139b89a7cc2SEnji Cooper int x = 0;
2140*28f6c2f2SEnji Cooper a2.Perform(std::make_tuple(nullptr, &x));
2141b89a7cc2SEnji Cooper EXPECT_EQ(x, 3);
2142b89a7cc2SEnji Cooper }
2143b89a7cc2SEnji Cooper
ACTION(ReturnArity)2144*28f6c2f2SEnji Cooper ACTION(ReturnArity) { return std::tuple_size<args_type>::value; }
2145b89a7cc2SEnji Cooper
TEST(ActionMacro,LargeArity)2146*28f6c2f2SEnji Cooper TEST(ActionMacro, LargeArity) {
2147*28f6c2f2SEnji Cooper EXPECT_EQ(
2148*28f6c2f2SEnji Cooper 1, testing::Action<int(int)>(ReturnArity()).Perform(std::make_tuple(0)));
2149*28f6c2f2SEnji Cooper EXPECT_EQ(
2150*28f6c2f2SEnji Cooper 10,
2151*28f6c2f2SEnji Cooper testing::Action<int(int, int, int, int, int, int, int, int, int, int)>(
2152*28f6c2f2SEnji Cooper ReturnArity())
2153*28f6c2f2SEnji Cooper .Perform(std::make_tuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
2154*28f6c2f2SEnji Cooper EXPECT_EQ(
2155*28f6c2f2SEnji Cooper 20,
2156*28f6c2f2SEnji Cooper testing::Action<int(int, int, int, int, int, int, int, int, int, int, int,
2157*28f6c2f2SEnji Cooper int, int, int, int, int, int, int, int, int)>(
2158*28f6c2f2SEnji Cooper ReturnArity())
2159*28f6c2f2SEnji Cooper .Perform(std::make_tuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
2160*28f6c2f2SEnji Cooper 14, 15, 16, 17, 18, 19)));
2161*28f6c2f2SEnji Cooper }
2162b89a7cc2SEnji Cooper
2163*28f6c2f2SEnji Cooper } // namespace
2164*28f6c2f2SEnji Cooper } // namespace testing
2165*28f6c2f2SEnji Cooper
2166*28f6c2f2SEnji Cooper #if defined(_MSC_VER) && (_MSC_VER == 1900)
2167*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4800
2168b89a7cc2SEnji Cooper #endif
2169*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100 4503
2170