
include(FetchContent)

FetchContent_Declare(Catch2
    GIT_REPOSITORY https://github.com/catchorg/Catch2.git
    GIT_TAG f981c9cbcac07a2690e5a86767eba490b5465463 # v3.5.1
    FIND_PACKAGE_ARGS
)

FetchContent_MakeAvailable(Catch2)

add_executable(test-flux

    test_concepts.cpp
    test_overflow.cpp
    test_optional.cpp
    test_predicates.cpp
    test_simple_sequence.cpp
    test_apply.cpp

    test_adjacent.cpp
    test_adjacent_filter.cpp
    test_adjacent_map.cpp
    test_all_any_none.cpp
    test_bounds_checked.cpp
    test_cache_last.cpp
    test_cartesian_product.cpp
    test_cartesian_product_with.cpp
    test_chain.cpp
    test_chunk.cpp
    test_chunk_by.cpp
    test_contains.cpp
    test_compare.cpp
    test_count.cpp
    test_count_if.cpp
    test_cursors.cpp
    test_cycle.cpp
    test_drop.cpp
    test_drop_while.cpp
    test_ends_with.cpp
    test_equal.cpp
    test_fill.cpp
    test_filter.cpp
    test_find.cpp
    test_find_min_max.cpp
    test_flatten.cpp
    test_for_each.cpp
    test_fold.cpp
    test_front_back.cpp
    test_generator.cpp
    test_map.cpp
    test_mask.cpp
    test_minmax.cpp
    test_output_to.cpp
    test_range_iface.cpp
    test_read_only.cpp
    test_reverse.cpp
    test_scan.cpp
    test_set_adaptors.cpp
    test_slide.cpp
    test_split.cpp
    test_sort.cpp
    test_starts_with.cpp
    test_stride.cpp
    test_take.cpp
    test_take_while.cpp
    test_to.cpp
    test_unchecked.cpp
    test_write_to.cpp
    test_zip.cpp
    test_zip_algorithms.cpp

    test_array_ptr.cpp
    test_bitset.cpp
    test_empty.cpp
    test_from_range.cpp
    test_getlines.cpp
    test_iota.cpp
    test_istream.cpp
    test_istreambuf.cpp
    test_repeat.cpp
    test_single.cpp
    test_unfold.cpp
)
target_link_libraries(test-flux flux-internal Catch2::Catch2WithMain)
target_compile_definitions(test-flux PUBLIC
    FLUX_UNWIND_ON_ERROR
    FLUX_ERROR_ON_OVERFLOW
    FLUX_DISABLE_STATIC_BOUNDS_CHECKING
)
target_precompile_headers(test-flux PRIVATE "catch.hpp")

if(FLUX_ENABLE_ASAN)
    target_compile_options(test-flux PRIVATE -fsanitize=address)
    target_link_options(test-flux PRIVATE -fsanitize=address)
endif()

if(FLUX_ENABLE_UBSAN)
    target_compile_options(test-flux PRIVATE -fsanitize=undefined)
    target_link_options(test-flux PRIVATE -fsanitize=undefined)
endif()

if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.28.0")
    if (FLUX_BUILD_TESTS_USING_MODULE)
        target_sources(test-flux PUBLIC
            FILE_SET CXX_MODULES
            BASE_DIRS ${PROJECT_SOURCE_DIR}/module
            FILES ${PROJECT_SOURCE_DIR}/module/flux.cpp
        )

        target_compile_definitions(test-flux PRIVATE -DUSE_MODULES)
        set_target_properties(test-flux PROPERTIES CXX_SCAN_FOR_MODULES On)

        # Squish MSVC warning when building the module, hopefully we're not actually doing anything wrong
        if(MSVC)
            target_compile_options(test-flux PRIVATE
                    /wd5244 # '#include <flux.hpp>' in the purview of module 'flux' appears erroneous
            )
        endif()
    endif()
endif()

if(FLUX_BUILD_MODULE)
    add_executable(test-module-import test_module_import.cpp)
    target_link_libraries(test-module-import PUBLIC flux-mod)
    set_target_properties(test-module-import PROPERTIES
                          CXX_EXTENSIONS Off
                          CXX_SCAN_FOR_MODULES On)
endif()

list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(Catch)
catch_discover_tests(test-flux)
