Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/absl/random/internal/mock_overload_set.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 <tuple>
0020 #include <type_traits>
0021 
0022 #include "gmock/gmock.h"
0023 #include "absl/base/config.h"
0024 #include "absl/random/internal/mock_helpers.h"
0025 #include "absl/random/mocking_bit_gen.h"
0026 
0027 namespace absl {
0028 ABSL_NAMESPACE_BEGIN
0029 namespace random_internal {
0030 
0031 template <typename DistrT, typename ValidatorT, typename Fn>
0032 struct MockSingleOverload;
0033 
0034 // MockSingleOverload
0035 //
0036 // MockSingleOverload hooks in to gMock's `ON_CALL` and `EXPECT_CALL` macros.
0037 // EXPECT_CALL(mock_single_overload, Call(...))` will expand to a call to
0038 // `mock_single_overload.gmock_Call(...)`. Because expectations are stored on
0039 // the MockingBitGen (an argument passed inside `Call(...)`), this forwards to
0040 // arguments to MockingBitGen::Register.
0041 //
0042 // The underlying KeyT must match the KeyT constructed by DistributionCaller.
0043 template <typename DistrT, typename ValidatorT, typename Ret, typename... Args>
0044 struct MockSingleOverload<DistrT, ValidatorT, Ret(MockingBitGen&, Args...)> {
0045   static_assert(std::is_same<typename DistrT::result_type, Ret>::value,
0046                 "Overload signature must have return type matching the "
0047                 "distribution result_type.");
0048   using KeyT = Ret(DistrT, std::tuple<Args...>);
0049 
0050   template <typename MockURBG>
0051   auto gmock_Call(MockURBG& gen, const ::testing::Matcher<Args>&... matchers)
0052       -> decltype(MockHelpers::MockFor<KeyT>(gen, ValidatorT())
0053                       .gmock_Call(matchers...)) {
0054     static_assert(
0055         std::is_base_of<MockingBitGenImpl<true>, MockURBG>::value ||
0056             std::is_base_of<MockingBitGenImpl<false>, MockURBG>::value,
0057         "Mocking requires an absl::MockingBitGen");
0058     return MockHelpers::MockFor<KeyT>(gen, ValidatorT())
0059         .gmock_Call(matchers...);
0060   }
0061 };
0062 
0063 template <typename DistrT, typename ValidatorT, typename Ret, typename Arg,
0064           typename... Args>
0065 struct MockSingleOverload<DistrT, ValidatorT,
0066                           Ret(Arg, MockingBitGen&, Args...)> {
0067   static_assert(std::is_same<typename DistrT::result_type, Ret>::value,
0068                 "Overload signature must have return type matching the "
0069                 "distribution result_type.");
0070   using KeyT = Ret(DistrT, std::tuple<Arg, Args...>);
0071 
0072   template <typename MockURBG>
0073   auto gmock_Call(const ::testing::Matcher<Arg>& matcher, MockURBG& gen,
0074                   const ::testing::Matcher<Args>&... matchers)
0075       -> decltype(MockHelpers::MockFor<KeyT>(gen, ValidatorT())
0076                       .gmock_Call(matcher, matchers...)) {
0077     static_assert(
0078         std::is_base_of<MockingBitGenImpl<true>, MockURBG>::value ||
0079             std::is_base_of<MockingBitGenImpl<false>, MockURBG>::value,
0080         "Mocking requires an absl::MockingBitGen");
0081     return MockHelpers::MockFor<KeyT>(gen, ValidatorT())
0082         .gmock_Call(matcher, matchers...);
0083   }
0084 };
0085 
0086 // MockOverloadSetWithValidator
0087 //
0088 // MockOverloadSetWithValidator is a wrapper around MockOverloadSet which takes
0089 // an additional Validator parameter, allowing for customization of the mock
0090 // behavior.
0091 //
0092 // `ValidatorT::Validate(result, args...)` will be called after the mock
0093 // distribution returns a value in `result`, allowing for validation against the
0094 // args.
0095 template <typename DistrT, typename ValidatorT, typename... Fns>
0096 struct MockOverloadSetWithValidator;
0097 
0098 template <typename DistrT, typename ValidatorT, typename Sig>
0099 struct MockOverloadSetWithValidator<DistrT, ValidatorT, Sig>
0100     : public MockSingleOverload<DistrT, ValidatorT, Sig> {
0101   using MockSingleOverload<DistrT, ValidatorT, Sig>::gmock_Call;
0102 };
0103 
0104 template <typename DistrT, typename ValidatorT, typename FirstSig,
0105           typename... Rest>
0106 struct MockOverloadSetWithValidator<DistrT, ValidatorT, FirstSig, Rest...>
0107     : public MockSingleOverload<DistrT, ValidatorT, FirstSig>,
0108       public MockOverloadSetWithValidator<DistrT, ValidatorT, Rest...> {
0109   using MockSingleOverload<DistrT, ValidatorT, FirstSig>::gmock_Call;
0110   using MockOverloadSetWithValidator<DistrT, ValidatorT, Rest...>::gmock_Call;
0111 };
0112 
0113 // MockOverloadSet
0114 //
0115 // MockOverloadSet takes a distribution and a collection of signatures and
0116 // performs overload resolution amongst all the overloads. This makes
0117 // `EXPECT_CALL(mock_overload_set, Call(...))` expand and do overload resolution
0118 // correctly.
0119 template <typename DistrT, typename... Signatures>
0120 using MockOverloadSet =
0121     MockOverloadSetWithValidator<DistrT, NoOpValidator, Signatures...>;
0122 
0123 }  // namespace random_internal
0124 ABSL_NAMESPACE_END
0125 }  // namespace absl
0126 #endif  // ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_