1# MIT License
2#
3# Copyright (c) 2018 Kamil Lorenc
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
23## Add unit test with mocking support
24#  \param name unit test name (excluding extension and 'test_' prefix)
25#  \param SOURCES optional list of source files to include in test executable
26#  (beside test_${name}.c)
27#  \param MOCKS optional list of functions to be mocked in executable
28#  \param COMPILE_OPTIONS optional list of options for the compiler
29#  \param LINK_LIBRARIES optional list of libraries to link (used as
30#  -l${LINK_LIBRARIES})
31#  \param LINK_OPTIONS optional list of options to be passed to linker
32function(add_cmocka_mock_test name)
33  # parse arguments passed to the function
34  set(options )
35  set(oneValueArgs )
36  set(multiValueArgs SOURCES MOCKS COMPILE_OPTIONS LINK_LIBRARIES LINK_OPTIONS)
37  cmake_parse_arguments(ADD_MOCKED_TEST "${options}" "${oneValueArgs}"
38    "${multiValueArgs}" ${ARGN} )
39
40  # create link flags for mocks
41  set(link_flags "")
42  foreach (mock ${ADD_MOCKED_TEST_MOCKS})
43    set(link_flags "${link_flags} -Wl,--wrap=${mock}")
44  endforeach(mock)
45
46  # define test
47  add_cmocka_test(${name}
48                  SOURCES ${ADD_MOCKED_TEST_SOURCES}
49                  COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS}
50                                  ${ADD_MOCKED_TEST_COMPILE_OPTIONS}
51                  LINK_LIBRARIES ${ADD_MOCKED_TEST_LINK_LIBRARIES}
52                  LINK_OPTIONS ${link_flags} ${ADD_MOCKED_TEST_LINK_OPTIONS})
53
54  # allow using includes from src/ directory
55  target_include_directories(${name} PRIVATE ${CMAKE_SOURCE_DIR}/src)
56endfunction(add_cmocka_mock_test)
57