6#ifndef MIMICPP_MOCK_HPP
7#define MIMICPP_MOCK_HPP
19namespace mimicpp::detail
21 template <
typename Derived,
typename Signature>
22 using call_interface_t =
typename call_convention_traits<
25 template <
typename Derived,
typename Signature,
typename... Params>
26 class DefaultCallInterface<
36 const std::source_location& from = std::source_location::current()) noexcept(
signature_is_noexcept_v<Signature>)
38 return static_cast<const Derived&
>(*this)
39 .handle_call(std::tuple{std::ref(params)...}, from);
43 template <
typename Derived,
typename Signature,
typename... Params>
44 class DefaultCallInterface<
54 const std::source_location& from = std::source_location::current()) const noexcept(
signature_is_noexcept_v<Signature>)
56 return static_cast<const Derived&
>(*this)
57 .handle_call(std::tuple{std::ref(params)...}, from);
61 template <
typename Derived,
typename Signature,
typename... Params>
62 class DefaultCallInterface<
74 return static_cast<const Derived&
>(*this)
75 .handle_call(std::tuple{std::ref(params)...}, from);
79 template <
typename Derived,
typename Signature,
typename... Params>
80 class DefaultCallInterface<
90 const std::source_location& from = std::source_location::current()) const& noexcept(
signature_is_noexcept_v<Signature>)
92 return static_cast<const Derived&
>(*this)
93 .handle_call(std::tuple{std::ref(params)...}, from);
97 template <
typename Derived,
typename Signature,
typename... Params>
98 class DefaultCallInterface<
103 type_list<Params...>>
110 return static_cast<const Derived&
>(*this)
111 .handle_call(std::tuple{std::ref(params)...}, from);
115 template <
typename Derived,
typename Signature,
typename... Params>
116 class DefaultCallInterface<
121 type_list<Params...>>
126 const std::source_location& from = std::source_location::current()) const&& noexcept(
signature_is_noexcept_v<Signature>)
128 return static_cast<const Derived&
>(*this)
129 .handle_call(std::tuple{std::ref(params)...}, from);
141 template <
typename Derived,
typename Signature,
typename... Params>
147 type_list<Params...>>
150 template <
typename... Args>
151 requires(... && requirement_for<Args, Params>)
153 constexpr auto expect_call(Args&&... args)
155 return static_cast<const Derived&
>(*this)
156 .make_expectation_builder(std::forward<Args>(args)...);
160 template <
typename Derived,
typename Signature,
typename... Params>
166 type_list<Params...>>
169 template <
typename... Args>
170 requires(... && requirement_for<Args, Params>)
172 constexpr auto expect_call(Args&&... args)
const
174 return static_cast<const Derived&
>(*this)
175 .make_expectation_builder(std::forward<Args>(args)...);
179 template <
typename Derived,
typename Signature,
typename... Params>
185 type_list<Params...>>
188 template <
typename... Args>
189 requires(... && requirement_for<Args, Params>)
191 constexpr auto expect_call(Args&&... args) &
193 return static_cast<const Derived&
>(*this)
194 .make_expectation_builder(std::forward<Args>(args)...);
198 template <
typename Derived,
typename Signature,
typename... Params>
204 type_list<Params...>>
207 template <
typename... Args>
208 requires(... && requirement_for<Args, Params>)
210 constexpr auto expect_call(Args&&... args)
const&
212 return static_cast<const Derived&
>(*this)
213 .make_expectation_builder(std::forward<Args>(args)...);
217 template <
typename Derived,
typename Signature,
typename... Params>
223 type_list<Params...>>
226 template <
typename... Args>
227 requires(... && requirement_for<Args, Params>)
229 constexpr auto expect_call(Args&&... args) &&
231 return static_cast<const Derived&
>(*this)
232 .make_expectation_builder(std::forward<Args>(args)...);
236 template <
typename Derived,
typename Signature,
typename... Params>
242 type_list<Params...>>
245 template <
typename... Args>
246 requires(... && requirement_for<Args, Params>)
248 constexpr auto expect_call(Args&&... args)
const&&
250 return static_cast<const Derived&
>(*this)
251 .make_expectation_builder(std::forward<Args>(args)...);
255 template <
typename Signature>
256 using expectation_collection_ptr_for = std::shared_ptr<ExpectationCollection<signature_decay_t<Signature>>>;
258 template <
typename Signature,
typename ParamList = signature_param_list_t<Signature>>
261 template <
typename Signature,
typename... Params>
262 class BasicMock<Signature, type_list<Params...>>
263 :
public MockFrontend<
265 BasicMock<Signature, type_list<Params...>>,
266 signature_remove_call_convention_t<Signature>>,
267 public call_interface_t<
268 BasicMock<Signature, type_list<Params...>>,
273 friend class MockFrontend<BasicMock, SignatureT>;
274 friend call_interface_t<BasicMock, Signature>;
280 using ExpectationCollectionPtrT = expectation_collection_ptr_for<SignatureT>;
284 ExpectationCollectionPtrT collection,
285 const std::size_t stacktraceSkip) noexcept
286 : m_Expectations{std::move(collection)},
287 m_StacktraceSkip{stacktraceSkip + 2u}
292 ExpectationCollectionPtrT m_Expectations;
293 std::size_t m_StacktraceSkip;
297 std::tuple<std::reference_wrapper<std::remove_reference_t<Params>>...>&& params,
298 const std::source_location& from)
const
300 return m_Expectations->handle_call(
302 .args = std::move(params),
303 .fromCategory = refQualification,
304 .fromConstness = constQualification,
305 .fromSourceLocation = from,
309 template <
typename... Args>
311 constexpr auto make_expectation_builder(Args&&... args)
const
313 return detail::make_expectation_builder(m_Expectations, std::forward<Args>(args)...)
314 && expectation_policies::Category<refQualification>{}
315 && expectation_policies::Constness<constQualification>{};
319 template <
typename List>
320 struct expectation_collection_factory;
322 template <
typename... UniqueSignatures>
323 struct expectation_collection_factory<type_list<UniqueSignatures...>>
329 std::make_shared<ExpectationCollection<UniqueSignatures>>()...};
392 template <
typename FirstSignature,
typename... OtherSignatures>
395 :
public detail::BasicMock<FirstSignature>,
396 public detail::BasicMock<OtherSignatures>...
399 using detail::BasicMock<FirstSignature>::operator();
400 using detail::BasicMock<FirstSignature>::expect_call;
401 using detail::BasicMock<OtherSignatures>::operator()...;
402 using detail::BasicMock<OtherSignatures>::expect_call...;
426 explicit Mock(
const std::size_t baseStacktraceSkip)
428 detail::expectation_collection_factory<
429 detail::unique_list_t<
458 template <
typename... Collections>
461 std::tuple<Collections...> collections,
462 const std::size_t stacktraceSkip) noexcept
463 : detail::BasicMock<FirstSignature>{
464 std::get<detail::expectation_collection_ptr_for<FirstSignature>>(collections),
467 detail::BasicMock<OtherSignatures>{
468 std::get<detail::expectation_collection_ptr_for<OtherSignatures>>(collections),
~Mock()=default
Defaulted destructor.
Mock & operator=(Mock &&)=default
Defaulted move assignment operator.
Mock(Mock &&)=default
Defaulted move constructor.
Mock(const Mock &)=delete
Deleted copy constructor.
Mock(const std::size_t baseStacktraceSkip)
Constructor, initializing the base-stacktrace-skip.
Definition Mock.hpp:426
Mock & operator=(const Mock &)=delete
Deleted copy assignment operator.
Mock()
Default constructor.
Definition Mock.hpp:413
constexpr detail::current_hook::current_fn current
Function object, which generates the current-stacktrace.
Definition Stacktrace.hpp:374
constexpr bool is_overload_set_v
Convenience constant, exposing the value member of the actual type-trait.
Definition Fwd.hpp:289
typename signature_call_convention< Signature >::type signature_call_convention_t
Convenience alias, exposing the type member alias of the actual type-trait.
Definition Fwd.hpp:77
constexpr Constness signature_const_qualification_v
Convenience constant, exposing the value member of the actual type-trait.
Definition Fwd.hpp:189
typename signature_decay< Signature >::type signature_decay_t
Convenience alias, exposing the type member alias of the actual type-trait.
Definition Fwd.hpp:157
constexpr bool signature_is_noexcept_v
Convenience constant, exposing the value member of the actual type-trait.
Definition Fwd.hpp:221
typename signature_param_list< Signature >::type signature_param_list_t
Convenience alias, exposing the type member alias of the actual type-trait.
Definition Fwd.hpp:253
constexpr ValueCategory signature_ref_qualification_v
Convenience constant, exposing the value member of the actual type-trait.
Definition Fwd.hpp:205
typename signature_remove_call_convention< Signature >::type signature_remove_call_convention_t
Convenience alias, exposing the type member alias of the actual type-trait.
Definition Fwd.hpp:93
typename signature_return_type< Signature >::type signature_return_type_t
Convenience alias, exposing the type member alias of the actual type-trait.
Definition Fwd.hpp:173
typename info_for_signature< Signature >::type info_for_signature_t
Definition Call.hpp:43
Definition BoostTest.hpp:20
ValueCategory
Definition Fwd.hpp:32
@ any
Definition Fwd.hpp:35
@ lvalue
Definition Fwd.hpp:33
@ rvalue
Definition Fwd.hpp:34
Constness
Definition Fwd.hpp:25
@ as_const
Definition Fwd.hpp:27
@ non_const
Definition Fwd.hpp:26