Add support for unlimited number of sequences in one call

This removes the need for those preprocessor macros and the MOCK_MAX_SEQUENCES define.
This commit is contained in:
Alexander Grund 2022-02-07 19:26:26 +01:00
parent c34e4224fe
commit bb77c1702f
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
5 changed files with 18 additions and 27 deletions

View file

@ -15,7 +15,7 @@ Released -
* Removed MOCK_*_TPL as they are no longer required, use the non _TPL variant even for templates
* Added MOCK_PROTECT_SIGNATURE to pass function signatures with commas in the return type
* Remove support for protecting function signatures via BOOST_IDENTITY_TYPE, use MOCK_PROTECT_SIGNATURE instead
* Add support for unlimitted number of arguments making MOCK_MAX_ARGS superflous
* Add support for unlimitted number of arguments and sequences making MOCK_MAX_ARGS and MOCK_MAX_SEQUENCES superflous
[endsect]

View file

@ -449,10 +449,7 @@ Synopsis :
Each sequence is an instance of mock::sequence.
The maximum number of sequences that can be set is MOCK_MAX_SEQUENCES which defaults to 10. If needed the value can be increased before including the library :
#define MOCK_MAX_SEQUENCES 12
#include <turtle/mock.hpp>
The maximum number of sequences that can be set is basically unlimited.
Example :
@ -467,7 +464,7 @@ An action performs additional treatments after an expectation has been deemed va
Synopsis :
MOCK_EXPECT( identifier ).returns( value ); // stored internally by copy
MOCK_EXPECT( identifier ).moves( value ); // stored internally by copy
MOCK_EXPECT( identifier ).moves( value ); // stored internally by copy/move
MOCK_EXPECT( identifier ).throws( exception ); // stored internally by copy
MOCK_EXPECT( identifier ).calls( functor ); // stored internally by copy, throws std::invalid_argument if empty

View file

@ -18,10 +18,6 @@
# define MOCK_USE_BOOST_TEST
#endif
#ifndef MOCK_MAX_SEQUENCES
# define MOCK_MAX_SEQUENCES 10
#endif
#if !defined(BOOST_NO_CXX11_HDR_MUTEX) && !defined(BOOST_NO_0X_HDR_MUTEX)
# ifndef MOCK_NO_HDR_MUTEX
# define MOCK_HDR_MUTEX

View file

@ -13,8 +13,6 @@
#include "expectation.hpp"
#include "mutex.hpp"
#include "verifiable.hpp"
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <boost/test/utils/lazy_ostream.hpp>
#include <list>
#include <memory>
@ -187,19 +185,14 @@ namespace mock { namespace detail {
return *this;
}
#define MOCK_FUNCTION_IN_ADD(z, n, d) this->e_->add(s##n);
#define MOCK_FUNCTION_IN(z, n, d) \
wrapper& in(BOOST_PP_ENUM_PARAMS(n, sequence& s)) \
{ \
BOOST_PP_REPEAT(n, MOCK_FUNCTION_IN_ADD, _) \
return *this; \
}
BOOST_PP_REPEAT(MOCK_MAX_SEQUENCES, MOCK_FUNCTION_IN, _)
#undef MOCK_FUNCTION_IN
#undef MOCK_FUNCTION_IN_ADD
/// Ensure the expectation is met in the given sequence(s)
template<class... MockSequences>
wrapper& in(sequence& s0, MockSequences&... s)
{
using expander = int[];
(void)expander{ (e_->add(s0), 0), (e_->add(s), 0)... };
return *this;
}
template<typename TT>
void calls(TT t)

View file

@ -10,6 +10,7 @@
#include <turtle/detail/function.hpp>
#include <turtle/sequence.hpp>
#include <boost/test/unit_test.hpp>
#include <array>
BOOST_FIXTURE_TEST_CASE(registering_to_a_sequence_and_calling_out_of_order_throws, mock_error_fixture)
{
@ -97,9 +98,13 @@ BOOST_FIXTURE_TEST_CASE(resetting_an_expectation_removes_it_from_order_call_enfo
BOOST_FIXTURE_TEST_CASE(an_expectation_can_be_used_in_several_sequences, mock_error_fixture)
{
mock::sequence s1, s2;
std::array<mock::sequence, 20> s;
mock::detail::function<void()> e;
e.expect().once().in(s1, s2);
// One can use any amount of sequences if wanted
// clang-format off
e.expect().once().in(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9],
s[10], s[11], s[12], s[13], s[14], s[15], s[16], s[17], s[18], s[19]);
// clang-format on
BOOST_CHECK_NO_THROW(e());
BOOST_CHECK(e.verify());
CHECK_CALLS(1);