[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cmocka mocking capability in windows is missing

On Tuesday 28 July 2015 18:35:09 Shai Peretz wrote:
> 1.
> ..\..\src\cmocka.c(1565): warning C4013: 'va_copy' undefined; assuming
> extern returning int
> ..\..\src\cmocka.c(2489): error C2059: syntax error
> Doesn't recognize va_copy (not available in VS10 probably)
> Doesn't like this syntax:
> cm_tests[i] = (struct CMUnitTestState) {
>             .test = &tests[i],
>             .status = CM_TEST_NOT_STARTED,
>             .state = NULL,
>         };

So if you're using old MSVC versions you probably need to add

#  ifndef va_copy
#   define va_copy(dest, src) (dest = src)
#  endif

to cmocka.c ...


> 2.
> Let's take the chef_cook as an example:
> You have the real chef_cook() and you have __wrap_chef_cook()
> Also you have waiter_process() which calls chef_cook().
> waiter_process() is the function you want to test.
> Let's say you have this files:
> chef_cook.h : contain chef_cook() declaration
> chef_cook.c : contain chef_cook() definition
> waiter_process.h : contain waiter_process() declaration
> waiter_process.c : contain waiter_process() definition
> Let's say you compile waiter_process.c and chef_cook.c into one library*
> rest.lib*
> also you have
> test_waiter_process.c
> which you compile (*and link with rest.lib*) and create an executable for
> it to run
> If I do
> #define chef_cook __wrap_chef_cook
> #endif
> as you suggested *in a common header shared by all above files* it will
> replace real chef_cook into __wrap_chef_cook in all above files which I
> don't want because I will not be able to mock it (maybe not even build it
> since i'll have 2 implementation of the same function).
> Meaning it will not only replace the function name in the chef_cook.h
> declaration but also the function name in chef_cook.c
> so you will have __wrap_chef_cook() in both chef_cook.c and in
> test_waiter_process.c which will cause a build error or at least I won't be
> able to mock it since waiter_process() will call the __wrap_chef_cook which
> exist in chef_cook.c and not the one that exists in test_waiter_process.c
> (which is the mock function)

Well you normally would only define it in module you test and include the .c 

#undef chef_cook
#define chef_cook __wrap_chef_cook

#include waiter_process.c

For that you do not create a library but only link what you need to the test 
executable ...

> If I do it (....#define chef_cook __wrap_chef_cook...) *only in
> the test_waiter_process.c* file then it will always call the real
> chef_cook() from waiter_process() and not the mock version of it
> (__wrap_chef_cook) since I already created the *rest.lib* *which is using
> the real function*

Don't create a lib ...

If you get it working it would be great to have an example in the cmocka tree.


	-- andreas

Andreas Schneider                   GPG-ID: CC014E3D
www.cryptomilk.org                asn@xxxxxxxxxxxxxx

cmocka mocking capability in windows is missingShai Peretz <ssperetz@xxxxxxxxx>
Re: cmocka mocking capability in windows is missingAndreas Schneider <asn@xxxxxxxxxxxxxx>