Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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
43namespace testing {
44namespace 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>).
51template <typename FunctionImpl>
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) {
61 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
62 }
63
64 private:
65 FunctionImpl function_impl_;
66
68};
69
70// Implements the Invoke(object_ptr, &Class::Method) action.
71template <class Class, typename MethodPtr>
73 public:
74 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
75 : method_ptr_(method_ptr), obj_ptr_(obj_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 // The order of these members matters. Reversing the order can trigger
85 // warning C4121 in MSVC (see
86 // http://computer-programming-forum.com/7-vc.net/6fbc30265f860ad1.htm ).
87 const MethodPtr method_ptr_;
88 Class* const obj_ptr_;
89
91};
92
93// An internal replacement for std::copy which mimics its behavior. This is
94// necessary because Visual Studio deprecates ::std::copy, issuing warning 4996.
95// However Visual Studio 2010 and later do not honor #pragmas which disable that
96// warning.
97template<typename InputIterator, typename OutputIterator>
98inline OutputIterator CopyElements(InputIterator first,
99 InputIterator last,
100 OutputIterator output) {
101 for (; first != last; ++first, ++output) {
102 *output = *first;
103 }
104 return output;
105}
106
107} // namespace internal
108
109// Various overloads for Invoke().
110
111// Creates an action that invokes 'function_impl' with the mock
112// function's arguments.
113template <typename FunctionImpl>
119
120// Creates an action that invokes the given method on the given object
121// with the mock function's arguments.
122template <class Class, typename MethodPtr>
124 Class* obj_ptr, MethodPtr method_ptr) {
127}
128
129// WithoutArgs(inner_action) can be used in a mock function with a
130// non-empty argument list to perform inner_action, which takes no
131// argument. In other words, it adapts an action accepting no
132// argument to one that accepts (and ignores) arguments.
133template <typename InnerAction>
134inline internal::WithArgsAction<InnerAction>
135WithoutArgs(const InnerAction& action) {
137}
138
139// WithArg<k>(an_action) creates an action that passes the k-th
140// (0-based) argument of the mock function to an_action and performs
141// it. It adapts an action accepting one argument to one that accepts
142// multiple arguments. For convenience, we also provide
143// WithArgs<k>(an_action) (defined below) as a synonym.
144template <int k, typename InnerAction>
145inline internal::WithArgsAction<InnerAction, k>
146WithArg(const InnerAction& action) {
148}
149
150// The ACTION*() macros trigger warning C4100 (unreferenced formal
151// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
152// the macro definition, as the warnings are generated when the macro
153// is expanded and macro expansion cannot contain #pragma. Therefore
154// we suppress them here.
155#ifdef _MSC_VER
156# pragma warning(push)
157# pragma warning(disable:4100)
158#endif
159
160// Action ReturnArg<k>() returns the k-th argument of the mock function.
162 HAS_1_TEMPLATE_PARAMS(int, k),
163 AND_0_VALUE_PARAMS()) {
164 return ::testing::get<k>(args);
165}
166
167// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
168// mock function to *pointer.
170 HAS_1_TEMPLATE_PARAMS(int, k),
171 AND_1_VALUE_PARAMS(pointer)) {
172 *pointer = ::testing::get<k>(args);
173}
174
175// Action SaveArgPointee<k>(pointer) saves the value pointed to
176// by the k-th (0-based) argument of the mock function to *pointer.
177ACTION_TEMPLATE(SaveArgPointee,
178 HAS_1_TEMPLATE_PARAMS(int, k),
179 AND_1_VALUE_PARAMS(pointer)) {
180 *pointer = *::testing::get<k>(args);
181}
182
183// Action SetArgReferee<k>(value) assigns 'value' to the variable
184// referenced by the k-th (0-based) argument of the mock function.
185ACTION_TEMPLATE(SetArgReferee,
186 HAS_1_TEMPLATE_PARAMS(int, k),
187 AND_1_VALUE_PARAMS(value)) {
188 typedef typename ::testing::tuple_element<k, args_type>::type argk_type;
189 // Ensures that argument #k is a reference. If you get a compiler
190 // error on the next line, you are using SetArgReferee<k>(value) in
191 // a mock function whose k-th (0-based) argument is not a reference.
193 SetArgReferee_must_be_used_with_a_reference_argument);
194 ::testing::get<k>(args) = value;
195}
196
197// Action SetArrayArgument<k>(first, last) copies the elements in
198// source range [first, last) to the array pointed to by the k-th
199// (0-based) argument, which can be either a pointer or an
200// iterator. The action does not take ownership of the elements in the
201// source range.
202ACTION_TEMPLATE(SetArrayArgument,
203 HAS_1_TEMPLATE_PARAMS(int, k),
204 AND_2_VALUE_PARAMS(first, last)) {
205 // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
206#ifdef _MSC_VER
207 internal::CopyElements(first, last, ::testing::get<k>(args));
208#else
209 ::std::copy(first, last, ::testing::get<k>(args));
210#endif
211}
212
213// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
214// function.
216 HAS_1_TEMPLATE_PARAMS(int, k),
217 AND_0_VALUE_PARAMS()) {
218 delete ::testing::get<k>(args);
219}
220
221// This action returns the value pointed to by 'pointer'.
222ACTION_P(ReturnPointee, pointer) { return *pointer; }
223
224// Action Throw(exception) can be used in a mock function of any type
225// to throw the given exception. Any copyable value can be thrown.
226#if GTEST_HAS_EXCEPTIONS
227
228// Suppresses the 'unreachable code' warning that VC generates in opt modes.
229# ifdef _MSC_VER
230# pragma warning(push) // Saves the current warning state.
231# pragma warning(disable:4702) // Temporarily disables warning 4702.
232# endif
233ACTION_P(Throw, exception) { throw exception; }
234# ifdef _MSC_VER
235# pragma warning(pop) // Restores the warning state.
236# endif
237
238#endif // GTEST_HAS_EXCEPTIONS
239
240#ifdef _MSC_VER
241# pragma warning(pop)
242#endif
243
244} // namespace testing
245
246#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
InvokeAction(FunctionImpl function_impl)
Result Perform(const ArgumentTuple &args)
InvokeMethodAction(Class *obj_ptr, MethodPtr method_ptr)
Result Perform(const ArgumentTuple &args) const
#define ACTION_P(name, p0)
#define ACTION_TEMPLATE(name, template_params, value_params)
#define GTEST_DISALLOW_ASSIGN_(type)
Definition gtest-port.h:912
#define GTEST_COMPILE_ASSERT_(expr, msg)
OutputIterator CopyElements(InputIterator first, InputIterator last, OutputIterator output)
internal::WithArgsAction< InnerAction, k > WithArg(const InnerAction &action)
PolymorphicAction< Impl > MakePolymorphicAction(const Impl &impl)
internal::WithArgsAction< InnerAction > WithoutArgs(const InnerAction &action)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
#define value
Definition pkcs11.h:157
const GenericPointer< typename T::ValueType > & pointer
Definition pointer.h:1181