# SPDX-FileCopyrightText: 2016 Copyright contributors to the cppTango project
#
# SPDX-License-Identifier: LGPL-3.0-or-later

set(COMMON_TEST_DEFS _REENTRANT)

if (WIN32)
    list(APPEND COMMON_TEST_DEFS NOMINMAX)
endif()

include(old_cxx_tests.cmake)

include(catch2/tango_catch2_tests.cmake)

find_package(nlohmann_json QUIET)

if (NOT nlohmann_json_FOUND)
    message(STATUS "JSON library not found, skipping QueryEventSystem() test")
endif()

include(CheckSymbolExists)
check_symbol_exists(fork "unistd.h" HAS_FORK)

tango_catch2_tests_create(
    catch2_alarm_event.cpp
    catch2_alarm.cpp
    catch2_attr_async_cb.cpp
    catch2_attr_manip.cpp
    catch2_attr_proxy.cpp
    catch2_attr_conf_event.cpp
    catch2_attr_polling.cpp
    catch2_attr_polling_history.cpp
    catch2_attr_read_write_simple.cpp
    catch2_attr_read_no_set.cpp
    catch2_cmd_polling.cpp
    catch2_cmd_polling_history.cpp
    catch2_cmd_query.cpp
    catch2_connection.cpp
    catch2_data_ready_event.cpp
    catch2_dev_intr_event.cpp
    catch2_client_cleanup.cpp
    catch2_event_old_client.cpp
    catch2_event_on_connection_failure.cpp
    catch2_event_reconnection.cpp
    catch2_event_queue.cpp
    catch2_test_dtypes.cpp
    catch2_event_subscription_mode.cpp
    catch2_device_server_cleanup.cpp
    catch2_state_status_events.cpp
    catch2_dev_state.cpp
    catch2_error_in_event_callback.cpp
    catch2_internal_utils.cpp
    catch2_internal_stl_helpers.cpp
    catch2_misc.cpp
    catch2_multi_thread_sighandler.cpp
    catch2_nodb_connection.cpp
    catch2_change_event.cpp
    catch2_change_event_on_nan.cpp
    catch2_server.cpp
    catch2_synchronised_queue.cpp
    catch2_configure_zmq_ports.cpp
    $<$<BOOL:${nlohmann_json_FOUND}>:${CMAKE_CURRENT_SOURCE_DIR}/catch2_query_event_system.cpp>
    $<$<BOOL:${TANGO_USE_TELEMETRY}>:${CMAKE_CURRENT_SOURCE_DIR}/catch2_telemetry.cpp>
    catch2_tango_monitor.cpp
    catch2_unit_test_device_data.cpp
    catch2_unit_test_event_queue.cpp
    catch2_w_attribute_set_write_value.cpp
    # These currrently fail on Windows and need investigating
    $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:catch2_filedatabase.cpp>
    catch2_int_float_parsing.cpp
    catch2_jpeg_encoding.cpp
    catch2_loggerstream_attribute.cpp
    catch2_device_proxy.cpp
    catch2_log4tango.cpp
    catch2_tango_type_traits.cpp
    $<$<AND:$<NOT:$<CXX_COMPILER_ID:MSVC>>,$<STREQUAL:$<TARGET_PROPERTY:tango,TYPE>,SHARED_LIBRARY>>:catch2_create_cpp_class.cpp>
    $<$<BOOL:${TANGO_USE_TELEMETRY}>:${CMAKE_CURRENT_SOURCE_DIR}/catch2_unit_test_telemetry_current_function.cpp>
    $<$<BOOL:${HAS_FORK}>:catch2_fork.cpp>
    )
target_link_libraries(Catch2Tests PRIVATE $<$<BOOL:${nlohmann_json_FOUND}>:nlohmann_json::nlohmann_json>)

if (TANGO_CHECK_HEADERS_SELF_CONTAINED)
    file(GLOB_RECURSE _all_headers LIST_DIRECTORIES FALSE RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" CONFIGURE_DEPENDS "*.h")
    list(FILTER _all_headers INCLUDE REGEX "^catch2")
    tango_add_check_headers_self_contained(PREFIX tests INCLUDE_PATHS ${_all_headers})
    target_include_directories(tests_check_headers_self_contained PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/catch2
    )
    get_target_property(_defs Catch2Tests COMPILE_DEFINITIONS)
    target_compile_definitions(tests_check_headers_self_contained PRIVATE ${_defs})
endif()
