Has anyone tried using Cmockery in C++ code (to mock C functions called from C++ code)?

121 views
Skip to first unread message

Daryl Spitzer

unread,
Jan 5, 2011, 8:18:54 PM1/5/11
to Cmockery
If you have, how did you work around errors like:

my_tests.cpp: In function ‘void test_some_stuff(void**)’:
my_tests.cpp:72: error: invalid conversion from ‘void*’ to ‘const
char*’
my_tests.cpp:72: error: initializing argument 5 of ‘void
_expect_string(const char*, const char*, const char*, int, const
char*, int)’

For details, see
http://stackoverflow.com/questions/4610892/how-do-i-tell-gcc-to-relax-its-restrictions-on-typecasting-when-calling-a-c-funct.

--
Daryl

Andreas Schneider

unread,
Jan 6, 2011, 3:28:04 AM1/6/11
to cmoc...@googlegroups.com
On Thursday 06 January 2011 02:18:54 Daryl Spitzer wrote:
> If you have, how did you work around errors like:
>
> my_tests.cpp: In function ‘void test_some_stuff(void**)’:
> my_tests.cpp:72: error: invalid conversion from ‘void*’ to ‘const
> char*’

Rewrite the code that it is correct and cast everything correctly. That's what
should be done...


-- andreas

Daryl Spitzer

unread,
Jan 6, 2011, 10:19:02 AM1/6/11
to cmoc...@googlegroups.com
It's the expect_string_count macro in cmockery.h that is casting
incorrectly (for C++):

#define expect_string(function, parameter, string) \
    expect_string_count(function, parameter, string, 1)
#define expect_string_count(function, parameter, string, count) \
    _expect_string(#function, #parameter, __FILE__, __LINE__, (void*)string, \
                  count)

Here's the prototype for _expect_string (from cmockery.h):

void _expect_string(
const char* const function, const char* const parameter,
const char* const file, const int line, const char* string,
const int count);

The expect_string_count macro is casting the "string" argument to
void*, and that argument is const char* in the _expect_string
function.

For now, I'm going to try #undef expect_string_count and then redefine
it without the cast (as described in one answer to my StackOverflow
question). Though I'd be happy to submit a patch using #if
__cplusplus, if the committers think it's worth the work.

--
Daryl

> --
> You received this message because you are subscribed to the Google Groups "Cmockery" group.
> To post to this group, send email to cmoc...@googlegroups.com.
> To unsubscribe from this group, send email to cmockery+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cmockery?hl=en.
>

Reply all
Reply to author
Forward
0 new messages