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 "gtest/gtest.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 Mocking::Register. 39 template <typename DistrT, typename Ret, typename... Args> 40 struct MockSingleOverload<DistrT, Ret(MockingBitGen&, Args...)> { 41 static_assert(std::is_same<typename DistrT::result_type, Ret>::value, 42 "Overload signature must have return type matching the " 43 "distributions result type."); 44 auto gmock_Call( 45 absl::MockingBitGen& gen, // NOLINT(google-runtime-references) 46 const ::testing::Matcher<Args>&... args) 47 -> decltype(gen.Register<DistrT, Args...>(args...)) { 48 return gen.Register<DistrT, Args...>(args...); 49 } 50 }; 51 52 template <typename DistrT, typename Ret, typename Arg, typename... Args> 53 struct MockSingleOverload<DistrT, Ret(Arg, MockingBitGen&, Args...)> { 54 static_assert(std::is_same<typename DistrT::result_type, Ret>::value, 55 "Overload signature must have return type matching the " 56 "distributions result type."); 57 auto gmock_Call( 58 const ::testing::Matcher<Arg>& arg, 59 absl::MockingBitGen& gen, // NOLINT(google-runtime-references) 60 const ::testing::Matcher<Args>&... args) 61 -> decltype(gen.Register<DistrT, Arg, Args...>(arg, args...)) { 62 return gen.Register<DistrT, Arg, Args...>(arg, args...); 63 } 64 }; 65 66 // MockOverloadSet 67 // 68 // MockOverloadSet takes a distribution and a collection of signatures and 69 // performs overload resolution amongst all the overloads. This makes 70 // `EXPECT_CALL(mock_overload_set, Call(...))` expand and do overload resolution 71 // correctly. 72 template <typename DistrT, typename... Signatures> 73 struct MockOverloadSet; 74 75 template <typename DistrT, typename Sig> 76 struct MockOverloadSet<DistrT, Sig> : public MockSingleOverload<DistrT, Sig> { 77 using MockSingleOverload<DistrT, Sig>::gmock_Call; 78 }; 79 80 template <typename DistrT, typename FirstSig, typename... Rest> 81 struct MockOverloadSet<DistrT, FirstSig, Rest...> 82 : public MockSingleOverload<DistrT, FirstSig>, 83 public MockOverloadSet<DistrT, Rest...> { 84 using MockSingleOverload<DistrT, FirstSig>::gmock_Call; 85 using MockOverloadSet<DistrT, Rest...>::gmock_Call; 86 }; 87 88 } // namespace random_internal 89 ABSL_NAMESPACE_END 90 } // namespace absl 91 #endif // ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_ 92