Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:01:08

0001 //
0002 // Copyright 2019 The Abseil Authors.
0003 //
0004 // Licensed under the Apache License, Version 2.0 (the "License");
0005 // you may not use this file except in compliance with the License.
0006 // You may obtain a copy of the License at
0007 //
0008 //      https://www.apache.org/licenses/LICENSE-2.0
0009 //
0010 // Unless required by applicable law or agreed to in writing, software
0011 // distributed under the License is distributed on an "AS IS" BASIS,
0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 // See the License for the specific language governing permissions and
0014 // limitations under the License.
0015 
0016 #ifndef ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_
0017 #define ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_
0018 
0019 #include <type_traits>
0020 
0021 #include "gmock/gmock.h"
0022 #include "absl/random/internal/mock_helpers.h"
0023 #include "absl/random/mocking_bit_gen.h"
0024 
0025 namespace absl {
0026 ABSL_NAMESPACE_BEGIN
0027 namespace random_internal {
0028 
0029 template <typename DistrT, typename Fn>
0030 struct MockSingleOverload;
0031 
0032 // MockSingleOverload
0033 //
0034 // MockSingleOverload hooks in to gMock's `ON_CALL` and `EXPECT_CALL` macros.
0035 // EXPECT_CALL(mock_single_overload, Call(...))` will expand to a call to
0036 // `mock_single_overload.gmock_Call(...)`. Because expectations are stored on
0037 // the MockingBitGen (an argument passed inside `Call(...)`), this forwards to
0038 // arguments to MockingBitGen::Register.
0039 //
0040 // The underlying KeyT must match the KeyT constructed by DistributionCaller.
0041 template <typename DistrT, typename Ret, typename... Args>
0042 struct MockSingleOverload<DistrT, Ret(MockingBitGen&, Args...)> {
0043   static_assert(std::is_same<typename DistrT::result_type, Ret>::value,
0044                 "Overload signature must have return type matching the "
0045                 "distribution result_type.");
0046   using KeyT = Ret(DistrT, std::tuple<Args...>);
0047 
0048   template <typename MockURBG>
0049   auto gmock_Call(MockURBG& gen, const ::testing::Matcher<Args>&... matchers)
0050       -> decltype(MockHelpers::MockFor<KeyT>(gen).gmock_Call(matchers...)) {
0051     static_assert(std::is_base_of<MockingBitGen, MockURBG>::value,
0052                   "Mocking requires an absl::MockingBitGen");
0053     return MockHelpers::MockFor<KeyT>(gen).gmock_Call(matchers...);
0054   }
0055 };
0056 
0057 template <typename DistrT, typename Ret, typename Arg, typename... Args>
0058 struct MockSingleOverload<DistrT, Ret(Arg, MockingBitGen&, Args...)> {
0059   static_assert(std::is_same<typename DistrT::result_type, Ret>::value,
0060                 "Overload signature must have return type matching the "
0061                 "distribution result_type.");
0062   using KeyT = Ret(DistrT, std::tuple<Arg, Args...>);
0063 
0064   template <typename MockURBG>
0065   auto gmock_Call(const ::testing::Matcher<Arg>& matcher, MockURBG& gen,
0066                   const ::testing::Matcher<Args>&... matchers)
0067       -> decltype(MockHelpers::MockFor<KeyT>(gen).gmock_Call(matcher,
0068                                                              matchers...)) {
0069     static_assert(std::is_base_of<MockingBitGen, MockURBG>::value,
0070                   "Mocking requires an absl::MockingBitGen");
0071     return MockHelpers::MockFor<KeyT>(gen).gmock_Call(matcher, matchers...);
0072   }
0073 };
0074 
0075 // MockOverloadSet
0076 //
0077 // MockOverloadSet takes a distribution and a collection of signatures and
0078 // performs overload resolution amongst all the overloads. This makes
0079 // `EXPECT_CALL(mock_overload_set, Call(...))` expand and do overload resolution
0080 // correctly.
0081 template <typename DistrT, typename... Signatures>
0082 struct MockOverloadSet;
0083 
0084 template <typename DistrT, typename Sig>
0085 struct MockOverloadSet<DistrT, Sig> : public MockSingleOverload<DistrT, Sig> {
0086   using MockSingleOverload<DistrT, Sig>::gmock_Call;
0087 };
0088 
0089 template <typename DistrT, typename FirstSig, typename... Rest>
0090 struct MockOverloadSet<DistrT, FirstSig, Rest...>
0091     : public MockSingleOverload<DistrT, FirstSig>,
0092       public MockOverloadSet<DistrT, Rest...> {
0093   using MockSingleOverload<DistrT, FirstSig>::gmock_Call;
0094   using MockOverloadSet<DistrT, Rest...>::gmock_Call;
0095 };
0096 
0097 }  // namespace random_internal
0098 ABSL_NAMESPACE_END
0099 }  // namespace absl
0100 #endif  // ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_