xref: /freebsd/contrib/googletest/googlemock/include/gmock/gmock-more-actions.h (revision b89a7cc2ed6e4398d5be502f5bb5885d1ec6ff0f)
1*b89a7cc2SEnji Cooper // Copyright 2007, Google Inc.
2*b89a7cc2SEnji Cooper // All rights reserved.
3*b89a7cc2SEnji Cooper //
4*b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5*b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6*b89a7cc2SEnji Cooper // met:
7*b89a7cc2SEnji Cooper //
8*b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9*b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10*b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11*b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12*b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13*b89a7cc2SEnji Cooper // distribution.
14*b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15*b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16*b89a7cc2SEnji Cooper // this software without specific prior written permission.
17*b89a7cc2SEnji Cooper //
18*b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*b89a7cc2SEnji Cooper 
30*b89a7cc2SEnji Cooper 
31*b89a7cc2SEnji Cooper // Google Mock - a framework for writing C++ mock classes.
32*b89a7cc2SEnji Cooper //
33*b89a7cc2SEnji Cooper // This file implements some actions that depend on gmock-generated-actions.h.
34*b89a7cc2SEnji Cooper 
35*b89a7cc2SEnji Cooper // GOOGLETEST_CM0002 DO NOT DELETE
36*b89a7cc2SEnji Cooper 
37*b89a7cc2SEnji Cooper #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
38*b89a7cc2SEnji Cooper #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
39*b89a7cc2SEnji Cooper 
40*b89a7cc2SEnji Cooper #include <algorithm>
41*b89a7cc2SEnji Cooper 
42*b89a7cc2SEnji Cooper #include "gmock/gmock-generated-actions.h"
43*b89a7cc2SEnji Cooper 
44*b89a7cc2SEnji Cooper namespace testing {
45*b89a7cc2SEnji Cooper namespace internal {
46*b89a7cc2SEnji Cooper 
47*b89a7cc2SEnji Cooper // Implements the Invoke(f) action.  The template argument
48*b89a7cc2SEnji Cooper // FunctionImpl is the implementation type of f, which can be either a
49*b89a7cc2SEnji Cooper // function pointer or a functor.  Invoke(f) can be used as an
50*b89a7cc2SEnji Cooper // Action<F> as long as f's type is compatible with F (i.e. f can be
51*b89a7cc2SEnji Cooper // assigned to a tr1::function<F>).
52*b89a7cc2SEnji Cooper template <typename FunctionImpl>
53*b89a7cc2SEnji Cooper class InvokeAction {
54*b89a7cc2SEnji Cooper  public:
55*b89a7cc2SEnji Cooper   // The c'tor makes a copy of function_impl (either a function
56*b89a7cc2SEnji Cooper   // pointer or a functor).
57*b89a7cc2SEnji Cooper   explicit InvokeAction(FunctionImpl function_impl)
58*b89a7cc2SEnji Cooper       : function_impl_(function_impl) {}
59*b89a7cc2SEnji Cooper 
60*b89a7cc2SEnji Cooper   template <typename Result, typename ArgumentTuple>
61*b89a7cc2SEnji Cooper   Result Perform(const ArgumentTuple& args) {
62*b89a7cc2SEnji Cooper     return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
63*b89a7cc2SEnji Cooper   }
64*b89a7cc2SEnji Cooper 
65*b89a7cc2SEnji Cooper  private:
66*b89a7cc2SEnji Cooper   FunctionImpl function_impl_;
67*b89a7cc2SEnji Cooper 
68*b89a7cc2SEnji Cooper   GTEST_DISALLOW_ASSIGN_(InvokeAction);
69*b89a7cc2SEnji Cooper };
70*b89a7cc2SEnji Cooper 
71*b89a7cc2SEnji Cooper // Implements the Invoke(object_ptr, &Class::Method) action.
72*b89a7cc2SEnji Cooper template <class Class, typename MethodPtr>
73*b89a7cc2SEnji Cooper class InvokeMethodAction {
74*b89a7cc2SEnji Cooper  public:
75*b89a7cc2SEnji Cooper   InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
76*b89a7cc2SEnji Cooper       : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {}
77*b89a7cc2SEnji Cooper 
78*b89a7cc2SEnji Cooper   template <typename Result, typename ArgumentTuple>
79*b89a7cc2SEnji Cooper   Result Perform(const ArgumentTuple& args) const {
80*b89a7cc2SEnji Cooper     return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
81*b89a7cc2SEnji Cooper         obj_ptr_, method_ptr_, args);
82*b89a7cc2SEnji Cooper   }
83*b89a7cc2SEnji Cooper 
84*b89a7cc2SEnji Cooper  private:
85*b89a7cc2SEnji Cooper   // The order of these members matters.  Reversing the order can trigger
86*b89a7cc2SEnji Cooper   // warning C4121 in MSVC (see
87*b89a7cc2SEnji Cooper   // http://computer-programming-forum.com/7-vc.net/6fbc30265f860ad1.htm ).
88*b89a7cc2SEnji Cooper   const MethodPtr method_ptr_;
89*b89a7cc2SEnji Cooper   Class* const obj_ptr_;
90*b89a7cc2SEnji Cooper 
91*b89a7cc2SEnji Cooper   GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
92*b89a7cc2SEnji Cooper };
93*b89a7cc2SEnji Cooper 
94*b89a7cc2SEnji Cooper // An internal replacement for std::copy which mimics its behavior. This is
95*b89a7cc2SEnji Cooper // necessary because Visual Studio deprecates ::std::copy, issuing warning 4996.
96*b89a7cc2SEnji Cooper // However Visual Studio 2010 and later do not honor #pragmas which disable that
97*b89a7cc2SEnji Cooper // warning.
98*b89a7cc2SEnji Cooper template<typename InputIterator, typename OutputIterator>
99*b89a7cc2SEnji Cooper inline OutputIterator CopyElements(InputIterator first,
100*b89a7cc2SEnji Cooper                                    InputIterator last,
101*b89a7cc2SEnji Cooper                                    OutputIterator output) {
102*b89a7cc2SEnji Cooper   for (; first != last; ++first, ++output) {
103*b89a7cc2SEnji Cooper     *output = *first;
104*b89a7cc2SEnji Cooper   }
105*b89a7cc2SEnji Cooper   return output;
106*b89a7cc2SEnji Cooper }
107*b89a7cc2SEnji Cooper 
108*b89a7cc2SEnji Cooper }  // namespace internal
109*b89a7cc2SEnji Cooper 
110*b89a7cc2SEnji Cooper // Various overloads for Invoke().
111*b89a7cc2SEnji Cooper 
112*b89a7cc2SEnji Cooper // Creates an action that invokes 'function_impl' with the mock
113*b89a7cc2SEnji Cooper // function's arguments.
114*b89a7cc2SEnji Cooper template <typename FunctionImpl>
115*b89a7cc2SEnji Cooper PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
116*b89a7cc2SEnji Cooper     FunctionImpl function_impl) {
117*b89a7cc2SEnji Cooper   return MakePolymorphicAction(
118*b89a7cc2SEnji Cooper       internal::InvokeAction<FunctionImpl>(function_impl));
119*b89a7cc2SEnji Cooper }
120*b89a7cc2SEnji Cooper 
121*b89a7cc2SEnji Cooper // Creates an action that invokes the given method on the given object
122*b89a7cc2SEnji Cooper // with the mock function's arguments.
123*b89a7cc2SEnji Cooper template <class Class, typename MethodPtr>
124*b89a7cc2SEnji Cooper PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
125*b89a7cc2SEnji Cooper     Class* obj_ptr, MethodPtr method_ptr) {
126*b89a7cc2SEnji Cooper   return MakePolymorphicAction(
127*b89a7cc2SEnji Cooper       internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
128*b89a7cc2SEnji Cooper }
129*b89a7cc2SEnji Cooper 
130*b89a7cc2SEnji Cooper // WithoutArgs(inner_action) can be used in a mock function with a
131*b89a7cc2SEnji Cooper // non-empty argument list to perform inner_action, which takes no
132*b89a7cc2SEnji Cooper // argument.  In other words, it adapts an action accepting no
133*b89a7cc2SEnji Cooper // argument to one that accepts (and ignores) arguments.
134*b89a7cc2SEnji Cooper template <typename InnerAction>
135*b89a7cc2SEnji Cooper inline internal::WithArgsAction<InnerAction>
136*b89a7cc2SEnji Cooper WithoutArgs(const InnerAction& action) {
137*b89a7cc2SEnji Cooper   return internal::WithArgsAction<InnerAction>(action);
138*b89a7cc2SEnji Cooper }
139*b89a7cc2SEnji Cooper 
140*b89a7cc2SEnji Cooper // WithArg<k>(an_action) creates an action that passes the k-th
141*b89a7cc2SEnji Cooper // (0-based) argument of the mock function to an_action and performs
142*b89a7cc2SEnji Cooper // it.  It adapts an action accepting one argument to one that accepts
143*b89a7cc2SEnji Cooper // multiple arguments.  For convenience, we also provide
144*b89a7cc2SEnji Cooper // WithArgs<k>(an_action) (defined below) as a synonym.
145*b89a7cc2SEnji Cooper template <int k, typename InnerAction>
146*b89a7cc2SEnji Cooper inline internal::WithArgsAction<InnerAction, k>
147*b89a7cc2SEnji Cooper WithArg(const InnerAction& action) {
148*b89a7cc2SEnji Cooper   return internal::WithArgsAction<InnerAction, k>(action);
149*b89a7cc2SEnji Cooper }
150*b89a7cc2SEnji Cooper 
151*b89a7cc2SEnji Cooper // The ACTION*() macros trigger warning C4100 (unreferenced formal
152*b89a7cc2SEnji Cooper // parameter) in MSVC with -W4.  Unfortunately they cannot be fixed in
153*b89a7cc2SEnji Cooper // the macro definition, as the warnings are generated when the macro
154*b89a7cc2SEnji Cooper // is expanded and macro expansion cannot contain #pragma.  Therefore
155*b89a7cc2SEnji Cooper // we suppress them here.
156*b89a7cc2SEnji Cooper #ifdef _MSC_VER
157*b89a7cc2SEnji Cooper # pragma warning(push)
158*b89a7cc2SEnji Cooper # pragma warning(disable:4100)
159*b89a7cc2SEnji Cooper #endif
160*b89a7cc2SEnji Cooper 
161*b89a7cc2SEnji Cooper // Action ReturnArg<k>() returns the k-th argument of the mock function.
162*b89a7cc2SEnji Cooper ACTION_TEMPLATE(ReturnArg,
163*b89a7cc2SEnji Cooper                 HAS_1_TEMPLATE_PARAMS(int, k),
164*b89a7cc2SEnji Cooper                 AND_0_VALUE_PARAMS()) {
165*b89a7cc2SEnji Cooper   return ::testing::get<k>(args);
166*b89a7cc2SEnji Cooper }
167*b89a7cc2SEnji Cooper 
168*b89a7cc2SEnji Cooper // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
169*b89a7cc2SEnji Cooper // mock function to *pointer.
170*b89a7cc2SEnji Cooper ACTION_TEMPLATE(SaveArg,
171*b89a7cc2SEnji Cooper                 HAS_1_TEMPLATE_PARAMS(int, k),
172*b89a7cc2SEnji Cooper                 AND_1_VALUE_PARAMS(pointer)) {
173*b89a7cc2SEnji Cooper   *pointer = ::testing::get<k>(args);
174*b89a7cc2SEnji Cooper }
175*b89a7cc2SEnji Cooper 
176*b89a7cc2SEnji Cooper // Action SaveArgPointee<k>(pointer) saves the value pointed to
177*b89a7cc2SEnji Cooper // by the k-th (0-based) argument of the mock function to *pointer.
178*b89a7cc2SEnji Cooper ACTION_TEMPLATE(SaveArgPointee,
179*b89a7cc2SEnji Cooper                 HAS_1_TEMPLATE_PARAMS(int, k),
180*b89a7cc2SEnji Cooper                 AND_1_VALUE_PARAMS(pointer)) {
181*b89a7cc2SEnji Cooper   *pointer = *::testing::get<k>(args);
182*b89a7cc2SEnji Cooper }
183*b89a7cc2SEnji Cooper 
184*b89a7cc2SEnji Cooper // Action SetArgReferee<k>(value) assigns 'value' to the variable
185*b89a7cc2SEnji Cooper // referenced by the k-th (0-based) argument of the mock function.
186*b89a7cc2SEnji Cooper ACTION_TEMPLATE(SetArgReferee,
187*b89a7cc2SEnji Cooper                 HAS_1_TEMPLATE_PARAMS(int, k),
188*b89a7cc2SEnji Cooper                 AND_1_VALUE_PARAMS(value)) {
189*b89a7cc2SEnji Cooper   typedef typename ::testing::tuple_element<k, args_type>::type argk_type;
190*b89a7cc2SEnji Cooper   // Ensures that argument #k is a reference.  If you get a compiler
191*b89a7cc2SEnji Cooper   // error on the next line, you are using SetArgReferee<k>(value) in
192*b89a7cc2SEnji Cooper   // a mock function whose k-th (0-based) argument is not a reference.
193*b89a7cc2SEnji Cooper   GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
194*b89a7cc2SEnji Cooper                         SetArgReferee_must_be_used_with_a_reference_argument);
195*b89a7cc2SEnji Cooper   ::testing::get<k>(args) = value;
196*b89a7cc2SEnji Cooper }
197*b89a7cc2SEnji Cooper 
198*b89a7cc2SEnji Cooper // Action SetArrayArgument<k>(first, last) copies the elements in
199*b89a7cc2SEnji Cooper // source range [first, last) to the array pointed to by the k-th
200*b89a7cc2SEnji Cooper // (0-based) argument, which can be either a pointer or an
201*b89a7cc2SEnji Cooper // iterator. The action does not take ownership of the elements in the
202*b89a7cc2SEnji Cooper // source range.
203*b89a7cc2SEnji Cooper ACTION_TEMPLATE(SetArrayArgument,
204*b89a7cc2SEnji Cooper                 HAS_1_TEMPLATE_PARAMS(int, k),
205*b89a7cc2SEnji Cooper                 AND_2_VALUE_PARAMS(first, last)) {
206*b89a7cc2SEnji Cooper   // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
207*b89a7cc2SEnji Cooper #ifdef _MSC_VER
208*b89a7cc2SEnji Cooper   internal::CopyElements(first, last, ::testing::get<k>(args));
209*b89a7cc2SEnji Cooper #else
210*b89a7cc2SEnji Cooper   ::std::copy(first, last, ::testing::get<k>(args));
211*b89a7cc2SEnji Cooper #endif
212*b89a7cc2SEnji Cooper }
213*b89a7cc2SEnji Cooper 
214*b89a7cc2SEnji Cooper // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
215*b89a7cc2SEnji Cooper // function.
216*b89a7cc2SEnji Cooper ACTION_TEMPLATE(DeleteArg,
217*b89a7cc2SEnji Cooper                 HAS_1_TEMPLATE_PARAMS(int, k),
218*b89a7cc2SEnji Cooper                 AND_0_VALUE_PARAMS()) {
219*b89a7cc2SEnji Cooper   delete ::testing::get<k>(args);
220*b89a7cc2SEnji Cooper }
221*b89a7cc2SEnji Cooper 
222*b89a7cc2SEnji Cooper // This action returns the value pointed to by 'pointer'.
223*b89a7cc2SEnji Cooper ACTION_P(ReturnPointee, pointer) { return *pointer; }
224*b89a7cc2SEnji Cooper 
225*b89a7cc2SEnji Cooper // Action Throw(exception) can be used in a mock function of any type
226*b89a7cc2SEnji Cooper // to throw the given exception.  Any copyable value can be thrown.
227*b89a7cc2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
228*b89a7cc2SEnji Cooper 
229*b89a7cc2SEnji Cooper // Suppresses the 'unreachable code' warning that VC generates in opt modes.
230*b89a7cc2SEnji Cooper # ifdef _MSC_VER
231*b89a7cc2SEnji Cooper #  pragma warning(push)          // Saves the current warning state.
232*b89a7cc2SEnji Cooper #  pragma warning(disable:4702)  // Temporarily disables warning 4702.
233*b89a7cc2SEnji Cooper # endif
234*b89a7cc2SEnji Cooper ACTION_P(Throw, exception) { throw exception; }
235*b89a7cc2SEnji Cooper # ifdef _MSC_VER
236*b89a7cc2SEnji Cooper #  pragma warning(pop)           // Restores the warning state.
237*b89a7cc2SEnji Cooper # endif
238*b89a7cc2SEnji Cooper 
239*b89a7cc2SEnji Cooper #endif  // GTEST_HAS_EXCEPTIONS
240*b89a7cc2SEnji Cooper 
241*b89a7cc2SEnji Cooper #ifdef _MSC_VER
242*b89a7cc2SEnji Cooper # pragma warning(pop)
243*b89a7cc2SEnji Cooper #endif
244*b89a7cc2SEnji Cooper 
245*b89a7cc2SEnji Cooper }  // namespace testing
246*b89a7cc2SEnji Cooper 
247*b89a7cc2SEnji Cooper #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
248