blob: c6fa6bbd70b4adf05d25157ffd750700b7b73429 [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 }
61 private:
62 FunctionImpl function_impl_;
63};
64
65// Implements the Invoke(object_ptr, &Class::Method) action.
66template <class Class, typename MethodPtr>
67class InvokeMethodAction {
68 public:
69 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
70 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
71
72 template <typename Result, typename ArgumentTuple>
73 Result Perform(const ArgumentTuple& args) const {
74 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
75 obj_ptr_, method_ptr_, args);
76 }
77 private:
78 Class* const obj_ptr_;
79 const MethodPtr method_ptr_;
80};
81
82} // namespace internal
83
84// Various overloads for Invoke().
85
86// Creates an action that invokes 'function_impl' with the mock
87// function's arguments.
88template <typename FunctionImpl>
89PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
90 FunctionImpl function_impl) {
91 return MakePolymorphicAction(
92 internal::InvokeAction<FunctionImpl>(function_impl));
93}
94
95// Creates an action that invokes the given method on the given object
96// with the mock function's arguments.
97template <class Class, typename MethodPtr>
98PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
99 Class* obj_ptr, MethodPtr method_ptr) {
100 return MakePolymorphicAction(
101 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
102}
103
104// WithoutArgs(inner_action) can be used in a mock function with a
105// non-empty argument list to perform inner_action, which takes no
106// argument. In other words, it adapts an action accepting no
107// argument to one that accepts (and ignores) arguments.
108template <typename InnerAction>
109inline internal::WithArgsAction<InnerAction>
110WithoutArgs(const InnerAction& action) {
111 return internal::WithArgsAction<InnerAction>(action);
112}
113
114// WithArg<k>(an_action) creates an action that passes the k-th
115// (0-based) argument of the mock function to an_action and performs
116// it. It adapts an action accepting one argument to one that accepts
117// multiple arguments. For convenience, we also provide
118// WithArgs<k>(an_action) (defined below) as a synonym.
119template <int k, typename InnerAction>
120inline internal::WithArgsAction<InnerAction, k>
121WithArg(const InnerAction& action) {
122 return internal::WithArgsAction<InnerAction, k>(action);
123}
124
125// Action ReturnArg<k>() returns the k-th argument of the mock function.
126ACTION_TEMPLATE(ReturnArg,
127 HAS_1_TEMPLATE_PARAMS(int, k),
128 AND_0_VALUE_PARAMS()) {
129 return std::tr1::get<k>(args);
130}
131
132// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
133// mock function to *pointer.
134ACTION_TEMPLATE(SaveArg,
135 HAS_1_TEMPLATE_PARAMS(int, k),
136 AND_1_VALUE_PARAMS(pointer)) {
137 *pointer = ::std::tr1::get<k>(args);
138}
139
140// Action SetArgReferee<k>(value) assigns 'value' to the variable
141// referenced by the k-th (0-based) argument of the mock function.
142ACTION_TEMPLATE(SetArgReferee,
143 HAS_1_TEMPLATE_PARAMS(int, k),
144 AND_1_VALUE_PARAMS(value)) {
145 typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type;
146 // Ensures that argument #k is a reference. If you get a compiler
147 // error on the next line, you are using SetArgReferee<k>(value) in
148 // a mock function whose k-th (0-based) argument is not a reference.
149 GMOCK_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
150 SetArgReferee_must_be_used_with_a_reference_argument);
151 ::std::tr1::get<k>(args) = value;
152}
153
154// Action SetArrayArgument<k>(first, last) copies the elements in
155// source range [first, last) to the array pointed to by the k-th
156// (0-based) argument, which can be either a pointer or an
157// iterator. The action does not take ownership of the elements in the
158// source range.
159ACTION_TEMPLATE(SetArrayArgument,
160 HAS_1_TEMPLATE_PARAMS(int, k),
161 AND_2_VALUE_PARAMS(first, last)) {
162 // Microsoft compiler deprecates ::std::copy, so we want to suppress warning
163 // 4996 (Function call with parameters that may be unsafe) there.
164#ifdef _MSC_VER
165#pragma warning(push) // Saves the current warning state.
166#pragma warning(disable:4996) // Temporarily disables warning 4996.
167#endif
168 ::std::copy(first, last, ::std::tr1::get<k>(args));
169#ifdef _MSC_VER
170#pragma warning(pop) // Restores the warning state.
171#endif
172}
173
174// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
175// function.
176ACTION_TEMPLATE(DeleteArg,
177 HAS_1_TEMPLATE_PARAMS(int, k),
178 AND_0_VALUE_PARAMS()) {
179 delete ::std::tr1::get<k>(args);
180}
181
182// Action Throw(exception) can be used in a mock function of any type
183// to throw the given exception. Any copyable value can be thrown.
184#if GTEST_HAS_EXCEPTIONS
185ACTION_P(Throw, exception) { throw exception; }
186#endif // GTEST_HAS_EXCEPTIONS
187
188} // namespace testing
189
190#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_