@@ -19,12 +19,31 @@ set(DEVICE_MODEL_CURRENT_EXAMPLE_CONFIG_LOCATION_V201 "${PROJECT_SOURCE_DIR}/con
1919set (TEST_PROFILES_LOCATION_V16 "${CMAKE_CURRENT_BINARY_DIR} /resources/profiles/v16" )
2020set (TEST_PROFILES_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR} /resources/profiles/v201" )
2121
22+ add_executable (libocpp_unit_tests)
2223
23- add_executable (libocpp_unit_tests
24- config/v201/resources_wrong/component_config_required_no_value/standardized/UnitTestCtrlr.json)
24+ # For libocpp tests, there is one big executable, which links against the ocpp lib and all other libs.
25+ # When it is useful to link only to the tested cpp files, a separate executable can be created for each file.
26+ # The source files can be added to this variable, which is a list. For example:
27+ # list(APPEND SEPARATE_UNIT_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/lib/ocpp/v201/functional_blocks/test_security.cpp)
28+ # CMake will then create a new test executable based on the filename and adds 'libocpp_' in front of it. In above
29+ # example, a test executable 'libocpp_test_security' will be created. In this example, in the CMakeLists of
30+ # `lib/ocpp/v201/functional_blocks`, files to link against can be added to this target / executable.
31+ #
32+ # For each test in this list, cmake will link agaist some 'default' cpp files (like utils and enums etc), set all
33+ # correct flags, add a test, set definitions, etc. See below.
34+ set (SEPARATE_UNIT_TESTS ${CMAKE_CURRENT_SOURCE_DIR} /lib/ocpp/common/utils_tests.cpp)
2535
26- target_compile_definitions (libocpp_unit_tests
27- PRIVATE
36+ # Add separate tests for V201 only.
37+ if (LIBOCPP_ENABLE_V201)
38+ # Add all v201 tests you don't want to include in the default test executable here.
39+ # Example (remove if there are real tests added):
40+ # list(APPEND SEPARATE_UNIT_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/lib/ocpp/v201/functional_blocks/test_security.cpp)
41+ endif ()
42+
43+ # Add variables that can be used for all tests if needed.
44+ set (GTEST_LIBRARIES GTest::gmock_main GTest::gtest_main)
45+ set (TEST_COMPILE_OPTIONS -pedantic-errors)
46+ set (TEST_COMPILE_DEFINITIONS
2847 CONFIG_FILE_LOCATION_V16="${CONFIG_FILE_RESOURCES_LOCATION_V16} "
2948 USER_CONFIG_FILE_LOCATION_V16="${USER_CONFIG_FILE_RESOURCES_LOCATION_V16} "
3049 CONFIG_DIR_V16="${CONFIG_DIR_V16} "
@@ -34,15 +53,64 @@ target_compile_definitions(libocpp_unit_tests
3453 MIGRATION_FILE_VERSION_V201=${MIGRATION_FILE_VERSION_V201}
3554 DEVICE_MODEL_DB_LOCATION_V201="${DEVICE_MODEL_DB_LOCATION_V201} "
3655 TEST_PROFILES_LOCATION_V16="${TEST_PROFILES_LOCATION_V16} "
37- TEST_PROFILES_LOCATION_V201="${TEST_PROFILES_LOCATION_V201} "
56+ TEST_PROFILES_LOCATION_V201="${TEST_PROFILES_LOCATION_V201} " )
57+ set (TEST_COMPILE_FEATURES cxx_std_17)
58+ set (LIBOCPP_INCLUDE_PATH ${PROJECT_SOURCE_DIR} /include )
59+ set (LIBOCPP_LIB_PATH ${PROJECT_SOURCE_DIR} /lib)
60+ set (LIBOCPP_3RDPARTY_PATH ${PROJECT_SOURCE_DIR} /3rd_party)
61+ set (TEST_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}
62+ ${CMAKE_CURRENT_SOURCE_DIR} /lib/ocpp/common
63+ ${LIBOCPP_INCLUDE_PATH}
64+ )
65+ # If the test is not linked against the ocpp library, most probably those libraries are needed to link against.
66+ set (LIBOCPP_TEST_DEFAULT_LINK_LIBRARIES
67+ SQLite::SQLite3
68+ nlohmann_json::nlohmann_json
69+ date::date-tz
70+ everest::log
71+ everest::evse_security
72+ )
73+
74+ # If the test is not linked against the ocpp library, those default sources can be linked against, they will often
75+ # be needed to link against.
76+ set (LIBOCPP_TEST_INCLUDE_COMMON_SOURCES ${LIBOCPP_LIB_PATH} /ocpp/common/types.cpp
77+ ${LIBOCPP_LIB_PATH} /ocpp/common/ocpp_logging.cpp
78+ ${LIBOCPP_LIB_PATH} /ocpp/common/utils.cpp
79+ ${LIBOCPP_LIB_PATH} /ocpp/common/call_types.cpp
80+ ${LIBOCPP_LIB_PATH} /ocpp/common/evse_security.cpp
81+ ${LIBOCPP_LIB_PATH} /ocpp/common/database/database_connection.cpp
82+ ${LIBOCPP_LIB_PATH} /ocpp/common/database/database_handler_common.cpp
83+ ${LIBOCPP_LIB_PATH} /ocpp/common/database/sqlite_statement.cpp
84+ ${LIBOCPP_LIB_PATH} /ocpp/common/database/database_schema_updater.cpp
3885)
3986
87+ # If the test is not linked against the ocpp library, those default sources for ocpp v2.0.1 can be linked against, they
88+ # will often be needed to link against.
89+ set (LIBOCPP_TEST_INCLUDE_V201_SOURCES ${LIBOCPP_LIB_PATH} /ocpp/v16/ocpp_enums.cpp # This is currently still needed but might be removed in the future.
90+ ${LIBOCPP_LIB_PATH} /ocpp/v201/ctrlr_component_variables.cpp
91+ ${LIBOCPP_LIB_PATH} /ocpp/v201/types.cpp
92+ ${LIBOCPP_LIB_PATH} /ocpp/v201/ocpp_types.cpp
93+ ${LIBOCPP_LIB_PATH} /ocpp/v201/enums.cpp
94+ ${LIBOCPP_LIB_PATH} /ocpp/v201/ocpp_enums.cpp
95+ ${LIBOCPP_LIB_PATH} /ocpp/v201/device_model.cpp
96+ ${LIBOCPP_LIB_PATH} /ocpp/v201/init_device_model_db.cpp
97+ ${LIBOCPP_LIB_PATH} /ocpp/v201/device_model_storage_sqlite.cpp
98+ ${LIBOCPP_LIB_PATH} /ocpp/v201/utils.cpp
99+ )
100+
101+ target_compile_definitions (libocpp_unit_tests
102+ PRIVATE
103+ ${TEST_COMPILE_DEFINITIONS}
104+ )
105+
106+ set (TEST_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} )
107+
40108target_compile_options (libocpp_unit_tests
41109 PRIVATE
42- -pedantic-errors
110+ ${TEST_COMPILE_OPTIONS}
43111)
44112
45- target_compile_features (libocpp_unit_tests PUBLIC cxx_std_17 )
113+ target_compile_features (libocpp_unit_tests PUBLIC ${TEST_COMPILE_FEATURES} )
46114
47115add_custom_command (TARGET libocpp_unit_tests POST_BUILD
48116 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR} /resources/unittest_device_model.db ${CMAKE_CURRENT_BINARY_DIR} /resources/unittest_device_model.db
@@ -69,16 +137,42 @@ add_custom_command(TARGET libocpp_unit_tests POST_BUILD
69137
70138add_test (libocpp_unit_tests libocpp_unit_tests)
71139
72- target_include_directories (libocpp_unit_tests PUBLIC
73- ${CMAKE_CURRENT_SOURCE_DIR}
74- ${CMAKE_CURRENT_SOURCE_DIR} /lib/ocpp/common
140+ target_include_directories (libocpp_unit_tests PUBLIC
141+ ${TEST_INCLUDE_DIRECTORIES}
75142)
76143target_link_libraries (libocpp_unit_tests PRIVATE
77144 ocpp
78- GTest::gmock_main
79- GTest::gtest_main
145+ ${GTEST_LIBRARIES}
80146)
81147
148+ set (GCOVR_DEPENDENCIES libocpp_unit_tests)
149+
150+ # Add executables, link libraries etc for the unit tests that are separate and do not link against the ocpp lib.
151+ # This loops over all tests added to `SEPARATE_UNIT_TESTS` and does the necessary things to make it build.
152+ # For now, everything is added that seems to be needed in every test. If later some sources and link libraries should
153+ # not be added here, they can always be removed from this loop and added to the test targets that actually need them.
154+ foreach (ITEM ${SEPARATE_UNIT_TESTS} )
155+ set (TEST_ROOT_NAME)
156+ cmake_path(GET ITEM STEM TEST_ROOT_NAME)
157+ set (TEST_NAME "libocpp_${TEST_ROOT_NAME} " )
158+ add_executable (${TEST_NAME} ${ITEM} )
159+ add_test (NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} )
160+ target_link_libraries (${TEST_NAME} PUBLIC ${GTEST_LIBRARIES} )
161+ list (APPEND GCOVR_DEPENDENCIES ${TEST_NAME} )
162+ target_compile_definitions (${TEST_NAME} PRIVATE ${TEST_COMPILE_DEFINITIONS} )
163+ target_compile_definitions (${TEST_NAME} PRIVATE
164+ MIGRATION_FILE_VERSION_V16=${MIGRATION_FILE_VERSION_V16}
165+ MIGRATION_FILE_VERSION_V201=${MIGRATION_FILE_VERSION_V201}
166+ MIGRATION_DEVICE_MODEL_FILE_VERSION_V201=${MIGRATION_DEVICE_MODEL_FILE_VERSION_V201} )
167+ target_compile_options (${TEST_NAME} PRIVATE ${TEST_COMPILE_OPTIONS} )
168+ target_compile_features (${TEST_NAME} PUBLIC ${TEST_COMPILE_FEATURES} )
169+ target_include_directories (${TEST_NAME} PRIVATE ${TEST_INCLUDE_DIRECTORIES} )
170+ target_link_libraries (${TEST_NAME} PRIVATE ${LIBOCPP_TEST_DEFAULT_LINK_LIBRARIES} )
171+ message ("Add test ${TEST_NAME} " )
172+ endforeach (ITEM)
173+
174+ # Subdirectories should be added only after adding the tests, because they have to exist for the CMakeLists.txt in the
175+ # child directories.
82176add_subdirectory (lib/ocpp/common)
83177
84178if (LIBOCPP_ENABLE_V16)
@@ -92,13 +186,13 @@ endif()
92186setup_target_for_coverage_gcovr_html(
93187 NAME ${PROJECT_NAME} _gcovr_coverage
94188 EXECUTABLE ctest
95- DEPENDENCIES libocpp_unit_tests
189+ DEPENDENCIES libocpp_unit_tests ${GCOVR_DEPENDENCIES}
96190 EXCLUDE "src/*" "tests/*"
97191)
98192
99193setup_target_for_coverage_gcovr_xml(
100194 NAME ${PROJECT_NAME} _gcovr_coverage_xml
101195 EXECUTABLE ctest
102- DEPENDENCIES libocpp_unit_tests
196+ DEPENDENCIES libocpp_unit_tests ${GCOVR_DEPENDENCIES}
103197 EXCLUDE "src/*" "tests/*"
104198)
0 commit comments