meekrosoft / fff

A testing micro framework for creating function test doubles
Other
761 stars 167 forks source link

Mocking System functions in gtest #84

Open singh-arulraj opened 4 years ago

singh-arulraj commented 4 years ago

Hi, I am required to mock calloc function in gtest framework(c++ test code). Please let me know if I am doing anything wrong.

My Code `

include "fff.h"

extern "C" { DEFINE_FFF_GLOBALS; //FAKE_VALUE_FUNC(void , calloc, size_t, size_t); FAKE_VALUE_FUNC(void , malloc, size_t);

} `

I get the following errors

` X86/staging_dir/include/fff.h:1679:68: error: declaration of ‘void* malloc(size_t)’ has a different exception specifier RETURN_TYPE FFF_GCC_FUNCTION_ATTRIBUTES FUNCNAME(ARG0_TYPE arg0){ \ ^ X86/staging_dir/include/fff.h:6524:19: note: in definition of macro ‘EXPAND’

define EXPAND(x) x

               ^

X86/staging_dir/include/fff.h:6548:32: note: in expansion of macro ‘EXPAND’

define FUNCVALUE(N,...) EXPAND(FUNC_VALUE_N(N,__VA_ARGS__))

                            ^

X86/staging_dir/include/fff.h:6550:33: note: in expansion of macro ‘EXPAND’

define FUNC_VALUE_N(N,...) EXPAND(FAKE_VALUE_FUNC ## N(__VA_ARGS__))

                             ^

X86/staging_dir/include/fff.h:1714:5: note: in expansion of macro ‘DEFINE_FAKE_VALUE_FUNC1’ DEFINE_FAKE_VALUE_FUNC1(RETURN_TYPE, FUNCNAME, ARG0_TYPE) \ ^ X86/staging_dir/include/fff.h:6550:40: note: in expansion of macro ‘FAKE_VALUE_FUNC1’

define FUNC_VALUE_N(N,...) EXPAND(FAKE_VALUE_FUNC ## N(__VA_ARGS__))

                                    ^

X86/staging_dir/include/fff.h:6548:39: note: in expansion of macro ‘FUNC_VALUE_N’

define FUNCVALUE(N,...) EXPAND(FUNC_VALUE_N(N,__VA_ARGS__))

                                   ^

X86/staging_dir/include/fff.h:6546:41: note: in expansion of macro ‘FUNCVALUE

define FAKE_VALUE_FUNC(...) EXPAND(FUNCVALUE(PP_NARG_MINUS2(VA_ARGS), VA_ARGS))

                                     ^

X86/tmp/tmp/test/test_tmp.cpp:10:5: note: in expansion of macro ‘FAKE_VALUE_FUNC’ FAKE_VALUE_FUNC(void , malloc, size_t); ^ X86/tmp/tmp/test/test_tmp.cpp:10:29: error: from previous declaration ‘void malloc(size_t) throw ()’ FAKE_VALUE_FUNC(void *, malloc, size_t); `

r7vme commented 3 years ago

@singh-arulraj linker-based wrapping this is what I use

#include <cstdio>
#include <cstdlib>
#include "fff.h"
DEFINE_FFF_GLOBALS;
extern "C" {
FAKE_VALUE_FUNC(void *, __wrap_malloc, size_t);
}

int main(int argc, char **argv)
{
    void* m = malloc(1);
    printf("Fake malloc called %d times.", __wrap_malloc_fake.call_count);
    return 0;
}
> g++ -o 1 -Wl,--wrap=malloc 1.c && ./1
Fake malloc called 1 times.%