cmake_minimum_required(VERSION 3.21)
project(glaze_autopkg_tests LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(glaze CONFIG REQUIRED)
find_package(openalgz-ut CONFIG REQUIRED)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(Asio REQUIRED)

enable_testing()

add_library(glz_asio INTERFACE)
target_link_libraries(glz_asio INTERFACE Asio::Asio)

add_library(glz_test_common INTERFACE)
target_compile_features(glz_test_common INTERFACE cxx_std_23)
target_link_libraries(glz_test_common INTERFACE openalgz-ut::openalgz-ut glaze::glaze glz_asio)

add_library(glz_test_exceptions INTERFACE)
target_compile_features(glz_test_exceptions INTERFACE cxx_std_23)
target_link_libraries(glz_test_exceptions INTERFACE openalgz-ut::openalgz-ut glaze::glaze glz_asio)

add_subdirectory(api_test)
add_subdirectory(beve_test)
add_subdirectory(cbor_test)
add_subdirectory(buffer_pool_test)
add_subdirectory(cli_menu_test)
add_subdirectory(compare_test)
add_subdirectory(csv_test)
add_subdirectory(eigen_test)
add_subdirectory(example_json)
add_subdirectory(exceptions_test)
add_subdirectory(inplace_vector)
option(glaze_BUILD_PERFORMANCE_TESTS "Build performance tests" ON)
if(glaze_BUILD_PERFORMANCE_TESTS)
    add_subdirectory(int_parsing)
endif()
add_subdirectory(jmespath)
add_subdirectory(jsonrpc_test)
add_subdirectory(key_transformers_test)
add_subdirectory(lib_test)
add_subdirectory(mock_json_test)
add_subdirectory(reflection)
add_subdirectory(roundtrip)
add_subdirectory(stencil)
add_subdirectory(threading_test)
add_subdirectory(toml_test)
add_subdirectory(utility_formats)
add_subdirectory(yaml_test)
add_subdirectory(msgpack_test)
add_subdirectory(ostream_buffer_test)
add_subdirectory(istream_buffer_test)

# simple_float_test runs exhaustive tests over all 2^32 float values
# Disabled by default - run via dedicated CI workflow in Release mode
option(glaze_SIMPLE_FLOAT_TEST "Build simple_float exhaustive tests (slow, Release mode recommended)" OFF)
if(glaze_SIMPLE_FLOAT_TEST)
    add_subdirectory(simple_float_test)
endif()

# JSON Tests
add_subdirectory(chrono_test)
add_subdirectory(json_conformance)
if(glaze_BUILD_PERFORMANCE_TESTS)
    add_subdirectory(json_performance)
endif()
add_subdirectory(json_reflection_test)
add_subdirectory(json_test)

option(glaze_BUILD_NETWORKING_TESTS "Build networking tests (requires OpenSSL)" ON)
if(glaze_BUILD_NETWORKING_TESTS)
    add_subdirectory(networking_tests)
endif()

if(glaze_EETF_FORMAT)
    add_subdirectory(eetf_test)
endif(glaze_EETF_FORMAT)

# We don't run find_package_test or glaze-install_test with MSVC/Windows, because the Github action runner often chokes
# Don't run find_package on Clang, because Linux runs with Clang try to use GCC standard library and have errors before Clang 18
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
    add_test(
    NAME glaze-install_test
    COMMAND
    "${CMAKE_COMMAND}"
    --install "${PROJECT_BINARY_DIR}"
    --prefix "${CMAKE_CURRENT_BINARY_DIR}/install"
    --config $<CONFIG>
    --verbose
    )

    add_test(
    NAME find_package_test
    COMMAND
    "${CMAKE_CTEST_COMMAND}"
    --verbose
    --output-on-failure
    --build-noclean
    --build-project "${PROJECT_NAME}" # helps msvc when --build-target
    --build-generator "${CMAKE_GENERATOR}"
    --build-config $<CONFIG>
    --build-and-test
    "${CMAKE_CURRENT_SOURCE_DIR}/find_package"
    "${CMAKE_CURRENT_BINARY_DIR}/find_package"
    --build-options
    "-Dglaze_ROOT:PATH=${CMAKE_CURRENT_BINARY_DIR}/install"
    "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
    "-DBUILD_TESTING=ON"
    "$<$<BOOL:${glaze_EETF_FORMAT}>:-Dglaze_EETF_FORMAT=ON>"
    "$<$<BOOL:${glaze_EETF_FORMAT}>:-DEXTERNAL_MODULE_PATH=${PROJECT_SOURCE_DIR}/cmake>"
    --test-command "${CMAKE_CTEST_COMMAND}" --verbose --output-on-failure # inner ctest command
    )

    set_tests_properties(glaze-install_test PROPERTIES FIXTURES_SETUP glaze-install-fixture)
    set_tests_properties(find_package_test PROPERTIES FIXTURES_REQUIRED glaze-install-fixture)
endif()
