• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: wan@google.com (Zhanyong Wan)
31 
32 // Google Mock - a framework for writing C++ mock classes.
33 //
34 // This file implements some actions that depend on gmock-generated-actions.h.
35 
36 // IWYU pragma: private, include "gmock/gmock.h"
37 
38 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
39 #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
40 
41 #include <algorithm>
42 
43 #include "gmock/gmock-generated-actions.h"
44 
45 namespace testing {
46 namespace internal {
47 
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>
54 class InvokeAction {
55  public:
56   // The c'tor makes a copy of function_impl (either a function
57   // pointer or a functor).
InvokeAction(FunctionImpl function_impl)58   explicit InvokeAction(FunctionImpl function_impl)
59       : function_impl_(function_impl) {}
60 
61   template <typename Result, typename ArgumentTuple>
Perform(const ArgumentTuple & args)62   Result Perform(const ArgumentTuple& args) {
63     return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
64   }
65 
66  private:
67   FunctionImpl function_impl_;
68 
69   GTEST_DISALLOW_ASSIGN_(InvokeAction);
70 };
71 
72 // Implements the Invoke(object_ptr, &Class::Method) action.
73 template <class Class, typename MethodPtr>
74 class InvokeMethodAction {
75  public:
InvokeMethodAction(Class * obj_ptr,MethodPtr method_ptr)76   InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
77       : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {}
78 
79   template <typename Result, typename ArgumentTuple>
Perform(const ArgumentTuple & args)80   Result Perform(const ArgumentTuple& args) const {
81     return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
82         obj_ptr_, method_ptr_, args);
83   }
84 
85  private:
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_;
91 
92   GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
93 };
94 
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
98 // warning.
99 template<typename InputIterator, typename OutputIterator>
CopyElements(InputIterator first,InputIterator last,OutputIterator output)100 inline OutputIterator CopyElements(InputIterator first,
101                                    InputIterator last,
102                                    OutputIterator output) {
103   for (; first != last; ++first, ++output) {
104     *output = *first;
105   }
106   return output;
107 }
108 
109 }  // namespace internal
110 
111 // Various overloads for Invoke().
112 
113 // Creates an action that invokes 'function_impl' with the mock
114 // function's arguments.
115 template <typename FunctionImpl>
Invoke(FunctionImpl function_impl)116 PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
117     FunctionImpl function_impl) {
118   return MakePolymorphicAction(
119       internal::InvokeAction<FunctionImpl>(function_impl));
120 }
121 
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>
Invoke(Class * obj_ptr,MethodPtr method_ptr)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));
129 }
130 
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>
WithoutArgs(const InnerAction & action)137 WithoutArgs(const InnerAction& action) {
138   return internal::WithArgsAction<InnerAction>(action);
139 }
140 
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>
WithArg(const InnerAction & action)148 WithArg(const InnerAction& action) {
149   return internal::WithArgsAction<InnerAction, k>(action);
150 }
151 
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.
157 #ifdef _MSC_VER
158 # pragma warning(push)
159 # pragma warning(disable:4100)
160 #endif
161 
162 // Action ReturnArg<k>() returns the k-th argument of the mock function.
ACTION_TEMPLATE(ReturnArg,HAS_1_TEMPLATE_PARAMS (int,k),AND_0_VALUE_PARAMS ())163 ACTION_TEMPLATE(ReturnArg,
164                 HAS_1_TEMPLATE_PARAMS(int, k),
165                 AND_0_VALUE_PARAMS()) {
166   return ::testing::get<k>(args);
167 }
168 
169 // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
170 // mock function to *pointer.
ACTION_TEMPLATE(SaveArg,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (pointer))171 ACTION_TEMPLATE(SaveArg,
172                 HAS_1_TEMPLATE_PARAMS(int, k),
173                 AND_1_VALUE_PARAMS(pointer)) {
174   *pointer = ::testing::get<k>(args);
175 }
176 
177 // Action SaveArgPointee<k>(pointer) saves the value pointed to
178 // by the k-th (0-based) argument of the mock function to *pointer.
ACTION_TEMPLATE(SaveArgPointee,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (pointer))179 ACTION_TEMPLATE(SaveArgPointee,
180                 HAS_1_TEMPLATE_PARAMS(int, k),
181                 AND_1_VALUE_PARAMS(pointer)) {
182   *pointer = *::testing::get<k>(args);
183 }
184 
185 // Action SetArgReferee<k>(value) assigns 'value' to the variable
186 // referenced by the k-th (0-based) argument of the mock function.
ACTION_TEMPLATE(SetArgReferee,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (value))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;
197 }
198 
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
203 // source range.
ACTION_TEMPLATE(SetArrayArgument,HAS_1_TEMPLATE_PARAMS (int,k),AND_2_VALUE_PARAMS (first,last))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.
208 #ifdef _MSC_VER
209   internal::CopyElements(first, last, ::testing::get<k>(args));
210 #else
211   ::std::copy(first, last, ::testing::get<k>(args));
212 #endif
213 }
214 
215 // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
216 // function.
ACTION_TEMPLATE(DeleteArg,HAS_1_TEMPLATE_PARAMS (int,k),AND_0_VALUE_PARAMS ())217 ACTION_TEMPLATE(DeleteArg,
218                 HAS_1_TEMPLATE_PARAMS(int, k),
219                 AND_0_VALUE_PARAMS()) {
220   delete ::testing::get<k>(args);
221 }
222 
223 // This action returns the value pointed to by 'pointer'.
ACTION_P(ReturnPointee,pointer)224 ACTION_P(ReturnPointee, pointer) { return *pointer; }
225 
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
229 
230 // Suppresses the 'unreachable code' warning that VC generates in opt modes.
231 # ifdef _MSC_VER
232 #  pragma warning(push)          // Saves the current warning state.
233 #  pragma warning(disable:4702)  // Temporarily disables warning 4702.
234 # endif
ACTION_P(Throw,exception)235 ACTION_P(Throw, exception) { throw exception; }
236 # ifdef _MSC_VER
237 #  pragma warning(pop)           // Restores the warning state.
238 # endif
239 
240 #endif  // GTEST_HAS_EXCEPTIONS
241 
242 #ifdef _MSC_VER
243 # pragma warning(pop)
244 #endif
245 
246 }  // namespace testing
247 
248 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
249