55049
|
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 |
|
|
31 |
// Google Mock - a framework for writing C++ mock classes.
|
|
32 |
//
|
|
33 |
// This file implements some actions that depend on gmock-generated-actions.h.
|
|
34 |
|
|
35 |
// GOOGLETEST_CM0002 DO NOT DELETE
|
|
36 |
|
|
37 |
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
|
|
38 |
#define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
|
|
39 |
|
|
40 |
#include <algorithm>
|
|
41 |
|
|
42 |
#include "gmock/gmock-generated-actions.h"
|
|
43 |
|
|
44 |
namespace testing {
|
|
45 |
namespace internal {
|
|
46 |
|
|
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>
|
|
53 |
class InvokeAction {
|
|
54 |
public:
|
|
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) {}
|
|
59 |
|
|
60 |
template <typename Result, typename ArgumentTuple>
|
|
61 |
Result Perform(const ArgumentTuple& args) {
|
|
62 |
return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
|
|
63 |
}
|
|
64 |
|
|
65 |
private:
|
|
66 |
FunctionImpl function_impl_;
|
|
67 |
|
|
68 |
GTEST_DISALLOW_ASSIGN_(InvokeAction);
|
|
69 |
};
|
|
70 |
|
|
71 |
// Implements the Invoke(object_ptr, &Class::Method) action.
|
|
72 |
template <class Class, typename MethodPtr>
|
|
73 |
class InvokeMethodAction {
|
|
74 |
public:
|
|
75 |
InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
|
|
76 |
: method_ptr_(method_ptr), obj_ptr_(obj_ptr) {}
|
|
77 |
|
|
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);
|
|
82 |
}
|
|
83 |
|
|
84 |
private:
|
|
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_;
|
|
90 |
|
|
91 |
GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
|
|
92 |
};
|
|
93 |
|
|
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
|
|
97 |
// warning.
|
|
98 |
template<typename InputIterator, typename OutputIterator>
|
|
99 |
inline OutputIterator CopyElements(InputIterator first,
|
|
100 |
InputIterator last,
|
|
101 |
OutputIterator output) {
|
|
102 |
for (; first != last; ++first, ++output) {
|
|
103 |
*output = *first;
|
|
104 |
}
|
|
105 |
return output;
|
|
106 |
}
|
|
107 |
|
|
108 |
} // namespace internal
|
|
109 |
|
|
110 |
// Various overloads for Invoke().
|
|
111 |
|
|
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));
|
|
119 |
}
|
|
120 |
|
|
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));
|
|
128 |
}
|
|
129 |
|
|
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);
|
|
138 |
}
|
|
139 |
|
|
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);
|
|
149 |
}
|
|
150 |
|
|
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.
|
|
156 |
#ifdef _MSC_VER
|
|
157 |
# pragma warning(push)
|
|
158 |
# pragma warning(disable:4100)
|
|
159 |
#endif
|
|
160 |
|
|
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);
|
|
166 |
}
|
|
167 |
|
|
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);
|
|
174 |
}
|
|
175 |
|
|
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);
|
|
182 |
}
|
|
183 |
|
|
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;
|
|
196 |
}
|
|
197 |
|
|
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
|
|
202 |
// source range.
|
|
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.
|
|
207 |
#ifdef _MSC_VER
|
|
208 |
internal::CopyElements(first, last, ::testing::get<k>(args));
|
|
209 |
#else
|
|
210 |
::std::copy(first, last, ::testing::get<k>(args));
|
|
211 |
#endif
|
|
212 |
}
|
|
213 |
|
|
214 |
// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
|
|
215 |
// function.
|
|
216 |
ACTION_TEMPLATE(DeleteArg,
|
|
217 |
HAS_1_TEMPLATE_PARAMS(int, k),
|
|
218 |
AND_0_VALUE_PARAMS()) {
|
|
219 |
delete ::testing::get<k>(args);
|
|
220 |
}
|
|
221 |
|
|
222 |
// This action returns the value pointed to by 'pointer'.
|
|
223 |
ACTION_P(ReturnPointee, pointer) { return *pointer; }
|
|
224 |
|
|
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
|
|
228 |
|
|
229 |
// Suppresses the 'unreachable code' warning that VC generates in opt modes.
|
|
230 |
# ifdef _MSC_VER
|
|
231 |
# pragma warning(push) // Saves the current warning state.
|
|
232 |
# pragma warning(disable:4702) // Temporarily disables warning 4702.
|
|
233 |
# endif
|
|
234 |
ACTION_P(Throw, exception) { throw exception; }
|
|
235 |
# ifdef _MSC_VER
|
|
236 |
# pragma warning(pop) // Restores the warning state.
|
|
237 |
# endif
|
|
238 |
|
|
239 |
#endif // GTEST_HAS_EXCEPTIONS
|
|
240 |
|
|
241 |
#ifdef _MSC_VER
|
|
242 |
# pragma warning(pop)
|
|
243 |
#endif
|
|
244 |
|
|
245 |
} // namespace testing
|
|
246 |
|
|
247 |
#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
|