blob: 90fd2ea69d273b0d26c09e135328757fa33ba155 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +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 commonly used actions.
35
36#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
37#define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
38
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000039#ifndef _WIN32_WCE
zhanyong.wan658ac0b2011-02-24 07:29:13 +000040# include <errno.h>
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000041#endif
42
jgm79a367e2012-04-10 16:02:11 +000043#include <algorithm>
44#include <string>
45
zhanyong.wan53e08c42010-09-14 05:38:21 +000046#include "gmock/internal/gmock-internal-utils.h"
47#include "gmock/internal/gmock-port.h"
shiqiane35fdd92008-12-10 05:08:54 +000048
Gennadiy Civilaf463c42018-03-13 11:13:37 -040049#if GTEST_LANG_CXX11 // Defined by gtest-port.h via gmock-port.h.
50#include <functional>
kosakd478a1f2015-02-14 02:45:40 +000051#include <type_traits>
Gennadiy Civilaf463c42018-03-13 11:13:37 -040052#endif // GTEST_LANG_CXX11
kosakd478a1f2015-02-14 02:45:40 +000053
shiqiane35fdd92008-12-10 05:08:54 +000054namespace testing {
55
56// To implement an action Foo, define:
57// 1. a class FooAction that implements the ActionInterface interface, and
58// 2. a factory function that creates an Action object from a
59// const FooAction*.
60//
61// The two-level delegation design follows that of Matcher, providing
62// consistency for extension developers. It also eases ownership
63// management as Action objects can now be copied like plain values.
64
65namespace internal {
66
shiqiane35fdd92008-12-10 05:08:54 +000067template <typename F1, typename F2>
68class ActionAdaptor;
69
kosakd478a1f2015-02-14 02:45:40 +000070// BuiltInDefaultValueGetter<T, true>::Get() returns a
71// default-constructed T value. BuiltInDefaultValueGetter<T,
72// false>::Get() crashes with an error.
73//
74// This primary template is used when kDefaultConstructible is true.
75template <typename T, bool kDefaultConstructible>
76struct BuiltInDefaultValueGetter {
77 static T Get() { return T(); }
78};
shiqiane35fdd92008-12-10 05:08:54 +000079template <typename T>
kosakd478a1f2015-02-14 02:45:40 +000080struct BuiltInDefaultValueGetter<T, false> {
shiqiane35fdd92008-12-10 05:08:54 +000081 static T Get() {
82 Assert(false, __FILE__, __LINE__,
83 "Default action undefined for the function return type.");
84 return internal::Invalid<T>();
85 // The above statement will never be reached, but is required in
86 // order for this function to compile.
87 }
88};
89
kosakd478a1f2015-02-14 02:45:40 +000090// BuiltInDefaultValue<T>::Get() returns the "built-in" default value
91// for type T, which is NULL when T is a raw pointer type, 0 when T is
92// a numeric type, false when T is bool, or "" when T is string or
93// std::string. In addition, in C++11 and above, it turns a
94// default-constructed T value if T is default constructible. For any
95// other type T, the built-in default T value is undefined, and the
96// function will abort the process.
97template <typename T>
98class BuiltInDefaultValue {
99 public:
Gennadiy Civilaf463c42018-03-13 11:13:37 -0400100#if GTEST_LANG_CXX11
kosakd478a1f2015-02-14 02:45:40 +0000101 // This function returns true iff type T has a built-in default value.
102 static bool Exists() {
103 return ::std::is_default_constructible<T>::value;
104 }
105
106 static T Get() {
107 return BuiltInDefaultValueGetter<
108 T, ::std::is_default_constructible<T>::value>::Get();
109 }
110
Gennadiy Civilaf463c42018-03-13 11:13:37 -0400111#else // GTEST_LANG_CXX11
kosakd478a1f2015-02-14 02:45:40 +0000112 // This function returns true iff type T has a built-in default value.
113 static bool Exists() {
114 return false;
115 }
116
117 static T Get() {
118 return BuiltInDefaultValueGetter<T, false>::Get();
119 }
120
Gennadiy Civilaf463c42018-03-13 11:13:37 -0400121#endif // GTEST_LANG_CXX11
kosakd478a1f2015-02-14 02:45:40 +0000122};
123
shiqiane35fdd92008-12-10 05:08:54 +0000124// This partial specialization says that we use the same built-in
125// default value for T and const T.
126template <typename T>
127class BuiltInDefaultValue<const T> {
128 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000129 static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
shiqiane35fdd92008-12-10 05:08:54 +0000130 static T Get() { return BuiltInDefaultValue<T>::Get(); }
131};
132
133// This partial specialization defines the default values for pointer
134// types.
135template <typename T>
136class BuiltInDefaultValue<T*> {
137 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000138 static bool Exists() { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000139 static T* Get() { return NULL; }
140};
141
142// The following specializations define the default values for
143// specific types we care about.
zhanyong.wane0d051e2009-02-19 00:33:37 +0000144#define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
shiqiane35fdd92008-12-10 05:08:54 +0000145 template <> \
146 class BuiltInDefaultValue<type> { \
147 public: \
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000148 static bool Exists() { return true; } \
shiqiane35fdd92008-12-10 05:08:54 +0000149 static type Get() { return value; } \
150 }
151
zhanyong.wane0d051e2009-02-19 00:33:37 +0000152GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, ); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000153#if GTEST_HAS_GLOBAL_STRING
zhanyong.wane0d051e2009-02-19 00:33:37 +0000154GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::string, "");
shiqiane35fdd92008-12-10 05:08:54 +0000155#endif // GTEST_HAS_GLOBAL_STRING
zhanyong.wane0d051e2009-02-19 00:33:37 +0000156GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, "");
zhanyong.wane0d051e2009-02-19 00:33:37 +0000157GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false);
158GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0');
159GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0');
160GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0');
shiqiane35fdd92008-12-10 05:08:54 +0000161
shiqiane35fdd92008-12-10 05:08:54 +0000162// There's no need for a default action for signed wchar_t, as that
163// type is the same as wchar_t for gcc, and invalid for MSVC.
164//
165// There's also no need for a default action for unsigned wchar_t, as
166// that type is the same as unsigned int for gcc, and invalid for
167// MSVC.
zhanyong.wan95b12332009-09-25 18:55:50 +0000168#if GMOCK_WCHAR_T_IS_NATIVE_
zhanyong.wane0d051e2009-02-19 00:33:37 +0000169GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000170#endif
171
zhanyong.wane0d051e2009-02-19 00:33:37 +0000172GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U); // NOLINT
173GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0); // NOLINT
174GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U);
175GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0);
176GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL); // NOLINT
177GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L); // NOLINT
178GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(UInt64, 0);
179GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(Int64, 0);
180GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0);
181GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0);
shiqiane35fdd92008-12-10 05:08:54 +0000182
zhanyong.wane0d051e2009-02-19 00:33:37 +0000183#undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
shiqiane35fdd92008-12-10 05:08:54 +0000184
185} // namespace internal
186
187// When an unexpected function call is encountered, Google Mock will
188// let it return a default value if the user has specified one for its
189// return type, or if the return type has a built-in default value;
190// otherwise Google Mock won't know what value to return and will have
191// to abort the process.
192//
193// The DefaultValue<T> class allows a user to specify the
194// default value for a type T that is both copyable and publicly
195// destructible (i.e. anything that can be used as a function return
196// type). The usage is:
197//
198// // Sets the default value for type T to be foo.
199// DefaultValue<T>::Set(foo);
200template <typename T>
201class DefaultValue {
202 public:
203 // Sets the default value for type T; requires T to be
204 // copy-constructable and have a public destructor.
205 static void Set(T x) {
kosakb5c81092014-01-29 06:41:44 +0000206 delete producer_;
207 producer_ = new FixedValueProducer(x);
208 }
209
210 // Provides a factory function to be called to generate the default value.
211 // This method can be used even if T is only move-constructible, but it is not
212 // limited to that case.
213 typedef T (*FactoryFunction)();
214 static void SetFactory(FactoryFunction factory) {
215 delete producer_;
216 producer_ = new FactoryValueProducer(factory);
shiqiane35fdd92008-12-10 05:08:54 +0000217 }
218
219 // Unsets the default value for type T.
220 static void Clear() {
kosakb5c81092014-01-29 06:41:44 +0000221 delete producer_;
222 producer_ = NULL;
shiqiane35fdd92008-12-10 05:08:54 +0000223 }
224
225 // Returns true iff the user has set the default value for type T.
kosakb5c81092014-01-29 06:41:44 +0000226 static bool IsSet() { return producer_ != NULL; }
shiqiane35fdd92008-12-10 05:08:54 +0000227
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000228 // Returns true if T has a default return value set by the user or there
229 // exists a built-in default value.
230 static bool Exists() {
231 return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
232 }
233
shiqiane35fdd92008-12-10 05:08:54 +0000234 // Returns the default value for type T if the user has set one;
kosakb5c81092014-01-29 06:41:44 +0000235 // otherwise returns the built-in default value. Requires that Exists()
236 // is true, which ensures that the return value is well-defined.
shiqiane35fdd92008-12-10 05:08:54 +0000237 static T Get() {
kosakb5c81092014-01-29 06:41:44 +0000238 return producer_ == NULL ?
239 internal::BuiltInDefaultValue<T>::Get() : producer_->Produce();
shiqiane35fdd92008-12-10 05:08:54 +0000240 }
jgm79a367e2012-04-10 16:02:11 +0000241
shiqiane35fdd92008-12-10 05:08:54 +0000242 private:
kosakb5c81092014-01-29 06:41:44 +0000243 class ValueProducer {
244 public:
245 virtual ~ValueProducer() {}
246 virtual T Produce() = 0;
247 };
248
249 class FixedValueProducer : public ValueProducer {
250 public:
251 explicit FixedValueProducer(T value) : value_(value) {}
252 virtual T Produce() { return value_; }
253
254 private:
255 const T value_;
256 GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer);
257 };
258
259 class FactoryValueProducer : public ValueProducer {
260 public:
261 explicit FactoryValueProducer(FactoryFunction factory)
262 : factory_(factory) {}
263 virtual T Produce() { return factory_(); }
264
265 private:
266 const FactoryFunction factory_;
267 GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer);
268 };
269
270 static ValueProducer* producer_;
shiqiane35fdd92008-12-10 05:08:54 +0000271};
272
273// This partial specialization allows a user to set default values for
274// reference types.
275template <typename T>
276class DefaultValue<T&> {
277 public:
278 // Sets the default value for type T&.
279 static void Set(T& x) { // NOLINT
280 address_ = &x;
281 }
282
283 // Unsets the default value for type T&.
284 static void Clear() {
285 address_ = NULL;
286 }
287
288 // Returns true iff the user has set the default value for type T&.
289 static bool IsSet() { return address_ != NULL; }
290
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000291 // Returns true if T has a default return value set by the user or there
292 // exists a built-in default value.
293 static bool Exists() {
294 return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
295 }
296
shiqiane35fdd92008-12-10 05:08:54 +0000297 // Returns the default value for type T& if the user has set one;
298 // otherwise returns the built-in default value if there is one;
299 // otherwise aborts the process.
300 static T& Get() {
301 return address_ == NULL ?
302 internal::BuiltInDefaultValue<T&>::Get() : *address_;
303 }
jgm79a367e2012-04-10 16:02:11 +0000304
shiqiane35fdd92008-12-10 05:08:54 +0000305 private:
306 static T* address_;
307};
308
309// This specialization allows DefaultValue<void>::Get() to
310// compile.
311template <>
312class DefaultValue<void> {
313 public:
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000314 static bool Exists() { return true; }
shiqiane35fdd92008-12-10 05:08:54 +0000315 static void Get() {}
316};
317
318// Points to the user-set default value for type T.
319template <typename T>
kosakb5c81092014-01-29 06:41:44 +0000320typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = NULL;
shiqiane35fdd92008-12-10 05:08:54 +0000321
322// Points to the user-set default value for type T&.
323template <typename T>
324T* DefaultValue<T&>::address_ = NULL;
325
326// Implement this interface to define an action for function type F.
327template <typename F>
328class ActionInterface {
329 public:
330 typedef typename internal::Function<F>::Result Result;
331 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
332
zhanyong.waned6c9272011-02-23 19:39:27 +0000333 ActionInterface() {}
shiqiane35fdd92008-12-10 05:08:54 +0000334 virtual ~ActionInterface() {}
335
336 // Performs the action. This method is not const, as in general an
337 // action can have side effects and be stateful. For example, a
338 // get-the-next-element-from-the-collection action will need to
339 // remember the current element.
340 virtual Result Perform(const ArgumentTuple& args) = 0;
341
shiqiane35fdd92008-12-10 05:08:54 +0000342 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000343 GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface);
shiqiane35fdd92008-12-10 05:08:54 +0000344};
345
346// An Action<F> is a copyable and IMMUTABLE (except by assignment)
347// object that represents an action to be taken when a mock function
348// of type F is called. The implementation of Action<T> is just a
349// linked_ptr to const ActionInterface<T>, so copying is fairly cheap.
350// Don't inherit from Action!
351//
352// You can view an object implementing ActionInterface<F> as a
353// concrete action (including its current state), and an Action<F>
354// object as a handle to it.
355template <typename F>
356class Action {
357 public:
358 typedef typename internal::Function<F>::Result Result;
359 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
360
361 // Constructs a null Action. Needed for storing Action objects in
362 // STL containers.
363 Action() : impl_(NULL) {}
364
zhanyong.waned6c9272011-02-23 19:39:27 +0000365 // Constructs an Action from its implementation. A NULL impl is
366 // used to represent the "do-default" action.
shiqiane35fdd92008-12-10 05:08:54 +0000367 explicit Action(ActionInterface<F>* impl) : impl_(impl) {}
368
369 // Copy constructor.
370 Action(const Action& action) : impl_(action.impl_) {}
371
372 // This constructor allows us to turn an Action<Func> object into an
373 // Action<F>, as long as F's arguments can be implicitly converted
vladloseva070cbd2009-11-18 00:09:28 +0000374 // to Func's and Func's return type can be implicitly converted to
shiqiane35fdd92008-12-10 05:08:54 +0000375 // F's.
376 template <typename Func>
377 explicit Action(const Action<Func>& action);
378
379 // Returns true iff this is the DoDefault() action.
zhanyong.waned6c9272011-02-23 19:39:27 +0000380 bool IsDoDefault() const { return impl_.get() == NULL; }
shiqiane35fdd92008-12-10 05:08:54 +0000381
382 // Performs the action. Note that this method is const even though
383 // the corresponding method in ActionInterface is not. The reason
384 // is that a const Action<F> means that it cannot be re-bound to
385 // another concrete action, not that the concrete action it binds to
386 // cannot change state. (Think of the difference between a const
387 // pointer and a pointer to const.)
388 Result Perform(const ArgumentTuple& args) const {
zhanyong.waned6c9272011-02-23 19:39:27 +0000389 internal::Assert(
390 !IsDoDefault(), __FILE__, __LINE__,
391 "You are using DoDefault() inside a composite action like "
392 "DoAll() or WithArgs(). This is not supported for technical "
393 "reasons. Please instead spell out the default action, or "
394 "assign the default action to an Action variable and use "
395 "the variable in various places.");
shiqiane35fdd92008-12-10 05:08:54 +0000396 return impl_->Perform(args);
397 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000398
shiqiane35fdd92008-12-10 05:08:54 +0000399 private:
400 template <typename F1, typename F2>
401 friend class internal::ActionAdaptor;
402
403 internal::linked_ptr<ActionInterface<F> > impl_;
404};
405
406// The PolymorphicAction class template makes it easy to implement a
407// polymorphic action (i.e. an action that can be used in mock
408// functions of than one type, e.g. Return()).
409//
410// To define a polymorphic action, a user first provides a COPYABLE
411// implementation class that has a Perform() method template:
412//
413// class FooAction {
414// public:
415// template <typename Result, typename ArgumentTuple>
416// Result Perform(const ArgumentTuple& args) const {
417// // Processes the arguments and returns a result, using
418// // tr1::get<N>(args) to get the N-th (0-based) argument in the tuple.
419// }
420// ...
421// };
422//
423// Then the user creates the polymorphic action using
424// MakePolymorphicAction(object) where object has type FooAction. See
425// the definition of Return(void) and SetArgumentPointee<N>(value) for
426// complete examples.
427template <typename Impl>
428class PolymorphicAction {
429 public:
430 explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
431
432 template <typename F>
433 operator Action<F>() const {
434 return Action<F>(new MonomorphicImpl<F>(impl_));
435 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000436
shiqiane35fdd92008-12-10 05:08:54 +0000437 private:
438 template <typename F>
439 class MonomorphicImpl : public ActionInterface<F> {
440 public:
441 typedef typename internal::Function<F>::Result Result;
442 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
443
444 explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
445
446 virtual Result Perform(const ArgumentTuple& args) {
447 return impl_.template Perform<Result>(args);
448 }
449
450 private:
451 Impl impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000452
453 GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000454 };
455
456 Impl impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000457
458 GTEST_DISALLOW_ASSIGN_(PolymorphicAction);
shiqiane35fdd92008-12-10 05:08:54 +0000459};
460
461// Creates an Action from its implementation and returns it. The
462// created Action object owns the implementation.
463template <typename F>
464Action<F> MakeAction(ActionInterface<F>* impl) {
465 return Action<F>(impl);
466}
467
468// Creates a polymorphic action from its implementation. This is
469// easier to use than the PolymorphicAction<Impl> constructor as it
470// doesn't require you to explicitly write the template argument, e.g.
471//
472// MakePolymorphicAction(foo);
473// vs
474// PolymorphicAction<TypeOfFoo>(foo);
475template <typename Impl>
476inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) {
477 return PolymorphicAction<Impl>(impl);
478}
479
480namespace internal {
481
482// Allows an Action<F2> object to pose as an Action<F1>, as long as F2
483// and F1 are compatible.
484template <typename F1, typename F2>
485class ActionAdaptor : public ActionInterface<F1> {
486 public:
487 typedef typename internal::Function<F1>::Result Result;
488 typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple;
489
490 explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {}
491
492 virtual Result Perform(const ArgumentTuple& args) {
493 return impl_->Perform(args);
494 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000495
shiqiane35fdd92008-12-10 05:08:54 +0000496 private:
497 const internal::linked_ptr<ActionInterface<F2> > impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000498
499 GTEST_DISALLOW_ASSIGN_(ActionAdaptor);
shiqiane35fdd92008-12-10 05:08:54 +0000500};
501
kosak3d1c78b2014-11-17 00:56:52 +0000502// Helper struct to specialize ReturnAction to execute a move instead of a copy
503// on return. Useful for move-only types, but could be used on any type.
504template <typename T>
505struct ByMoveWrapper {
kosakd370f852014-11-17 01:14:16 +0000506 explicit ByMoveWrapper(T value) : payload(internal::move(value)) {}
kosak3d1c78b2014-11-17 00:56:52 +0000507 T payload;
508};
509
shiqiane35fdd92008-12-10 05:08:54 +0000510// Implements the polymorphic Return(x) action, which can be used in
511// any function that returns the type of x, regardless of the argument
512// types.
vladloseva070cbd2009-11-18 00:09:28 +0000513//
514// Note: The value passed into Return must be converted into
515// Function<F>::Result when this action is cast to Action<F> rather than
516// when that action is performed. This is important in scenarios like
517//
518// MOCK_METHOD1(Method, T(U));
519// ...
520// {
521// Foo foo;
522// X x(&foo);
523// EXPECT_CALL(mock, Method(_)).WillOnce(Return(x));
524// }
525//
526// In the example above the variable x holds reference to foo which leaves
527// scope and gets destroyed. If copying X just copies a reference to foo,
528// that copy will be left with a hanging reference. If conversion to T
529// makes a copy of foo, the above code is safe. To support that scenario, we
530// need to make sure that the type conversion happens inside the EXPECT_CALL
531// statement, and conversion of the result of Return to Action<T(U)> is a
532// good place for that.
533//
shiqiane35fdd92008-12-10 05:08:54 +0000534template <typename R>
535class ReturnAction {
536 public:
537 // Constructs a ReturnAction object from the value to be returned.
538 // 'value' is passed by value instead of by const reference in order
539 // to allow Return("string literal") to compile.
kosakd370f852014-11-17 01:14:16 +0000540 explicit ReturnAction(R value) : value_(new R(internal::move(value))) {}
shiqiane35fdd92008-12-10 05:08:54 +0000541
542 // This template type conversion operator allows Return(x) to be
543 // used in ANY function that returns x's type.
544 template <typename F>
545 operator Action<F>() const {
546 // Assert statement belongs here because this is the best place to verify
547 // conditions on F. It produces the clearest error messages
548 // in most compilers.
549 // Impl really belongs in this scope as a local class but can't
550 // because MSVC produces duplicate symbols in different translation units
551 // in this case. Until MS fixes that bug we put Impl into the class scope
552 // and put the typedef both here (for use in assert statement) and
553 // in the Impl class. But both definitions must be the same.
554 typedef typename Function<F>::Result Result;
zhanyong.wan02f71062010-05-10 17:14:29 +0000555 GTEST_COMPILE_ASSERT_(
kosak3d1c78b2014-11-17 00:56:52 +0000556 !is_reference<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000557 use_ReturnRef_instead_of_Return_to_return_a_reference);
kosak3d1c78b2014-11-17 00:56:52 +0000558 return Action<F>(new Impl<R, F>(value_));
shiqiane35fdd92008-12-10 05:08:54 +0000559 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000560
shiqiane35fdd92008-12-10 05:08:54 +0000561 private:
562 // Implements the Return(x) action for a particular function type F.
kosak3d1c78b2014-11-17 00:56:52 +0000563 template <typename R_, typename F>
shiqiane35fdd92008-12-10 05:08:54 +0000564 class Impl : public ActionInterface<F> {
565 public:
566 typedef typename Function<F>::Result Result;
567 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
568
vladloseva070cbd2009-11-18 00:09:28 +0000569 // The implicit cast is necessary when Result has more than one
570 // single-argument constructor (e.g. Result is std::vector<int>) and R
571 // has a type conversion operator template. In that case, value_(value)
572 // won't compile as the compiler doesn't known which constructor of
zhanyong.wan5b61ce32011-02-01 00:00:03 +0000573 // Result to call. ImplicitCast_ forces the compiler to convert R to
vladloseva070cbd2009-11-18 00:09:28 +0000574 // Result without considering explicit constructors, thus resolving the
575 // ambiguity. value_ is then initialized using its copy constructor.
kosak3d1c78b2014-11-17 00:56:52 +0000576 explicit Impl(const linked_ptr<R>& value)
kosak7123d832014-11-17 02:04:46 +0000577 : value_before_cast_(*value),
578 value_(ImplicitCast_<Result>(value_before_cast_)) {}
shiqiane35fdd92008-12-10 05:08:54 +0000579
580 virtual Result Perform(const ArgumentTuple&) { return value_; }
581
582 private:
kosak3d1c78b2014-11-17 00:56:52 +0000583 GTEST_COMPILE_ASSERT_(!is_reference<Result>::value,
vladloseva070cbd2009-11-18 00:09:28 +0000584 Result_cannot_be_a_reference_type);
kosak7123d832014-11-17 02:04:46 +0000585 // We save the value before casting just in case it is being cast to a
586 // wrapper type.
587 R value_before_cast_;
vladloseva070cbd2009-11-18 00:09:28 +0000588 Result value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000589
kosak7123d832014-11-17 02:04:46 +0000590 GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000591 };
592
kosak3d1c78b2014-11-17 00:56:52 +0000593 // Partially specialize for ByMoveWrapper. This version of ReturnAction will
594 // move its contents instead.
595 template <typename R_, typename F>
596 class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> {
597 public:
598 typedef typename Function<F>::Result Result;
599 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
600
601 explicit Impl(const linked_ptr<R>& wrapper)
602 : performed_(false), wrapper_(wrapper) {}
603
604 virtual Result Perform(const ArgumentTuple&) {
605 GTEST_CHECK_(!performed_)
606 << "A ByMove() action should only be performed once.";
607 performed_ = true;
kosakd370f852014-11-17 01:14:16 +0000608 return internal::move(wrapper_->payload);
kosak3d1c78b2014-11-17 00:56:52 +0000609 }
610
611 private:
612 bool performed_;
613 const linked_ptr<R> wrapper_;
614
615 GTEST_DISALLOW_ASSIGN_(Impl);
616 };
617
618 const linked_ptr<R> value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000619
620 GTEST_DISALLOW_ASSIGN_(ReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000621};
622
623// Implements the ReturnNull() action.
624class ReturnNullAction {
625 public:
kosak53d49dc2015-01-08 03:03:09 +0000626 // Allows ReturnNull() to be used in any pointer-returning function. In C++11
627 // this is enforced by returning nullptr, and in non-C++11 by asserting a
628 // pointer type on compile time.
shiqiane35fdd92008-12-10 05:08:54 +0000629 template <typename Result, typename ArgumentTuple>
630 static Result Perform(const ArgumentTuple&) {
kosak53d49dc2015-01-08 03:03:09 +0000631#if GTEST_LANG_CXX11
632 return nullptr;
633#else
zhanyong.wan02f71062010-05-10 17:14:29 +0000634 GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000635 ReturnNull_can_be_used_to_return_a_pointer_only);
shiqiane35fdd92008-12-10 05:08:54 +0000636 return NULL;
kosak53d49dc2015-01-08 03:03:09 +0000637#endif // GTEST_LANG_CXX11
shiqiane35fdd92008-12-10 05:08:54 +0000638 }
639};
640
641// Implements the Return() action.
642class ReturnVoidAction {
643 public:
644 // Allows Return() to be used in any void-returning function.
645 template <typename Result, typename ArgumentTuple>
646 static void Perform(const ArgumentTuple&) {
647 CompileAssertTypesEqual<void, Result>();
648 }
649};
650
651// Implements the polymorphic ReturnRef(x) action, which can be used
652// in any function that returns a reference to the type of x,
653// regardless of the argument types.
654template <typename T>
655class ReturnRefAction {
656 public:
657 // Constructs a ReturnRefAction object from the reference to be returned.
658 explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT
659
660 // This template type conversion operator allows ReturnRef(x) to be
661 // used in ANY function that returns a reference to x's type.
662 template <typename F>
663 operator Action<F>() const {
664 typedef typename Function<F>::Result Result;
665 // Asserts that the function return type is a reference. This
666 // catches the user error of using ReturnRef(x) when Return(x)
667 // should be used, and generates some helpful error message.
zhanyong.wan02f71062010-05-10 17:14:29 +0000668 GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value,
zhanyong.wane0d051e2009-02-19 00:33:37 +0000669 use_Return_instead_of_ReturnRef_to_return_a_value);
shiqiane35fdd92008-12-10 05:08:54 +0000670 return Action<F>(new Impl<F>(ref_));
671 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000672
shiqiane35fdd92008-12-10 05:08:54 +0000673 private:
674 // Implements the ReturnRef(x) action for a particular function type F.
675 template <typename F>
676 class Impl : public ActionInterface<F> {
677 public:
678 typedef typename Function<F>::Result Result;
679 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
680
681 explicit Impl(T& ref) : ref_(ref) {} // NOLINT
682
683 virtual Result Perform(const ArgumentTuple&) {
684 return ref_;
685 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000686
shiqiane35fdd92008-12-10 05:08:54 +0000687 private:
688 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000689
690 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000691 };
692
693 T& ref_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000694
695 GTEST_DISALLOW_ASSIGN_(ReturnRefAction);
shiqiane35fdd92008-12-10 05:08:54 +0000696};
697
zhanyong.wane3bd0982010-07-03 00:16:42 +0000698// Implements the polymorphic ReturnRefOfCopy(x) action, which can be
699// used in any function that returns a reference to the type of x,
700// regardless of the argument types.
701template <typename T>
702class ReturnRefOfCopyAction {
703 public:
704 // Constructs a ReturnRefOfCopyAction object from the reference to
705 // be returned.
706 explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT
707
708 // This template type conversion operator allows ReturnRefOfCopy(x) to be
709 // used in ANY function that returns a reference to x's type.
710 template <typename F>
711 operator Action<F>() const {
712 typedef typename Function<F>::Result Result;
713 // Asserts that the function return type is a reference. This
714 // catches the user error of using ReturnRefOfCopy(x) when Return(x)
715 // should be used, and generates some helpful error message.
716 GTEST_COMPILE_ASSERT_(
717 internal::is_reference<Result>::value,
718 use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
719 return Action<F>(new Impl<F>(value_));
720 }
721
722 private:
723 // Implements the ReturnRefOfCopy(x) action for a particular function type F.
724 template <typename F>
725 class Impl : public ActionInterface<F> {
726 public:
727 typedef typename Function<F>::Result Result;
728 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
729
730 explicit Impl(const T& value) : value_(value) {} // NOLINT
731
732 virtual Result Perform(const ArgumentTuple&) {
733 return value_;
734 }
735
736 private:
737 T value_;
738
739 GTEST_DISALLOW_ASSIGN_(Impl);
740 };
741
742 const T value_;
743
744 GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
745};
746
shiqiane35fdd92008-12-10 05:08:54 +0000747// Implements the polymorphic DoDefault() action.
748class DoDefaultAction {
749 public:
750 // This template type conversion operator allows DoDefault() to be
751 // used in any function.
752 template <typename F>
zhanyong.waned6c9272011-02-23 19:39:27 +0000753 operator Action<F>() const { return Action<F>(NULL); }
shiqiane35fdd92008-12-10 05:08:54 +0000754};
755
756// Implements the Assign action to set a given pointer referent to a
757// particular value.
758template <typename T1, typename T2>
759class AssignAction {
760 public:
761 AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
762
763 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000764 void Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000765 *ptr_ = value_;
766 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000767
shiqiane35fdd92008-12-10 05:08:54 +0000768 private:
769 T1* const ptr_;
770 const T2 value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000771
772 GTEST_DISALLOW_ASSIGN_(AssignAction);
shiqiane35fdd92008-12-10 05:08:54 +0000773};
774
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000775#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000776
shiqiane35fdd92008-12-10 05:08:54 +0000777// Implements the SetErrnoAndReturn action to simulate return from
778// various system calls and libc functions.
779template <typename T>
780class SetErrnoAndReturnAction {
781 public:
782 SetErrnoAndReturnAction(int errno_value, T result)
783 : errno_(errno_value),
784 result_(result) {}
785 template <typename Result, typename ArgumentTuple>
zhanyong.wan3fbd2dd2009-03-26 19:06:45 +0000786 Result Perform(const ArgumentTuple& /* args */) const {
shiqiane35fdd92008-12-10 05:08:54 +0000787 errno = errno_;
788 return result_;
789 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000790
shiqiane35fdd92008-12-10 05:08:54 +0000791 private:
792 const int errno_;
793 const T result_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000794
795 GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction);
shiqiane35fdd92008-12-10 05:08:54 +0000796};
797
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000798#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000799
shiqiane35fdd92008-12-10 05:08:54 +0000800// Implements the SetArgumentPointee<N>(x) action for any function
801// whose N-th argument (0-based) is a pointer to x's type. The
802// template parameter kIsProto is true iff type A is ProtocolMessage,
803// proto2::Message, or a sub-class of those.
804template <size_t N, typename A, bool kIsProto>
805class SetArgumentPointeeAction {
806 public:
807 // Constructs an action that sets the variable pointed to by the
808 // N-th function argument to 'value'.
809 explicit SetArgumentPointeeAction(const A& value) : value_(value) {}
810
811 template <typename Result, typename ArgumentTuple>
812 void Perform(const ArgumentTuple& args) const {
813 CompileAssertTypesEqual<void, Result>();
kosakbd018832014-04-02 20:30:00 +0000814 *::testing::get<N>(args) = value_;
shiqiane35fdd92008-12-10 05:08:54 +0000815 }
816
817 private:
818 const A value_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000819
820 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000821};
822
823template <size_t N, typename Proto>
824class SetArgumentPointeeAction<N, Proto, true> {
825 public:
826 // Constructs an action that sets the variable pointed to by the
827 // N-th function argument to 'proto'. Both ProtocolMessage and
828 // proto2::Message have the CopyFrom() method, so the same
829 // implementation works for both.
830 explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) {
831 proto_->CopyFrom(proto);
832 }
833
834 template <typename Result, typename ArgumentTuple>
835 void Perform(const ArgumentTuple& args) const {
836 CompileAssertTypesEqual<void, Result>();
kosakbd018832014-04-02 20:30:00 +0000837 ::testing::get<N>(args)->CopyFrom(*proto_);
shiqiane35fdd92008-12-10 05:08:54 +0000838 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000839
shiqiane35fdd92008-12-10 05:08:54 +0000840 private:
841 const internal::linked_ptr<Proto> proto_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000842
843 GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction);
shiqiane35fdd92008-12-10 05:08:54 +0000844};
845
shiqiane35fdd92008-12-10 05:08:54 +0000846// Implements the InvokeWithoutArgs(f) action. The template argument
847// FunctionImpl is the implementation type of f, which can be either a
848// function pointer or a functor. InvokeWithoutArgs(f) can be used as an
849// Action<F> as long as f's type is compatible with F (i.e. f can be
850// assigned to a tr1::function<F>).
851template <typename FunctionImpl>
852class InvokeWithoutArgsAction {
853 public:
854 // The c'tor makes a copy of function_impl (either a function
855 // pointer or a functor).
856 explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
857 : function_impl_(function_impl) {}
858
859 // Allows InvokeWithoutArgs(f) to be used as any action whose type is
860 // compatible with f.
861 template <typename Result, typename ArgumentTuple>
862 Result Perform(const ArgumentTuple&) { return function_impl_(); }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000863
shiqiane35fdd92008-12-10 05:08:54 +0000864 private:
865 FunctionImpl function_impl_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000866
867 GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000868};
869
870// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
871template <class Class, typename MethodPtr>
872class InvokeMethodWithoutArgsAction {
873 public:
874 InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr)
875 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
876
877 template <typename Result, typename ArgumentTuple>
878 Result Perform(const ArgumentTuple&) const {
879 return (obj_ptr_->*method_ptr_)();
880 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000881
shiqiane35fdd92008-12-10 05:08:54 +0000882 private:
883 Class* const obj_ptr_;
884 const MethodPtr method_ptr_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000885
886 GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction);
shiqiane35fdd92008-12-10 05:08:54 +0000887};
888
889// Implements the IgnoreResult(action) action.
890template <typename A>
891class IgnoreResultAction {
892 public:
893 explicit IgnoreResultAction(const A& action) : action_(action) {}
894
895 template <typename F>
896 operator Action<F>() const {
897 // Assert statement belongs here because this is the best place to verify
898 // conditions on F. It produces the clearest error messages
899 // in most compilers.
900 // Impl really belongs in this scope as a local class but can't
901 // because MSVC produces duplicate symbols in different translation units
902 // in this case. Until MS fixes that bug we put Impl into the class scope
903 // and put the typedef both here (for use in assert statement) and
904 // in the Impl class. But both definitions must be the same.
905 typedef typename internal::Function<F>::Result Result;
906
907 // Asserts at compile time that F returns void.
908 CompileAssertTypesEqual<void, Result>();
909
910 return Action<F>(new Impl<F>(action_));
911 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000912
shiqiane35fdd92008-12-10 05:08:54 +0000913 private:
914 template <typename F>
915 class Impl : public ActionInterface<F> {
916 public:
917 typedef typename internal::Function<F>::Result Result;
918 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
919
920 explicit Impl(const A& action) : action_(action) {}
921
922 virtual void Perform(const ArgumentTuple& args) {
923 // Performs the action and ignores its result.
924 action_.Perform(args);
925 }
926
927 private:
928 // Type OriginalFunction is the same as F except that its return
929 // type is IgnoredValue.
930 typedef typename internal::Function<F>::MakeResultIgnoredValue
931 OriginalFunction;
932
933 const Action<OriginalFunction> action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000934
935 GTEST_DISALLOW_ASSIGN_(Impl);
shiqiane35fdd92008-12-10 05:08:54 +0000936 };
937
938 const A action_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000939
940 GTEST_DISALLOW_ASSIGN_(IgnoreResultAction);
shiqiane35fdd92008-12-10 05:08:54 +0000941};
942
zhanyong.wana18423e2009-07-22 23:58:19 +0000943// A ReferenceWrapper<T> object represents a reference to type T,
944// which can be either const or not. It can be explicitly converted
945// from, and implicitly converted to, a T&. Unlike a reference,
946// ReferenceWrapper<T> can be copied and can survive template type
947// inference. This is used to support by-reference arguments in the
948// InvokeArgument<N>(...) action. The idea was from "reference
949// wrappers" in tr1, which we don't have in our source tree yet.
950template <typename T>
951class ReferenceWrapper {
952 public:
953 // Constructs a ReferenceWrapper<T> object from a T&.
954 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT
955
956 // Allows a ReferenceWrapper<T> object to be implicitly converted to
957 // a T&.
958 operator T&() const { return *pointer_; }
959 private:
960 T* pointer_;
961};
962
963// Allows the expression ByRef(x) to be printed as a reference to x.
964template <typename T>
965void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) {
966 T& value = ref;
967 UniversalPrinter<T&>::Print(value, os);
968}
969
970// Does two actions sequentially. Used for implementing the DoAll(a1,
971// a2, ...) action.
972template <typename Action1, typename Action2>
973class DoBothAction {
974 public:
975 DoBothAction(Action1 action1, Action2 action2)
976 : action1_(action1), action2_(action2) {}
977
978 // This template type conversion operator allows DoAll(a1, ..., a_n)
979 // to be used in ANY function of compatible type.
980 template <typename F>
981 operator Action<F>() const {
982 return Action<F>(new Impl<F>(action1_, action2_));
983 }
984
985 private:
986 // Implements the DoAll(...) action for a particular function type F.
987 template <typename F>
988 class Impl : public ActionInterface<F> {
989 public:
990 typedef typename Function<F>::Result Result;
991 typedef typename Function<F>::ArgumentTuple ArgumentTuple;
992 typedef typename Function<F>::MakeResultVoid VoidResult;
993
994 Impl(const Action<VoidResult>& action1, const Action<F>& action2)
995 : action1_(action1), action2_(action2) {}
996
997 virtual Result Perform(const ArgumentTuple& args) {
998 action1_.Perform(args);
999 return action2_.Perform(args);
1000 }
1001
1002 private:
1003 const Action<VoidResult> action1_;
1004 const Action<F> action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +00001005
1006 GTEST_DISALLOW_ASSIGN_(Impl);
zhanyong.wana18423e2009-07-22 23:58:19 +00001007 };
1008
1009 Action1 action1_;
1010 Action2 action2_;
zhanyong.wan32de5f52009-12-23 00:13:23 +00001011
1012 GTEST_DISALLOW_ASSIGN_(DoBothAction);
zhanyong.wana18423e2009-07-22 23:58:19 +00001013};
1014
shiqiane35fdd92008-12-10 05:08:54 +00001015} // namespace internal
1016
1017// An Unused object can be implicitly constructed from ANY value.
1018// This is handy when defining actions that ignore some or all of the
1019// mock function arguments. For example, given
1020//
1021// MOCK_METHOD3(Foo, double(const string& label, double x, double y));
1022// MOCK_METHOD3(Bar, double(int index, double x, double y));
1023//
1024// instead of
1025//
1026// double DistanceToOriginWithLabel(const string& label, double x, double y) {
1027// return sqrt(x*x + y*y);
1028// }
1029// double DistanceToOriginWithIndex(int index, double x, double y) {
1030// return sqrt(x*x + y*y);
1031// }
1032// ...
Hector Dearman41ad2432017-06-19 18:43:55 +01001033// EXPECT_CALL(mock, Foo("abc", _, _))
shiqiane35fdd92008-12-10 05:08:54 +00001034// .WillOnce(Invoke(DistanceToOriginWithLabel));
Hector Dearman41ad2432017-06-19 18:43:55 +01001035// EXPECT_CALL(mock, Bar(5, _, _))
shiqiane35fdd92008-12-10 05:08:54 +00001036// .WillOnce(Invoke(DistanceToOriginWithIndex));
1037//
1038// you could write
1039//
1040// // We can declare any uninteresting argument as Unused.
1041// double DistanceToOrigin(Unused, double x, double y) {
1042// return sqrt(x*x + y*y);
1043// }
1044// ...
Hector Dearman41ad2432017-06-19 18:43:55 +01001045// EXPECT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
1046// EXPECT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
shiqiane35fdd92008-12-10 05:08:54 +00001047typedef internal::IgnoredValue Unused;
1048
1049// This constructor allows us to turn an Action<From> object into an
1050// Action<To>, as long as To's arguments can be implicitly converted
1051// to From's and From's return type cann be implicitly converted to
1052// To's.
1053template <typename To>
1054template <typename From>
1055Action<To>::Action(const Action<From>& from)
1056 : impl_(new internal::ActionAdaptor<To, From>(from)) {}
1057
1058// Creates an action that returns 'value'. 'value' is passed by value
1059// instead of const reference - otherwise Return("string literal")
1060// will trigger a compiler error about using array as initializer.
1061template <typename R>
1062internal::ReturnAction<R> Return(R value) {
kosak3d1c78b2014-11-17 00:56:52 +00001063 return internal::ReturnAction<R>(internal::move(value));
shiqiane35fdd92008-12-10 05:08:54 +00001064}
1065
1066// Creates an action that returns NULL.
1067inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
1068 return MakePolymorphicAction(internal::ReturnNullAction());
1069}
1070
1071// Creates an action that returns from a void function.
1072inline PolymorphicAction<internal::ReturnVoidAction> Return() {
1073 return MakePolymorphicAction(internal::ReturnVoidAction());
1074}
1075
1076// Creates an action that returns the reference to a variable.
1077template <typename R>
1078inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT
1079 return internal::ReturnRefAction<R>(x);
1080}
1081
zhanyong.wane3bd0982010-07-03 00:16:42 +00001082// Creates an action that returns the reference to a copy of the
1083// argument. The copy is created when the action is constructed and
1084// lives as long as the action.
1085template <typename R>
1086inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
1087 return internal::ReturnRefOfCopyAction<R>(x);
1088}
1089
kosak3d1c78b2014-11-17 00:56:52 +00001090// Modifies the parent action (a Return() action) to perform a move of the
1091// argument instead of a copy.
1092// Return(ByMove()) actions can only be executed once and will assert this
1093// invariant.
1094template <typename R>
1095internal::ByMoveWrapper<R> ByMove(R x) {
1096 return internal::ByMoveWrapper<R>(internal::move(x));
1097}
1098
shiqiane35fdd92008-12-10 05:08:54 +00001099// Creates an action that does the default action for the give mock function.
1100inline internal::DoDefaultAction DoDefault() {
1101 return internal::DoDefaultAction();
1102}
1103
1104// Creates an action that sets the variable pointed by the N-th
1105// (0-based) function argument to 'value'.
1106template <size_t N, typename T>
1107PolymorphicAction<
1108 internal::SetArgumentPointeeAction<
1109 N, T, internal::IsAProtocolMessage<T>::value> >
zhanyong.wan59214832010-10-05 05:58:51 +00001110SetArgPointee(const T& x) {
1111 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1112 N, T, internal::IsAProtocolMessage<T>::value>(x));
1113}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001114
1115#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
zhanyong.wana684b5a2010-12-02 23:30:50 +00001116// This overload allows SetArgPointee() to accept a string literal.
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001117// GCC prior to the version 4.0 and Symbian C++ compiler cannot distinguish
1118// this overload from the templated version and emit a compile error.
zhanyong.wana684b5a2010-12-02 23:30:50 +00001119template <size_t N>
1120PolymorphicAction<
1121 internal::SetArgumentPointeeAction<N, const char*, false> >
1122SetArgPointee(const char* p) {
1123 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1124 N, const char*, false>(p));
1125}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +00001126
1127template <size_t N>
1128PolymorphicAction<
1129 internal::SetArgumentPointeeAction<N, const wchar_t*, false> >
1130SetArgPointee(const wchar_t* p) {
1131 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1132 N, const wchar_t*, false>(p));
1133}
1134#endif
1135
zhanyong.wan59214832010-10-05 05:58:51 +00001136// The following version is DEPRECATED.
1137template <size_t N, typename T>
1138PolymorphicAction<
1139 internal::SetArgumentPointeeAction<
1140 N, T, internal::IsAProtocolMessage<T>::value> >
shiqiane35fdd92008-12-10 05:08:54 +00001141SetArgumentPointee(const T& x) {
1142 return MakePolymorphicAction(internal::SetArgumentPointeeAction<
1143 N, T, internal::IsAProtocolMessage<T>::value>(x));
1144}
1145
shiqiane35fdd92008-12-10 05:08:54 +00001146// Creates an action that sets a pointer referent to a given value.
1147template <typename T1, typename T2>
1148PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
1149 return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
1150}
1151
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001152#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001153
shiqiane35fdd92008-12-10 05:08:54 +00001154// Creates an action that sets errno and returns the appropriate error.
1155template <typename T>
1156PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
1157SetErrnoAndReturn(int errval, T result) {
1158 return MakePolymorphicAction(
1159 internal::SetErrnoAndReturnAction<T>(errval, result));
1160}
1161
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001162#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001163
shiqiane35fdd92008-12-10 05:08:54 +00001164// Various overloads for InvokeWithoutArgs().
1165
1166// Creates an action that invokes 'function_impl' with no argument.
1167template <typename FunctionImpl>
1168PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> >
1169InvokeWithoutArgs(FunctionImpl function_impl) {
1170 return MakePolymorphicAction(
1171 internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl));
1172}
1173
1174// Creates an action that invokes the given method on the given object
1175// with no argument.
1176template <class Class, typename MethodPtr>
1177PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> >
1178InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
1179 return MakePolymorphicAction(
1180 internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>(
1181 obj_ptr, method_ptr));
1182}
1183
1184// Creates an action that performs an_action and throws away its
1185// result. In other words, it changes the return type of an_action to
1186// void. an_action MUST NOT return void, or the code won't compile.
1187template <typename A>
1188inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
1189 return internal::IgnoreResultAction<A>(an_action);
1190}
1191
zhanyong.wana18423e2009-07-22 23:58:19 +00001192// Creates a reference wrapper for the given L-value. If necessary,
1193// you can explicitly specify the type of the reference. For example,
1194// suppose 'derived' is an object of type Derived, ByRef(derived)
1195// would wrap a Derived&. If you want to wrap a const Base& instead,
1196// where Base is a base class of Derived, just write:
1197//
1198// ByRef<const Base>(derived)
1199template <typename T>
1200inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT
1201 return internal::ReferenceWrapper<T>(l_value);
1202}
1203
shiqiane35fdd92008-12-10 05:08:54 +00001204} // namespace testing
1205
1206#endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_