-
-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Quick definition:
The term stub is like a mock a configurable callable object, but in a more permissive manner.
Calls do not need to be explicitly enabled and will always redirect to a specific target.
Other than that, all expectations can be formulated.
GMock for example all calls are "naggy", which means they are accepted and return a default value when no match can be made.
see: https://google.github.io/googletest/gmock_cook_book.html#NiceStrictNaggy
So, in fact Mock vs Stub boils more or less down to strict vs permissive.
To make it consistent for all return-types, we should always require a target for the redirect. This needs to be enforced by the Stub Ctor.
As mock supports multiple signatures, Stub should support that aswell. An important question to be answered is, do we allow a 1:1 relation between Target and Signature or do we require that a single Target must be callable with all signatures.
The latter is probably the easier approach to implement.
As Mock is already a solid building block, we should build Stubs on top of them. This is the first draft:
namespace mimicpp
{
template <typename Signature, typename Target>
[[nodiscard]]
ScopedExpectation InitStub(detail::BasicMock<Signature>& mock, Target target)
{
return std::apply(
[&]<typename... M>(M&&... m) -> ScopedExpectation {
return mock.expect_call(std::forward<M>(m)...)
&& expect::at_least(0)
&& finally::returns_apply_all_result_of(std::move(target));
},
std::array<WildcardMatcher, signature_arity_v<Signature>>{});
}
template <typename FirstSignature, typename... OtherSignatures>
class Stub
: private Mock<FirstSignature, OtherSignatures...>
{
using Super = Mock<FirstSignature, OtherSignatures...>;
public:
explicit Stub(auto target)
: m_Expectations{
InitStub<FirstSignature>(*this, target),
InitStub<OtherSignatures>(*this, target)...}
{
}
using Super::expect_call;
using Super::operator();
private:
std::array<ScopedExpectation, 1u + sizeof...(OtherSignatures)> m_Expectations;
};
}Beside that, we need to think about enhancing the existing MAKE_MOCK Macros and adapt them for the new Stub.