From 4455222a62e8880ee6718847db76362880ccbd70 Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Thu, 29 Mar 2018 15:39:15 +0200 Subject: [PATCH] Removed support for directly mocking a protected member function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ed36823235ce1d0d847ee3d180ede9b3cdc00df8 might not be needed any more… --- ....cpp => limitations_protected_private_method.cpp} | 11 +++++++---- doc/limitations.qbk | 9 +++++---- include/turtle/mock.hpp | 12 ++++-------- test/test_integration.cpp | 8 ++++---- test/test_mock.cpp | 5 ++--- 5 files changed, 22 insertions(+), 23 deletions(-) rename doc/example/{limitations_private_method.cpp => limitations_protected_private_method.cpp} (62%) diff --git a/doc/example/limitations_private_method.cpp b/doc/example/limitations_protected_private_method.cpp similarity index 62% rename from doc/example/limitations_private_method.cpp rename to doc/example/limitations_protected_private_method.cpp index b03c086..327cf2a 100644 --- a/doc/example/limitations_private_method.cpp +++ b/doc/example/limitations_protected_private_method.cpp @@ -12,18 +12,21 @@ namespace { -//[ limitations_private_method_problem +//[ limitations_protected_private_method_problem class base { 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_METHOD( method, 0, void() ) + MOCK_METHOD( method_1, 0, void() ) + MOCK_METHOD( method_2, 0, void() ) }; //] } diff --git a/doc/limitations.qbk b/doc/limitations.qbk index e128b74..ece78fd 100644 --- a/doc/limitations.qbk +++ b/doc/limitations.qbk @@ -11,7 +11,7 @@ [import example/limitations_non_virtual_method.cpp] [import example/limitations_template_base_class_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_const_parameter_warning.cpp] @@ -116,18 +116,19 @@ A workaround would be to add the signature to MOCK_METHOD : Given : -[limitations_private_method_problem] +[limitations_protected_private_method_problem] the following code does not compile : 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 : -[limitations_private_method_solution] +[limitations_protected_private_method_solution] [endsect] diff --git a/include/turtle/mock.hpp b/include/turtle/mock.hpp index 78fb185..e92f458 100644 --- a/include/turtle/mock.hpp +++ b/include/turtle/mock.hpp @@ -43,8 +43,8 @@ #else // MOCK_VARIADIC_MACROS -#define MOCK_BASE_CLASS(T, B) \ - struct T : B, mock::object, mock::detail::base< B > +#define MOCK_BASE_CLASS(T, I) \ + struct T : I, mock::object, mock::detail::base< I > #define MOCK_FUNCTOR(f, S) \ 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_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, ...) \ - MOCK_METHOD_SIGNATURE(M, \ + MOCK_METHOD_EXT(M, \ MOCK_VARIADIC_ELEM_0(__VA_ARGS__ ), \ MOCK_VARIADIC_ELEM_1(__VA_ARGS__, MOCK_SIGNATURE(M)), \ MOCK_VARIADIC_ELEM_2(__VA_ARGS__, M, M)) @@ -228,8 +225,7 @@ #else // MOCK_VARIADIC_MACROS #define MOCK_METHOD(M, n) \ - typedef MOCK_SIGNATURE(M) M##_sig_type; \ - MOCK_METHOD_EXT(M, n, M##_sig_type, M) + MOCK_METHOD_EXT(M, n, MOCK_SIGNATURE(M), M) #define MOCK_FUNCTION(F, n, S, t) \ MOCK_FUNCTION_AUX(F, n, S, t, inline,) diff --git a/test/test_integration.cpp b/test/test_integration.cpp index 7636e47..d1b7567 100644 --- a/test/test_integration.cpp +++ b/test/test_integration.cpp @@ -84,8 +84,8 @@ namespace class my_ambiguited_interface : boost::noncopyable { public: - virtual ~my_ambiguited_interface() {} - + virtual ~my_ambiguited_interface() + {} virtual void my_method() = 0; virtual void my_method( int ) = 0; }; @@ -110,8 +110,8 @@ namespace class my_const_ambiguited_interface : boost::noncopyable { public: - virtual ~my_const_ambiguited_interface() {} - + virtual ~my_const_ambiguited_interface() + {} virtual void my_method() = 0; virtual void my_method() const = 0; }; diff --git a/test/test_mock.cpp b/test/test_mock.cpp index 9eff85b..60838c2 100644 --- a/test/test_mock.cpp +++ b/test/test_mock.cpp @@ -364,7 +364,7 @@ namespace { virtual ~base() {} - protected: + virtual void m1() = 0; }; @@ -413,7 +413,7 @@ namespace { virtual ~base() {} - protected: + virtual void m1() = 0; }; @@ -444,7 +444,6 @@ namespace stdcall virtual ~base() {} - protected: virtual void MOCK_STDCALL m1() = 0; };