1- set (CONFIG_DIR_V16 "${CMAKE_CURRENT_BINARY_DIR} /resources/v16/" )
2- set (MIGRATION_FILES_LOCATION_V16 "${CMAKE_CURRENT_BINARY_DIR} /resources/v16/migration_files" )
3- set (MIGRATION_FILES_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR} /resources/v201/migration_files" )
4- set (CONFIG_FILE_LOCATION_V16 ${CMAKE_CURRENT_SOURCE_DIR} /config/v16/resources/config.json)
5- set (USER_CONFIG_FILE_LOCATION_V16 ${CMAKE_CURRENT_SOURCE_DIR} /config/v16/resources/user_config.json)
6- set (CONFIG_FILE_RESOURCES_LOCATION_V16 "${CMAKE_CURRENT_BINARY_DIR} /resources/config/v16/config.json" )
7- set (USER_CONFIG_FILE_RESOURCES_LOCATION_V16 "${CMAKE_CURRENT_BINARY_DIR} /resources/config/v16/user_config.json" )
1+ include (${CMAKE_CURRENT_SOURCE_DIR} /libocpp-unittests.cmake)
82
9- set (MIGRATION_FILES_DEVICE_MODEL_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR} /resources/v201/device_model_migration_files" )
10- set (DEVICE_MODEL_DB_LOCATION_V201 "${CMAKE_INSTALL_PREFIX} /${CMAKE_INSTALL_DATADIR} /everest/modules/OCPP201/device_model_storage.db" )
11- set (DEVICE_MODEL_RESOURCES_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR} /resources/config/v201" )
12- set (DEVICE_MODEL_RESOURCES_CHANGED_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR} /resources/config/v201/changed" )
13- set (DEVICE_MODEL_RESOURCES_WRONG_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR} /resources/config/v201/wrong" )
14- set (DEVICE_MODEL_CURRENT_RESOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR} /config/v201/resources)
15- set (DEVICE_MODEL_CURRENT_CHANGED_RESOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR} /config/v201/resources_changed)
16- set (DEVICE_MODEL_CURRENT_WRONG_RESOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR} /config/v201/resources_wrong)
17- set (DEVICE_MODEL_EXAMPLE_CONFIG_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR} /resources/example_config/v201/component_config" )
18- set (DEVICE_MODEL_CURRENT_EXAMPLE_CONFIG_LOCATION_V201 "${PROJECT_SOURCE_DIR} /config/v201/component_config" )
19- set (TEST_PROFILES_LOCATION_V16 "${CMAKE_CURRENT_BINARY_DIR} /resources/profiles/v16" )
20- set (TEST_PROFILES_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR} /resources/profiles/v201" )
3+ # For libocpp tests, there is one big executable, which links against the ocpp lib and all other libs.
4+ # When it is useful to link only to the tested cpp files, a separate executable can be created for each file.
5+ # The source files can be added to this variable, which is a list. For example:
6+ # list(APPEND SEPARATE_UNIT_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/lib/ocpp/v201/functional_blocks/test_security.cpp)
7+ # CMake will then create a new test executable based on the filename and adds 'libocpp_' in front of it. In above
8+ # example, a test executable 'libocpp_test_security' will be created. In this example, in the CMakeLists of
9+ # `lib/ocpp/v201/functional_blocks`, files to link against can be added to this target / executable.
10+ #
11+ # For each test in this list, cmake will link agaist some 'default' cpp files (like utils and enums etc), set all
12+ # correct flags, add a test, set definitions, etc. See below.
13+ set (SEPARATE_UNIT_TESTS ${CMAKE_CURRENT_SOURCE_DIR} /lib/ocpp/common/utils_tests.cpp)
2114
15+ # Add separate tests for V201 only.
16+ if (LIBOCPP_ENABLE_V201)
17+ # Add all v201 tests you don't want to include in the default test executable here.
18+ # Example (remove if there are real tests added):
19+ # list(APPEND SEPARATE_UNIT_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/lib/ocpp/v201/functional_blocks/test_security.cpp)
20+ endif ()
2221
23- add_executable (libocpp_unit_tests
24- config/v201/resources_wrong/component_config_required_no_value/standardized/UnitTestCtrlr.json)
2522
26- target_compile_definitions (libocpp_unit_tests
27- PRIVATE
28- CONFIG_FILE_LOCATION_V16="${CONFIG_FILE_RESOURCES_LOCATION_V16} "
29- USER_CONFIG_FILE_LOCATION_V16="${USER_CONFIG_FILE_RESOURCES_LOCATION_V16} "
30- CONFIG_DIR_V16="${CONFIG_DIR_V16} "
31- MIGRATION_FILES_LOCATION_V16="${MIGRATION_FILES_LOCATION_V16} "
32- MIGRATION_FILES_LOCATION_V201="${MIGRATION_FILES_LOCATION_V201} "
33- MIGRATION_FILE_VERSION_V16=${MIGRATION_FILE_VERSION_V16}
34- MIGRATION_FILE_VERSION_V201=${MIGRATION_FILE_VERSION_V201}
35- DEVICE_MODEL_DB_LOCATION_V201="${DEVICE_MODEL_DB_LOCATION_V201} "
36- TEST_PROFILES_LOCATION_V16="${TEST_PROFILES_LOCATION_V16} "
37- TEST_PROFILES_LOCATION_V201="${TEST_PROFILES_LOCATION_V201} "
38- )
23+ set (TEST_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} )
24+ set (GCOVR_DEPENDENCIES)
25+
26+
27+ add_libocpp_unittest(NAME libocpp_unit_tests PATH "" )
3928
40- target_compile_options (libocpp_unit_tests
41- PRIVATE
42- -pedantic-errors
29+ target_link_libraries (libocpp_unit_tests PRIVATE
30+ ocpp
31+ ${GTEST_LIBRARIES}
4332)
4433
45- target_compile_features (libocpp_unit_tests PUBLIC cxx_std_17)
34+
35+ # Add executables, link libraries etc for the unit tests that are separate and do not link against the ocpp lib.
36+ # This loops over all tests added to `SEPARATE_UNIT_TESTS` and does the necessary things to make it build.
37+ # For now, everything is added that seems to be needed in every test. If later some sources and link libraries should
38+ # not be added here, they can always be removed from this loop and added to the test targets that actually need them.
39+ foreach (ITEM ${SEPARATE_UNIT_TESTS} )
40+ set (TEST_ROOT_NAME)
41+ cmake_path(GET ITEM STEM TEST_ROOT_NAME)
42+ set (TEST_NAME "libocpp_${TEST_ROOT_NAME} " )
43+ add_libocpp_unittest(NAME ${TEST_NAME} PATH ${ITEM} )
44+ endforeach (ITEM)
45+
46+ # Subdirectories should be added only after adding the tests, because they have to exist for the CMakeLists.txt in the
47+ # child directories.
48+ add_subdirectory (lib/ocpp/common)
49+
50+ if (LIBOCPP_ENABLE_V16)
51+ add_subdirectory (lib/ocpp/v16)
52+ endif ()
53+
54+ if (LIBOCPP_ENABLE_V201)
55+ add_subdirectory (lib/ocpp/v201)
56+ endif ()
4657
4758add_custom_command (TARGET libocpp_unit_tests POST_BUILD
4859 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR} /resources/unittest_device_model.db ${CMAKE_CURRENT_BINARY_DIR} /resources/unittest_device_model.db
@@ -67,38 +78,16 @@ add_custom_command(TARGET libocpp_unit_tests POST_BUILD
6778 COMMAND ${CMAKE_COMMAND} -E copy_directory ${DEVICE_MODEL_CURRENT_EXAMPLE_CONFIG_LOCATION_V201} ${DEVICE_MODEL_EXAMPLE_CONFIG_LOCATION_V201}
6879)
6980
70- add_test (libocpp_unit_tests libocpp_unit_tests)
71-
72- target_include_directories (libocpp_unit_tests PUBLIC
73- ${CMAKE_CURRENT_SOURCE_DIR}
74- ${CMAKE_CURRENT_SOURCE_DIR} /lib/ocpp/common
75- )
76- target_link_libraries (libocpp_unit_tests PRIVATE
77- ocpp
78- GTest::gmock_main
79- GTest::gtest_main
80- )
81-
82- add_subdirectory (lib/ocpp/common)
83-
84- if (LIBOCPP_ENABLE_V16)
85- add_subdirectory (lib/ocpp/v16)
86- endif ()
87-
88- if (LIBOCPP_ENABLE_V201)
89- add_subdirectory (lib/ocpp/v201)
90- endif ()
91-
9281setup_target_for_coverage_gcovr_html(
9382 NAME ${PROJECT_NAME} _gcovr_coverage
9483 EXECUTABLE ctest
95- DEPENDENCIES libocpp_unit_tests
84+ DEPENDENCIES libocpp_unit_tests ${GCOVR_DEPENDENCIES}
9685 EXCLUDE "src/*" "tests/*"
9786)
9887
9988setup_target_for_coverage_gcovr_xml(
10089 NAME ${PROJECT_NAME} _gcovr_coverage_xml
10190 EXECUTABLE ctest
102- DEPENDENCIES libocpp_unit_tests
91+ DEPENDENCIES libocpp_unit_tests ${GCOVR_DEPENDENCIES}
10392 EXCLUDE "src/*" "tests/*"
10493)
0 commit comments