I’ve not written anything here in about 4 years, and many things have changed in the meantime. After becoming a father and start rising my twins, I was less active in the community. But I hope this will change in future.
Recently, I’ve put some effort to enhance PowerFake further to fix some known issues and enhance its feature set, and also provide some brief docs! Among other things, I figured that Google Test/Google Mock are pretty popular; while I personally prefer Boost Test / FakeIt. Therefore, I also decided to look into integrating with Google Mock (gMock); and I was able to put something together for it. So, it is now possible to mock non-virtual member functions, static member functions and free functions using gMock too. All someone needs to do is to use macros provided by PowerFake instead of gMock macros to define mocked functions, and to inherit from GPowerFake<Class>
instead of Class
to mock member functions:
class MockFreeFunctions
{
public:
GPFK_MOCK_FUNCTION(void, normal_func, (int b, const char **const *c,
const std::string &d, const char *(*e)(const char *)));
GPFK_MOCK_FUNCTION(std::string, overloaded2, (int b));
};
class MockSample: public GPowerFake<SampleClass>
{
public:
GPFK_MOCK_METHOD(void, CallThis, (), (const));
GPFK_MOCK_METHOD(int, OverloadedCall, ());
// free functions can be mocked here too
GPFK_MOCK_FUNCTION(void, noexcept_func, ());
};
void GMockSamples()
{
MockSample mock;
MockFreeFunctions freemock;
EXPECT_CALL(mock, CallThis()).Times(AtLeast(1));
EXPECT_CALL(mock, OverloadedCall()).Times(AtLeast(1));
EXPECT_CALL(freemock, normal_func(1, nullptr, "", nullptr))
.Times(AtLeast(1));
EXPECT_CALL(freemock, overloaded2(_)).Times(AtLeast(1));
EXPECT_CALL(mock, noexcept_func()).Times(AtLeast(1));
normal_func(1, nullptr, "", nullptr);
noexcept_func();
SampleClass s;
s.CallThis();
s.OverloadedCall();
overloaded2(60);
}
In addition to gMock integration support, there are also a number of improvements:
- Cleaner API: A unified
WRAP_FUNCTION()
macro instead of having separate macros for private and static member functions HIDE_FUNCTION()
macro: supports capturing function calls in the same translation unit (if the call is not optimized out completely), although you’ll lose access the the real function completely. Also, it only works with GNU LD linker currently.- Add support for Clang & LLD linker in addition to GCC/MinGW & LD linker
- Much faster link times
- Easier CMake integration, specially for compiling with MinGW
- Add a brief usage doc to README
- Unified
TAG_PRIVATE()
macro
That’s it for now!