1 // 2 // Copyright 2019 The Abseil Authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // https://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 #ifndef ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_ 17 #define ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_ 18 19 #include <type_traits> 20 21 #include "gmock/gmock.h" 22 #include "absl/random/internal/mock_helpers.h" 23 #include "absl/random/mocking_bit_gen.h" 24 25 namespace absl { 26 ABSL_NAMESPACE_BEGIN 27 namespace random_internal { 28 29 template <typename DistrT, typename Fn> 30 struct MockSingleOverload; 31 32 // MockSingleOverload 33 // 34 // MockSingleOverload hooks in to gMock's `ON_CALL` and `EXPECT_CALL` macros. 35 // EXPECT_CALL(mock_single_overload, Call(...))` will expand to a call to 36 // `mock_single_overload.gmock_Call(...)`. Because expectations are stored on 37 // the MockingBitGen (an argument passed inside `Call(...)`), this forwards to 38 // arguments to MockingBitGen::Register. 39 // 40 // The underlying KeyT must match the KeyT constructed by DistributionCaller. 41 template <typename DistrT, typename Ret, typename... Args> 42 struct MockSingleOverload<DistrT, Ret(MockingBitGen&, Args...)> { 43 static_assert(std::is_same<typename DistrT::result_type, Ret>::value, 44 "Overload signature must have return type matching the " 45 "distribution result_type."); 46 using KeyT = Ret(DistrT, std::tuple<Args...>); 47 48 template <typename MockURBG> 49 auto gmock_Call(MockURBG& gen, const ::testing::Matcher<Args>&... matchers) 50 -> decltype(MockHelpers::MockFor<KeyT>(gen).gmock_Call(matchers...)) { 51 static_assert(std::is_base_of<MockingBitGen, MockURBG>::value, 52 "Mocking requires an absl::MockingBitGen"); 53 return MockHelpers::MockFor<KeyT>(gen).gmock_Call(matchers...); 54 } 55 }; 56 57 template <typename DistrT, typename Ret, typename Arg, typename... Args> 58 struct MockSingleOverload<DistrT, Ret(Arg, MockingBitGen&, Args...)> { 59 static_assert(std::is_same<typename DistrT::result_type, Ret>::value, 60 "Overload signature must have return type matching the " 61 "distribution result_type."); 62 using KeyT = Ret(DistrT, std::tuple<Arg, Args...>); 63 64 template <typename MockURBG> 65 auto gmock_Call(const ::testing::Matcher<Arg>& matcher, MockURBG& gen, 66 const ::testing::Matcher<Args>&... matchers) 67 -> decltype(MockHelpers::MockFor<KeyT>(gen).gmock_Call(matcher, 68 matchers...)) { 69 static_assert(std::is_base_of<MockingBitGen, MockURBG>::value, 70 "Mocking requires an absl::MockingBitGen"); 71 return MockHelpers::MockFor<KeyT>(gen).gmock_Call(matcher, matchers...); 72 } 73 }; 74 75 // MockOverloadSet 76 // 77 // MockOverloadSet takes a distribution and a collection of signatures and 78 // performs overload resolution amongst all the overloads. This makes 79 // `EXPECT_CALL(mock_overload_set, Call(...))` expand and do overload resolution 80 // correctly. 81 template <typename DistrT, typename... Signatures> 82 struct MockOverloadSet; 83 84 template <typename DistrT, typename Sig> 85 struct MockOverloadSet<DistrT, Sig> : public MockSingleOverload<DistrT, Sig> { 86 using MockSingleOverload<DistrT, Sig>::gmock_Call; 87 }; 88 89 template <typename DistrT, typename FirstSig, typename... Rest> 90 struct MockOverloadSet<DistrT, FirstSig, Rest...> 91 : public MockSingleOverload<DistrT, FirstSig>, 92 public MockOverloadSet<DistrT, Rest...> { 93 using MockSingleOverload<DistrT, FirstSig>::gmock_Call; 94 using MockOverloadSet<DistrT, Rest...>::gmock_Call; 95 }; 96 97 } // namespace random_internal 98 ABSL_NAMESPACE_END 99 } // namespace absl 100 #endif // ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_ 101