Orxonox  0.0.5 Codename: Arcturus
gmock-more-actions.h
Go to the documentation of this file.
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 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
37 #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
38 
39 #include <algorithm>
40 
42 
43 namespace testing {
44 namespace internal {
45 
46 // Implements the Invoke(f) action. The template argument
47 // FunctionImpl is the implementation type of f, which can be either a
48 // function pointer or a functor. Invoke(f) can be used as an
49 // Action<F> as long as f's type is compatible with F (i.e. f can be
50 // assigned to a tr1::function<F>).
51 template <typename FunctionImpl>
52 class InvokeAction {
53  public:
54  // The c'tor makes a copy of function_impl (either a function
55  // pointer or a functor).
56  explicit InvokeAction(FunctionImpl function_impl)
57  : function_impl_(function_impl) {}
58 
59  template <typename Result, typename ArgumentTuple>
60  Result Perform(const ArgumentTuple& args) {
62  }
63 
64  private:
65  FunctionImpl function_impl_;
66 
68 };
69 
70 // Implements the Invoke(object_ptr, &Class::Method) action.
71 template <class Class, typename MethodPtr>
73  public:
74  InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
75  : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
76 
77  template <typename Result, typename ArgumentTuple>
78  Result Perform(const ArgumentTuple& args) const {
80  obj_ptr_, method_ptr_, args);
81  }
82 
83  private:
84  Class* const obj_ptr_;
85  const MethodPtr method_ptr_;
86 
88 };
89 
90 } // namespace internal
91 
92 // Various overloads for Invoke().
93 
94 // Creates an action that invokes 'function_impl' with the mock
95 // function's arguments.
96 template <typename FunctionImpl>
98  FunctionImpl function_impl) {
99  return MakePolymorphicAction(
100  internal::InvokeAction<FunctionImpl>(function_impl));
101 }
102 
103 // Creates an action that invokes the given method on the given object
104 // with the mock function's arguments.
105 template <class Class, typename MethodPtr>
107  Class* obj_ptr, MethodPtr method_ptr) {
108  return MakePolymorphicAction(
110 }
111 
112 // WithoutArgs(inner_action) can be used in a mock function with a
113 // non-empty argument list to perform inner_action, which takes no
114 // argument. In other words, it adapts an action accepting no
115 // argument to one that accepts (and ignores) arguments.
116 template <typename InnerAction>
118 WithoutArgs(const InnerAction& action) {
120 }
121 
122 // WithArg<k>(an_action) creates an action that passes the k-th
123 // (0-based) argument of the mock function to an_action and performs
124 // it. It adapts an action accepting one argument to one that accepts
125 // multiple arguments. For convenience, we also provide
126 // WithArgs<k>(an_action) (defined below) as a synonym.
127 template <int k, typename InnerAction>
129 WithArg(const InnerAction& action) {
131 }
132 
133 // The ACTION*() macros trigger warning C4100 (unreferenced formal
134 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
135 // the macro definition, as the warnings are generated when the macro
136 // is expanded and macro expansion cannot contain #pragma. Therefore
137 // we suppress them here.
138 #ifdef _MSC_VER
139 # pragma warning(push)
140 # pragma warning(disable:4100)
141 #endif
142 
143 // Action ReturnArg<k>() returns the k-th argument of the mock function.
144 ACTION_TEMPLATE(ReturnArg,
145  HAS_1_TEMPLATE_PARAMS(int, k),
146  AND_0_VALUE_PARAMS()) {
147  return std::tr1::get<k>(args);
148 }
149 
150 // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
151 // mock function to *pointer.
153  HAS_1_TEMPLATE_PARAMS(int, k),
154  AND_1_VALUE_PARAMS(pointer)) {
155  *pointer = ::std::tr1::get<k>(args);
156 }
157 
158 // Action SaveArgPointee<k>(pointer) saves the value pointed to
159 // by the k-th (0-based) argument of the mock function to *pointer.
160 ACTION_TEMPLATE(SaveArgPointee,
161  HAS_1_TEMPLATE_PARAMS(int, k),
162  AND_1_VALUE_PARAMS(pointer)) {
163  *pointer = *::std::tr1::get<k>(args);
164 }
165 
166 // Action SetArgReferee<k>(value) assigns 'value' to the variable
167 // referenced by the k-th (0-based) argument of the mock function.
168 ACTION_TEMPLATE(SetArgReferee,
169  HAS_1_TEMPLATE_PARAMS(int, k),
170  AND_1_VALUE_PARAMS(value)) {
171  typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type;
172  // Ensures that argument #k is a reference. If you get a compiler
173  // error on the next line, you are using SetArgReferee<k>(value) in
174  // a mock function whose k-th (0-based) argument is not a reference.
176  SetArgReferee_must_be_used_with_a_reference_argument);
177  ::std::tr1::get<k>(args) = value;
178 }
179 
180 // Action SetArrayArgument<k>(first, last) copies the elements in
181 // source range [first, last) to the array pointed to by the k-th
182 // (0-based) argument, which can be either a pointer or an
183 // iterator. The action does not take ownership of the elements in the
184 // source range.
185 ACTION_TEMPLATE(SetArrayArgument,
186  HAS_1_TEMPLATE_PARAMS(int, k),
187  AND_2_VALUE_PARAMS(first, last)) {
188  // Microsoft compiler deprecates ::std::copy, so we want to suppress warning
189  // 4996 (Function call with parameters that may be unsafe) there.
190 #ifdef _MSC_VER
191 # pragma warning(push) // Saves the current warning state.
192 # pragma warning(disable:4996) // Temporarily disables warning 4996.
193 #endif
194  ::std::copy(first, last, ::std::tr1::get<k>(args));
195 #ifdef _MSC_VER
196 # pragma warning(pop) // Restores the warning state.
197 #endif
198 }
199 
200 // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
201 // function.
202 ACTION_TEMPLATE(DeleteArg,
203  HAS_1_TEMPLATE_PARAMS(int, k),
204  AND_0_VALUE_PARAMS()) {
205  delete ::std::tr1::get<k>(args);
206 }
207 
208 // This action returns the value pointed to by 'pointer'.
209 ACTION_P(ReturnPointee, pointer) { return *pointer; }
210 
211 // Action Throw(exception) can be used in a mock function of any type
212 // to throw the given exception. Any copyable value can be thrown.
213 #if GTEST_HAS_EXCEPTIONS
214 
215 // Suppresses the 'unreachable code' warning that VC generates in opt modes.
216 # ifdef _MSC_VER
217 # pragma warning(push) // Saves the current warning state.
218 # pragma warning(disable:4702) // Temporarily disables warning 4702.
219 # endif
220 ACTION_P(Throw, exception) { throw exception; }
221 # ifdef _MSC_VER
222 # pragma warning(pop) // Restores the warning state.
223 # endif
224 
225 #endif // GTEST_HAS_EXCEPTIONS
226 
227 #ifdef _MSC_VER
228 # pragma warning(pop)
229 #endif
230 
231 } // namespace testing
232 
233 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
Definition: gmock-actions.h:49
Definition: gmock-generated-actions.h:589
Definition: gmock-more-actions.h:72
Definition: gmock-generated-actions.h:51
PolymorphicAction< Impl > MakePolymorphicAction(const Impl &impl)
Definition: gmock-actions.h:398
internal::WithArgsAction< InnerAction, k > WithArg(const InnerAction &action)
Definition: gmock-more-actions.h:129
Definition: gmock-more-actions.h:52
Class *const obj_ptr_
Definition: gmock-more-actions.h:84
const MethodPtr method_ptr_
Definition: gmock-more-actions.h:85
#define GTEST_COMPILE_ASSERT_(expr, msg)
Definition: gtest-port.h:699
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
Definition: gmock-more-actions.h:97
ACTION_TEMPLATE(InvokeArgument, HAS_1_TEMPLATE_PARAMS(int, k), AND_0_VALUE_PARAMS())
Definition: gmock-generated-actions.h:2265
InvokeMethodAction(Class *obj_ptr, MethodPtr method_ptr)
Definition: gmock-more-actions.h:74
Result Perform(const ArgumentTuple &args)
Definition: gmock-more-actions.h:60
Definition: gmock-internal-utils.h:338
InvokeAction(FunctionImpl function_impl)
Definition: gmock-more-actions.h:56
Definition: gmock-actions.h:350
Result Perform(const ArgumentTuple &args) const
Definition: gmock-more-actions.h:78
FunctionImpl function_impl_
Definition: gmock-more-actions.h:65
PolymorphicAction< internal::InvokeMethodAction< Class, MethodPtr > > Invoke(Class *obj_ptr, MethodPtr method_ptr)
Definition: gmock-more-actions.h:106
internal::WithArgsAction< InnerAction > WithoutArgs(const InnerAction &action)
Definition: gmock-more-actions.h:118
ACTION_P(ReturnPointee, pointer)
Definition: gmock-more-actions.h:209
#define Class(ClassName)
Returns the Identifier of the given class.
Definition: CoreIncludes.h:147