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

Re: mocking without wrap functions


On Monday 13 January 2014 22:33:21 maxxedev maxxedev wrote:
> Hi,

Hi M,

> I have an idea that may make it easier to define mocks. Problem right now,
> as it appears to me, is that anytime you want to do a simple
> expect-and-verify mock on a function, you have to write a wrapper mock
> function by hand. I came up with a way to auto-generate wrapper mock
> functions that forward calls to a generic expect/verify function.
> 
> Consider the following test_string_equals() test case here where I mock
> string_equals() function:
> 
>     $$Mocks(string_equals)
>     TestCase(test_string_equals) {
>         int rv;
> 
>         defineMock(string_equals) as {
>             doExpectString("a0exp");
>             doExpectString("a1exp");
>             doReturn(123);
>         } finish;
> 
>         rv = string_equals("a0exp", "a1exp");
>         assert_int_equal(rv, 123);
>     }

this sounds really interesting. Mocking can normally only be done using 
function pointers. But for this your application needs to be written like 
that.

With Linux and gcc/ld we have the special wrap function which makes our life 
easier.

> I did not have to write __wrap_string_equals function; $$Mocks "annotation"
> (similar to java @foobar annotations) generates a forwarding wrapper
> function. The defineMock() defines the mock behavior for the generic
> expect/verify function.
> The same would be if I wanted to mock a string_startsWith function: I
> wouldn't need to write a wrapper function.
> 
> The complete example is included as an attachment. Note that my method does
> use gcc C extensions[1]. I suspect it may also be possible to write a
> similar thing using vararg macros.

If we add macros then they should be generic and not gcc specific.


Please keep the mission statement in mind:


The goal of this project is to provide a powerful testing framework for C, on 
different platforms and operating systems, which only requires the standard C 
library.


Great work!


	-- andreas

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


Follow-Ups:
Re: mocking without wrap functionsmaxxedev maxxedev <maxxedev@xxxxxxxxx>
References:
mocking without wrap functionsmaxxedev maxxedev <maxxedev@xxxxxxxxx>