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