blob: 4d9a28ea8a85376f1d45d78414d4f1c4837379b1 [file] [log] [blame]
zhanyong.wana18423e2009-07-22 23:58:19 +00001// 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.
Gennadiy Civila3c0dd02018-08-14 14:04:07 -040029
zhanyong.wana18423e2009-07-22 23:58:19 +000030
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
Gennadiy Civil984cba32018-07-27 11:15:08 -040035// GOOGLETEST_CM0002 DO NOT DELETE
36
zhanyong.wana18423e2009-07-22 23:58:19 +000037#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
38#define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
39
zhanyong.wan2321b2a2010-10-14 06:51:27 +000040#include <algorithm>
41
zhanyong.wan53e08c42010-09-14 05:38:21 +000042#include "gmock/gmock-generated-actions.h"
zhanyong.wana18423e2009-07-22 23:58:19 +000043
44namespace testing {
45namespace 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>).
52template <typename FunctionImpl>
53class 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 }
zhanyong.wan32de5f52009-12-23 00:13:23 +000064
zhanyong.wana18423e2009-07-22 23:58:19 +000065 private:
66 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +000067
68 GTEST_DISALLOW_ASSIGN_(InvokeAction);
zhanyong.wana18423e2009-07-22 23:58:19 +000069};
70
71// Implements the Invoke(object_ptr, &Class::Method) action.
72template <class Class, typename MethodPtr>
73class InvokeMethodAction {
74 public:
75 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
kosak6b817802015-01-08 02:38:14 +000076 : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {}
zhanyong.wana18423e2009-07-22 23:58:19 +000077
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 }
zhanyong.wan32de5f52009-12-23 00:13:23 +000083
zhanyong.wana18423e2009-07-22 23:58:19 +000084 private:
kosak6b817802015-01-08 02:38:14 +000085 // 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 ).
zhanyong.wana18423e2009-07-22 23:58:19 +000088 const MethodPtr method_ptr_;
kosak6b817802015-01-08 02:38:14 +000089 Class* const obj_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +000090
91 GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
zhanyong.wana18423e2009-07-22 23:58:19 +000092};
93
kosakb93d0f12014-01-13 22:28:01 +000094// 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.
98template<typename InputIterator, typename OutputIterator>
99inline 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
zhanyong.wana18423e2009-07-22 23:58:19 +0000108} // namespace internal
109
110// Various overloads for Invoke().
111
112// Creates an action that invokes 'function_impl' with the mock
113// function's arguments.
114template <typename FunctionImpl>
115PolymorphicAction<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.
123template <class Class, typename MethodPtr>
124PolymorphicAction<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.
134template <typename InnerAction>
135inline internal::WithArgsAction<InnerAction>
136WithoutArgs(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.
145template <int k, typename InnerAction>
146inline internal::WithArgsAction<InnerAction, k>
147WithArg(const InnerAction& action) {
148 return internal::WithArgsAction<InnerAction, k>(action);
149}
150
zhanyong.wan32de5f52009-12-23 00:13:23 +0000151// 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
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000157# pragma warning(push)
158# pragma warning(disable:4100)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000159#endif
160
zhanyong.wana18423e2009-07-22 23:58:19 +0000161// Action ReturnArg<k>() returns the k-th argument of the mock function.
162ACTION_TEMPLATE(ReturnArg,
163 HAS_1_TEMPLATE_PARAMS(int, k),
164 AND_0_VALUE_PARAMS()) {
kosakbd018832014-04-02 20:30:00 +0000165 return ::testing::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +0000166}
167
168// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
169// mock function to *pointer.
170ACTION_TEMPLATE(SaveArg,
171 HAS_1_TEMPLATE_PARAMS(int, k),
172 AND_1_VALUE_PARAMS(pointer)) {
kosakbd018832014-04-02 20:30:00 +0000173 *pointer = ::testing::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +0000174}
175
zhanyong.wan2321b2a2010-10-14 06:51:27 +0000176// Action SaveArgPointee<k>(pointer) saves the value pointed to
177// by the k-th (0-based) argument of the mock function to *pointer.
178ACTION_TEMPLATE(SaveArgPointee,
179 HAS_1_TEMPLATE_PARAMS(int, k),
180 AND_1_VALUE_PARAMS(pointer)) {
kosakbd018832014-04-02 20:30:00 +0000181 *pointer = *::testing::get<k>(args);
zhanyong.wan2321b2a2010-10-14 06:51:27 +0000182}
183
zhanyong.wana18423e2009-07-22 23:58:19 +0000184// Action SetArgReferee<k>(value) assigns 'value' to the variable
185// referenced by the k-th (0-based) argument of the mock function.
186ACTION_TEMPLATE(SetArgReferee,
187 HAS_1_TEMPLATE_PARAMS(int, k),
188 AND_1_VALUE_PARAMS(value)) {
kosakbd018832014-04-02 20:30:00 +0000189 typedef typename ::testing::tuple_element<k, args_type>::type argk_type;
zhanyong.wana18423e2009-07-22 23:58:19 +0000190 // 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.
zhanyong.wan02f71062010-05-10 17:14:29 +0000193 GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
zhanyong.wana18423e2009-07-22 23:58:19 +0000194 SetArgReferee_must_be_used_with_a_reference_argument);
kosakbd018832014-04-02 20:30:00 +0000195 ::testing::get<k>(args) = value;
zhanyong.wana18423e2009-07-22 23:58:19 +0000196}
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.
203ACTION_TEMPLATE(SetArrayArgument,
204 HAS_1_TEMPLATE_PARAMS(int, k),
205 AND_2_VALUE_PARAMS(first, last)) {
kosakb93d0f12014-01-13 22:28:01 +0000206 // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
zhanyong.wana18423e2009-07-22 23:58:19 +0000207#ifdef _MSC_VER
kosakbd018832014-04-02 20:30:00 +0000208 internal::CopyElements(first, last, ::testing::get<k>(args));
kosakb93d0f12014-01-13 22:28:01 +0000209#else
kosakbd018832014-04-02 20:30:00 +0000210 ::std::copy(first, last, ::testing::get<k>(args));
zhanyong.wana18423e2009-07-22 23:58:19 +0000211#endif
212}
213
214// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
215// function.
216ACTION_TEMPLATE(DeleteArg,
217 HAS_1_TEMPLATE_PARAMS(int, k),
218 AND_0_VALUE_PARAMS()) {
kosakbd018832014-04-02 20:30:00 +0000219 delete ::testing::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +0000220}
221
zhanyong.wane3bd0982010-07-03 00:16:42 +0000222// This action returns the value pointed to by 'pointer'.
223ACTION_P(ReturnPointee, pointer) { return *pointer; }
224
zhanyong.wana18423e2009-07-22 23:58:19 +0000225// 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
vladloseve2e8ba42010-05-13 18:16:03 +0000228
229// Suppresses the 'unreachable code' warning that VC generates in opt modes.
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000230# ifdef _MSC_VER
231# pragma warning(push) // Saves the current warning state.
232# pragma warning(disable:4702) // Temporarily disables warning 4702.
233# endif
zhanyong.wana18423e2009-07-22 23:58:19 +0000234ACTION_P(Throw, exception) { throw exception; }
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000235# ifdef _MSC_VER
236# pragma warning(pop) // Restores the warning state.
237# endif
vladloseve2e8ba42010-05-13 18:16:03 +0000238
zhanyong.wana18423e2009-07-22 23:58:19 +0000239#endif // GTEST_HAS_EXCEPTIONS
240
zhanyong.wan32de5f52009-12-23 00:13:23 +0000241#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000242# pragma warning(pop)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000243#endif
244
zhanyong.wana18423e2009-07-22 23:58:19 +0000245} // namespace testing
246
247#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_