mock_helpers.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_HELPERS_H_
  16. #define ABSL_RANDOM_INTERNAL_MOCK_HELPERS_H_
  17. #include <utility>
  18. #include "absl/base/config.h"
  19. #include "absl/base/internal/fast_type_id.h"
  20. #include "absl/types/optional.h"
  21. namespace absl {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace random_internal {
  24. // A no-op validator meeting the ValidatorT requirements for MockHelpers.
  25. //
  26. // Custom validators should follow a similar structure, passing the type to
  27. // MockHelpers::MockFor<KeyT>(m, CustomValidatorT()).
  28. struct NoOpValidator {
  29. // Default validation: do nothing.
  30. template <typename ResultT, typename... Args>
  31. static void Validate(ResultT, Args&&...) {}
  32. };
  33. // MockHelpers works in conjunction with MockOverloadSet, MockingBitGen, and
  34. // BitGenRef to enable the mocking capability for absl distribution functions.
  35. //
  36. // MockingBitGen registers mocks based on the typeid of a mock signature, KeyT,
  37. // which is used to generate a unique id.
  38. //
  39. // KeyT is a signature of the form:
  40. // result_type(discriminator_type, std::tuple<args...>)
  41. // The mocked function signature will be composed from KeyT as:
  42. // result_type(args...)
  43. //
  44. class MockHelpers {
  45. using IdType = ::absl::base_internal::FastTypeIdType;
  46. // Given a key signature type used to index the mock, extract the components.
  47. // KeyT is expected to have the form:
  48. // result_type(discriminator_type, arg_tuple_type)
  49. template <typename KeyT>
  50. struct KeySignature;
  51. template <typename ResultT, typename DiscriminatorT, typename ArgTupleT>
  52. struct KeySignature<ResultT(DiscriminatorT, ArgTupleT)> {
  53. using result_type = ResultT;
  54. using discriminator_type = DiscriminatorT;
  55. using arg_tuple_type = ArgTupleT;
  56. };
  57. // Detector for InvokeMock.
  58. template <class T>
  59. using invoke_mock_t = decltype(std::declval<T*>()->InvokeMock(
  60. std::declval<IdType>(), std::declval<void*>(), std::declval<void*>()));
  61. // Empty implementation of InvokeMock.
  62. template <typename KeyT, typename ReturnT, typename ArgTupleT, typename URBG,
  63. typename... Args>
  64. static absl::optional<ReturnT> InvokeMockImpl(char, URBG*, Args&&...) {
  65. return absl::nullopt;
  66. }
  67. // Non-empty implementation of InvokeMock.
  68. template <typename KeyT, typename ReturnT, typename ArgTupleT, typename URBG,
  69. typename = invoke_mock_t<URBG>, typename... Args>
  70. static absl::optional<ReturnT> InvokeMockImpl(int, URBG* urbg,
  71. Args&&... args) {
  72. ArgTupleT arg_tuple(std::forward<Args>(args)...);
  73. ReturnT result;
  74. if (urbg->InvokeMock(::absl::base_internal::FastTypeId<KeyT>(), &arg_tuple,
  75. &result)) {
  76. return result;
  77. }
  78. return absl::nullopt;
  79. }
  80. public:
  81. // InvokeMock is private; this provides access for some specialized use cases.
  82. template <typename URBG>
  83. static inline bool PrivateInvokeMock(URBG* urbg, IdType type,
  84. void* args_tuple, void* result) {
  85. return urbg->InvokeMock(type, args_tuple, result);
  86. }
  87. // Invoke a mock for the KeyT (may or may not be a signature).
  88. //
  89. // KeyT is used to generate a typeid-based lookup key for the mock.
  90. // KeyT is a signature of the form:
  91. // result_type(discriminator_type, std::tuple<args...>)
  92. // The mocked function signature will be composed from KeyT as:
  93. // result_type(args...)
  94. //
  95. // An instance of arg_tuple_type must be constructable from Args..., since
  96. // the underlying mechanism requires a pointer to an argument tuple.
  97. template <typename KeyT, typename URBG, typename... Args>
  98. static auto MaybeInvokeMock(URBG* urbg, Args&&... args)
  99. -> absl::optional<typename KeySignature<KeyT>::result_type> {
  100. // Use function overloading to dispatch to the implementation since
  101. // more modern patterns (e.g. require + constexpr) are not supported in all
  102. // compiler configurations.
  103. return InvokeMockImpl<KeyT, typename KeySignature<KeyT>::result_type,
  104. typename KeySignature<KeyT>::arg_tuple_type, URBG>(
  105. 0, urbg, std::forward<Args>(args)...);
  106. }
  107. // Acquire a mock for the KeyT (may or may not be a signature), set up to use
  108. // the ValidatorT to verify that the result is in the range of the RNG
  109. // function.
  110. //
  111. // KeyT is used to generate a typeid-based lookup for the mock.
  112. // KeyT is a signature of the form:
  113. // result_type(discriminator_type, std::tuple<args...>)
  114. // The mocked function signature will be composed from KeyT as:
  115. // result_type(args...)
  116. // ValidatorT::Validate will be called after the result of the RNG. The
  117. // signature is expected to be of the form:
  118. // ValidatorT::Validate(result, args...)
  119. template <typename KeyT, typename ValidatorT, typename MockURBG>
  120. static auto MockFor(MockURBG& m, ValidatorT)
  121. -> decltype(m.template RegisterMock<
  122. typename KeySignature<KeyT>::result_type,
  123. typename KeySignature<KeyT>::arg_tuple_type>(
  124. m, std::declval<IdType>(), ValidatorT())) {
  125. return m.template RegisterMock<typename KeySignature<KeyT>::result_type,
  126. typename KeySignature<KeyT>::arg_tuple_type>(
  127. m, ::absl::base_internal::FastTypeId<KeyT>(), ValidatorT());
  128. }
  129. // Acquire a mock for the KeyT (may or may not be a signature).
  130. //
  131. // KeyT is used to generate a typeid-based lookup for the mock.
  132. // KeyT is a signature of the form:
  133. // result_type(discriminator_type, std::tuple<args...>)
  134. // The mocked function signature will be composed from KeyT as:
  135. // result_type(args...)
  136. template <typename KeyT, typename MockURBG>
  137. static decltype(auto) MockFor(MockURBG& m) {
  138. return MockFor<KeyT>(m, NoOpValidator());
  139. }
  140. };
  141. } // namespace random_internal
  142. ABSL_NAMESPACE_END
  143. } // namespace absl
  144. #endif // ABSL_RANDOM_INTERNAL_MOCK_HELPERS_H_