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.
31 // Google Mock - a framework for writing C++ mock classes.
33 // This file implements some actions that depend on gmock-generated-actions.h.
35 // GOOGLETEST_CM0002 DO NOT DELETE
37 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
38 #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
42 #include "gmock/gmock-generated-actions.h"
47 // Implements the Invoke(f) action. The template argument
48 // FunctionImpl is the implementation type of f, which can be either a
49 // function pointer or a functor. Invoke(f) can be used as an
50 // Action<F> as long as f's type is compatible with F (i.e. f can be
51 // assigned to a tr1::function<F>).
52 template <typename FunctionImpl>
55 // The c'tor makes a copy of function_impl (either a function
56 // pointer or a functor).
57 explicit InvokeAction(FunctionImpl function_impl)
58 : function_impl_(function_impl) {}
60 template <typename Result, typename ArgumentTuple>
61 Result Perform(const ArgumentTuple& args) {
62 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
66 FunctionImpl function_impl_;
68 GTEST_DISALLOW_ASSIGN_(InvokeAction);
71 // Implements the Invoke(object_ptr, &Class::Method) action.
72 template <class Class, typename MethodPtr>
73 class InvokeMethodAction {
75 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
76 : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {}
78 template <typename Result, typename ArgumentTuple>
79 Result Perform(const ArgumentTuple& args) const {
80 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
81 obj_ptr_, method_ptr_, args);
85 // The order of these members matters. Reversing the order can trigger
86 // warning C4121 in MSVC (see
87 // http://computer-programming-forum.com/7-vc.net/6fbc30265f860ad1.htm ).
88 const MethodPtr method_ptr_;
89 Class* const obj_ptr_;
91 GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
94 // An internal replacement for std::copy which mimics its behavior. This is
95 // necessary because Visual Studio deprecates ::std::copy, issuing warning 4996.
96 // However Visual Studio 2010 and later do not honor #pragmas which disable that
98 template<typename InputIterator, typename OutputIterator>
99 inline OutputIterator CopyElements(InputIterator first,
101 OutputIterator output) {
102 for (; first != last; ++first, ++output) {
108 } // namespace internal
110 // Various overloads for Invoke().
112 // Creates an action that invokes 'function_impl' with the mock
113 // function's arguments.
114 template <typename FunctionImpl>
115 PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
116 FunctionImpl function_impl) {
117 return MakePolymorphicAction(
118 internal::InvokeAction<FunctionImpl>(function_impl));
121 // Creates an action that invokes the given method on the given object
122 // with the mock function's arguments.
123 template <class Class, typename MethodPtr>
124 PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
125 Class* obj_ptr, MethodPtr method_ptr) {
126 return MakePolymorphicAction(
127 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
130 // WithoutArgs(inner_action) can be used in a mock function with a
131 // non-empty argument list to perform inner_action, which takes no
132 // argument. In other words, it adapts an action accepting no
133 // argument to one that accepts (and ignores) arguments.
134 template <typename InnerAction>
135 inline internal::WithArgsAction<InnerAction>
136 WithoutArgs(const InnerAction& action) {
137 return internal::WithArgsAction<InnerAction>(action);
140 // WithArg<k>(an_action) creates an action that passes the k-th
141 // (0-based) argument of the mock function to an_action and performs
142 // it. It adapts an action accepting one argument to one that accepts
143 // multiple arguments. For convenience, we also provide
144 // WithArgs<k>(an_action) (defined below) as a synonym.
145 template <int k, typename InnerAction>
146 inline internal::WithArgsAction<InnerAction, k>
147 WithArg(const InnerAction& action) {
148 return internal::WithArgsAction<InnerAction, k>(action);
151 // The ACTION*() macros trigger warning C4100 (unreferenced formal
152 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
153 // the macro definition, as the warnings are generated when the macro
154 // is expanded and macro expansion cannot contain #pragma. Therefore
155 // we suppress them here.
157 # pragma warning(push)
158 # pragma warning(disable:4100)
161 // Action ReturnArg<k>() returns the k-th argument of the mock function.
162 ACTION_TEMPLATE(ReturnArg,
163 HAS_1_TEMPLATE_PARAMS(int, k),
164 AND_0_VALUE_PARAMS()) {
165 return ::testing::get<k>(args);
168 // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
169 // mock function to *pointer.
170 ACTION_TEMPLATE(SaveArg,
171 HAS_1_TEMPLATE_PARAMS(int, k),
172 AND_1_VALUE_PARAMS(pointer)) {
173 *pointer = ::testing::get<k>(args);
176 // Action SaveArgPointee<k>(pointer) saves the value pointed to
177 // by the k-th (0-based) argument of the mock function to *pointer.
178 ACTION_TEMPLATE(SaveArgPointee,
179 HAS_1_TEMPLATE_PARAMS(int, k),
180 AND_1_VALUE_PARAMS(pointer)) {
181 *pointer = *::testing::get<k>(args);
184 // Action SetArgReferee<k>(value) assigns 'value' to the variable
185 // referenced by the k-th (0-based) argument of the mock function.
186 ACTION_TEMPLATE(SetArgReferee,
187 HAS_1_TEMPLATE_PARAMS(int, k),
188 AND_1_VALUE_PARAMS(value)) {
189 typedef typename ::testing::tuple_element<k, args_type>::type argk_type;
190 // Ensures that argument #k is a reference. If you get a compiler
191 // error on the next line, you are using SetArgReferee<k>(value) in
192 // a mock function whose k-th (0-based) argument is not a reference.
193 GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
194 SetArgReferee_must_be_used_with_a_reference_argument);
195 ::testing::get<k>(args) = value;
198 // Action SetArrayArgument<k>(first, last) copies the elements in
199 // source range [first, last) to the array pointed to by the k-th
200 // (0-based) argument, which can be either a pointer or an
201 // iterator. The action does not take ownership of the elements in the
203 ACTION_TEMPLATE(SetArrayArgument,
204 HAS_1_TEMPLATE_PARAMS(int, k),
205 AND_2_VALUE_PARAMS(first, last)) {
206 // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
208 internal::CopyElements(first, last, ::testing::get<k>(args));
210 ::std::copy(first, last, ::testing::get<k>(args));
214 // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
216 ACTION_TEMPLATE(DeleteArg,
217 HAS_1_TEMPLATE_PARAMS(int, k),
218 AND_0_VALUE_PARAMS()) {
219 delete ::testing::get<k>(args);
222 // This action returns the value pointed to by 'pointer'.
223 ACTION_P(ReturnPointee, pointer) { return *pointer; }
225 // Action Throw(exception) can be used in a mock function of any type
226 // to throw the given exception. Any copyable value can be thrown.
227 #if GTEST_HAS_EXCEPTIONS
229 // Suppresses the 'unreachable code' warning that VC generates in opt modes.
231 # pragma warning(push) // Saves the current warning state.
232 # pragma warning(disable:4702) // Temporarily disables warning 4702.
234 ACTION_P(Throw, exception) { throw exception; }
236 # pragma warning(pop) // Restores the warning state.
239 #endif // GTEST_HAS_EXCEPTIONS
242 # pragma warning(pop)
245 } // namespace testing
247 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_