I need to mock macro defined in linux header:
#include <asm/types.h>
#include <linux/netlink.h>
for (msg_ptr = (struct nlmsghdr *) reply;
NLMSG_OK(msg_ptr, len);
msg_ptr = NLMSG_NEXT(msg_ptr, len))
{
// do something
}
NLMSG_OK
is defined in some linux header linux/netlink.h
.
How to mock it using google mock?
Does google mock support mocking macro?
How to mock it using google mock?
You cannot.
Does google mock support mocking macro?
No.
First of all, macros aren't functions! They're just text processing templates that are expanded from the preprocessing phase.
OK, let's assume the macro somehow expands to a global function call, they've given the following answer in their FAQ:
My code calls a static/global function. Can I mock it?
You can, but you need to make some changes.
In general, if you find yourself needing to mock a static function, it's a sign that your modules are too tightly coupled (and less flexible, less reusable, less testable, etc). You are probably better off defining a small interface and call the function through that interface, which then can be easily mocked. It's a bit of work initially, but usually pays for itself quickly.
This Google Testing Blog post says it excellently. Check it out
As for your comment "what you mean by using #undef and re #define?":
I meant you need to compile your code under test with your own macro definition, that injects a Google Mock class/function instead.
#if defined(UNDER_TEST)
#undef NLMSG_OK
#define NLMSG_OK(msg_ptr, len) \
// your mocking code
#endif
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments