gmock-actions.h 40 KB

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