gmock-actions.h
Go to the documentation of this file.
1 // 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 
39 #ifndef _WIN32_WCE
40 # include <errno.h>
41 #endif
42 
43 #include <algorithm>
44 #include <string>
45 
48 
49 namespace testing
50 {
51 
52 // To implement an action Foo, define:
53 // 1. a class FooAction that implements the ActionInterface interface, and
54 // 2. a factory function that creates an Action object from a
55 // const FooAction*.
56 //
57 // The two-level delegation design follows that of Matcher, providing
58 // consistency for extension developers. It also eases ownership
59 // management as Action objects can now be copied like plain values.
60 
61 namespace internal
62 {
63 
64 template <typename F1, typename F2>
65 class ActionAdaptor;
66 
67 // BuiltInDefaultValue<T>::Get() returns the "built-in" default
68 // value for type T, which is NULL when T is a pointer type, 0 when T
69 // is a numeric type, false when T is bool, or "" when T is string or
70 // std::string. For any other type T, this value is undefined and the
71 // function will abort the process.
72 template <typename T>
73 class BuiltInDefaultValue
74 {
75 public:
76  // This function returns true iff type T has a built-in default value.
77  static bool Exists() { return false; }
78  static T Get()
79  {
80  Assert(false, __FILE__, __LINE__,
81  "Default action undefined for the function return type.");
82  return internal::Invalid<T>();
83  // The above statement will never be reached, but is required in
84  // order for this function to compile.
85  }
86 };
87 
88 // This partial specialization says that we use the same built-in
89 // default value for T and const T.
90 template <typename T>
91 class BuiltInDefaultValue<const T>
92 {
93 public:
94  static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
95  static T Get() { return BuiltInDefaultValue<T>::Get(); }
96 };
97 
98 // This partial specialization defines the default values for pointer
99 // types.
100 template <typename T>
101 class BuiltInDefaultValue<T *>
102 {
103 public:
104  static bool Exists() { return true; }
105  static T * Get() { return NULL; }
106 };
107 
108 // The following specializations define the default values for
109 // specific types we care about.
110 #define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
111  template <> \
112  class BuiltInDefaultValue<type> { \
113  public: \
114  static bool Exists() { return true; } \
115  static type Get() { return value; } \
116  }
117 
119 #if GTEST_HAS_GLOBAL_STRING
121 #endif // GTEST_HAS_GLOBAL_STRING
127 
128 // There's no need for a default action for signed wchar_t, as that
129 // type is the same as wchar_t for gcc, and invalid for MSVC.
130 //
131 // There's also no need for a default action for unsigned wchar_t, as
132 // that type is the same as unsigned int for gcc, and invalid for
133 // MSVC.
134 #if GMOCK_WCHAR_T_IS_NATIVE_
135 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT
136 #endif
137 
138 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U); // NOLINT
139 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0); // NOLINT
142 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL); // NOLINT
143 GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L); // NOLINT
148 
149 #undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
150 
151 } // namespace internal
152 
153 // When an unexpected function call is encountered, Google Mock will
154 // let it return a default value if the user has specified one for its
155 // return type, or if the return type has a built-in default value;
156 // otherwise Google Mock won't know what value to return and will have
157 // to abort the process.
158 //
159 // The DefaultValue<T> class allows a user to specify the
160 // default value for a type T that is both copyable and publicly
161 // destructible (i.e. anything that can be used as a function return
162 // type). The usage is:
163 //
164 // // Sets the default value for type T to be foo.
165 // DefaultValue<T>::Set(foo);
166 template <typename T>
167 class DefaultValue
168 {
169 public:
170  // Sets the default value for type T; requires T to be
171  // copy-constructable and have a public destructor.
172  static void Set(T x)
173  {
174  delete value_;
175  value_ = new T(x);
176  }
177 
178  // Unsets the default value for type T.
179  static void Clear()
180  {
181  delete value_;
182  value_ = NULL;
183  }
184 
185  // Returns true iff the user has set the default value for type T.
186  static bool IsSet() { return value_ != NULL; }
187 
188  // Returns true if T has a default return value set by the user or there
189  // exists a built-in default value.
190  static bool Exists()
191  {
192  return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
193  }
194 
195  // Returns the default value for type T if the user has set one;
196  // otherwise returns the built-in default value if there is one;
197  // otherwise aborts the process.
198  static T Get()
199  {
200  return value_ == NULL ?
202  }
203 
204 private:
205  static const T * value_;
206 };
207 
208 // This partial specialization allows a user to set default values for
209 // reference types.
210 template <typename T>
211 class DefaultValue<T &>
212 {
213 public:
214  // Sets the default value for type T&.
215  static void Set(T & x) // NOLINT
216  {
217  address_ = &x;
218  }
219 
220  // Unsets the default value for type T&.
221  static void Clear()
222  {
223  address_ = NULL;
224  }
225 
226  // Returns true iff the user has set the default value for type T&.
227  static bool IsSet() { return address_ != NULL; }
228 
229  // Returns true if T has a default return value set by the user or there
230  // exists a built-in default value.
231  static bool Exists()
232  {
233  return IsSet() || internal::BuiltInDefaultValue<T &>::Exists();
234  }
235 
236  // Returns the default value for type T& if the user has set one;
237  // otherwise returns the built-in default value if there is one;
238  // otherwise aborts the process.
239  static T & Get()
240  {
241  return address_ == NULL ?
243  }
244 
245 private:
246  static T * address_;
247 };
248 
249 // This specialization allows DefaultValue<void>::Get() to
250 // compile.
251 template <>
252 class DefaultValue<void>
253 {
254 public:
255  static bool Exists() { return true; }
256  static void Get() {}
257 };
258 
259 // Points to the user-set default value for type T.
260 template <typename T>
261 const T * DefaultValue<T>::value_ = NULL;
262 
263 // Points to the user-set default value for type T&.
264 template <typename T>
265 T * DefaultValue<T &>::address_ = NULL;
266 
267 // Implement this interface to define an action for function type F.
268 template <typename F>
269 class ActionInterface
270 {
271 public:
274 
276  virtual ~ActionInterface() {}
277 
278  // Performs the action. This method is not const, as in general an
279  // action can have side effects and be stateful. For example, a
280  // get-the-next-element-from-the-collection action will need to
281  // remember the current element.
282  virtual Result Perform(const ArgumentTuple & args) = 0;
283 
284 private:
286 };
287 
288 // An Action<F> is a copyable and IMMUTABLE (except by assignment)
289 // object that represents an action to be taken when a mock function
290 // of type F is called. The implementation of Action<T> is just a
291 // linked_ptr to const ActionInterface<T>, so copying is fairly cheap.
292 // Don't inherit from Action!
293 //
294 // You can view an object implementing ActionInterface<F> as a
295 // concrete action (including its current state), and an Action<F>
296 // object as a handle to it.
297 template <typename F>
298 class Action
299 {
300 public:
303 
304  // Constructs a null Action. Needed for storing Action objects in
305  // STL containers.
306  Action() : impl_(NULL) {}
307 
308  // Constructs an Action from its implementation. A NULL impl is
309  // used to represent the "do-default" action.
310  explicit Action(ActionInterface<F> * impl) : impl_(impl) {}
311 
312  // Copy constructor.
313  Action(const Action & action) : impl_(action.impl_) {}
314 
315  // This constructor allows us to turn an Action<Func> object into an
316  // Action<F>, as long as F's arguments can be implicitly converted
317  // to Func's and Func's return type can be implicitly converted to
318  // F's.
319  template <typename Func>
320  explicit Action(const Action<Func> & action);
321 
322  // Returns true iff this is the DoDefault() action.
323  bool IsDoDefault() const { return impl_.get() == NULL; }
324 
325  // Performs the action. Note that this method is const even though
326  // the corresponding method in ActionInterface is not. The reason
327  // is that a const Action<F> means that it cannot be re-bound to
328  // another concrete action, not that the concrete action it binds to
329  // cannot change state. (Think of the difference between a const
330  // pointer and a pointer to const.)
331  Result Perform(const ArgumentTuple & args) const
332  {
334  !IsDoDefault(), __FILE__, __LINE__,
335  "You are using DoDefault() inside a composite action like "
336  "DoAll() or WithArgs(). This is not supported for technical "
337  "reasons. Please instead spell out the default action, or "
338  "assign the default action to an Action variable and use "
339  "the variable in various places.");
340  return impl_->Perform(args);
341  }
342 
343 private:
344  template <typename F1, typename F2>
346 
348 };
349 
350 // The PolymorphicAction class template makes it easy to implement a
351 // polymorphic action (i.e. an action that can be used in mock
352 // functions of than one type, e.g. Return()).
353 //
354 // To define a polymorphic action, a user first provides a COPYABLE
355 // implementation class that has a Perform() method template:
356 //
357 // class FooAction {
358 // public:
359 // template <typename Result, typename ArgumentTuple>
360 // Result Perform(const ArgumentTuple& args) const {
361 // // Processes the arguments and returns a result, using
362 // // tr1::get<N>(args) to get the N-th (0-based) argument in the tuple.
363 // }
364 // ...
365 // };
366 //
367 // Then the user creates the polymorphic action using
368 // MakePolymorphicAction(object) where object has type FooAction. See
369 // the definition of Return(void) and SetArgumentPointee<N>(value) for
370 // complete examples.
371 template <typename Impl>
372 class PolymorphicAction
373 {
374 public:
375  explicit PolymorphicAction(const Impl & impl) : impl_(impl) {}
376 
377  template <typename F>
378  operator Action<F>() const
379  {
380  return Action<F>(new MonomorphicImpl<F>(impl_));
381  }
382 
383 private:
384  template <typename F>
385  class MonomorphicImpl : public ActionInterface<F>
386  {
387  public:
390 
391  explicit MonomorphicImpl(const Impl & impl) : impl_(impl) {}
392 
393  virtual Result Perform(const ArgumentTuple & args)
394  {
395  return impl_.template Perform<Result>(args);
396  }
397 
398  private:
399  Impl impl_;
400 
402  };
403 
404  Impl impl_;
405 
407 };
408 
409 // Creates an Action from its implementation and returns it. The
410 // created Action object owns the implementation.
411 template <typename F>
413 {
414  return Action<F>(impl);
415 }
416 
417 // Creates a polymorphic action from its implementation. This is
418 // easier to use than the PolymorphicAction<Impl> constructor as it
419 // doesn't require you to explicitly write the template argument, e.g.
420 //
421 // MakePolymorphicAction(foo);
422 // vs
423 // PolymorphicAction<TypeOfFoo>(foo);
424 template <typename Impl>
425 inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl & impl)
426 {
427  return PolymorphicAction<Impl>(impl);
428 }
429 
430 namespace internal
431 {
432 
433 // Allows an Action<F2> object to pose as an Action<F1>, as long as F2
434 // and F1 are compatible.
435 template <typename F1, typename F2>
436 class ActionAdaptor : public ActionInterface<F1>
437 {
438 public:
441 
442  explicit ActionAdaptor(const Action<F2> & from) : impl_(from.impl_) {}
443 
444  virtual Result Perform(const ArgumentTuple & args)
445  {
446  return impl_->Perform(args);
447  }
448 
449 private:
451 
453 };
454 
455 // Implements the polymorphic Return(x) action, which can be used in
456 // any function that returns the type of x, regardless of the argument
457 // types.
458 //
459 // Note: The value passed into Return must be converted into
460 // Function<F>::Result when this action is cast to Action<F> rather than
461 // when that action is performed. This is important in scenarios like
462 //
463 // MOCK_METHOD1(Method, T(U));
464 // ...
465 // {
466 // Foo foo;
467 // X x(&foo);
468 // EXPECT_CALL(mock, Method(_)).WillOnce(Return(x));
469 // }
470 //
471 // In the example above the variable x holds reference to foo which leaves
472 // scope and gets destroyed. If copying X just copies a reference to foo,
473 // that copy will be left with a hanging reference. If conversion to T
474 // makes a copy of foo, the above code is safe. To support that scenario, we
475 // need to make sure that the type conversion happens inside the EXPECT_CALL
476 // statement, and conversion of the result of Return to Action<T(U)> is a
477 // good place for that.
478 //
479 template <typename R>
480 class ReturnAction
481 {
482 public:
483  // Constructs a ReturnAction object from the value to be returned.
484  // 'value' is passed by value instead of by const reference in order
485  // to allow Return("string literal") to compile.
486  explicit ReturnAction(R value) : value_(value) {}
487 
488  // This template type conversion operator allows Return(x) to be
489  // used in ANY function that returns x's type.
490  template <typename F>
491  operator Action<F>() const
492  {
493  // Assert statement belongs here because this is the best place to verify
494  // conditions on F. It produces the clearest error messages
495  // in most compilers.
496  // Impl really belongs in this scope as a local class but can't
497  // because MSVC produces duplicate symbols in different translation units
498  // in this case. Until MS fixes that bug we put Impl into the class scope
499  // and put the typedef both here (for use in assert statement) and
500  // in the Impl class. But both definitions must be the same.
501  typedef typename Function<F>::Result Result;
504  use_ReturnRef_instead_of_Return_to_return_a_reference);
505  return Action<F>(new Impl<F>(value_));
506  }
507 
508 private:
509  // Implements the Return(x) action for a particular function type F.
510  template <typename F>
511  class Impl : public ActionInterface<F>
512  {
513  public:
514  typedef typename Function<F>::Result Result;
516 
517  // The implicit cast is necessary when Result has more than one
518  // single-argument constructor (e.g. Result is std::vector<int>) and R
519  // has a type conversion operator template. In that case, value_(value)
520  // won't compile as the compiler doesn't known which constructor of
521  // Result to call. ImplicitCast_ forces the compiler to convert R to
522  // Result without considering explicit constructors, thus resolving the
523  // ambiguity. value_ is then initialized using its copy constructor.
524  explicit Impl(R value)
525  : value_(::testing::internal::ImplicitCast_<Result>(value)) {}
526 
527  virtual Result Perform(const ArgumentTuple &) { return value_; }
528 
529  private:
531  Result_cannot_be_a_reference_type);
532  Result value_;
533 
535  };
536 
537  R value_;
538 
540 };
541 
542 // Implements the ReturnNull() action.
543 class ReturnNullAction
544 {
545 public:
546  // Allows ReturnNull() to be used in any pointer-returning function.
547  template <typename Result, typename ArgumentTuple>
548  static Result Perform(const ArgumentTuple &)
549  {
551  ReturnNull_can_be_used_to_return_a_pointer_only);
552  return NULL;
553  }
554 };
555 
556 // Implements the Return() action.
557 class ReturnVoidAction
558 {
559 public:
560  // Allows Return() to be used in any void-returning function.
561  template <typename Result, typename ArgumentTuple>
562  static void Perform(const ArgumentTuple &)
563  {
565  }
566 };
567 
568 // Implements the polymorphic ReturnRef(x) action, which can be used
569 // in any function that returns a reference to the type of x,
570 // regardless of the argument types.
571 template <typename T>
572 class ReturnRefAction
573 {
574 public:
575  // Constructs a ReturnRefAction object from the reference to be returned.
576  explicit ReturnRefAction(T & ref) : ref_(ref) {} // NOLINT
577 
578  // This template type conversion operator allows ReturnRef(x) to be
579  // used in ANY function that returns a reference to x's type.
580  template <typename F>
581  operator Action<F>() const
582  {
583  typedef typename Function<F>::Result Result;
584  // Asserts that the function return type is a reference. This
585  // catches the user error of using ReturnRef(x) when Return(x)
586  // should be used, and generates some helpful error message.
588  use_Return_instead_of_ReturnRef_to_return_a_value);
589  return Action<F>(new Impl<F>(ref_));
590  }
591 
592 private:
593  // Implements the ReturnRef(x) action for a particular function type F.
594  template <typename F>
595  class Impl : public ActionInterface<F>
596  {
597  public:
598  typedef typename Function<F>::Result Result;
600 
601  explicit Impl(T & ref) : ref_(ref) {} // NOLINT
602 
603  virtual Result Perform(const ArgumentTuple &)
604  {
605  return ref_;
606  }
607 
608  private:
609  T & ref_;
610 
612  };
613 
614  T & ref_;
615 
617 };
618 
619 // Implements the polymorphic ReturnRefOfCopy(x) action, which can be
620 // used in any function that returns a reference to the type of x,
621 // regardless of the argument types.
622 template <typename T>
624 {
625 public:
626  // Constructs a ReturnRefOfCopyAction object from the reference to
627  // be returned.
628  explicit ReturnRefOfCopyAction(const T & value) : value_(value) {} // NOLINT
629 
630  // This template type conversion operator allows ReturnRefOfCopy(x) to be
631  // used in ANY function that returns a reference to x's type.
632  template <typename F>
633  operator Action<F>() const
634  {
635  typedef typename Function<F>::Result Result;
636  // Asserts that the function return type is a reference. This
637  // catches the user error of using ReturnRefOfCopy(x) when Return(x)
638  // should be used, and generates some helpful error message.
641  use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
642  return Action<F>(new Impl<F>(value_));
643  }
644 
645 private:
646  // Implements the ReturnRefOfCopy(x) action for a particular function type F.
647  template <typename F>
648  class Impl : public ActionInterface<F>
649  {
650  public:
651  typedef typename Function<F>::Result Result;
653 
654  explicit Impl(const T & value) : value_(value) {} // NOLINT
655 
656  virtual Result Perform(const ArgumentTuple &)
657  {
658  return value_;
659  }
660 
661  private:
662  T value_;
663 
665  };
666 
667  const T value_;
668 
670 };
671 
672 // Implements the polymorphic DoDefault() action.
673 class DoDefaultAction
674 {
675 public:
676  // This template type conversion operator allows DoDefault() to be
677  // used in any function.
678  template <typename F>
679  operator Action<F>() const { return Action<F>(NULL); }
680 };
681 
682 // Implements the Assign action to set a given pointer referent to a
683 // particular value.
684 template <typename T1, typename T2>
685 class AssignAction
686 {
687 public:
688  AssignAction(T1 * ptr, T2 value) : ptr_(ptr), value_(value) {}
689 
690  template <typename Result, typename ArgumentTuple>
691  void Perform(const ArgumentTuple & /* args */) const
692  {
693  *ptr_ = value_;
694  }
695 
696 private:
697  T1 * const ptr_;
698  const T2 value_;
699 
701 };
702 
703 #if !GTEST_OS_WINDOWS_MOBILE
704 
705 // Implements the SetErrnoAndReturn action to simulate return from
706 // various system calls and libc functions.
707 template <typename T>
709 {
710 public:
711  SetErrnoAndReturnAction(int errno_value, T result)
712  : errno_(errno_value),
713  result_(result) {}
714  template <typename Result, typename ArgumentTuple>
715  Result Perform(const ArgumentTuple & /* args */) const
716  {
717  errno = errno_;
718  return result_;
719  }
720 
721 private:
722  const int errno_;
723  const T result_;
724 
726 };
727 
728 #endif // !GTEST_OS_WINDOWS_MOBILE
729 
730 // Implements the SetArgumentPointee<N>(x) action for any function
731 // whose N-th argument (0-based) is a pointer to x's type. The
732 // template parameter kIsProto is true iff type A is ProtocolMessage,
733 // proto2::Message, or a sub-class of those.
734 template <size_t N, typename A, bool kIsProto>
736 {
737 public:
738  // Constructs an action that sets the variable pointed to by the
739  // N-th function argument to 'value'.
740  explicit SetArgumentPointeeAction(const A & value) : value_(value) {}
741 
742  template <typename Result, typename ArgumentTuple>
743  void Perform(const ArgumentTuple & args) const
744  {
746  *::std::tr1::get<N>(args) = value_;
747  }
748 
749 private:
750  const A value_;
751 
753 };
754 
755 template <size_t N, typename Proto>
756 class SetArgumentPointeeAction<N, Proto, true>
757 {
758 public:
759  // Constructs an action that sets the variable pointed to by the
760  // N-th function argument to 'proto'. Both ProtocolMessage and
761  // proto2::Message have the CopyFrom() method, so the same
762  // implementation works for both.
763  explicit SetArgumentPointeeAction(const Proto & proto) : proto_(new Proto)
764  {
765  proto_->CopyFrom(proto);
766  }
767 
768  template <typename Result, typename ArgumentTuple>
769  void Perform(const ArgumentTuple & args) const
770  {
772  ::std::tr1::get<N>(args)->CopyFrom(*proto_);
773  }
774 
775 private:
776  const internal::linked_ptr<Proto> proto_;
777 
779 };
780 
781 // Implements the InvokeWithoutArgs(f) action. The template argument
782 // FunctionImpl is the implementation type of f, which can be either a
783 // function pointer or a functor. InvokeWithoutArgs(f) can be used as an
784 // Action<F> as long as f's type is compatible with F (i.e. f can be
785 // assigned to a tr1::function<F>).
786 template <typename FunctionImpl>
788 {
789 public:
790  // The c'tor makes a copy of function_impl (either a function
791  // pointer or a functor).
792  explicit InvokeWithoutArgsAction(FunctionImpl function_impl)
793  : function_impl_(function_impl) {}
794 
795  // Allows InvokeWithoutArgs(f) to be used as any action whose type is
796  // compatible with f.
797  template <typename Result, typename ArgumentTuple>
798  Result Perform(const ArgumentTuple &) { return function_impl_(); }
799 
800 private:
801  FunctionImpl function_impl_;
802 
804 };
805 
806 // Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
807 template <class Class, typename MethodPtr>
809 {
810 public:
811  InvokeMethodWithoutArgsAction(Class * obj_ptr, MethodPtr method_ptr)
812  : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
813 
814  template <typename Result, typename ArgumentTuple>
815  Result Perform(const ArgumentTuple &) const
816  {
817  return (obj_ptr_->*method_ptr_)();
818  }
819 
820 private:
821  Class * const obj_ptr_;
822  const MethodPtr method_ptr_;
823 
825 };
826 
827 // Implements the IgnoreResult(action) action.
828 template <typename A>
829 class IgnoreResultAction
830 {
831 public:
832  explicit IgnoreResultAction(const A & action) : action_(action) {}
833 
834  template <typename F>
835  operator Action<F>() const
836  {
837  // Assert statement belongs here because this is the best place to verify
838  // conditions on F. It produces the clearest error messages
839  // in most compilers.
840  // Impl really belongs in this scope as a local class but can't
841  // because MSVC produces duplicate symbols in different translation units
842  // in this case. Until MS fixes that bug we put Impl into the class scope
843  // and put the typedef both here (for use in assert statement) and
844  // in the Impl class. But both definitions must be the same.
845  typedef typename internal::Function<F>::Result Result;
846 
847  // Asserts at compile time that F returns void.
849 
850  return Action<F>(new Impl<F>(action_));
851  }
852 
853 private:
854  template <typename F>
855  class Impl : public ActionInterface<F>
856  {
857  public:
860 
861  explicit Impl(const A & action) : action_(action) {}
862 
863  virtual void Perform(const ArgumentTuple & args)
864  {
865  // Performs the action and ignores its result.
866  action_.Perform(args);
867  }
868 
869  private:
870  // Type OriginalFunction is the same as F except that its return
871  // type is IgnoredValue.
874 
875  const Action<OriginalFunction> action_;
876 
878  };
879 
880  const A action_;
881 
883 };
884 
885 // A ReferenceWrapper<T> object represents a reference to type T,
886 // which can be either const or not. It can be explicitly converted
887 // from, and implicitly converted to, a T&. Unlike a reference,
888 // ReferenceWrapper<T> can be copied and can survive template type
889 // inference. This is used to support by-reference arguments in the
890 // InvokeArgument<N>(...) action. The idea was from "reference
891 // wrappers" in tr1, which we don't have in our source tree yet.
892 template <typename T>
893 class ReferenceWrapper
894 {
895 public:
896  // Constructs a ReferenceWrapper<T> object from a T&.
897  explicit ReferenceWrapper(T & l_value) : pointer_(&l_value) {} // NOLINT
898 
899  // Allows a ReferenceWrapper<T> object to be implicitly converted to
900  // a T&.
901  operator T & () const { return *pointer_; }
902 private:
903  T * pointer_;
904 };
905 
906 // Allows the expression ByRef(x) to be printed as a reference to x.
907 template <typename T>
908 void PrintTo(const ReferenceWrapper<T> & ref, ::std::ostream * os)
909 {
910  T & value = ref;
911  UniversalPrinter<T &>::Print(value, os);
912 }
913 
914 // Does two actions sequentially. Used for implementing the DoAll(a1,
915 // a2, ...) action.
916 template <typename Action1, typename Action2>
917 class DoBothAction
918 {
919 public:
920  DoBothAction(Action1 action1, Action2 action2)
921  : action1_(action1), action2_(action2) {}
922 
923  // This template type conversion operator allows DoAll(a1, ..., a_n)
924  // to be used in ANY function of compatible type.
925  template <typename F>
926  operator Action<F>() const
927  {
928  return Action<F>(new Impl<F>(action1_, action2_));
929  }
930 
931 private:
932  // Implements the DoAll(...) action for a particular function type F.
933  template <typename F>
934  class Impl : public ActionInterface<F>
935  {
936  public:
937  typedef typename Function<F>::Result Result;
940 
941  Impl(const Action<VoidResult> & action1, const Action<F> & action2)
942  : action1_(action1), action2_(action2) {}
943 
944  virtual Result Perform(const ArgumentTuple & args)
945  {
946  action1_.Perform(args);
947  return action2_.Perform(args);
948  }
949 
950  private:
951  const Action<VoidResult> action1_;
952  const Action<F> action2_;
953 
955  };
956 
957  Action1 action1_;
958  Action2 action2_;
959 
961 };
962 
963 } // namespace internal
964 
965 // An Unused object can be implicitly constructed from ANY value.
966 // This is handy when defining actions that ignore some or all of the
967 // mock function arguments. For example, given
968 //
969 // MOCK_METHOD3(Foo, double(const string& label, double x, double y));
970 // MOCK_METHOD3(Bar, double(int index, double x, double y));
971 //
972 // instead of
973 //
974 // double DistanceToOriginWithLabel(const string& label, double x, double y) {
975 // return sqrt(x*x + y*y);
976 // }
977 // double DistanceToOriginWithIndex(int index, double x, double y) {
978 // return sqrt(x*x + y*y);
979 // }
980 // ...
981 // EXEPCT_CALL(mock, Foo("abc", _, _))
982 // .WillOnce(Invoke(DistanceToOriginWithLabel));
983 // EXEPCT_CALL(mock, Bar(5, _, _))
984 // .WillOnce(Invoke(DistanceToOriginWithIndex));
985 //
986 // you could write
987 //
988 // // We can declare any uninteresting argument as Unused.
989 // double DistanceToOrigin(Unused, double x, double y) {
990 // return sqrt(x*x + y*y);
991 // }
992 // ...
993 // EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
994 // EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
996 
997 // This constructor allows us to turn an Action<From> object into an
998 // Action<To>, as long as To's arguments can be implicitly converted
999 // to From's and From's return type cann be implicitly converted to
1000 // To's.
1001 template <typename To>
1002 template <typename From>
1003 Action<To>::Action(const Action<From> & from)
1004  : impl_(new internal::ActionAdaptor<To, From>(from)) {}
1005 
1006 // Creates an action that returns 'value'. 'value' is passed by value
1007 // instead of const reference - otherwise Return("string literal")
1008 // will trigger a compiler error about using array as initializer.
1009 template <typename R>
1011 {
1012  return internal::ReturnAction<R>(value);
1013 }
1014 
1015 // Creates an action that returns NULL.
1017 {
1019 }
1020 
1021 // Creates an action that returns from a void function.
1023 {
1025 }
1026 
1027 // Creates an action that returns the reference to a variable.
1028 template <typename R>
1029 inline internal::ReturnRefAction<R> ReturnRef(R & x) // NOLINT
1030 {
1031  return internal::ReturnRefAction<R>(x);
1032 }
1033 
1034 // Creates an action that returns the reference to a copy of the
1035 // argument. The copy is created when the action is constructed and
1036 // lives as long as the action.
1037 template <typename R>
1039 {
1041 }
1042 
1043 // Creates an action that does the default action for the give mock function.
1045 {
1046  return internal::DoDefaultAction();
1047 }
1048 
1049 // Creates an action that sets the variable pointed by the N-th
1050 // (0-based) function argument to 'value'.
1051 template <size_t N, typename T>
1055 SetArgPointee(const T & x)
1056 {
1059 }
1060 
1061 #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
1062 // This overload allows SetArgPointee() to accept a string literal.
1063 // GCC prior to the version 4.0 and Symbian C++ compiler cannot distinguish
1064 // this overload from the templated version and emit a compile error.
1065 template <size_t N>
1068 SetArgPointee(const char * p)
1069 {
1071  N, const char *, false > (p));
1072 }
1073 
1074 template <size_t N>
1077 SetArgPointee(const wchar_t * p)
1078 {
1080  N, const wchar_t *, false > (p));
1081 }
1082 #endif
1083 
1084 // The following version is DEPRECATED.
1085 template <size_t N, typename T>
1089 SetArgumentPointee(const T & x)
1090 {
1093 }
1094 
1095 // Creates an action that sets a pointer referent to a given value.
1096 template <typename T1, typename T2>
1098 {
1100 }
1101 
1102 #if !GTEST_OS_WINDOWS_MOBILE
1103 
1104 // Creates an action that sets errno and returns the appropriate error.
1105 template <typename T>
1107 SetErrnoAndReturn(int errval, T result)
1108 {
1109  return MakePolymorphicAction(
1110  internal::SetErrnoAndReturnAction<T>(errval, result));
1111 }
1112 
1113 #endif // !GTEST_OS_WINDOWS_MOBILE
1114 
1115 // Various overloads for InvokeWithoutArgs().
1116 
1117 // Creates an action that invokes 'function_impl' with no argument.
1118 template <typename FunctionImpl>
1120 InvokeWithoutArgs(FunctionImpl function_impl)
1121 {
1122  return MakePolymorphicAction(
1124 }
1125 
1126 // Creates an action that invokes the given method on the given object
1127 // with no argument.
1128 template <class Class, typename MethodPtr>
1130 InvokeWithoutArgs(Class * obj_ptr, MethodPtr method_ptr)
1131 {
1132  return MakePolymorphicAction(
1134  obj_ptr, method_ptr));
1135 }
1136 
1137 // Creates an action that performs an_action and throws away its
1138 // result. In other words, it changes the return type of an_action to
1139 // void. an_action MUST NOT return void, or the code won't compile.
1140 template <typename A>
1141 inline internal::IgnoreResultAction<A> IgnoreResult(const A & an_action)
1142 {
1143  return internal::IgnoreResultAction<A>(an_action);
1144 }
1145 
1146 // Creates a reference wrapper for the given L-value. If necessary,
1147 // you can explicitly specify the type of the reference. For example,
1148 // suppose 'derived' is an object of type Derived, ByRef(derived)
1149 // would wrap a Derived&. If you want to wrap a const Base& instead,
1150 // where Base is a base class of Derived, just write:
1151 //
1152 // ByRef<const Base>(derived)
1153 template <typename T>
1154 inline internal::ReferenceWrapper<T> ByRef(T & l_value) // NOLINT
1155 {
1156  return internal::ReferenceWrapper<T>(l_value);
1157 }
1158 
1159 } // namespace testing
1160 
1161 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
TypeWithSize< 8 >::Int Int64
internal::Function< F >::Result Result
static Result Perform(const ArgumentTuple &)
virtual Result Perform(const ArgumentTuple &args)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
internal::Function< F >::ArgumentTuple ArgumentTuple
internal::IgnoredValue Unused
virtual Result Perform(const ArgumentTuple &)
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgumentPointee(const T &x)
static void Set(T x)
internal::Function< F1 >::ArgumentTuple ArgumentTuple
AssignAction(T1 *ptr, T2 value)
PolymorphicAction< Impl > MakePolymorphicAction(const Impl &impl)
PolymorphicAction(const Impl &impl)
DoBothAction(Action1 action1, Action2 action2)
InvokeMethodWithoutArgsAction(Class *obj_ptr, MethodPtr method_ptr)
TypeWithSize< 8 >::UInt UInt64
virtual Result Perform(const ArgumentTuple &args)
#define GTEST_COMPILE_ASSERT_(expr, msg)
Function< F >::MakeResultVoid VoidResult
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
virtual Result Perform(const ArgumentTuple &)
internal::ReturnRefAction< R > ReturnRef(R &x)
Function< F >::ArgumentTuple ArgumentTuple
static void Perform(const ArgumentTuple &)
Function< F >::ArgumentTuple ArgumentTuple
Function< F >::ArgumentTuple ArgumentTuple
internal::Function< F >::MakeResultIgnoredValue OriginalFunction
internal::Function< F >::ArgumentTuple ArgumentTuple
Action< F > MakeAction(ActionInterface< F > *impl)
virtual void Perform(const ArgumentTuple &args)
virtual Result Perform(const ArgumentTuple &)
Action(ActionInterface< F > *impl)
static bool Exists()
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgPointee(const T &x)
InvokeWithoutArgsAction(FunctionImpl function_impl)
Impl(const Action< VoidResult > &action1, const Action< F > &action2)
SetErrnoAndReturnAction(int errno_value, T result)
#define GTEST_DISALLOW_ASSIGN_(type)
internal::Function< F >::Result Result
internal::Function< F >::ArgumentTuple ArgumentTuple
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void,)
internal::Function< F >::Result Result
void Perform(const ArgumentTuple &args) const
internal::Function< F1 >::Result Result
PolymorphicAction< internal::ReturnNullAction > ReturnNull()
bool IsDoDefault() const
internal::Function< F >::ArgumentTuple ArgumentTuple
internal::DoDefaultAction DoDefault()
void Assert(bool condition, const char *file, int line)
Result Perform(const ArgumentTuple &args) const
ActionAdaptor(const Action< F2 > &from)
Matcher< T > A()
Result Perform(const ArgumentTuple &) const
Function< F >::ArgumentTuple ArgumentTuple
internal::ReferenceWrapper< T > ByRef(T &l_value)
Result Perform(const ArgumentTuple &)
virtual Result Perform(const ArgumentTuple &args)
Result Perform(const ArgumentTuple &) const
PolymorphicAction< internal::AssignAction< T1, T2 > > Assign(T1 *ptr, T2 val)
internal::Function< F >::Result Result
void Perform(const ArgumentTuple &) const
internal::IgnoreResultAction< A > IgnoreResult(const A &an_action)
PolymorphicAction< internal::SetErrnoAndReturnAction< T > > SetErrnoAndReturn(int errval, T result)
static void Print(const T &value,::std::ostream *os)
Action(const Action &action)
internal::ReturnRefOfCopyAction< R > ReturnRefOfCopy(const R &x)
void PrintTo(const ReferenceWrapper< T > &ref,::std::ostream *os)
internal::ReturnAction< R > Return(R value)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:05