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

mocking without wrap functions


Hi,

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);
    }

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.

Would people behind cmocka be interested in developing this idea further?

Regards,
M

[1] http://gcc.gnu.org/onlinedocs/gcc/Constructing-Calls.html

Attachment: generic_mock.zip
Description: Zip archive


Follow-Ups:
Re: mocking without wrap functionsAndreas Schneider <asn@xxxxxxxxxxxxxx>