6#ifndef MIMICPP_MOCK_HPP
7#define MIMICPP_MOCK_HPP
23#ifndef MIMICPP_DETAIL_IS_MODULE
28 #include <type_traits>
37 std::optional<StringT>
name{};
42namespace mimicpp::detail
44 template <
typename Derived,
typename Signature>
45 using call_interface_t =
typename call_convention_traits<
48 template <
typename Derived,
typename Signature,
typename... Params>
49 class DefaultCallInterface<
54 util::type_list<Params...>>
61 return static_cast<Derived const&
>(*this)
64 std::tuple{std::ref(params)...},
69 template <
typename Derived,
typename Signature,
typename... Params>
70 class DefaultCallInterface<
75 util::type_list<Params...>>
82 return static_cast<Derived const&
>(*this)
85 std::tuple{std::ref(params)...},
90 template <
typename Derived,
typename Signature,
typename... Params>
91 class DefaultCallInterface<
96 util::type_list<Params...>>
103 return static_cast<Derived const&
>(*this)
106 std::tuple{std::ref(params)...},
111 template <
typename Derived,
typename Signature,
typename... Params>
112 class DefaultCallInterface<
117 util::type_list<Params...>>
124 return static_cast<Derived const&
>(*this)
127 std::tuple{std::ref(params)...},
132 template <
typename Derived,
typename Signature,
typename... Params>
133 class DefaultCallInterface<
138 util::type_list<Params...>>
145 return static_cast<Derived const&
>(*this)
148 std::tuple{std::ref(params)...},
153 template <
typename Derived,
typename Signature,
typename... Params>
154 class DefaultCallInterface<
159 util::type_list<Params...>>
166 return static_cast<Derived const&
>(*this)
169 std::tuple{std::ref(params)...},
182 template <
typename Derived,
typename Signature,
typename... Params>
188 util::type_list<Params...>>
191 template <
typename... Args>
192 requires(... && requirement_for<Args, Params>)
194 constexpr auto expect_call(Args&&... args)
196 return static_cast<Derived const&
>(*this)
201 template <
typename Derived,
typename Signature,
typename... Params>
207 util::type_list<Params...>>
210 template <
typename... Args>
211 requires(... && requirement_for<Args, Params>)
213 constexpr auto expect_call(Args&&... args)
const
215 return static_cast<Derived const&
>(*this)
220 template <
typename Derived,
typename Signature,
typename... Params>
226 util::type_list<Params...>>
229 template <
typename... Args>
230 requires(... && requirement_for<Args, Params>)
232 constexpr auto expect_call(Args&&... args) &
234 return static_cast<Derived const&
>(*this)
239 template <
typename Derived,
typename Signature,
typename... Params>
245 util::type_list<Params...>>
248 template <
typename... Args>
249 requires(... && requirement_for<Args, Params>)
251 constexpr auto expect_call(Args&&... args)
const&
253 return static_cast<Derived const&
>(*this)
258 template <
typename Derived,
typename Signature,
typename... Params>
264 util::type_list<Params...>>
267 template <
typename... Args>
268 requires(... && requirement_for<Args, Params>)
270 constexpr auto expect_call(Args&&... args) &&
272 return static_cast<Derived const&
>(*this)
277 template <
typename Derived,
typename Signature,
typename... Params>
283 util::type_list<Params...>>
286 template <
typename... Args>
287 requires(... && requirement_for<Args, Params>)
289 constexpr auto expect_call(Args&&... args)
const&&
291 return static_cast<Derived const&
>(*this)
296 template <
typename Signature>
297 using expectation_collection_ptr_for = std::shared_ptr<ExpectationCollection<signature_decay_t<Signature>>>;
299 template <
typename Signature,
typename ParamList = signature_param_list_t<Signature>>
302 template <
typename Signature,
typename... Params>
303 class BasicMock<Signature, util::type_list<Params...>>
304 :
public MockFrontend<
306 BasicMock<Signature, util::type_list<Params...>>,
307 signature_remove_call_convention_t<Signature>>,
308 public call_interface_t<
309 BasicMock<Signature, util::type_list<Params...>>,
314 friend class MockFrontend<BasicMock, SignatureT>;
315 friend call_interface_t<BasicMock, Signature>;
321 using ExpectationCollectionPtrT = expectation_collection_ptr_for<SignatureT>;
325 ExpectationCollectionPtrT collection,
326 MockSettings settings) noexcept
327 : m_Expectations{std::move(collection)},
328 m_Settings{std::move(settings)}
332 m_Settings.stacktraceSkip += 2u;
336 ExpectationCollectionPtrT m_Expectations;
337 MockSettings m_Settings;
341 reporting::TypeReport overloadReport,
342 std::tuple<std::reference_wrapper<std::remove_reference_t<Params>>...>&& params,
343 util::SourceLocation from)
const
345 return m_Expectations->handle_call(
346 reporting::TargetReport{
347 .name{*m_Settings.name},
348 .overloadReport{std::move(overloadReport)}},
350 .args{std::move(params)},
351 .fromCategory{refQualification},
352 .fromConstness{constQualification},
353 .fromSourceLocation{std::move(from)},
354 .baseStacktraceSkip{m_Settings.stacktraceSkip}});
357 template <
typename... Args>
359 constexpr auto make_expectation_builder(reporting::TypeReport overloadReport, Args&&... args)
const
361 return detail::make_expectation_builder(
363 reporting::TargetReport{.name = *m_Settings.name, .overloadReport = std::move(overloadReport)},
364 std::forward<Args>(args)...)
365 && expectation_policies::Category<refQualification>{}
366 && expectation_policies::Constness<constQualification>{};
370 template <
typename List>
371 struct expectation_collection_factory;
373 template <
typename... UniqueSignatures>
374 struct expectation_collection_factory<util::type_list<UniqueSignatures...>>
380 std::make_shared<ExpectationCollection<UniqueSignatures>>()...};
384 template <
typename... Signatures>
390 printing::type::detail::print_separated(
391 std::ostreambuf_iterator{out},
393 util::type_list<Signatures...>{});
396 return std::move(out).str();
458 template <
typename FirstSignature,
typename... OtherSignatures>
461 :
public detail::BasicMock<FirstSignature>,
462 public detail::BasicMock<OtherSignatures>...
465 using detail::BasicMock<FirstSignature>::operator();
466 using detail::BasicMock<FirstSignature>::expect_call;
467 using detail::BasicMock<OtherSignatures>::operator()...;
468 using detail::BasicMock<OtherSignatures>::expect_call...;
491 detail::expectation_collection_factory<
492 util::detail::unique_list_t<
495 complete_settings(std::move(
settings))}
521 template <
typename... Collections>
525 : detail::BasicMock<FirstSignature>{
526 util::detail::get<detail::expectation_collection_ptr_for<FirstSignature>>(collections),
528 detail::BasicMock<OtherSignatures>{
529 util::detail::get<detail::expectation_collection_ptr_for<OtherSignatures>>(collections),
536 static MockSettings complete_settings(MockSettings settings)
540 settings.name = detail::generate_mock_name<FirstSignature, OtherSignatures...>();
#define MIMICPP_ASSERT(condition, msg)
Definition Config.hpp:51
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
~Mock()=default
Defaulted destructor.
Mock & operator=(Mock &&)=default
Defaulted move assignment operator.
Mock(Mock &&)=default
Defaulted move constructor.
Mock(MockSettings settings)
Constructor, applying customized settings.
Definition Mock.hpp:489
Mock(const Mock &)=delete
Deleted copy constructor.
Mock & operator=(const Mock &)=delete
Deleted copy assignment operator.
Mock()
Default constructor.
Definition Mock.hpp:479
std::optional< StringT > name
Definition Mock.hpp:37
std::size_t stacktraceSkip
Definition Mock.hpp:38
static constexpr TypeReport make() noexcept
Definition TypeReport.hpp:39
constexpr bool is_overload_set_v
Convenience constant, exposing the value member of the actual type-trait.
Definition Fwd.hpp:347
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:102
constexpr Constness signature_const_qualification_v
Convenience constant, exposing the value member of the actual type-trait.
Definition Fwd.hpp:246
typename signature_decay< Signature >::type signature_decay_t
Convenience alias, exposing the type member alias of the actual type-trait.
Definition Fwd.hpp:214
constexpr bool signature_is_noexcept_v
Convenience constant, exposing the value member of the actual type-trait.
Definition Fwd.hpp:86
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:294
constexpr ValueCategory signature_ref_qualification_v
Convenience constant, exposing the value member of the actual type-trait.
Definition Fwd.hpp:262
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:118
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:230
typename info_for_signature< Signature >::type info_for_signature_t
Definition Call.hpp:47
Definition Settings.hpp:16
ValueCategory
Definition Fwd.hpp:34
@ any
Definition Fwd.hpp:37
@ lvalue
Definition Fwd.hpp:35
@ rvalue
Definition Fwd.hpp:36
std::basic_ostringstream< CharT, CharTraitsT > StringStreamT
Definition Format.hpp:35
Constness
Definition Fwd.hpp:27
@ as_const
Definition Fwd.hpp:29
@ non_const
Definition Fwd.hpp:28
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:391