blob: c754440eedbb92ab7995a574466b851bcc2a8a41 [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
36#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
37#define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
38
zhanyong.wan2321b2a2010-10-14 06:51:27 +000039#include <algorithm>
40
zhanyong.wan53e08c42010-09-14 05:38:21 +000041#include "gmock/gmock-generated-actions.h"
zhanyong.wana18423e2009-07-22 23:58:19 +000042
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>
52class 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) {
61 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
62 }
zhanyong.wan32de5f52009-12-23 00:13:23 +000063
zhanyong.wana18423e2009-07-22 23:58:19 +000064 private:
65 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +000066
67 GTEST_DISALLOW_ASSIGN_(InvokeAction);
zhanyong.wana18423e2009-07-22 23:58:19 +000068};
69
70// Implements the Invoke(object_ptr, &Class::Method) action.
71template <class Class, typename MethodPtr>
72class InvokeMethodAction {
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 {
79 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
80 obj_ptr_, method_ptr_, args);
81 }
zhanyong.wan32de5f52009-12-23 00:13:23 +000082
zhanyong.wana18423e2009-07-22 23:58:19 +000083 private:
84 Class* const obj_ptr_;
85 const MethodPtr method_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +000086
87 GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
zhanyong.wana18423e2009-07-22 23:58:19 +000088};
89
kosakb93d0f12014-01-13 22:28:01 +000090// An internal replacement for std::copy which mimics its behavior. This is
91// necessary because Visual Studio deprecates ::std::copy, issuing warning 4996.
92// However Visual Studio 2010 and later do not honor #pragmas which disable that
93// warning.
94template<typename InputIterator, typename OutputIterator>
95inline OutputIterator CopyElements(InputIterator first,
96 InputIterator last,
97 OutputIterator output) {
98 for (; first != last; ++first, ++output) {
99 *output = *first;
100 }
101 return output;
102}
103
zhanyong.wana18423e2009-07-22 23:58:19 +0000104} // namespace internal
105
106// Various overloads for Invoke().
107
108// Creates an action that invokes 'function_impl' with the mock
109// function's arguments.
110template <typename FunctionImpl>
111PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
112 FunctionImpl function_impl) {
113 return MakePolymorphicAction(
114 internal::InvokeAction<FunctionImpl>(function_impl));
115}
116
117// Creates an action that invokes the given method on the given object
118// with the mock function's arguments.
119template <class Class, typename MethodPtr>
120PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
121 Class* obj_ptr, MethodPtr method_ptr) {
122 return MakePolymorphicAction(
123 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
124}
125
126// WithoutArgs(inner_action) can be used in a mock function with a
127// non-empty argument list to perform inner_action, which takes no
128// argument. In other words, it adapts an action accepting no
129// argument to one that accepts (and ignores) arguments.
130template <typename InnerAction>
131inline internal::WithArgsAction<InnerAction>
132WithoutArgs(const InnerAction& action) {
133 return internal::WithArgsAction<InnerAction>(action);
134}
135
136// WithArg<k>(an_action) creates an action that passes the k-th
137// (0-based) argument of the mock function to an_action and performs
138// it. It adapts an action accepting one argument to one that accepts
139// multiple arguments. For convenience, we also provide
140// WithArgs<k>(an_action) (defined below) as a synonym.
141template <int k, typename InnerAction>
142inline internal::WithArgsAction<InnerAction, k>
143WithArg(const InnerAction& action) {
144 return internal::WithArgsAction<InnerAction, k>(action);
145}
146
zhanyong.wan32de5f52009-12-23 00:13:23 +0000147// The ACTION*() macros trigger warning C4100 (unreferenced formal
148// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
149// the macro definition, as the warnings are generated when the macro
150// is expanded and macro expansion cannot contain #pragma. Therefore
151// we suppress them here.
152#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000153# pragma warning(push)
154# pragma warning(disable:4100)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000155#endif
156
zhanyong.wana18423e2009-07-22 23:58:19 +0000157// Action ReturnArg<k>() returns the k-th argument of the mock function.
158ACTION_TEMPLATE(ReturnArg,
159 HAS_1_TEMPLATE_PARAMS(int, k),
160 AND_0_VALUE_PARAMS()) {
kosakbd018832014-04-02 20:30:00 +0000161 return ::testing::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +0000162}
163
164// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
165// mock function to *pointer.
166ACTION_TEMPLATE(SaveArg,
167 HAS_1_TEMPLATE_PARAMS(int, k),
168 AND_1_VALUE_PARAMS(pointer)) {
kosakbd018832014-04-02 20:30:00 +0000169 *pointer = ::testing::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +0000170}
171
zhanyong.wan2321b2a2010-10-14 06:51:27 +0000172// Action SaveArgPointee<k>(pointer) saves the value pointed to
173// by the k-th (0-based) argument of the mock function to *pointer.
174ACTION_TEMPLATE(SaveArgPointee,
175 HAS_1_TEMPLATE_PARAMS(int, k),
176 AND_1_VALUE_PARAMS(pointer)) {
kosakbd018832014-04-02 20:30:00 +0000177 *pointer = *::testing::get<k>(args);
zhanyong.wan2321b2a2010-10-14 06:51:27 +0000178}
179
zhanyong.wana18423e2009-07-22 23:58:19 +0000180// Action SetArgReferee<k>(value) assigns 'value' to the variable
181// referenced by the k-th (0-based) argument of the mock function.
182ACTION_TEMPLATE(SetArgReferee,
183 HAS_1_TEMPLATE_PARAMS(int, k),
184 AND_1_VALUE_PARAMS(value)) {
kosakbd018832014-04-02 20:30:00 +0000185 typedef typename ::testing::tuple_element<k, args_type>::type argk_type;
zhanyong.wana18423e2009-07-22 23:58:19 +0000186 // Ensures that argument #k is a reference. If you get a compiler
187 // error on the next line, you are using SetArgReferee<k>(value) in
188 // a mock function whose k-th (0-based) argument is not a reference.
zhanyong.wan02f71062010-05-10 17:14:29 +0000189 GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
zhanyong.wana18423e2009-07-22 23:58:19 +0000190 SetArgReferee_must_be_used_with_a_reference_argument);
kosakbd018832014-04-02 20:30:00 +0000191 ::testing::get<k>(args) = value;
zhanyong.wana18423e2009-07-22 23:58:19 +0000192}
193
194// Action SetArrayArgument<k>(first, last) copies the elements in
195// source range [first, last) to the array pointed to by the k-th
196// (0-based) argument, which can be either a pointer or an
197// iterator. The action does not take ownership of the elements in the
198// source range.
199ACTION_TEMPLATE(SetArrayArgument,
200 HAS_1_TEMPLATE_PARAMS(int, k),
201 AND_2_VALUE_PARAMS(first, last)) {
kosakb93d0f12014-01-13 22:28:01 +0000202 // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
zhanyong.wana18423e2009-07-22 23:58:19 +0000203#ifdef _MSC_VER
kosakbd018832014-04-02 20:30:00 +0000204 internal::CopyElements(first, last, ::testing::get<k>(args));
kosakb93d0f12014-01-13 22:28:01 +0000205#else
kosakbd018832014-04-02 20:30:00 +0000206 ::std::copy(first, last, ::testing::get<k>(args));
zhanyong.wana18423e2009-07-22 23:58:19 +0000207#endif
208}
209
210// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
211// function.
212ACTION_TEMPLATE(DeleteArg,
213 HAS_1_TEMPLATE_PARAMS(int, k),
214 AND_0_VALUE_PARAMS()) {
kosakbd018832014-04-02 20:30:00 +0000215 delete ::testing::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +0000216}
217
zhanyong.wane3bd0982010-07-03 00:16:42 +0000218// This action returns the value pointed to by 'pointer'.
219ACTION_P(ReturnPointee, pointer) { return *pointer; }
220
zhanyong.wana18423e2009-07-22 23:58:19 +0000221// Action Throw(exception) can be used in a mock function of any type
222// to throw the given exception. Any copyable value can be thrown.
223#if GTEST_HAS_EXCEPTIONS
vladloseve2e8ba42010-05-13 18:16:03 +0000224
225// Suppresses the 'unreachable code' warning that VC generates in opt modes.
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000226# ifdef _MSC_VER
227# pragma warning(push) // Saves the current warning state.
228# pragma warning(disable:4702) // Temporarily disables warning 4702.
229# endif
zhanyong.wana18423e2009-07-22 23:58:19 +0000230ACTION_P(Throw, exception) { throw exception; }
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000231# ifdef _MSC_VER
232# pragma warning(pop) // Restores the warning state.
233# endif
vladloseve2e8ba42010-05-13 18:16:03 +0000234
zhanyong.wana18423e2009-07-22 23:58:19 +0000235#endif // GTEST_HAS_EXCEPTIONS
236
zhanyong.wan32de5f52009-12-23 00:13:23 +0000237#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000238# pragma warning(pop)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000239#endif
240
zhanyong.wana18423e2009-07-22 23:58:19 +0000241} // namespace testing
242
243#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_