mock_overload_set.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_
  16. #define ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_
  17. #include <type_traits>
  18. #include "gmock/gmock.h"
  19. #include "absl/random/internal/mock_helpers.h"
  20. #include "absl/random/mocking_bit_gen.h"
  21. namespace absl {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace random_internal {
  24. template <typename DistrT, typename Fn>
  25. struct MockSingleOverload;
  26. // MockSingleOverload
  27. //
  28. // MockSingleOverload hooks in to gMock's `ON_CALL` and `EXPECT_CALL` macros.
  29. // EXPECT_CALL(mock_single_overload, Call(...))` will expand to a call to
  30. // `mock_single_overload.gmock_Call(...)`. Because expectations are stored on
  31. // the MockingBitGen (an argument passed inside `Call(...)`), this forwards to
  32. // arguments to MockingBitGen::Register.
  33. //
  34. // The underlying KeyT must match the KeyT constructed by DistributionCaller.
  35. template <typename DistrT, typename Ret, typename... Args>
  36. struct MockSingleOverload<DistrT, Ret(MockingBitGen&, Args...)> {
  37. static_assert(std::is_same<typename DistrT::result_type, Ret>::value,
  38. "Overload signature must have return type matching the "
  39. "distribution result_type.");
  40. using KeyT = Ret(DistrT, std::tuple<Args...>);
  41. template <typename MockURBG>
  42. auto gmock_Call(MockURBG& gen, const ::testing::Matcher<Args>&... matchers)
  43. -> decltype(MockHelpers::MockFor<KeyT>(gen).gmock_Call(matchers...)) {
  44. static_assert(std::is_base_of<MockingBitGen, MockURBG>::value,
  45. "Mocking requires an absl::MockingBitGen");
  46. return MockHelpers::MockFor<KeyT>(gen).gmock_Call(matchers...);
  47. }
  48. };
  49. template <typename DistrT, typename Ret, typename Arg, typename... Args>
  50. struct MockSingleOverload<DistrT, Ret(Arg, MockingBitGen&, Args...)> {
  51. static_assert(std::is_same<typename DistrT::result_type, Ret>::value,
  52. "Overload signature must have return type matching the "
  53. "distribution result_type.");
  54. using KeyT = Ret(DistrT, std::tuple<Arg, Args...>);
  55. template <typename MockURBG>
  56. auto gmock_Call(const ::testing::Matcher<Arg>& matcher, MockURBG& gen,
  57. const ::testing::Matcher<Args>&... matchers)
  58. -> decltype(MockHelpers::MockFor<KeyT>(gen).gmock_Call(matcher,
  59. matchers...)) {
  60. static_assert(std::is_base_of<MockingBitGen, MockURBG>::value,
  61. "Mocking requires an absl::MockingBitGen");
  62. return MockHelpers::MockFor<KeyT>(gen).gmock_Call(matcher, matchers...);
  63. }
  64. };
  65. // MockOverloadSet
  66. //
  67. // MockOverloadSet takes a distribution and a collection of signatures and
  68. // performs overload resolution amongst all the overloads. This makes
  69. // `EXPECT_CALL(mock_overload_set, Call(...))` expand and do overload resolution
  70. // correctly.
  71. template <typename DistrT, typename... Signatures>
  72. struct MockOverloadSet;
  73. template <typename DistrT, typename Sig>
  74. struct MockOverloadSet<DistrT, Sig> : public MockSingleOverload<DistrT, Sig> {
  75. using MockSingleOverload<DistrT, Sig>::gmock_Call;
  76. };
  77. template <typename DistrT, typename FirstSig, typename... Rest>
  78. struct MockOverloadSet<DistrT, FirstSig, Rest...>
  79. : public MockSingleOverload<DistrT, FirstSig>,
  80. public MockOverloadSet<DistrT, Rest...> {
  81. using MockSingleOverload<DistrT, FirstSig>::gmock_Call;
  82. using MockOverloadSet<DistrT, Rest...>::gmock_Call;
  83. };
  84. } // namespace random_internal
  85. ABSL_NAMESPACE_END
  86. } // namespace absl
  87. #endif // ABSL_RANDOM_INTERNAL_MOCK_OVERLOAD_SET_H_