Removed support for directly mocking a protected member function

Taking a function pointer on a base member protected function is actually invalid.
This could work with a pointer on the derived class member function, that is &derived::method instead of &base::method however there is no way to pass the derived class from MOCK_BASE_CLASS to the mock::detail::base helper if template classes are to be supported.

Anyway the now documented workaround is simple enough and the same as for private member functions.

As a side note the changes from ed36823235 might not be needed any more…
This commit is contained in:
Mathieu Champlon 2018-03-29 15:39:15 +02:00 committed by Mathieu Champlon
parent caef00d2e3
commit 4455222a62
5 changed files with 22 additions and 23 deletions

View file

@ -12,18 +12,21 @@
namespace namespace
{ {
//[ limitations_private_method_problem //[ limitations_protected_private_method_problem
class base class base
{ {
private: private:
virtual void method() = 0; virtual void method_1() = 0;
private:
virtual void method_2() = 0;
}; };
//] //]
//[ limitations_private_method_solution //[ limitations_protected_private_method_solution
MOCK_BASE_CLASS( mock_base, base ) MOCK_BASE_CLASS( mock_base, base )
{ {
MOCK_METHOD( method, 0, void() ) MOCK_METHOD( method_1, 0, void() )
MOCK_METHOD( method_2, 0, void() )
}; };
//] //]
} }

View file

@ -11,7 +11,7 @@
[import example/limitations_non_virtual_method.cpp] [import example/limitations_non_virtual_method.cpp]
[import example/limitations_template_base_class_method.cpp] [import example/limitations_template_base_class_method.cpp]
[import example/limitations_template_method.cpp] [import example/limitations_template_method.cpp]
[import example/limitations_private_method.cpp] [import example/limitations_protected_private_method.cpp]
[import example/limitations_comma_in_macro.cpp] [import example/limitations_comma_in_macro.cpp]
[import example/limitations_const_parameter_warning.cpp] [import example/limitations_const_parameter_warning.cpp]
@ -116,18 +116,19 @@ A workaround would be to add the signature to MOCK_METHOD :
Given : Given :
[limitations_private_method_problem] [limitations_protected_private_method_problem]
the following code does not compile : the following code does not compile :
MOCK_BASE_CLASS( mock_base, base ) MOCK_BASE_CLASS( mock_base, base )
{ {
MOCK_METHOD( method, 0 ) // this fails because 'method' is not visible MOCK_METHOD( method_1, 0 ) // this fails because a function pointer on 'base::method_1' is not allowed
MOCK_METHOD( method_2, 0 ) // this fails because 'method_2' is not visible
}; };
A workaround would be to add the signature to MOCK_METHOD : A workaround would be to add the signature to MOCK_METHOD :
[limitations_private_method_solution] [limitations_protected_private_method_solution]
[endsect] [endsect]

View file

@ -43,8 +43,8 @@
#else // MOCK_VARIADIC_MACROS #else // MOCK_VARIADIC_MACROS
#define MOCK_BASE_CLASS(T, B) \ #define MOCK_BASE_CLASS(T, I) \
struct T : B, mock::object, mock::detail::base< B > struct T : I, mock::object, mock::detail::base< I >
#define MOCK_FUNCTOR(f, S) \ #define MOCK_FUNCTOR(f, S) \
mock::detail::functor< MOCK_FUNCTION_TYPE((S),) > f, f##_mock mock::detail::functor< MOCK_FUNCTION_TYPE((S),) > f, f##_mock
@ -177,11 +177,8 @@
#define MOCK_VARIADIC_ELEM_1(e0, e1, ...) e1 #define MOCK_VARIADIC_ELEM_1(e0, e1, ...) e1
#define MOCK_VARIADIC_ELEM_2(e0, e1, e2, ...) e2 #define MOCK_VARIADIC_ELEM_2(e0, e1, e2, ...) e2
#define MOCK_METHOD_SIGNATURE(M, n, S, t) \
typedef MOCK_FUNCTION_TYPE((S),) BOOST_PP_CAT(t,_sig_type); \
MOCK_METHOD_EXT(M, n, BOOST_PP_CAT(t,_sig_type), t)
#define MOCK_METHOD(M, ...) \ #define MOCK_METHOD(M, ...) \
MOCK_METHOD_SIGNATURE(M, \ MOCK_METHOD_EXT(M, \
MOCK_VARIADIC_ELEM_0(__VA_ARGS__ ), \ MOCK_VARIADIC_ELEM_0(__VA_ARGS__ ), \
MOCK_VARIADIC_ELEM_1(__VA_ARGS__, MOCK_SIGNATURE(M)), \ MOCK_VARIADIC_ELEM_1(__VA_ARGS__, MOCK_SIGNATURE(M)), \
MOCK_VARIADIC_ELEM_2(__VA_ARGS__, M, M)) MOCK_VARIADIC_ELEM_2(__VA_ARGS__, M, M))
@ -228,8 +225,7 @@
#else // MOCK_VARIADIC_MACROS #else // MOCK_VARIADIC_MACROS
#define MOCK_METHOD(M, n) \ #define MOCK_METHOD(M, n) \
typedef MOCK_SIGNATURE(M) M##_sig_type; \ MOCK_METHOD_EXT(M, n, MOCK_SIGNATURE(M), M)
MOCK_METHOD_EXT(M, n, M##_sig_type, M)
#define MOCK_FUNCTION(F, n, S, t) \ #define MOCK_FUNCTION(F, n, S, t) \
MOCK_FUNCTION_AUX(F, n, S, t, inline,) MOCK_FUNCTION_AUX(F, n, S, t, inline,)

View file

@ -84,8 +84,8 @@ namespace
class my_ambiguited_interface : boost::noncopyable class my_ambiguited_interface : boost::noncopyable
{ {
public: public:
virtual ~my_ambiguited_interface() {} virtual ~my_ambiguited_interface()
{}
virtual void my_method() = 0; virtual void my_method() = 0;
virtual void my_method( int ) = 0; virtual void my_method( int ) = 0;
}; };
@ -110,8 +110,8 @@ namespace
class my_const_ambiguited_interface : boost::noncopyable class my_const_ambiguited_interface : boost::noncopyable
{ {
public: public:
virtual ~my_const_ambiguited_interface() {} virtual ~my_const_ambiguited_interface()
{}
virtual void my_method() = 0; virtual void my_method() = 0;
virtual void my_method() const = 0; virtual void my_method() const = 0;
}; };

View file

@ -364,7 +364,7 @@ namespace
{ {
virtual ~base() virtual ~base()
{} {}
protected:
virtual void m1() = 0; virtual void m1() = 0;
}; };
@ -413,7 +413,7 @@ namespace
{ {
virtual ~base() virtual ~base()
{} {}
protected:
virtual void m1() = 0; virtual void m1() = 0;
}; };
@ -444,7 +444,6 @@ namespace stdcall
virtual ~base() virtual ~base()
{} {}
protected:
virtual void MOCK_STDCALL m1() = 0; virtual void MOCK_STDCALL m1() = 0;
}; };