summaryrefslogtreecommitdiffstats
path: root/googlemock/include/gmock/gmock-actions.h
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2020-02-20 04:05:01 (GMT)
committerCJ Johnson <johnsoncj@google.com>2020-02-28 21:40:53 (GMT)
commitfd538161f484c202e68512ca37aaa73f2e06f606 (patch)
treed3b2bbc727b7808154f3893ee102aaa98a33e68a /googlemock/include/gmock/gmock-actions.h
parent23b2a3b1cf803999fb38175f6e9e038a4495c8a5 (diff)
downloadgoogletest-fd538161f484c202e68512ca37aaa73f2e06f606.zip
googletest-fd538161f484c202e68512ca37aaa73f2e06f606.tar.gz
googletest-fd538161f484c202e68512ca37aaa73f2e06f606.tar.bz2
Googletest export
Allow construction of an Action from a callable of zero args Action already allows construction from a callable with the same args as the mocked function, without needing to wrap the callable in Invoke. However, if you don't care about the arguments to the mocked function you need to either accept all of them or wrap your callable in InvokeWithoutArgs. This change makes both of those unnecessary, since it allows you to pass a no-args callable to Action directly. PiperOrigin-RevId: 296117034
Diffstat (limited to 'googlemock/include/gmock/gmock-actions.h')
-rw-r--r--googlemock/include/gmock/gmock-actions.h36
1 files changed, 33 insertions, 3 deletions
diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h
index e46bcaa..b4127e9 100644
--- a/googlemock/include/gmock/gmock-actions.h
+++ b/googlemock/include/gmock/gmock-actions.h
@@ -263,6 +263,10 @@ GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0);
#undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_
+// Simple two-arg form of std::disjunction.
+template <typename P, typename Q>
+using disjunction = typename ::std::conditional<P::value, P, Q>::type;
+
} // namespace internal
// When an unexpected function call is encountered, Google Mock will
@@ -456,9 +460,15 @@ class Action {
// This cannot take std::function directly, because then Action would not be
// directly constructible from lambda (it would require two conversions).
template <typename G,
- typename = typename ::std::enable_if<
- ::std::is_constructible<::std::function<F>, G>::value>::type>
- Action(G&& fun) : fun_(::std::forward<G>(fun)) {} // NOLINT
+ typename IsCompatibleFunctor =
+ ::std::is_constructible<::std::function<F>, G>,
+ typename IsNoArgsFunctor =
+ ::std::is_constructible<::std::function<Result()>, G>,
+ typename = typename ::std::enable_if<internal::disjunction<
+ IsCompatibleFunctor, IsNoArgsFunctor>::value>::type>
+ Action(G&& fun) { // NOLINT
+ Init(::std::forward<G>(fun), IsCompatibleFunctor());
+ }
// Constructs an Action from its implementation.
explicit Action(ActionInterface<F>* impl)
@@ -490,6 +500,26 @@ class Action {
template <typename G>
friend class Action;
+ template <typename G>
+ void Init(G&& g, ::std::true_type) {
+ fun_ = ::std::forward<G>(g);
+ }
+
+ template <typename G>
+ void Init(G&& g, ::std::false_type) {
+ fun_ = IgnoreArgs<typename ::std::decay<G>::type>{::std::forward<G>(g)};
+ }
+
+ template <typename FunctionImpl>
+ struct IgnoreArgs {
+ template <typename... Args>
+ Result operator()(const Args&...) const {
+ return function_impl();
+ }
+
+ FunctionImpl function_impl;
+ };
+
// fun_ is an empty function if and only if this is the DoDefault() action.
::std::function<F> fun_;
};