Skip to content

Commit 0c7d64a

Browse files
committed
Make MOCK_METHOD_EXT public and allow passing specifiers to MOCK_METHOD
1 parent 48ba2e2 commit 0c7d64a

3 files changed

Lines changed: 58 additions & 21 deletions

File tree

include/turtle/detail/mock_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace mock { namespace detail {
7575
BOOST_PP_TUPLE_ELEM(3, M_n_S_t), \
7676
qualifier)
7777

78-
#define MOCK_METHOD_EXT(name, arity, signature, identifier, qualifiers) \
78+
#define MOCK_METHOD_EXT_I(name, arity, signature, identifier, qualifiers) \
7979
static_assert(arity == mock::detail::function_arity_t<signature>::value, "Arity mismatch"); \
8080
MOCK_PP_TUPLE_FOR_EACH(MOCK_METHOD_ITER, (name, arity, signature, identifier), qualifiers) \
8181
MOCK_METHOD_HELPER(signature, identifier)

include/turtle/mock.hpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,45 @@
7272
} \
7373
MOCK_METHOD_HELPER(void(), identifier)
7474

75-
/// MOCK_METHOD( [calling convention] name, arity[, signature[, identifier]] )
76-
/// generates both const and non-const methods
75+
/// MOCK_METHOD( [calling convention] name, arity[, signature[, identifier, [, specifiers]]] )
76+
/// generates both const and non-const methods by default, but can be changed by passing a specifier tuple.
7777
/// The 'signature' can be omitted if it can be uniquely identified from the base class
7878
/// if 'identifier' is omitted it will default to 'name'
79-
#define MOCK_METHOD(M, ...) \
80-
MOCK_METHOD_EXT(M, \
81-
BOOST_PP_VARIADIC_ELEM(0, __VA_ARGS__, ), \
82-
BOOST_PP_VARIADIC_ELEM(1, __VA_ARGS__, MOCK_SIGNATURE(M), ), \
83-
BOOST_PP_VARIADIC_ELEM(2, __VA_ARGS__, M, M, ), \
84-
(const, ))
79+
/// 'specifiers' is a potentially empty, tuple of method specifiers, e.g. (&, &&) or (const override)
80+
#define MOCK_METHOD(M, ...) \
81+
MOCK_METHOD_EXT_I(M, \
82+
BOOST_PP_VARIADIC_ELEM(0, __VA_ARGS__, ), \
83+
BOOST_PP_VARIADIC_ELEM(1, __VA_ARGS__, MOCK_SIGNATURE(M), ), \
84+
BOOST_PP_VARIADIC_ELEM(2, __VA_ARGS__, M, M, ), \
85+
BOOST_PP_VARIADIC_ELEM(3, __VA_ARGS__, (const, ), (const, ), (const, ), ))
8586
/// MOCK_CONST_METHOD( [calling convention] name, arity[, signature[, identifier]] )
8687
/// generates only the const version of the method
8788
/// The 'signature' can be omitted if it can be uniquely identified from the base class
8889
/// if 'identifier' is omitted it will default to 'name'
89-
#define MOCK_CONST_METHOD(M, ...) \
90-
MOCK_METHOD_EXT(M, \
91-
BOOST_PP_VARIADIC_ELEM(0, __VA_ARGS__, ), \
92-
BOOST_PP_VARIADIC_ELEM(1, __VA_ARGS__, MOCK_SIGNATURE(M), ), \
93-
BOOST_PP_VARIADIC_ELEM(2, __VA_ARGS__, M, M, ), \
94-
(const))
90+
#define MOCK_CONST_METHOD(M, ...) \
91+
MOCK_METHOD_EXT_I(M, \
92+
BOOST_PP_VARIADIC_ELEM(0, __VA_ARGS__, ), \
93+
BOOST_PP_VARIADIC_ELEM(1, __VA_ARGS__, MOCK_SIGNATURE(M), ), \
94+
BOOST_PP_VARIADIC_ELEM(2, __VA_ARGS__, M, M, ), \
95+
(const))
9596
/// MOCK_NON_CONST_METHOD( [calling convention] name, arity[, signature[, identifier]] )
9697
/// generates only the non-const version of the method
9798
/// The 'signature' can be omitted if it can be uniquely identified from the base class
9899
/// if 'identifier' is omitted it will default to 'name'
99-
#define MOCK_NON_CONST_METHOD(M, ...) \
100-
MOCK_METHOD_EXT(M, \
101-
BOOST_PP_VARIADIC_ELEM(0, __VA_ARGS__, ), \
102-
BOOST_PP_VARIADIC_ELEM(1, __VA_ARGS__, MOCK_SIGNATURE(M), ), \
103-
BOOST_PP_VARIADIC_ELEM(2, __VA_ARGS__, M, M, ), \
104-
())
100+
#define MOCK_NON_CONST_METHOD(M, ...) \
101+
MOCK_METHOD_EXT_I(M, \
102+
BOOST_PP_VARIADIC_ELEM(0, __VA_ARGS__, ), \
103+
BOOST_PP_VARIADIC_ELEM(1, __VA_ARGS__, MOCK_SIGNATURE(M), ), \
104+
BOOST_PP_VARIADIC_ELEM(2, __VA_ARGS__, M, M, ), \
105+
())
106+
107+
/// MOCK_METHOD_EXT( [calling convention] name, arity, qualifiers [, signature, [identifier]] )
108+
#define MOCK_METHOD_EXT(M, arity, ...) \
109+
MOCK_METHOD_EXT_I(M, \
110+
arity, \
111+
BOOST_PP_VARIADIC_ELEM(1, __VA_ARGS__, MOCK_SIGNATURE(M), ), \
112+
BOOST_PP_VARIADIC_ELEM(2, __VA_ARGS__, M, M, ), \
113+
BOOST_PP_VARIADIC_ELEM(0, __VA_ARGS__, ))
105114

