|
1 | 1 | # Prevent multiple "inclusions" of TestFramework
|
2 |
| -if (NOT TARGET unity) |
| 2 | +if (NOT COMMAND ADD_UNITY_TEST) |
3 | 3 | message(STATUS "TestFramework from ${CMAKE_CURRENT_SOURCE_DIR} included")
|
4 | 4 | add_definitions( -DUNIT_TEST )
|
5 | 5 |
|
6 | 6 | set(TEST_TOOLS_DIR ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL test_tools_dir)
|
7 | 7 |
|
8 | 8 | find_package(Python3 COMPONENTS Interpreter)
|
9 | 9 |
|
10 |
| - include(CMakeFunctions.cmake) |
| 10 | + ## |
| 11 | + # @b Syntax |
| 12 | + # |
| 13 | + #   @c add_unity_test( \b NAME \<name\> [TEST_BASE \<file.\<c|cpp\>\>] FILES \<fileN\> LIBRARIES \<libN\> [USE_CPP]) |
| 14 | + # |
| 15 | + # Function for adding a unit test CMake target. |
| 16 | + # |
| 17 | + # |
| 18 | + # @b Example @b 1 |
| 19 | + # |
| 20 | + # |
| 21 | + # from @b Components\\FileSystem\\Test\\CMakeLists.txt file |
| 22 | + # @code{.py} |
| 23 | + # add_unity_test(NAME TestFileSystem FILES ../FileSystem.c TestFileSystem.cpp LIBRARIES Assert USE_CPP) |
| 24 | + # @endcode |
| 25 | + # |
| 26 | + # |
| 27 | + # @b Example @b 2 |
| 28 | + # |
| 29 | + # |
| 30 | + # from @b ZWave\\Protocol\\Test\\CMakeLists.txt file |
| 31 | + # @code{.py} |
| 32 | + # add_unity_test(NAME TestTransmitSingleCastLinkLayer |
| 33 | + # FILES ../ZW_DataLinkLayer.c TestTransmitSingleCastLinkLayer.c |
| 34 | + # LIBRARIES mock ZW_RadioPhyMock Assert ) |
| 35 | + # @endcode |
| 36 | + # |
| 37 | + # |
| 38 | + # @b Parameters |
| 39 | + # <TABLE cellspacing=0 cellborder=0> |
| 40 | + # <TR> |
| 41 | + # <TD>   </TD><TD><TT>\[in\]</TT></TD><TD><b>NAME \<name\></b></TD> |
| 42 | + # <TD>Name of test executeable to build</TD> |
| 43 | + # </TR> |
| 44 | + # <TR> |
| 45 | + # <TD></TD><TD><TT>\[in\]</TT></TD><TD><b>TEST_BASE \<file.\<c|cpp\>\></b></TD> |
| 46 | + # <TD>\[Optional\] Unit test file from which the runner should be generated, if TEST_BASE is not |
| 47 | + # provided, the \<name\>.\<c|cpp\> from first parameter will be used.</TD> |
| 48 | + # </TR> |
| 49 | + # <TR> |
| 50 | + # <TD></TD><TD><TT>\[in\]</TT></TD><TD><b>FILES \<fileN\></b></TD> |
| 51 | + # <TD>List of files used for building the test</TD> |
| 52 | + # </TR> |
| 53 | + # <TR> |
| 54 | + # <TD></TD><TD><TT>\[in\]</TT></TD><TD><b>LIBRARIES \<libN\></b></TD> |
| 55 | + # <TD>List of libraries to link for the test executable (unity is automatically included)</TD> |
| 56 | + # </TR> |
| 57 | + # <TR> |
| 58 | + # <TD></TD><TD><TT>\[in\]</TT></TD><TD><b>USE_CPP</b></TD> |
| 59 | + # <TD>\[Optional\] Set this flag if the test executable must be compiled using C++</TD> |
| 60 | + # </TR> |
| 61 | + # </TABLE> |
| 62 | + # |
| 63 | + function(ADD_UNITY_TEST) |
| 64 | + set(OPTIONS USE_CPP DISABLED USE_UNITY_WITH_CMOCK) |
| 65 | + set(SINGLE_VALUE_ARGS "NAME" "TEST_BASE") |
| 66 | + set(MULTI_VALUE_ARGS "FILES" "LIBRARIES" "INCLUDES") |
| 67 | + cmake_parse_arguments(ADD_UNITY_TEST "${OPTIONS}" "${SINGLE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN} ) |
| 68 | + |
| 69 | + set(RUNNER_EXTENSION c) |
| 70 | + if (ADD_UNITY_TEST_USE_CPP) |
| 71 | + set(RUNNER_EXTENSION cpp) |
| 72 | + endif (ADD_UNITY_TEST_USE_CPP) |
| 73 | + |
| 74 | + set(RUNNER_BASE "${ADD_UNITY_TEST_NAME}.${RUNNER_EXTENSION}") |
| 75 | + if (NOT ${ADD_UNITY_TEST_TEST_BASE} STREQUAL "") |
| 76 | + set(RUNNER_BASE "${ADD_UNITY_TEST_TEST_BASE}") |
| 77 | + endif (NOT ${ADD_UNITY_TEST_TEST_BASE} STREQUAL "") |
| 78 | + |
| 79 | + if ("${ADD_UNITY_TEST_NAME}" STREQUAL "") |
| 80 | + list(GET ADD_UNITY_TEST_UNPARSED_ARGUMENTS 0 ADD_UNITY_TEST_NAME) |
| 81 | + list(REMOVE_AT ADD_UNITY_TEST_UNPARSED_ARGUMENTS 0) |
| 82 | + set(RUNNER_BASE "${ADD_UNITY_TEST_NAME}.${RUNNER_EXTENSION}") |
| 83 | + set(ADD_UNITY_TEST_FILES "${ADD_UNITY_TEST_UNPARSED_ARGUMENTS}") |
| 84 | + endif ("${ADD_UNITY_TEST_NAME}" STREQUAL "") |
| 85 | + |
| 86 | + add_executable(${ADD_UNITY_TEST_NAME} ${ADD_UNITY_TEST_NAME}_runner.${RUNNER_EXTENSION} ${RUNNER_BASE} ${ADD_UNITY_TEST_FILES}) |
| 87 | + |
| 88 | + add_custom_command(OUTPUT ${ADD_UNITY_TEST_NAME}_runner.${RUNNER_EXTENSION} |
| 89 | + COMMAND ${PYTHON_EXECUTABLE} ${TEST_TOOLS_DIR}/gen_test_runner.py ${RUNNER_BASE} > ${CMAKE_CURRENT_BINARY_DIR}/${ADD_UNITY_TEST_NAME}_runner.${RUNNER_EXTENSION} |
| 90 | + DEPENDS ${RUNNER_BASE} ${TEST_TOOLS_DIR}/gen_test_runner.py |
| 91 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 92 | + ) |
| 93 | + add_test(${ADD_UNITY_TEST_NAME} ${ADD_UNITY_TEST_NAME} ) |
| 94 | + target_link_libraries( ${ADD_UNITY_TEST_NAME} ${ADD_UNITY_TEST_LIBRARIES}) |
| 95 | + if (NOT ADD_UNITY_TEST_USE_UNITY_WITH_CMOCK) |
| 96 | + target_link_libraries( ${ADD_UNITY_TEST_NAME} unity) |
| 97 | + endif (NOT ADD_UNITY_TEST_USE_UNITY_WITH_CMOCK) |
| 98 | + |
| 99 | + target_include_directories(${ADD_UNITY_TEST_NAME} PRIVATE . ${ADD_UNITY_TEST_INCLUDES}) |
| 100 | + |
| 101 | + if(${ADD_UNITY_TEST_DISABLED}) |
| 102 | + set_tests_properties(${TEST_NAME} PROPERTIES DISABLED True) |
| 103 | + endif() |
| 104 | + endfunction(ADD_UNITY_TEST) |
11 | 105 |
|
12 |
| - add_subdirectory(unity) |
13 | 106 | # Check whether the building of mock framework is disabled.
|
14 |
| - # Building of the mock framework can be disabled for the c51 or asip simply by adding the following line to the specific project at higher level: |
| 107 | + # Building of the mock framework can be disabled by chip vendors simply by adding the following line to the specific project at higher level: |
15 | 108 | # e.g.: set(DISABLE_MOCK 1)
|
16 | 109 | # Warning, Do not set DISABLE_MOCK in this file.
|
17 | 110 | if (NOT DEFINED DISABLE_MOCK)
|
18 | 111 | add_subdirectory(mock)
|
19 | 112 | endif (NOT DEFINED DISABLE_MOCK)
|
20 |
| -else() |
| 113 | +else(NOT COMMAND ADD_UNITY_TEST) |
21 | 114 | message(STATUS "TestFramework from ${CMAKE_CURRENT_SOURCE_DIR} NOT included")
|
22 |
| -endif() |
| 115 | +endif(NOT COMMAND ADD_UNITY_TEST) |
0 commit comments