gmock-actions.h 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // The ACTION* family of macros can be used in a namespace scope to
  32. // define custom actions easily. The syntax:
  33. //
  34. // ACTION(name) { statements; }
  35. //
  36. // will define an action with the given name that executes the
  37. // statements. The value returned by the statements will be used as
  38. // the return value of the action. Inside the statements, you can
  39. // refer to the K-th (0-based) argument of the mock function by
  40. // 'argK', and refer to its type by 'argK_type'. For example:
  41. //
  42. // ACTION(IncrementArg1) {
  43. // arg1_type temp = arg1;
  44. // return ++(*temp);
  45. // }
  46. //
  47. // allows you to write
  48. //
  49. // ...WillOnce(IncrementArg1());
  50. //
  51. // You can also refer to the entire argument tuple and its type by
  52. // 'args' and 'args_type', and refer to the mock function type and its
  53. // return type by 'function_type' and 'return_type'.
  54. //
  55. // Note that you don't need to specify the types of the mock function
  56. // arguments. However rest assured that your code is still type-safe:
  57. // you'll get a compiler error if *arg1 doesn't support the ++
  58. // operator, or if the type of ++(*arg1) isn't compatible with the
  59. // mock function's return type, for example.
  60. //
  61. // Sometimes you'll want to parameterize the action. For that you can use
  62. // another macro:
  63. //
  64. // ACTION_P(name, param_name) { statements; }
  65. //
  66. // For example:
  67. //
  68. // ACTION_P(Add, n) { return arg0 + n; }
  69. //
  70. // will allow you to write:
  71. //
  72. // ...WillOnce(Add(5));
  73. //
  74. // Note that you don't need to provide the type of the parameter
  75. // either. If you need to reference the type of a parameter named
  76. // 'foo', you can write 'foo_type'. For example, in the body of
  77. // ACTION_P(Add, n) above, you can write 'n_type' to refer to the type
  78. // of 'n'.
  79. //
  80. // We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P10 to support
  81. // multi-parameter actions.
  82. //
  83. // For the purpose of typing, you can view
  84. //
  85. // ACTION_Pk(Foo, p1, ..., pk) { ... }
  86. //
  87. // as shorthand for
  88. //
  89. // template <typename p1_type, ..., typename pk_type>
  90. // FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... }
  91. //
  92. // In particular, you can provide the template type arguments
  93. // explicitly when invoking Foo(), as in Foo<long, bool>(5, false);
  94. // although usually you can rely on the compiler to infer the types
  95. // for you automatically. You can assign the result of expression
  96. // Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ...,
  97. // pk_type>. This can be useful when composing actions.
  98. //
  99. // You can also overload actions with different numbers of parameters:
  100. //
  101. // ACTION_P(Plus, a) { ... }
  102. // ACTION_P2(Plus, a, b) { ... }
  103. //
  104. // While it's tempting to always use the ACTION* macros when defining
  105. // a new action, you should also consider implementing ActionInterface
  106. // or using MakePolymorphicAction() instead, especially if you need to
  107. // use the action a lot. While these approaches require more work,
  108. // they give you more control on the types of the mock function
  109. // arguments and the action parameters, which in general leads to
  110. // better compiler error messages that pay off in the long run. They
  111. // also allow overloading actions based on parameter types (as opposed
  112. // to just based on the number of parameters).
  113. //
  114. // CAVEAT:
  115. //
  116. // ACTION*() can only be used in a namespace scope as templates cannot be
  117. // declared inside of a local class.
  118. // Users can, however, define any local functors (e.g. a lambda) that
  119. // can be used as actions.
  120. //
  121. // MORE INFORMATION:
  122. //
  123. // To learn more about using these macros, please search for 'ACTION' on
  124. // https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md
  125. // IWYU pragma: private, include "gmock/gmock.h"
  126. // IWYU pragma: friend gmock/.*
  127. #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
  128. #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
  129. #ifndef _WIN32_WCE
  130. # include <errno.h>
  131. #endif
  132. #include <algorithm>
  133. #include <functional>
  134. #include <memory>
  135. #include <string>
  136. #include <tuple>
  137. #include <type_traits>
  138. #include <utility>
  139. #include "gmock/internal/gmock-internal-utils.h"
  140. #include "gmock/internal/gmock-port.h"
  141. #include "gmock/internal/gmock-pp.h"
  142. #ifdef _MSC_VER
  143. # pragma warning(push)
  144. # pragma warning(disable:4100)
  145. #endif
  146. namespace testing {
  147. // To implement an action Foo, define:
  148. // 1. a class FooAction that implements the ActionInterface interface, and
  149. // 2. a factory function that creates an Action object from a
  150. // const FooAction*.
  151. //
  152. // The two-level delegation design follows that of Matcher, providing
  153. // consistency for extension developers. It also eases ownership
  154. // management as Action objects can now be copied like plain values.
  155. namespace internal {
  156. // BuiltInDefaultValueGetter<T, true>::Get() returns a
  157. // default-constructed T value. BuiltInDefaultValueGetter<T,
  158. // false>::Get() crashes with an error.
  159. //
  160. // This primary template is used when kDefaultConstructible is true.
  161. template <typename T, bool kDefaultConstructible>
  162. struct BuiltInDefaultValueGetter {
  163. static T Get() { return T(); }
  164. };
  165. template <typename T>
  166. struct BuiltInDefaultValueGetter<T, false> {
  167. static T Get() {
  168. Assert(false, __FILE__, __LINE__,
  169. "Default action undefined for the function return type.");
  170. return internal::Invalid<T>();
  171. // The above statement will never be reached, but is required in
  172. // order for this function to compile.
  173. }
  174. };
  175. // BuiltInDefaultValue<T>::Get() returns the "built-in" default value
  176. // for type T, which is NULL when T is a raw pointer type, 0 when T is
  177. // a numeric type, false when T is bool, or "" when T is string or
  178. // std::string. In addition, in C++11 and above, it turns a
  179. // default-constructed T value if T is default constructible. For any
  180. // other type T, the built-in default T value is undefined, and the
  181. // function will abort the process.
  182. template <typename T>
  183. class BuiltInDefaultValue {
  184. public:
  185. // This function returns true if and only if type T has a built-in default
  186. // value.
  187. static bool Exists() {
  188. return ::std::is_default_constructible<T>::value;
  189. }
  190. static T Get() {
  191. return BuiltInDefaultValueGetter<
  192. T, ::std::is_default_constructible<T>::value>::Get();
  193. }
  194. };
  195. // This partial specialization says that we use the same built-in
  196. // default value for T and const T.
  197. template <typename T>
  198. class BuiltInDefaultValue<const T> {
  199. public:
  200. static bool Exists() { return BuiltInDefaultValue<T>::Exists(); }
  201. static T Get() { return BuiltInDefaultValue<T>::Get(); }
  202. };
  203. // This partial specialization defines the default values for pointer
  204. // types.
  205. template <typename T>
  206. class BuiltInDefaultValue<T*> {
  207. public:
  208. static bool Exists() { return true; }
  209. static T* Get() { return nullptr; }
  210. };
  211. // The following specializations define the default values for
  212. // specific types we care about.
  213. #define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \
  214. template <> \
  215. class BuiltInDefaultValue<type> { \
  216. public: \
  217. static bool Exists() { return true; } \
  218. static type Get() { return value; } \
  219. }
  220. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, ); // NOLINT
  221. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, "");
  222. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false);
  223. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0');
  224. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0');
  225. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0');
  226. // There's no need for a default action for signed wchar_t, as that
  227. // type is the same as wchar_t for gcc, and invalid for MSVC.
  228. //
  229. // There's also no need for a default action for unsigned wchar_t, as
  230. // that type is the same as unsigned int for gcc, and invalid for
  231. // MSVC.
  232. #if GMOCK_WCHAR_T_IS_NATIVE_
  233. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT
  234. #endif
  235. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U); // NOLINT
  236. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0); // NOLINT
  237. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U);
  238. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0);
  239. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL); // NOLINT
  240. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L); // NOLINT
  241. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long long, 0); // NOLINT
  242. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long long, 0); // NOLINT
  243. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0);
  244. GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0);
  245. #undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
  246. // Simple two-arg form of std::disjunction.
  247. template <typename P, typename Q>
  248. using disjunction = typename ::std::conditional<P::value, P, Q>::type;
  249. } // namespace internal
  250. // When an unexpected function call is encountered, Google Mock will
  251. // let it return a default value if the user has specified one for its
  252. // return type, or if the return type has a built-in default value;
  253. // otherwise Google Mock won't know what value to return and will have
  254. // to abort the process.
  255. //
  256. // The DefaultValue<T> class allows a user to specify the
  257. // default value for a type T that is both copyable and publicly
  258. // destructible (i.e. anything that can be used as a function return
  259. // type). The usage is:
  260. //
  261. // // Sets the default value for type T to be foo.
  262. // DefaultValue<T>::Set(foo);
  263. template <typename T>
  264. class DefaultValue {
  265. public:
  266. // Sets the default value for type T; requires T to be
  267. // copy-constructable and have a public destructor.
  268. static void Set(T x) {
  269. delete producer_;
  270. producer_ = new FixedValueProducer(x);
  271. }
  272. // Provides a factory function to be called to generate the default value.
  273. // This method can be used even if T is only move-constructible, but it is not
  274. // limited to that case.
  275. typedef T (*FactoryFunction)();
  276. static void SetFactory(FactoryFunction factory) {
  277. delete producer_;
  278. producer_ = new FactoryValueProducer(factory);
  279. }
  280. // Unsets the default value for type T.
  281. static void Clear() {
  282. delete producer_;
  283. producer_ = nullptr;
  284. }
  285. // Returns true if and only if the user has set the default value for type T.
  286. static bool IsSet() { return producer_ != nullptr; }
  287. // Returns true if T has a default return value set by the user or there
  288. // exists a built-in default value.
  289. static bool Exists() {
  290. return IsSet() || internal::BuiltInDefaultValue<T>::Exists();
  291. }
  292. // Returns the default value for type T if the user has set one;
  293. // otherwise returns the built-in default value. Requires that Exists()
  294. // is true, which ensures that the return value is well-defined.
  295. static T Get() {
  296. return producer_ == nullptr ? internal::BuiltInDefaultValue<T>::Get()
  297. : producer_->Produce();
  298. }
  299. private:
  300. class ValueProducer {
  301. public:
  302. virtual ~ValueProducer() {}
  303. virtual T Produce() = 0;
  304. };
  305. class FixedValueProducer : public ValueProducer {
  306. public:
  307. explicit FixedValueProducer(T value) : value_(value) {}
  308. T Produce() override { return value_; }
  309. private:
  310. const T value_;
  311. GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer);
  312. };
  313. class FactoryValueProducer : public ValueProducer {
  314. public:
  315. explicit FactoryValueProducer(FactoryFunction factory)
  316. : factory_(factory) {}
  317. T Produce() override { return factory_(); }
  318. private:
  319. const FactoryFunction factory_;
  320. GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer);
  321. };
  322. static ValueProducer* producer_;
  323. };
  324. // This partial specialization allows a user to set default values for
  325. // reference types.
  326. template <typename T>
  327. class DefaultValue<T&> {
  328. public:
  329. // Sets the default value for type T&.
  330. static void Set(T& x) { // NOLINT
  331. address_ = &x;
  332. }
  333. // Unsets the default value for type T&.
  334. static void Clear() { address_ = nullptr; }
  335. // Returns true if and only if the user has set the default value for type T&.
  336. static bool IsSet() { return address_ != nullptr; }
  337. // Returns true if T has a default return value set by the user or there
  338. // exists a built-in default value.
  339. static bool Exists() {
  340. return IsSet() || internal::BuiltInDefaultValue<T&>::Exists();
  341. }
  342. // Returns the default value for type T& if the user has set one;
  343. // otherwise returns the built-in default value if there is one;
  344. // otherwise aborts the process.
  345. static T& Get() {
  346. return address_ == nullptr ? internal::BuiltInDefaultValue<T&>::Get()
  347. : *address_;
  348. }
  349. private:
  350. static T* address_;
  351. };
  352. // This specialization allows DefaultValue<void>::Get() to
  353. // compile.
  354. template <>
  355. class DefaultValue<void> {
  356. public:
  357. static bool Exists() { return true; }
  358. static void Get() {}
  359. };
  360. // Points to the user-set default value for type T.
  361. template <typename T>
  362. typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = nullptr;
  363. // Points to the user-set default value for type T&.
  364. template <typename T>
  365. T* DefaultValue<T&>::address_ = nullptr;
  366. // Implement this interface to define an action for function type F.
  367. template <typename F>
  368. class ActionInterface {
  369. public:
  370. typedef typename internal::Function<F>::Result Result;
  371. typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
  372. ActionInterface() {}
  373. virtual ~ActionInterface() {}
  374. // Performs the action. This method is not const, as in general an
  375. // action can have side effects and be stateful. For example, a
  376. // get-the-next-element-from-the-collection action will need to
  377. // remember the current element.
  378. virtual Result Perform(const ArgumentTuple& args) = 0;
  379. private:
  380. GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface);
  381. };
  382. // An Action<F> is a copyable and IMMUTABLE (except by assignment)
  383. // object that represents an action to be taken when a mock function
  384. // of type F is called. The implementation of Action<T> is just a
  385. // std::shared_ptr to const ActionInterface<T>. Don't inherit from Action!
  386. // You can view an object implementing ActionInterface<F> as a
  387. // concrete action (including its current state), and an Action<F>
  388. // object as a handle to it.
  389. template <typename F>
  390. class Action {
  391. // Adapter class to allow constructing Action from a legacy ActionInterface.
  392. // New code should create Actions from functors instead.
  393. struct ActionAdapter {
  394. // Adapter must be copyable to satisfy std::function requirements.
  395. ::std::shared_ptr<ActionInterface<F>> impl_;
  396. template <typename... Args>
  397. typename internal::Function<F>::Result operator()(Args&&... args) {
  398. return impl_->Perform(
  399. ::std::forward_as_tuple(::std::forward<Args>(args)...));
  400. }
  401. };
  402. template <typename G>
  403. using IsCompatibleFunctor = std::is_constructible<std::function<F>, G>;
  404. public:
  405. typedef typename internal::Function<F>::Result Result;
  406. typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
  407. // Constructs a null Action. Needed for storing Action objects in
  408. // STL containers.
  409. Action() {}
  410. // Construct an Action from a specified callable.
  411. // This cannot take std::function directly, because then Action would not be
  412. // directly constructible from lambda (it would require two conversions).
  413. template <
  414. typename G,
  415. typename = typename std::enable_if<internal::disjunction<
  416. IsCompatibleFunctor<G>, std::is_constructible<std::function<Result()>,
  417. G>>::value>::type>
  418. Action(G&& fun) { // NOLINT
  419. Init(::std::forward<G>(fun), IsCompatibleFunctor<G>());
  420. }
  421. // Constructs an Action from its implementation.
  422. explicit Action(ActionInterface<F>* impl)
  423. : fun_(ActionAdapter{::std::shared_ptr<ActionInterface<F>>(impl)}) {}
  424. // This constructor allows us to turn an Action<Func> object into an
  425. // Action<F>, as long as F's arguments can be implicitly converted
  426. // to Func's and Func's return type can be implicitly converted to F's.
  427. template <typename Func>
  428. explicit Action(const Action<Func>& action) : fun_(action.fun_) {}
  429. // Returns true if and only if this is the DoDefault() action.
  430. bool IsDoDefault() const { return fun_ == nullptr; }
  431. // Performs the action. Note that this method is const even though
  432. // the corresponding method in ActionInterface is not. The reason
  433. // is that a const Action<F> means that it cannot be re-bound to
  434. // another concrete action, not that the concrete action it binds to
  435. // cannot change state. (Think of the difference between a const
  436. // pointer and a pointer to const.)
  437. Result Perform(ArgumentTuple args) const {
  438. if (IsDoDefault()) {
  439. internal::IllegalDoDefault(__FILE__, __LINE__);
  440. }
  441. return internal::Apply(fun_, ::std::move(args));
  442. }
  443. private:
  444. template <typename G>
  445. friend class Action;
  446. template <typename G>
  447. void Init(G&& g, ::std::true_type) {
  448. fun_ = ::std::forward<G>(g);
  449. }
  450. template <typename G>
  451. void Init(G&& g, ::std::false_type) {
  452. fun_ = IgnoreArgs<typename ::std::decay<G>::type>{::std::forward<G>(g)};
  453. }
  454. template <typename FunctionImpl>
  455. struct IgnoreArgs {
  456. template <typename... Args>
  457. Result operator()(const Args&...) const {
  458. return function_impl();
  459. }
  460. FunctionImpl function_impl;
  461. };
  462. // fun_ is an empty function if and only if this is the DoDefault() action.
  463. ::std::function<F> fun_;
  464. };
  465. // The PolymorphicAction class template makes it easy to implement a
  466. // polymorphic action (i.e. an action that can be used in mock
  467. // functions of than one type, e.g. Return()).
  468. //
  469. // To define a polymorphic action, a user first provides a COPYABLE
  470. // implementation class that has a Perform() method template:
  471. //
  472. // class FooAction {
  473. // public:
  474. // template <typename Result, typename ArgumentTuple>
  475. // Result Perform(const ArgumentTuple& args) const {
  476. // // Processes the arguments and returns a result, using
  477. // // std::get<N>(args) to get the N-th (0-based) argument in the tuple.
  478. // }
  479. // ...
  480. // };
  481. //
  482. // Then the user creates the polymorphic action using
  483. // MakePolymorphicAction(object) where object has type FooAction. See
  484. // the definition of Return(void) and SetArgumentPointee<N>(value) for
  485. // complete examples.
  486. template <typename Impl>
  487. class PolymorphicAction {
  488. public:
  489. explicit PolymorphicAction(const Impl& impl) : impl_(impl) {}
  490. template <typename F>
  491. operator Action<F>() const {
  492. return Action<F>(new MonomorphicImpl<F>(impl_));
  493. }
  494. private:
  495. template <typename F>
  496. class MonomorphicImpl : public ActionInterface<F> {
  497. public:
  498. typedef typename internal::Function<F>::Result Result;
  499. typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
  500. explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
  501. Result Perform(const ArgumentTuple& args) override {
  502. return impl_.template Perform<Result>(args);
  503. }
  504. private:
  505. Impl impl_;
  506. };
  507. Impl impl_;
  508. };
  509. // Creates an Action from its implementation and returns it. The
  510. // created Action object owns the implementation.
  511. template <typename F>
  512. Action<F> MakeAction(ActionInterface<F>* impl) {
  513. return Action<F>(impl);
  514. }
  515. // Creates a polymorphic action from its implementation. This is
  516. // easier to use than the PolymorphicAction<Impl> constructor as it
  517. // doesn't require you to explicitly write the template argument, e.g.
  518. //
  519. // MakePolymorphicAction(foo);
  520. // vs
  521. // PolymorphicAction<TypeOfFoo>(foo);
  522. template <typename Impl>
  523. inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) {
  524. return PolymorphicAction<Impl>(impl);
  525. }
  526. namespace internal {
  527. // Helper struct to specialize ReturnAction to execute a move instead of a copy
  528. // on return. Useful for move-only types, but could be used on any type.
  529. template <typename T>
  530. struct ByMoveWrapper {
  531. explicit ByMoveWrapper(T value) : payload(std::move(value)) {}
  532. T payload;
  533. };
  534. // Implements the polymorphic Return(x) action, which can be used in
  535. // any function that returns the type of x, regardless of the argument
  536. // types.
  537. //
  538. // Note: The value passed into Return must be converted into
  539. // Function<F>::Result when this action is cast to Action<F> rather than
  540. // when that action is performed. This is important in scenarios like
  541. //
  542. // MOCK_METHOD1(Method, T(U));
  543. // ...
  544. // {
  545. // Foo foo;
  546. // X x(&foo);
  547. // EXPECT_CALL(mock, Method(_)).WillOnce(Return(x));
  548. // }
  549. //
  550. // In the example above the variable x holds reference to foo which leaves
  551. // scope and gets destroyed. If copying X just copies a reference to foo,
  552. // that copy will be left with a hanging reference. If conversion to T
  553. // makes a copy of foo, the above code is safe. To support that scenario, we
  554. // need to make sure that the type conversion happens inside the EXPECT_CALL
  555. // statement, and conversion of the result of Return to Action<T(U)> is a
  556. // good place for that.
  557. //
  558. // The real life example of the above scenario happens when an invocation
  559. // of gtl::Container() is passed into Return.
  560. //
  561. template <typename R>
  562. class ReturnAction {
  563. public:
  564. // Constructs a ReturnAction object from the value to be returned.
  565. // 'value' is passed by value instead of by const reference in order
  566. // to allow Return("string literal") to compile.
  567. explicit ReturnAction(R value) : value_(new R(std::move(value))) {}
  568. // This template type conversion operator allows Return(x) to be
  569. // used in ANY function that returns x's type.
  570. template <typename F>
  571. operator Action<F>() const { // NOLINT
  572. // Assert statement belongs here because this is the best place to verify
  573. // conditions on F. It produces the clearest error messages
  574. // in most compilers.
  575. // Impl really belongs in this scope as a local class but can't
  576. // because MSVC produces duplicate symbols in different translation units
  577. // in this case. Until MS fixes that bug we put Impl into the class scope
  578. // and put the typedef both here (for use in assert statement) and
  579. // in the Impl class. But both definitions must be the same.
  580. typedef typename Function<F>::Result Result;
  581. GTEST_COMPILE_ASSERT_(
  582. !std::is_reference<Result>::value,
  583. use_ReturnRef_instead_of_Return_to_return_a_reference);
  584. static_assert(!std::is_void<Result>::value,
  585. "Can't use Return() on an action expected to return `void`.");
  586. return Action<F>(new Impl<R, F>(value_));
  587. }
  588. private:
  589. // Implements the Return(x) action for a particular function type F.
  590. template <typename R_, typename F>
  591. class Impl : public ActionInterface<F> {
  592. public:
  593. typedef typename Function<F>::Result Result;
  594. typedef typename Function<F>::ArgumentTuple ArgumentTuple;
  595. // The implicit cast is necessary when Result has more than one
  596. // single-argument constructor (e.g. Result is std::vector<int>) and R
  597. // has a type conversion operator template. In that case, value_(value)
  598. // won't compile as the compiler doesn't known which constructor of
  599. // Result to call. ImplicitCast_ forces the compiler to convert R to
  600. // Result without considering explicit constructors, thus resolving the
  601. // ambiguity. value_ is then initialized using its copy constructor.
  602. explicit Impl(const std::shared_ptr<R>& value)
  603. : value_before_cast_(*value),
  604. value_(ImplicitCast_<Result>(value_before_cast_)) {}
  605. Result Perform(const ArgumentTuple&) override { return value_; }
  606. private:
  607. GTEST_COMPILE_ASSERT_(!std::is_reference<Result>::value,
  608. Result_cannot_be_a_reference_type);
  609. // We save the value before casting just in case it is being cast to a
  610. // wrapper type.
  611. R value_before_cast_;
  612. Result value_;
  613. GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
  614. };
  615. // Partially specialize for ByMoveWrapper. This version of ReturnAction will
  616. // move its contents instead.
  617. template <typename R_, typename F>
  618. class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> {
  619. public:
  620. typedef typename Function<F>::Result Result;
  621. typedef typename Function<F>::ArgumentTuple ArgumentTuple;
  622. explicit Impl(const std::shared_ptr<R>& wrapper)
  623. : performed_(false), wrapper_(wrapper) {}
  624. Result Perform(const ArgumentTuple&) override {
  625. GTEST_CHECK_(!performed_)
  626. << "A ByMove() action should only be performed once.";
  627. performed_ = true;
  628. return std::move(wrapper_->payload);
  629. }
  630. private:
  631. bool performed_;
  632. const std::shared_ptr<R> wrapper_;
  633. };
  634. const std::shared_ptr<R> value_;
  635. };
  636. // Implements the ReturnNull() action.
  637. class ReturnNullAction {
  638. public:
  639. // Allows ReturnNull() to be used in any pointer-returning function. In C++11
  640. // this is enforced by returning nullptr, and in non-C++11 by asserting a
  641. // pointer type on compile time.
  642. template <typename Result, typename ArgumentTuple>
  643. static Result Perform(const ArgumentTuple&) {
  644. return nullptr;
  645. }
  646. };
  647. // Implements the Return() action.
  648. class ReturnVoidAction {
  649. public:
  650. // Allows Return() to be used in any void-returning function.
  651. template <typename Result, typename ArgumentTuple>
  652. static void Perform(const ArgumentTuple&) {
  653. static_assert(std::is_void<Result>::value, "Result should be void.");
  654. }
  655. };
  656. // Implements the polymorphic ReturnRef(x) action, which can be used
  657. // in any function that returns a reference to the type of x,
  658. // regardless of the argument types.
  659. template <typename T>
  660. class ReturnRefAction {
  661. public:
  662. // Constructs a ReturnRefAction object from the reference to be returned.
  663. explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT
  664. // This template type conversion operator allows ReturnRef(x) to be
  665. // used in ANY function that returns a reference to x's type.
  666. template <typename F>
  667. operator Action<F>() const {
  668. typedef typename Function<F>::Result Result;
  669. // Asserts that the function return type is a reference. This
  670. // catches the user error of using ReturnRef(x) when Return(x)
  671. // should be used, and generates some helpful error message.
  672. GTEST_COMPILE_ASSERT_(std::is_reference<Result>::value,
  673. use_Return_instead_of_ReturnRef_to_return_a_value);
  674. return Action<F>(new Impl<F>(ref_));
  675. }
  676. private:
  677. // Implements the ReturnRef(x) action for a particular function type F.
  678. template <typename F>
  679. class Impl : public ActionInterface<F> {
  680. public:
  681. typedef typename Function<F>::Result Result;
  682. typedef typename Function<F>::ArgumentTuple ArgumentTuple;
  683. explicit Impl(T& ref) : ref_(ref) {} // NOLINT
  684. Result Perform(const ArgumentTuple&) override { return ref_; }
  685. private:
  686. T& ref_;
  687. };
  688. T& ref_;
  689. };
  690. // Implements the polymorphic ReturnRefOfCopy(x) action, which can be
  691. // used in any function that returns a reference to the type of x,
  692. // regardless of the argument types.
  693. template <typename T>
  694. class ReturnRefOfCopyAction {
  695. public:
  696. // Constructs a ReturnRefOfCopyAction object from the reference to
  697. // be returned.
  698. explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT
  699. // This template type conversion operator allows ReturnRefOfCopy(x) to be
  700. // used in ANY function that returns a reference to x's type.
  701. template <typename F>
  702. operator Action<F>() const {
  703. typedef typename Function<F>::Result Result;
  704. // Asserts that the function return type is a reference. This
  705. // catches the user error of using ReturnRefOfCopy(x) when Return(x)
  706. // should be used, and generates some helpful error message.
  707. GTEST_COMPILE_ASSERT_(
  708. std::is_reference<Result>::value,
  709. use_Return_instead_of_ReturnRefOfCopy_to_return_a_value);
  710. return Action<F>(new Impl<F>(value_));
  711. }
  712. private:
  713. // Implements the ReturnRefOfCopy(x) action for a particular function type F.
  714. template <typename F>
  715. class Impl : public ActionInterface<F> {
  716. public:
  717. typedef typename Function<F>::Result Result;
  718. typedef typename Function<F>::ArgumentTuple ArgumentTuple;
  719. explicit Impl(const T& value) : value_(value) {} // NOLINT
  720. Result Perform(const ArgumentTuple&) override { return value_; }
  721. private:
  722. T value_;
  723. };
  724. const T value_;
  725. };
  726. // Implements the polymorphic ReturnRoundRobin(v) action, which can be
  727. // used in any function that returns the element_type of v.
  728. template <typename T>
  729. class ReturnRoundRobinAction {
  730. public:
  731. explicit ReturnRoundRobinAction(std::vector<T> values) {
  732. GTEST_CHECK_(!values.empty())
  733. << "ReturnRoundRobin requires at least one element.";
  734. state_->values = std::move(values);
  735. }
  736. template <typename... Args>
  737. T operator()(Args&&...) const {
  738. return state_->Next();
  739. }
  740. private:
  741. struct State {
  742. T Next() {
  743. T ret_val = values[i++];
  744. if (i == values.size()) i = 0;
  745. return ret_val;
  746. }
  747. std::vector<T> values;
  748. size_t i = 0;
  749. };
  750. std::shared_ptr<State> state_ = std::make_shared<State>();
  751. };
  752. // Implements the polymorphic DoDefault() action.
  753. class DoDefaultAction {
  754. public:
  755. // This template type conversion operator allows DoDefault() to be
  756. // used in any function.
  757. template <typename F>
  758. operator Action<F>() const { return Action<F>(); } // NOLINT
  759. };
  760. // Implements the Assign action to set a given pointer referent to a
  761. // particular value.
  762. template <typename T1, typename T2>
  763. class AssignAction {
  764. public:
  765. AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {}
  766. template <typename Result, typename ArgumentTuple>
  767. void Perform(const ArgumentTuple& /* args */) const {
  768. *ptr_ = value_;
  769. }
  770. private:
  771. T1* const ptr_;
  772. const T2 value_;
  773. };
  774. #if !GTEST_OS_WINDOWS_MOBILE
  775. // Implements the SetErrnoAndReturn action to simulate return from
  776. // various system calls and libc functions.
  777. template <typename T>
  778. class SetErrnoAndReturnAction {
  779. public:
  780. SetErrnoAndReturnAction(int errno_value, T result)
  781. : errno_(errno_value),
  782. result_(result) {}
  783. template <typename Result, typename ArgumentTuple>
  784. Result Perform(const ArgumentTuple& /* args */) const {
  785. errno = errno_;
  786. return result_;
  787. }
  788. private:
  789. const int errno_;
  790. const T result_;
  791. };
  792. #endif // !GTEST_OS_WINDOWS_MOBILE
  793. // Implements the SetArgumentPointee<N>(x) action for any function
  794. // whose N-th argument (0-based) is a pointer to x's type.
  795. template <size_t N, typename A, typename = void>
  796. struct SetArgumentPointeeAction {
  797. A value;
  798. template <typename... Args>
  799. void operator()(const Args&... args) const {
  800. *::std::get<N>(std::tie(args...)) = value;
  801. }
  802. };
  803. // Implements the Invoke(object_ptr, &Class::Method) action.
  804. template <class Class, typename MethodPtr>
  805. struct InvokeMethodAction {
  806. Class* const obj_ptr;
  807. const MethodPtr method_ptr;
  808. template <typename... Args>
  809. auto operator()(Args&&... args) const
  810. -> decltype((obj_ptr->*method_ptr)(std::forward<Args>(args)...)) {
  811. return (obj_ptr->*method_ptr)(std::forward<Args>(args)...);
  812. }
  813. };
  814. // Implements the InvokeWithoutArgs(f) action. The template argument
  815. // FunctionImpl is the implementation type of f, which can be either a
  816. // function pointer or a functor. InvokeWithoutArgs(f) can be used as an
  817. // Action<F> as long as f's type is compatible with F.
  818. template <typename FunctionImpl>
  819. struct InvokeWithoutArgsAction {
  820. FunctionImpl function_impl;
  821. // Allows InvokeWithoutArgs(f) to be used as any action whose type is
  822. // compatible with f.
  823. template <typename... Args>
  824. auto operator()(const Args&...) -> decltype(function_impl()) {
  825. return function_impl();
  826. }
  827. };
  828. // Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
  829. template <class Class, typename MethodPtr>
  830. struct InvokeMethodWithoutArgsAction {
  831. Class* const obj_ptr;
  832. const MethodPtr method_ptr;
  833. using ReturnType =
  834. decltype((std::declval<Class*>()->*std::declval<MethodPtr>())());
  835. template <typename... Args>
  836. ReturnType operator()(const Args&...) const {
  837. return (obj_ptr->*method_ptr)();
  838. }
  839. };
  840. // Implements the IgnoreResult(action) action.
  841. template <typename A>
  842. class IgnoreResultAction {
  843. public:
  844. explicit IgnoreResultAction(const A& action) : action_(action) {}
  845. template <typename F>
  846. operator Action<F>() const {
  847. // Assert statement belongs here because this is the best place to verify
  848. // conditions on F. It produces the clearest error messages
  849. // in most compilers.
  850. // Impl really belongs in this scope as a local class but can't
  851. // because MSVC produces duplicate symbols in different translation units
  852. // in this case. Until MS fixes that bug we put Impl into the class scope
  853. // and put the typedef both here (for use in assert statement) and
  854. // in the Impl class. But both definitions must be the same.
  855. typedef typename internal::Function<F>::Result Result;
  856. // Asserts at compile time that F returns void.
  857. static_assert(std::is_void<Result>::value, "Result type should be void.");
  858. return Action<F>(new Impl<F>(action_));
  859. }
  860. private:
  861. template <typename F>
  862. class Impl : public ActionInterface<F> {
  863. public:
  864. typedef typename internal::Function<F>::Result Result;
  865. typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
  866. explicit Impl(const A& action) : action_(action) {}
  867. void Perform(const ArgumentTuple& args) override {
  868. // Performs the action and ignores its result.
  869. action_.Perform(args);
  870. }
  871. private:
  872. // Type OriginalFunction is the same as F except that its return
  873. // type is IgnoredValue.
  874. typedef typename internal::Function<F>::MakeResultIgnoredValue
  875. OriginalFunction;
  876. const Action<OriginalFunction> action_;
  877. };
  878. const A action_;
  879. };
  880. template <typename InnerAction, size_t... I>
  881. struct WithArgsAction {
  882. InnerAction action;
  883. // The inner action could be anything convertible to Action<X>.
  884. // We use the conversion operator to detect the signature of the inner Action.
  885. template <typename R, typename... Args>
  886. operator Action<R(Args...)>() const { // NOLINT
  887. using TupleType = std::tuple<Args...>;
  888. Action<R(typename std::tuple_element<I, TupleType>::type...)>
  889. converted(action);
  890. return [converted](Args... args) -> R {
  891. return converted.Perform(std::forward_as_tuple(
  892. std::get<I>(std::forward_as_tuple(std::forward<Args>(args)...))...));
  893. };
  894. }
  895. };
  896. template <typename... Actions>
  897. struct DoAllAction {
  898. private:
  899. template <typename T>
  900. using NonFinalType =
  901. typename std::conditional<std::is_scalar<T>::value, T, const T&>::type;
  902. template <typename ActionT, size_t... I>
  903. std::vector<ActionT> Convert(IndexSequence<I...>) const {
  904. return {ActionT(std::get<I>(actions))...};
  905. }
  906. public:
  907. std::tuple<Actions...> actions;
  908. template <typename R, typename... Args>
  909. operator Action<R(Args...)>() const { // NOLINT
  910. struct Op {
  911. std::vector<Action<void(NonFinalType<Args>...)>> converted;
  912. Action<R(Args...)> last;
  913. R operator()(Args... args) const {
  914. auto tuple_args = std::forward_as_tuple(std::forward<Args>(args)...);
  915. for (auto& a : converted) {
  916. a.Perform(tuple_args);
  917. }
  918. return last.Perform(std::move(tuple_args));
  919. }
  920. };
  921. return Op{Convert<Action<void(NonFinalType<Args>...)>>(
  922. MakeIndexSequence<sizeof...(Actions) - 1>()),
  923. std::get<sizeof...(Actions) - 1>(actions)};
  924. }
  925. };
  926. template <typename T, typename... Params>
  927. struct ReturnNewAction {
  928. T* operator()() const {
  929. return internal::Apply(
  930. [](const Params&... unpacked_params) {
  931. return new T(unpacked_params...);
  932. },
  933. params);
  934. }
  935. std::tuple<Params...> params;
  936. };
  937. template <size_t k>
  938. struct ReturnArgAction {
  939. template <typename... Args>
  940. auto operator()(Args&&... args) const -> decltype(std::get<k>(
  941. std::forward_as_tuple(std::forward<Args>(args)...))) {
  942. return std::get<k>(std::forward_as_tuple(std::forward<Args>(args)...));
  943. }
  944. };
  945. template <size_t k, typename Ptr>
  946. struct SaveArgAction {
  947. Ptr pointer;
  948. template <typename... Args>
  949. void operator()(const Args&... args) const {
  950. *pointer = std::get<k>(std::tie(args...));
  951. }
  952. };
  953. template <size_t k, typename Ptr>
  954. struct SaveArgPointeeAction {
  955. Ptr pointer;
  956. template <typename... Args>
  957. void operator()(const Args&... args) const {
  958. *pointer = *std::get<k>(std::tie(args...));
  959. }
  960. };
  961. template <size_t k, typename T>
  962. struct SetArgRefereeAction {
  963. T value;
  964. template <typename... Args>
  965. void operator()(Args&&... args) const {
  966. using argk_type =
  967. typename ::std::tuple_element<k, std::tuple<Args...>>::type;
  968. static_assert(std::is_lvalue_reference<argk_type>::value,
  969. "Argument must be a reference type.");
  970. std::get<k>(std::tie(args...)) = value;
  971. }
  972. };
  973. template <size_t k, typename I1, typename I2>
  974. struct SetArrayArgumentAction {
  975. I1 first;
  976. I2 last;
  977. template <typename... Args>
  978. void operator()(const Args&... args) const {
  979. auto value = std::get<k>(std::tie(args...));
  980. for (auto it = first; it != last; ++it, (void)++value) {
  981. *value = *it;
  982. }
  983. }
  984. };
  985. template <size_t k>
  986. struct DeleteArgAction {
  987. template <typename... Args>
  988. void operator()(const Args&... args) const {
  989. delete std::get<k>(std::tie(args...));
  990. }
  991. };
  992. template <typename Ptr>
  993. struct ReturnPointeeAction {
  994. Ptr pointer;
  995. template <typename... Args>
  996. auto operator()(const Args&...) const -> decltype(*pointer) {
  997. return *pointer;
  998. }
  999. };
  1000. #if GTEST_HAS_EXCEPTIONS
  1001. template <typename T>
  1002. struct ThrowAction {
  1003. T exception;
  1004. // We use a conversion operator to adapt to any return type.
  1005. template <typename R, typename... Args>
  1006. operator Action<R(Args...)>() const { // NOLINT
  1007. T copy = exception;
  1008. return [copy](Args...) -> R { throw copy; };
  1009. }
  1010. };
  1011. #endif // GTEST_HAS_EXCEPTIONS
  1012. } // namespace internal
  1013. // An Unused object can be implicitly constructed from ANY value.
  1014. // This is handy when defining actions that ignore some or all of the
  1015. // mock function arguments. For example, given
  1016. //
  1017. // MOCK_METHOD3(Foo, double(const string& label, double x, double y));
  1018. // MOCK_METHOD3(Bar, double(int index, double x, double y));
  1019. //
  1020. // instead of
  1021. //
  1022. // double DistanceToOriginWithLabel(const string& label, double x, double y) {
  1023. // return sqrt(x*x + y*y);
  1024. // }
  1025. // double DistanceToOriginWithIndex(int index, double x, double y) {
  1026. // return sqrt(x*x + y*y);
  1027. // }
  1028. // ...
  1029. // EXPECT_CALL(mock, Foo("abc", _, _))
  1030. // .WillOnce(Invoke(DistanceToOriginWithLabel));
  1031. // EXPECT_CALL(mock, Bar(5, _, _))
  1032. // .WillOnce(Invoke(DistanceToOriginWithIndex));
  1033. //
  1034. // you could write
  1035. //
  1036. // // We can declare any uninteresting argument as Unused.
  1037. // double DistanceToOrigin(Unused, double x, double y) {
  1038. // return sqrt(x*x + y*y);
  1039. // }
  1040. // ...
  1041. // EXPECT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin));
  1042. // EXPECT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
  1043. typedef internal::IgnoredValue Unused;
  1044. // Creates an action that does actions a1, a2, ..., sequentially in
  1045. // each invocation. All but the last action will have a readonly view of the
  1046. // arguments.
  1047. template <typename... Action>
  1048. internal::DoAllAction<typename std::decay<Action>::type...> DoAll(
  1049. Action&&... action) {
  1050. return {std::forward_as_tuple(std::forward<Action>(action)...)};
  1051. }
  1052. // WithArg<k>(an_action) creates an action that passes the k-th
  1053. // (0-based) argument of the mock function to an_action and performs
  1054. // it. It adapts an action accepting one argument to one that accepts
  1055. // multiple arguments. For convenience, we also provide
  1056. // WithArgs<k>(an_action) (defined below) as a synonym.
  1057. template <size_t k, typename InnerAction>
  1058. internal::WithArgsAction<typename std::decay<InnerAction>::type, k>
  1059. WithArg(InnerAction&& action) {
  1060. return {std::forward<InnerAction>(action)};
  1061. }
  1062. // WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes
  1063. // the selected arguments of the mock function to an_action and
  1064. // performs it. It serves as an adaptor between actions with
  1065. // different argument lists.
  1066. template <size_t k, size_t... ks, typename InnerAction>
  1067. internal::WithArgsAction<typename std::decay<InnerAction>::type, k, ks...>
  1068. WithArgs(InnerAction&& action) {
  1069. return {std::forward<InnerAction>(action)};
  1070. }
  1071. // WithoutArgs(inner_action) can be used in a mock function with a
  1072. // non-empty argument list to perform inner_action, which takes no
  1073. // argument. In other words, it adapts an action accepting no
  1074. // argument to one that accepts (and ignores) arguments.
  1075. template <typename InnerAction>
  1076. internal::WithArgsAction<typename std::decay<InnerAction>::type>
  1077. WithoutArgs(InnerAction&& action) {
  1078. return {std::forward<InnerAction>(action)};
  1079. }
  1080. // Creates an action that returns 'value'. 'value' is passed by value
  1081. // instead of const reference - otherwise Return("string literal")
  1082. // will trigger a compiler error about using array as initializer.
  1083. template <typename R>
  1084. internal::ReturnAction<R> Return(R value) {
  1085. return internal::ReturnAction<R>(std::move(value));
  1086. }
  1087. // Creates an action that returns NULL.
  1088. inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() {
  1089. return MakePolymorphicAction(internal::ReturnNullAction());
  1090. }
  1091. // Creates an action that returns from a void function.
  1092. inline PolymorphicAction<internal::ReturnVoidAction> Return() {
  1093. return MakePolymorphicAction(internal::ReturnVoidAction());
  1094. }
  1095. // Creates an action that returns the reference to a variable.
  1096. template <typename R>
  1097. inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT
  1098. return internal::ReturnRefAction<R>(x);
  1099. }
  1100. // Prevent using ReturnRef on reference to temporary.
  1101. template <typename R, R* = nullptr>
  1102. internal::ReturnRefAction<R> ReturnRef(R&&) = delete;
  1103. // Creates an action that returns the reference to a copy of the
  1104. // argument. The copy is created when the action is constructed and
  1105. // lives as long as the action.
  1106. template <typename R>
  1107. inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
  1108. return internal::ReturnRefOfCopyAction<R>(x);
  1109. }
  1110. // Modifies the parent action (a Return() action) to perform a move of the
  1111. // argument instead of a copy.
  1112. // Return(ByMove()) actions can only be executed once and will assert this
  1113. // invariant.
  1114. template <typename R>
  1115. internal::ByMoveWrapper<R> ByMove(R x) {
  1116. return internal::ByMoveWrapper<R>(std::move(x));
  1117. }
  1118. // Creates an action that returns an element of `vals`. Calling this action will
  1119. // repeatedly return the next value from `vals` until it reaches the end and
  1120. // will restart from the beginning.
  1121. template <typename T>
  1122. internal::ReturnRoundRobinAction<T> ReturnRoundRobin(std::vector<T> vals) {
  1123. return internal::ReturnRoundRobinAction<T>(std::move(vals));
  1124. }
  1125. // Creates an action that returns an element of `vals`. Calling this action will
  1126. // repeatedly return the next value from `vals` until it reaches the end and
  1127. // will restart from the beginning.
  1128. template <typename T>
  1129. internal::ReturnRoundRobinAction<T> ReturnRoundRobin(
  1130. std::initializer_list<T> vals) {
  1131. return internal::ReturnRoundRobinAction<T>(std::vector<T>(vals));
  1132. }
  1133. // Creates an action that does the default action for the give mock function.
  1134. inline internal::DoDefaultAction DoDefault() {
  1135. return internal::DoDefaultAction();
  1136. }
  1137. // Creates an action that sets the variable pointed by the N-th
  1138. // (0-based) function argument to 'value'.
  1139. template <size_t N, typename T>
  1140. internal::SetArgumentPointeeAction<N, T> SetArgPointee(T value) {
  1141. return {std::move(value)};
  1142. }
  1143. // The following version is DEPRECATED.
  1144. template <size_t N, typename T>
  1145. internal::SetArgumentPointeeAction<N, T> SetArgumentPointee(T value) {
  1146. return {std::move(value)};
  1147. }
  1148. // Creates an action that sets a pointer referent to a given value.
  1149. template <typename T1, typename T2>
  1150. PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) {
  1151. return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
  1152. }
  1153. #if !GTEST_OS_WINDOWS_MOBILE
  1154. // Creates an action that sets errno and returns the appropriate error.
  1155. template <typename T>
  1156. PolymorphicAction<internal::SetErrnoAndReturnAction<T> >
  1157. SetErrnoAndReturn(int errval, T result) {
  1158. return MakePolymorphicAction(
  1159. internal::SetErrnoAndReturnAction<T>(errval, result));
  1160. }
  1161. #endif // !GTEST_OS_WINDOWS_MOBILE
  1162. // Various overloads for Invoke().
  1163. // Legacy function.
  1164. // Actions can now be implicitly constructed from callables. No need to create
  1165. // wrapper objects.
  1166. // This function exists for backwards compatibility.
  1167. template <typename FunctionImpl>
  1168. typename std::decay<FunctionImpl>::type Invoke(FunctionImpl&& function_impl) {
  1169. return std::forward<FunctionImpl>(function_impl);
  1170. }
  1171. // Creates an action that invokes the given method on the given object
  1172. // with the mock function's arguments.
  1173. template <class Class, typename MethodPtr>
  1174. internal::InvokeMethodAction<Class, MethodPtr> Invoke(Class* obj_ptr,
  1175. MethodPtr method_ptr) {
  1176. return {obj_ptr, method_ptr};
  1177. }
  1178. // Creates an action that invokes 'function_impl' with no argument.
  1179. template <typename FunctionImpl>
  1180. internal::InvokeWithoutArgsAction<typename std::decay<FunctionImpl>::type>
  1181. InvokeWithoutArgs(FunctionImpl function_impl) {
  1182. return {std::move(function_impl)};
  1183. }
  1184. // Creates an action that invokes the given method on the given object
  1185. // with no argument.
  1186. template <class Class, typename MethodPtr>
  1187. internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> InvokeWithoutArgs(
  1188. Class* obj_ptr, MethodPtr method_ptr) {
  1189. return {obj_ptr, method_ptr};
  1190. }
  1191. // Creates an action that performs an_action and throws away its
  1192. // result. In other words, it changes the return type of an_action to
  1193. // void. an_action MUST NOT return void, or the code won't compile.
  1194. template <typename A>
  1195. inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) {
  1196. return internal::IgnoreResultAction<A>(an_action);
  1197. }
  1198. // Creates a reference wrapper for the given L-value. If necessary,
  1199. // you can explicitly specify the type of the reference. For example,
  1200. // suppose 'derived' is an object of type Derived, ByRef(derived)
  1201. // would wrap a Derived&. If you want to wrap a const Base& instead,
  1202. // where Base is a base class of Derived, just write:
  1203. //
  1204. // ByRef<const Base>(derived)
  1205. //
  1206. // N.B. ByRef is redundant with std::ref, std::cref and std::reference_wrapper.
  1207. // However, it may still be used for consistency with ByMove().
  1208. template <typename T>
  1209. inline ::std::reference_wrapper<T> ByRef(T& l_value) { // NOLINT
  1210. return ::std::reference_wrapper<T>(l_value);
  1211. }
  1212. // The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new
  1213. // instance of type T, constructed on the heap with constructor arguments
  1214. // a1, a2, ..., and a_k. The caller assumes ownership of the returned value.
  1215. template <typename T, typename... Params>
  1216. internal::ReturnNewAction<T, typename std::decay<Params>::type...> ReturnNew(
  1217. Params&&... params) {
  1218. return {std::forward_as_tuple(std::forward<Params>(params)...)};
  1219. }
  1220. // Action ReturnArg<k>() returns the k-th argument of the mock function.
  1221. template <size_t k>
  1222. internal::ReturnArgAction<k> ReturnArg() {
  1223. return {};
  1224. }
  1225. // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
  1226. // mock function to *pointer.
  1227. template <size_t k, typename Ptr>
  1228. internal::SaveArgAction<k, Ptr> SaveArg(Ptr pointer) {
  1229. return {pointer};
  1230. }
  1231. // Action SaveArgPointee<k>(pointer) saves the value pointed to
  1232. // by the k-th (0-based) argument of the mock function to *pointer.
  1233. template <size_t k, typename Ptr>
  1234. internal::SaveArgPointeeAction<k, Ptr> SaveArgPointee(Ptr pointer) {
  1235. return {pointer};
  1236. }
  1237. // Action SetArgReferee<k>(value) assigns 'value' to the variable
  1238. // referenced by the k-th (0-based) argument of the mock function.
  1239. template <size_t k, typename T>
  1240. internal::SetArgRefereeAction<k, typename std::decay<T>::type> SetArgReferee(
  1241. T&& value) {
  1242. return {std::forward<T>(value)};
  1243. }
  1244. // Action SetArrayArgument<k>(first, last) copies the elements in
  1245. // source range [first, last) to the array pointed to by the k-th
  1246. // (0-based) argument, which can be either a pointer or an
  1247. // iterator. The action does not take ownership of the elements in the
  1248. // source range.
  1249. template <size_t k, typename I1, typename I2>
  1250. internal::SetArrayArgumentAction<k, I1, I2> SetArrayArgument(I1 first,
  1251. I2 last) {
  1252. return {first, last};
  1253. }
  1254. // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
  1255. // function.
  1256. template <size_t k>
  1257. internal::DeleteArgAction<k> DeleteArg() {
  1258. return {};
  1259. }
  1260. // This action returns the value pointed to by 'pointer'.
  1261. template <typename Ptr>
  1262. internal::ReturnPointeeAction<Ptr> ReturnPointee(Ptr pointer) {
  1263. return {pointer};
  1264. }
  1265. // Action Throw(exception) can be used in a mock function of any type
  1266. // to throw the given exception. Any copyable value can be thrown.
  1267. #if GTEST_HAS_EXCEPTIONS
  1268. template <typename T>
  1269. internal::ThrowAction<typename std::decay<T>::type> Throw(T&& exception) {
  1270. return {std::forward<T>(exception)};
  1271. }
  1272. #endif // GTEST_HAS_EXCEPTIONS
  1273. namespace internal {
  1274. // A macro from the ACTION* family (defined later in gmock-generated-actions.h)
  1275. // defines an action that can be used in a mock function. Typically,
  1276. // these actions only care about a subset of the arguments of the mock
  1277. // function. For example, if such an action only uses the second
  1278. // argument, it can be used in any mock function that takes >= 2
  1279. // arguments where the type of the second argument is compatible.
  1280. //
  1281. // Therefore, the action implementation must be prepared to take more
  1282. // arguments than it needs. The ExcessiveArg type is used to
  1283. // represent those excessive arguments. In order to keep the compiler
  1284. // error messages tractable, we define it in the testing namespace
  1285. // instead of testing::internal. However, this is an INTERNAL TYPE
  1286. // and subject to change without notice, so a user MUST NOT USE THIS
  1287. // TYPE DIRECTLY.
  1288. struct ExcessiveArg {};
  1289. // Builds an implementation of an Action<> for some particular signature, using
  1290. // a class defined by an ACTION* macro.
  1291. template <typename F, typename Impl> struct ActionImpl;
  1292. template <typename Impl>
  1293. struct ImplBase {
  1294. struct Holder {
  1295. // Allows each copy of the Action<> to get to the Impl.
  1296. explicit operator const Impl&() const { return *ptr; }
  1297. std::shared_ptr<Impl> ptr;
  1298. };
  1299. using type = typename std::conditional<std::is_constructible<Impl>::value,
  1300. Impl, Holder>::type;
  1301. };
  1302. template <typename R, typename... Args, typename Impl>
  1303. struct ActionImpl<R(Args...), Impl> : ImplBase<Impl>::type {
  1304. using Base = typename ImplBase<Impl>::type;
  1305. using function_type = R(Args...);
  1306. using args_type = std::tuple<Args...>;
  1307. ActionImpl() = default; // Only defined if appropriate for Base.
  1308. explicit ActionImpl(std::shared_ptr<Impl> impl) : Base{std::move(impl)} { }
  1309. R operator()(Args&&... arg) const {
  1310. static constexpr size_t kMaxArgs =
  1311. sizeof...(Args) <= 10 ? sizeof...(Args) : 10;
  1312. return Apply(MakeIndexSequence<kMaxArgs>{},
  1313. MakeIndexSequence<10 - kMaxArgs>{},
  1314. args_type{std::forward<Args>(arg)...});
  1315. }
  1316. template <std::size_t... arg_id, std::size_t... excess_id>
  1317. R Apply(IndexSequence<arg_id...>, IndexSequence<excess_id...>,
  1318. const args_type& args) const {
  1319. // Impl need not be specific to the signature of action being implemented;
  1320. // only the implementing function body needs to have all of the specific
  1321. // types instantiated. Up to 10 of the args that are provided by the
  1322. // args_type get passed, followed by a dummy of unspecified type for the
  1323. // remainder up to 10 explicit args.
  1324. static constexpr ExcessiveArg kExcessArg{};
  1325. return static_cast<const Impl&>(*this).template gmock_PerformImpl<
  1326. /*function_type=*/function_type, /*return_type=*/R,
  1327. /*args_type=*/args_type,
  1328. /*argN_type=*/typename std::tuple_element<arg_id, args_type>::type...>(
  1329. /*args=*/args, std::get<arg_id>(args)...,
  1330. ((void)excess_id, kExcessArg)...);
  1331. }
  1332. };
  1333. // Stores a default-constructed Impl as part of the Action<>'s
  1334. // std::function<>. The Impl should be trivial to copy.
  1335. template <typename F, typename Impl>
  1336. ::testing::Action<F> MakeAction() {
  1337. return ::testing::Action<F>(ActionImpl<F, Impl>());
  1338. }
  1339. // Stores just the one given instance of Impl.
  1340. template <typename F, typename Impl>
  1341. ::testing::Action<F> MakeAction(std::shared_ptr<Impl> impl) {
  1342. return ::testing::Action<F>(ActionImpl<F, Impl>(std::move(impl)));
  1343. }
  1344. #define GMOCK_INTERNAL_ARG_UNUSED(i, data, el) \
  1345. , const arg##i##_type& arg##i GTEST_ATTRIBUTE_UNUSED_
  1346. #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_ \
  1347. const args_type& args GTEST_ATTRIBUTE_UNUSED_ GMOCK_PP_REPEAT( \
  1348. GMOCK_INTERNAL_ARG_UNUSED, , 10)
  1349. #define GMOCK_INTERNAL_ARG(i, data, el) , const arg##i##_type& arg##i
  1350. #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_ \
  1351. const args_type& args GMOCK_PP_REPEAT(GMOCK_INTERNAL_ARG, , 10)
  1352. #define GMOCK_INTERNAL_TEMPLATE_ARG(i, data, el) , typename arg##i##_type
  1353. #define GMOCK_ACTION_TEMPLATE_ARGS_NAMES_ \
  1354. GMOCK_PP_TAIL(GMOCK_PP_REPEAT(GMOCK_INTERNAL_TEMPLATE_ARG, , 10))
  1355. #define GMOCK_INTERNAL_TYPENAME_PARAM(i, data, param) , typename param##_type
  1356. #define GMOCK_ACTION_TYPENAME_PARAMS_(params) \
  1357. GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPENAME_PARAM, , params))
  1358. #define GMOCK_INTERNAL_TYPE_PARAM(i, data, param) , param##_type
  1359. #define GMOCK_ACTION_TYPE_PARAMS_(params) \
  1360. GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPE_PARAM, , params))
  1361. #define GMOCK_INTERNAL_TYPE_GVALUE_PARAM(i, data, param) \
  1362. , param##_type gmock_p##i
  1363. #define GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params) \
  1364. GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPE_GVALUE_PARAM, , params))
  1365. #define GMOCK_INTERNAL_GVALUE_PARAM(i, data, param) \
  1366. , std::forward<param##_type>(gmock_p##i)
  1367. #define GMOCK_ACTION_GVALUE_PARAMS_(params) \
  1368. GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_GVALUE_PARAM, , params))
  1369. #define GMOCK_INTERNAL_INIT_PARAM(i, data, param) \
  1370. , param(::std::forward<param##_type>(gmock_p##i))
  1371. #define GMOCK_ACTION_INIT_PARAMS_(params) \
  1372. GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_INIT_PARAM, , params))
  1373. #define GMOCK_INTERNAL_FIELD_PARAM(i, data, param) param##_type param;
  1374. #define GMOCK_ACTION_FIELD_PARAMS_(params) \
  1375. GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_FIELD_PARAM, , params)
  1376. #define GMOCK_INTERNAL_ACTION(name, full_name, params) \
  1377. template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
  1378. class full_name { \
  1379. public: \
  1380. explicit full_name(GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) \
  1381. : impl_(std::make_shared<gmock_Impl>( \
  1382. GMOCK_ACTION_GVALUE_PARAMS_(params))) { } \
  1383. full_name(const full_name&) = default; \
  1384. full_name(full_name&&) noexcept = default; \
  1385. template <typename F> \
  1386. operator ::testing::Action<F>() const { \
  1387. return ::testing::internal::MakeAction<F>(impl_); \
  1388. } \
  1389. private: \
  1390. class gmock_Impl { \
  1391. public: \
  1392. explicit gmock_Impl(GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) \
  1393. : GMOCK_ACTION_INIT_PARAMS_(params) {} \
  1394. template <typename function_type, typename return_type, \
  1395. typename args_type, GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
  1396. return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \
  1397. GMOCK_ACTION_FIELD_PARAMS_(params) \
  1398. }; \
  1399. std::shared_ptr<const gmock_Impl> impl_; \
  1400. }; \
  1401. template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
  1402. inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name( \
  1403. GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) { \
  1404. return full_name<GMOCK_ACTION_TYPE_PARAMS_(params)>( \
  1405. GMOCK_ACTION_GVALUE_PARAMS_(params)); \
  1406. } \
  1407. template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
  1408. template <typename function_type, typename return_type, typename args_type, \
  1409. GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
  1410. return_type full_name<GMOCK_ACTION_TYPE_PARAMS_(params)>::gmock_Impl:: \
  1411. gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
  1412. } // namespace internal
  1413. // Similar to GMOCK_INTERNAL_ACTION, but no bound parameters are stored.
  1414. #define ACTION(name) \
  1415. class name##Action { \
  1416. public: \
  1417. explicit name##Action() noexcept {} \
  1418. name##Action(const name##Action&) noexcept {} \
  1419. template <typename F> \
  1420. operator ::testing::Action<F>() const { \
  1421. return ::testing::internal::MakeAction<F, gmock_Impl>(); \
  1422. } \
  1423. private: \
  1424. class gmock_Impl { \
  1425. public: \
  1426. template <typename function_type, typename return_type, \
  1427. typename args_type, GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
  1428. return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \
  1429. }; \
  1430. }; \
  1431. inline name##Action name() GTEST_MUST_USE_RESULT_; \
  1432. inline name##Action name() { return name##Action(); } \
  1433. template <typename function_type, typename return_type, typename args_type, \
  1434. GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
  1435. return_type name##Action::gmock_Impl::gmock_PerformImpl( \
  1436. GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const
  1437. #define ACTION_P(name, ...) \
  1438. GMOCK_INTERNAL_ACTION(name, name##ActionP, (__VA_ARGS__))
  1439. #define ACTION_P2(name, ...) \
  1440. GMOCK_INTERNAL_ACTION(name, name##ActionP2, (__VA_ARGS__))
  1441. #define ACTION_P3(name, ...) \
  1442. GMOCK_INTERNAL_ACTION(name, name##ActionP3, (__VA_ARGS__))
  1443. #define ACTION_P4(name, ...) \
  1444. GMOCK_INTERNAL_ACTION(name, name##ActionP4, (__VA_ARGS__))
  1445. #define ACTION_P5(name, ...) \
  1446. GMOCK_INTERNAL_ACTION(name, name##ActionP5, (__VA_ARGS__))
  1447. #define ACTION_P6(name, ...) \
  1448. GMOCK_INTERNAL_ACTION(name, name##ActionP6, (__VA_ARGS__))
  1449. #define ACTION_P7(name, ...) \
  1450. GMOCK_INTERNAL_ACTION(name, name##ActionP7, (__VA_ARGS__))
  1451. #define ACTION_P8(name, ...) \
  1452. GMOCK_INTERNAL_ACTION(name, name##ActionP8, (__VA_ARGS__))
  1453. #define ACTION_P9(name, ...) \
  1454. GMOCK_INTERNAL_ACTION(name, name##ActionP9, (__VA_ARGS__))
  1455. #define ACTION_P10(name, ...) \
  1456. GMOCK_INTERNAL_ACTION(name, name##ActionP10, (__VA_ARGS__))
  1457. } // namespace testing
  1458. #ifdef _MSC_VER
  1459. # pragma warning(pop)
  1460. #endif
  1461. #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_