blob: 6d686cd1e414a44dce4b9f014ba129c8904f73c3 [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
39#include <gmock/gmock-generated-actions.h>
40
41namespace testing {
42namespace internal {
43
44// Implements the Invoke(f) action. The template argument
45// FunctionImpl is the implementation type of f, which can be either a
46// function pointer or a functor. Invoke(f) can be used as an
47// Action<F> as long as f's type is compatible with F (i.e. f can be
48// assigned to a tr1::function<F>).
49template <typename FunctionImpl>
50class InvokeAction {
51 public:
52 // The c'tor makes a copy of function_impl (either a function
53 // pointer or a functor).
54 explicit InvokeAction(FunctionImpl function_impl)
55 : function_impl_(function_impl) {}
56
57 template <typename Result, typename ArgumentTuple>
58 Result Perform(const ArgumentTuple& args) {
59 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
60 }
zhanyong.wan32de5f52009-12-23 00:13:23 +000061
zhanyong.wana18423e2009-07-22 23:58:19 +000062 private:
63 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +000064
65 GTEST_DISALLOW_ASSIGN_(InvokeAction);
zhanyong.wana18423e2009-07-22 23:58:19 +000066};
67
68// Implements the Invoke(object_ptr, &Class::Method) action.
69template <class Class, typename MethodPtr>
70class InvokeMethodAction {
71 public:
72 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
73 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
74
75 template <typename Result, typename ArgumentTuple>
76 Result Perform(const ArgumentTuple& args) const {
77 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
78 obj_ptr_, method_ptr_, args);
79 }
zhanyong.wan32de5f52009-12-23 00:13:23 +000080
zhanyong.wana18423e2009-07-22 23:58:19 +000081 private:
82 Class* const obj_ptr_;
83 const MethodPtr method_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +000084
85 GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
zhanyong.wana18423e2009-07-22 23:58:19 +000086};
87
88} // namespace internal
89
90// Various overloads for Invoke().
91
92// Creates an action that invokes 'function_impl' with the mock
93// function's arguments.
94template <typename FunctionImpl>
95PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
96 FunctionImpl function_impl) {
97 return MakePolymorphicAction(
98 internal::InvokeAction<FunctionImpl>(function_impl));
99}
100
101// Creates an action that invokes the given method on the given object
102// with the mock function's arguments.
103template <class Class, typename MethodPtr>
104PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
105 Class* obj_ptr, MethodPtr method_ptr) {
106 return MakePolymorphicAction(
107 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
108}
109
110// WithoutArgs(inner_action) can be used in a mock function with a
111// non-empty argument list to perform inner_action, which takes no
112// argument. In other words, it adapts an action accepting no
113// argument to one that accepts (and ignores) arguments.
114template <typename InnerAction>
115inline internal::WithArgsAction<InnerAction>
116WithoutArgs(const InnerAction& action) {
117 return internal::WithArgsAction<InnerAction>(action);
118}
119
120// WithArg<k>(an_action) creates an action that passes the k-th
121// (0-based) argument of the mock function to an_action and performs
122// it. It adapts an action accepting one argument to one that accepts
123// multiple arguments. For convenience, we also provide
124// WithArgs<k>(an_action) (defined below) as a synonym.
125template <int k, typename InnerAction>
126inline internal::WithArgsAction<InnerAction, k>
127WithArg(const InnerAction& action) {
128 return internal::WithArgsAction<InnerAction, k>(action);
129}
130
zhanyong.wan32de5f52009-12-23 00:13:23 +0000131// The ACTION*() macros trigger warning C4100 (unreferenced formal
132// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
133// the macro definition, as the warnings are generated when the macro
134// is expanded and macro expansion cannot contain #pragma. Therefore
135// we suppress them here.
136#ifdef _MSC_VER
137#pragma warning(push)
138#pragma warning(disable:4100)
139#endif
140
zhanyong.wana18423e2009-07-22 23:58:19 +0000141// Action ReturnArg<k>() returns the k-th argument of the mock function.
142ACTION_TEMPLATE(ReturnArg,
143 HAS_1_TEMPLATE_PARAMS(int, k),
144 AND_0_VALUE_PARAMS()) {
145 return std::tr1::get<k>(args);
146}
147
148// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
149// mock function to *pointer.
150ACTION_TEMPLATE(SaveArg,
151 HAS_1_TEMPLATE_PARAMS(int, k),
152 AND_1_VALUE_PARAMS(pointer)) {
153 *pointer = ::std::tr1::get<k>(args);
154}
155
156// Action SetArgReferee<k>(value) assigns 'value' to the variable
157// referenced by the k-th (0-based) argument of the mock function.
158ACTION_TEMPLATE(SetArgReferee,
159 HAS_1_TEMPLATE_PARAMS(int, k),
160 AND_1_VALUE_PARAMS(value)) {
161 typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type;
162 // Ensures that argument #k is a reference. If you get a compiler
163 // error on the next line, you are using SetArgReferee<k>(value) in
164 // a mock function whose k-th (0-based) argument is not a reference.
zhanyong.wan02f71062010-05-10 17:14:29 +0000165 GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
zhanyong.wana18423e2009-07-22 23:58:19 +0000166 SetArgReferee_must_be_used_with_a_reference_argument);
167 ::std::tr1::get<k>(args) = value;
168}
169
170// Action SetArrayArgument<k>(first, last) copies the elements in
171// source range [first, last) to the array pointed to by the k-th
172// (0-based) argument, which can be either a pointer or an
173// iterator. The action does not take ownership of the elements in the
174// source range.
175ACTION_TEMPLATE(SetArrayArgument,
176 HAS_1_TEMPLATE_PARAMS(int, k),
177 AND_2_VALUE_PARAMS(first, last)) {
178 // Microsoft compiler deprecates ::std::copy, so we want to suppress warning
179 // 4996 (Function call with parameters that may be unsafe) there.
180#ifdef _MSC_VER
181#pragma warning(push) // Saves the current warning state.
182#pragma warning(disable:4996) // Temporarily disables warning 4996.
183#endif
184 ::std::copy(first, last, ::std::tr1::get<k>(args));
185#ifdef _MSC_VER
186#pragma warning(pop) // Restores the warning state.
187#endif
188}
189
190// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
191// function.
192ACTION_TEMPLATE(DeleteArg,
193 HAS_1_TEMPLATE_PARAMS(int, k),
194 AND_0_VALUE_PARAMS()) {
195 delete ::std::tr1::get<k>(args);
196}
197
198// Action Throw(exception) can be used in a mock function of any type
199// to throw the given exception. Any copyable value can be thrown.
200#if GTEST_HAS_EXCEPTIONS
vladloseve2e8ba42010-05-13 18:16:03 +0000201
202// Suppresses the 'unreachable code' warning that VC generates in opt modes.
203#ifdef _MSC_VER
204#pragma warning(push) // Saves the current warning state.
205#pragma warning(disable:4702) // Temporarily disables warning 4702.
206#endif
zhanyong.wana18423e2009-07-22 23:58:19 +0000207ACTION_P(Throw, exception) { throw exception; }
vladloseve2e8ba42010-05-13 18:16:03 +0000208#ifdef _MSC_VER
209#pragma warning(pop) // Restores the warning state.
210#endif
211
zhanyong.wana18423e2009-07-22 23:58:19 +0000212#endif // GTEST_HAS_EXCEPTIONS
213
zhanyong.wan32de5f52009-12-23 00:13:23 +0000214#ifdef _MSC_VER
215#pragma warning(pop)
216#endif
217
zhanyong.wana18423e2009-07-22 23:58:19 +0000218} // namespace testing
219
220#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_