vberlier / narmock

A minimal mocking utility for C projects.
MIT License
6 stars 1 forks source link

Compiler error messages with and without function pointers in MOCK() #8

Closed eerimoq closed 4 years ago

eerimoq commented 5 years ago

Hello!

I replaced the MOCK() macro and its function pointers with plain functions in my fork for better error messags from the compiler when the wrong arguments are given. See below for compiler error messages with MOCK() and plain functions.

MOCK()

poetry run make -C tests
make: Entering directory '/home/erik/workspace/narmock/tests'
cc -E dummy_functions.c main.c narwhal.c | narmock -g
cc -Wall -Wextra -Wconversion -std=c11 -g -Og -fsanitize=address -fno-omit-frame-pointer $(narmock -f) *.c -o run_tests
In file included from main.c:7:
main.c: In function ‘_narwhal_test_function_pipe_function’:
__mocks__.h:19:25: error: too many arguments to function ‘_narmock_get_mock_for_pipe((const void *)&pipe)->mock_once’
 #define MOCK(function) (_narmock_get_mock_for_##function((void *)&function))
                        ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:95:5: note: in expansion of macro ‘MOCK’
     MOCK(pipe)
     ^~~~
make: *** [Makefile:22: run_tests] Error 1
make: Leaving directory '/home/erik/workspace/narmock/tests'

Plain function

poetry run make -C tests
make: Entering directory '/home/erik/workspace/narmock/tests'
cc -E dummy_functions.c main.c narwhal.c | narmock -g
cc -Wall -Wextra -Wconversion -std=c11 -g -Og -fsanitize=address -fno-omit-frame-pointer $(narmock -f) *.c -o run_tests
main.c: In function ‘_narwhal_test_function_pipe_function_set_out’:
main.c:93:5: error: too many arguments to function ‘pipe_mock_once’
     pipe_mock_once(42, 1);
     ^~~~~~~~~~~~~~
In file included from main.c:7:
__mocks__.h:123:6: note: declared here
 void pipe_mock_once(int return_value);
      ^~~~~~~~~~~~~~
make: *** [Makefile:22: run_tests] Error 1
make: Leaving directory '/home/erik/workspace/narmock/tests'
vberlier commented 4 years ago

Yeah there's a level of indirection, but I think it's not too bad. I quite like the fluent api.