Replace Boost PP_Iterate by C++11 variadic templates

This allows support for any number of arguments and makes setting MOCK_MAX_ARGS unnecessary.
It also allows for easier debugging due to being able to step into actual code instead of preprocessor generated stuff
This commit is contained in:
Alexander Grund 2022-01-31 19:27:57 +01:00
parent 90d9ac8055
commit fca30e7780
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
17 changed files with 368 additions and 472 deletions

View file

@ -40,7 +40,7 @@ set(testsUsingUndefinedCPP test_function test_integration)
foreach(testFile IN LISTS testFiles)
get_filename_component(name ${testFile} NAME_WE)
foreach(testVariant IN ITEMS "" _max_args _use_conversions _thread_safe)
foreach(testVariant IN ITEMS "" _use_conversions _thread_safe)
set(curName ${name}${testVariant})
add_executable(${curName} ${testFile})
if(name IN_LIST testsUsingUndefinedCPP)
@ -50,7 +50,6 @@ foreach(testFile IN LISTS testFiles)
add_test(NAME ${curName} COMMAND ${curName})
endforeach()
target_compile_definitions(${name}_max_args PRIVATE MOCK_MAX_ARGS=21)
target_compile_definitions(${name}_use_conversions PRIVATE MOCK_USE_CONVERSIONS)
target_link_libraries(${name}_thread_safe PRIVATE Boost::thread)

View file

@ -24,7 +24,6 @@ alias mock_inspect :
rule run-test ( name )
{
run $(name) defined_1.cpp defined_2.cpp undefined.cpp /boost//unit_test_framework : : : : $(name)_ ;
run $(name) undefined.cpp /boost//unit_test_framework : : : <define>MOCK_MAX_ARGS=21 : $(name)_max_args ;
run $(name) undefined.cpp /boost//unit_test_framework : : : <define>MOCK_USE_CONVERSIONS : $(name)_use_conversions ;
run $(name) undefined.cpp /boost//unit_test_framework /boost//thread : : : <define>MOCK_THREAD_SAFE <define>BOOST_THREAD_USES_MOVE <threading>multi : $(name)_thread_safe ;
}

View file

@ -1,24 +0,0 @@
// http://turtle.sourceforge.net
//
// Copyright Mathieu Champlon 2011
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <turtle/mock.hpp>
BOOST_STATIC_ASSERT(MOCK_MAX_ARGS == 9);
namespace {
struct my_base
{
virtual ~my_base() = default;
virtual void my_method(int, int, int, int, int, int, int, int, int, int) = 0;
};
MOCK_BASE_CLASS(my_class, my_base)
{
MOCK_METHOD(my_method, 10)
};
} // namespace

View file

@ -6,7 +6,7 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <turtle/detail/function.hpp>
#include <turtle/detail/expectation.hpp>
#include <turtle/matcher.hpp>
#include <boost/test/unit_test.hpp>
#include <functional>
@ -88,16 +88,16 @@ std::string serialize(const T& t)
BOOST_AUTO_TEST_CASE(default_matcher_is_serialized_to_any)
{
using mock::detail::default_matcher;
BOOST_TEST(serialize(default_matcher<void()>{}) == "");
BOOST_TEST(serialize(default_matcher<void(int)>{}) == "any");
BOOST_TEST(serialize(default_matcher<void(int, int)>{}) == "any, any");
BOOST_TEST(serialize(default_matcher<void(int, int, int, int, int)>{}) == "any, any, any, any, any");
BOOST_TEST(serialize(default_matcher<>{}) == "");
BOOST_TEST(serialize(default_matcher<int>{}) == "any");
BOOST_TEST(serialize(default_matcher<int, int>{}) == "any, any");
BOOST_TEST(serialize(default_matcher<int, int, int, int, int>{}) == "any, any, any, any, any");
}
namespace {
struct custom_constraint
{
int expected_ = 42;
int expected_;
custom_constraint(int expected = 42) : expected_(expected) {}
friend std::ostream& operator<<(std::ostream& s, const custom_constraint& c)
{
@ -110,16 +110,15 @@ struct custom_constraint
BOOST_AUTO_TEST_CASE(single_matcher_serializes)
{
using mock::detail::single_matcher;
BOOST_TEST(serialize(single_matcher<void(int), void(int)>(1)) == "1");
BOOST_TEST(serialize(single_matcher<void(int, int), void(int, int)>(1, 2)) == "1, 2");
BOOST_TEST(serialize(single_matcher<void(int), int>(1)) == "1");
BOOST_TEST(serialize(single_matcher<void(int, int), int, int>(1, 2)) == "1, 2");
BOOST_TEST(
serialize(
single_matcher<void(int, int, mock::constraint<custom_constraint>, int, int), void(int, int, int, int, int)>(
1, 2, custom_constraint(), 4, 5)) == "1, 2, custom42, 4, 5");
serialize(single_matcher<void(int, int, mock::constraint<custom_constraint>, int, int), int, int, int, int, int>(
1, 2, custom_constraint(), 4, 5)) == "1, 2, custom42, 4, 5");
}
BOOST_AUTO_TEST_CASE(multi_matcher_serializes)
{
using mock::detail::multi_matcher;
BOOST_TEST(serialize(multi_matcher<custom_constraint, void(int)>(custom_constraint(1337))) == "custom1337");
BOOST_TEST(serialize(multi_matcher<custom_constraint, int>(custom_constraint(1337))) == "custom1337");
}

View file

@ -11,28 +11,30 @@
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/test/unit_test.hpp>
#define IDENTITY(z, n, d) d
#define IDENTITY(z, n, text) text
// Number of arguments is (now) unlimited, so take any high value here
#define NUM_TEST_ARGS 100
namespace {
struct my_custom_mock
{
MOCK_METHOD(method, MOCK_MAX_ARGS, void(BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, int)), tag)
MOCK_METHOD(method2, MOCK_MAX_ARGS, int(BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, int)), tag_2)
MOCK_METHOD(method, NUM_TEST_ARGS, void(BOOST_PP_ENUM(NUM_TEST_ARGS, IDENTITY, int)), tag)
MOCK_METHOD(method2, NUM_TEST_ARGS, int(BOOST_PP_ENUM(NUM_TEST_ARGS, IDENTITY, int)), tag_2)
};
} // namespace
BOOST_FIXTURE_TEST_CASE(call_mock_method_with_max_number_of_args, mock_error_fixture)
{
my_custom_mock m;
MOCK_EXPECT(m.tag).once().with(BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, 0));
m.method(BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, 0));
MOCK_EXPECT(m.tag).once().with(BOOST_PP_ENUM(NUM_TEST_ARGS, IDENTITY, 0));
m.method(BOOST_PP_ENUM(NUM_TEST_ARGS, IDENTITY, 0));
CHECK_CALLS(1);
}
BOOST_FIXTURE_TEST_CASE(call_mock_method_with_max_number_of_args_and_a_return_value, mock_error_fixture)
{
my_custom_mock m;
MOCK_EXPECT(m.tag_2).once().with(BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, 0)).returns(42);
BOOST_CHECK_EQUAL(42, m.method2(BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, 0)));
MOCK_EXPECT(m.tag_2).once().with(BOOST_PP_ENUM(NUM_TEST_ARGS, IDENTITY, 0)).returns(42);
BOOST_CHECK_EQUAL(42, m.method2(BOOST_PP_ENUM(NUM_TEST_ARGS, IDENTITY, 0)));
CHECK_CALLS(1);
}