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

Re: mocking without wrap functions


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

Yes but as I explained previously, you need to write that special wrap
function by hand wheres with what I am suggesting, you don't need to.

I've attached a more complete example. Take a look stringutils_test.c. Also
included is a script that can generate a runner file with main(), given
such a foo_test.c file.


On Tue, Jan 14, 2014 at 4:06 AM, Andreas Schneider <asn@xxxxxxxxxxxxxx>wrote:

> 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
>
>

Attachment: cmocka.zip
Description: Zip archive


References:
mocking without wrap functionsmaxxedev maxxedev <maxxedev@xxxxxxxxx>
Re: mocking without wrap functionsAndreas Schneider <asn@xxxxxxxxxxxxxx>