blob: 1106818dd74c645b3c7927b65c0403b69676a3fa [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.
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
Gennadiy Civil984cba32018-07-27 11:15:08 -040036// GOOGLETEST_CM0002 DO NOT DELETE
37
zhanyong.wana18423e2009-07-22 23:58:19 +000038#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
39#define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
40
zhanyong.wan2321b2a2010-10-14 06:51:27 +000041#include <algorithm>
42
zhanyong.wan53e08c42010-09-14 05:38:21 +000043#include "gmock/gmock-generated-actions.h"
zhanyong.wana18423e2009-07-22 23:58:19 +000044
45namespace testing {
46namespace 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>).
53template <typename FunctionImpl>
54class InvokeAction {
55 public:
56 // The c'tor makes a copy of function_impl (either a function
57 // pointer or a functor).
58 explicit InvokeAction(FunctionImpl function_impl)
59 : function_impl_(function_impl) {}
60
61 template <typename Result, typename ArgumentTuple>
62 Result Perform(const ArgumentTuple& args) {
63 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
64 }
zhanyong.wan32de5f52009-12-23 00:13:23 +000065
zhanyong.wana18423e2009-07-22 23:58:19 +000066 private:
67 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +000068
69 GTEST_DISALLOW_ASSIGN_(InvokeAction);
zhanyong.wana18423e2009-07-22 23:58:19 +000070};
71
72// Implements the Invoke(object_ptr, &Class::Method) action.
73template <class Class, typename MethodPtr>
74class InvokeMethodAction {
75 public:
76 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
kosak6b817802015-01-08 02:38:14 +000077 : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {}
zhanyong.wana18423e2009-07-22 23:58:19 +000078
79 template <typename Result, typename ArgumentTuple>
80 Result Perform(const ArgumentTuple& args) const {
81 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
82 obj_ptr_, method_ptr_, args);
83 }
zhanyong.wan32de5f52009-12-23 00:13:23 +000084
zhanyong.wana18423e2009-07-22 23:58:19 +000085 private:
kosak6b817802015-01-08 02:38:14 +000086 // 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 ).
zhanyong.wana18423e2009-07-22 23:58:19 +000089 const MethodPtr method_ptr_;
kosak6b817802015-01-08 02:38:14 +000090 Class* const obj_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +000091
92 GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
zhanyong.wana18423e2009-07-22 23:58:19 +000093};
94
kosakb93d0f12014-01-13 22:28:01 +000095// 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.
99template<typename InputIterator, typename OutputIterator>
100inline 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
zhanyong.wana18423e2009-07-22 23:58:19 +0000109} // namespace internal
110
111// Various overloads for Invoke().
112
113// Creates an action that invokes 'function_impl' with the mock
114// function's arguments.
115template <typename FunctionImpl>
116PolymorphicAction<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.
124template <class Class, typename MethodPtr>
125PolymorphicAction<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.
135template <typename InnerAction>
136inline internal::WithArgsAction<InnerAction>
137WithoutArgs(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.
146template <int k, typename InnerAction>
147inline internal::WithArgsAction<InnerAction, k>
148WithArg(const InnerAction& action) {
149 return internal::WithArgsAction<InnerAction, k>(action);
150}
151
zhanyong.wan32de5f52009-12-23 00:13:23 +0000152// 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
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000158# pragma warning(push)
159# pragma warning(disable:4100)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000160#endif
161
zhanyong.wana18423e2009-07-22 23:58:19 +0000162// Action ReturnArg<k>() returns the k-th argument of the mock function.
163ACTION_TEMPLATE(ReturnArg,
164 HAS_1_TEMPLATE_PARAMS(int, k),
165 AND_0_VALUE_PARAMS()) {
kosakbd018832014-04-02 20:30:00 +0000166 return ::testing::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +0000167}
168
169// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
170// mock function to *pointer.
171ACTION_TEMPLATE(SaveArg,
172 HAS_1_TEMPLATE_PARAMS(int, k),
173 AND_1_VALUE_PARAMS(pointer)) {
kosakbd018832014-04-02 20:30:00 +0000174 *pointer = ::testing::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +0000175}
176
zhanyong.wan2321b2a2010-10-14 06:51:27 +0000177// Action SaveArgPointee<k>(pointer) saves the value pointed to
178// by the k-th (0-based) argument of the mock function to *pointer.
179ACTION_TEMPLATE(SaveArgPointee,
180 HAS_1_TEMPLATE_PARAMS(int, k),
181 AND_1_VALUE_PARAMS(pointer)) {
kosakbd018832014-04-02 20:30:00 +0000182 *pointer = *::testing::get<k>(args);
zhanyong.wan2321b2a2010-10-14 06:51:27 +0000183}
184
zhanyong.wana18423e2009-07-22 23:58:19 +0000185// Action SetArgReferee<k>(value) assigns 'value' to the variable
186// referenced by the k-th (0-based) argument of the mock function.
187ACTION_TEMPLATE(SetArgReferee,
188 HAS_1_TEMPLATE_PARAMS(int, k),
189 AND_1_VALUE_PARAMS(value)) {
kosakbd018832014-04-02 20:30:00 +0000190 typedef typename ::testing::tuple_element<k, args_type>::type argk_type;
zhanyong.wana18423e2009-07-22 23:58:19 +0000191 // 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.
zhanyong.wan02f71062010-05-10 17:14:29 +0000194 GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
zhanyong.wana18423e2009-07-22 23:58:19 +0000195 SetArgReferee_must_be_used_with_a_reference_argument);
kosakbd018832014-04-02 20:30:00 +0000196 ::testing::get<k>(args) = value;
zhanyong.wana18423e2009-07-22 23:58:19 +0000197}
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.
204ACTION_TEMPLATE(SetArrayArgument,
205 HAS_1_TEMPLATE_PARAMS(int, k),
206 AND_2_VALUE_PARAMS(first, last)) {
kosakb93d0f12014-01-13 22:28:01 +0000207 // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
zhanyong.wana18423e2009-07-22 23:58:19 +0000208#ifdef _MSC_VER
kosakbd018832014-04-02 20:30:00 +0000209 internal::CopyElements(first, last, ::testing::get<k>(args));
kosakb93d0f12014-01-13 22:28:01 +0000210#else
kosakbd018832014-04-02 20:30:00 +0000211 ::std::copy(first, last, ::testing::get<k>(args));
zhanyong.wana18423e2009-07-22 23:58:19 +0000212#endif
213}
214
215// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
216// function.
217ACTION_TEMPLATE(DeleteArg,
218 HAS_1_TEMPLATE_PARAMS(int, k),
219 AND_0_VALUE_PARAMS()) {
kosakbd018832014-04-02 20:30:00 +0000220 delete ::testing::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +0000221}
222
zhanyong.wane3bd0982010-07-03 00:16:42 +0000223// This action returns the value pointed to by 'pointer'.
224ACTION_P(ReturnPointee, pointer) { return *pointer; }
225
zhanyong.wana18423e2009-07-22 23:58:19 +0000226// 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
vladloseve2e8ba42010-05-13 18:16:03 +0000229
230// Suppresses the 'unreachable code' warning that VC generates in opt modes.
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000231# ifdef _MSC_VER
232# pragma warning(push) // Saves the current warning state.
233# pragma warning(disable:4702) // Temporarily disables warning 4702.
234# endif
zhanyong.wana18423e2009-07-22 23:58:19 +0000235ACTION_P(Throw, exception) { throw exception; }
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000236# ifdef _MSC_VER
237# pragma warning(pop) // Restores the warning state.
238# endif
vladloseve2e8ba42010-05-13 18:16:03 +0000239
zhanyong.wana18423e2009-07-22 23:58:19 +0000240#endif // GTEST_HAS_EXCEPTIONS
241
zhanyong.wan32de5f52009-12-23 00:13:23 +0000242#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000243# pragma warning(pop)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000244#endif
245
zhanyong.wana18423e2009-07-22 23:58:19 +0000246} // namespace testing
247
248#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_