106115
/// MOCK_FUNCTION( [calling convention] name, arity, signature[, identifier] )
107116
/// if 'identifier' is omitted it will default to 'name'

test/test_mock.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,15 @@ struct base
327327
virtual void m1() = 0;
328328
virtual void m10() const = 0;
329329
virtual void m11() = 0;
330+
virtual void m12() noexcept = 0;
331+
virtual void m13() && = 0;
332+
virtual void m14() = 0;
333+
virtual void m14(int, float) = 0;
334+
virtual void m14(int, int) = 0;
335+
virtual void m14(int) = 0;
336+
virtual void m14(int) const noexcept = 0;
337+
virtual void m15() & = 0;
338+
virtual void m15() && = 0;
330339
};
331340

332341
MOCK_BASE_CLASS(variadic, base)
@@ -340,9 +349,28 @@ MOCK_BASE_CLASS(variadic, base)
340349
MOCK_NON_CONST_METHOD(m11, 0)
341350
MOCK_NON_CONST_METHOD(m6, 0, void())
342351
MOCK_NON_CONST_METHOD(m7, 0, void(), m7)
352+
353+
MOCK_METHOD_EXT(m12, 0, (noexcept override))
354+
MOCK_METHOD_EXT(m13, 0, (&&override))
355+
// Overloaded method
356+
MOCK_METHOD(m14, 0, void(), m14_empty)
357+
MOCK_METHOD(m14, 2, void(int, float), m14_int_float, ())
358+
MOCK_METHOD(m14, 2, void(int, int), m14_int_int, (override))
359+
MOCK_METHOD(m14, 1, void(int), m14_int, (override, const noexcept override))
360+
MOCK_METHOD_EXT(m15, 0, (&override, &&override), void())
343361
MOCK_STATIC_METHOD(m8, 0, void())
344362
MOCK_STATIC_METHOD(m9, 0, void(), m9)
345363
};
364+
void instantiate_class()
365+
{
366+
variadic inst; // If this compiles all pure virtual methods were mocked
367+
const variadic& cinst = inst;
368+
static_assert(noexcept(inst.m12()), "noexcept should be kept");
369+
static_assert(!noexcept(inst.m14()), "noexcept should not be set");
370+
static_assert(noexcept(cinst.m14(1)), "noexcept should be kept");
371+
static_assert(!noexcept(inst.m14(1)), "noexcept should not be set");
372+
static_assert(noexcept(std::declval<const variadic>().m14(1)), "noexcept should be kept");
373+
}
346374

347375
template<typename T>
348376
MOCK_BASE_CLASS(variadic_tpl, base)

0 commit comments

Comments
 (0)