enable_testing()
# MUST do this before including IDNA
# because it will otherwise poison GTest with BUILD_GMOCK=OFF
find_package(GTest CONFIG REQUIRED COMPONENTS gtest gmock)

find_package(jsoncpp REQUIRED)
find_package(CURL REQUIRED)

# ICU Components Reference:
# https://cmake.org/cmake/help/latest/module/FindICU.html
# ICU components = data, i18n, io, le, lx, test, tu and uc.
find_package(ICU 77.1 COMPONENTS uc i18n)

FetchContent_Declare(
  json_schema_test_suite
  GIT_REPOSITORY https://github.com/json-schema-org/JSON-Schema-Test-Suite.git
  GIT_TAG        Test-JSON-Schema-Acceptance-1.037
)
FetchContent_MakeAvailable(json_schema_test_suite)

include(GoogleTest)

set(JVALIDATE_UNIT_TESTS
    annotation_test extension_test validation_visitor_test enum_test detail_test
    regex_test adapter_test jsoncpp_adapter_test)

set(JVALIDATE_TESTS selfvalidate_test ${JVALIDATE_UNIT_TESTS})

# Each test executable matches with its filename.
foreach(CASE IN LISTS JVALIDATE_TESTS)
  add_executable(${CASE} ${CASE}.cxx)
endforeach()

if (JVALIDATE_COVERAGE)
  # Include all test files in a single executable for coverage purposes
  add_executable(jvalidate_mono_test $<LIST:TRANSFORM,${JVALIDATE_TESTS},APPEND,.cxx>)
  # Disable main() in other source files
  target_compile_definitions(jvalidate_mono_test PRIVATE JVALIDATE_MONOTEST)
  # Enable coverage
  target_compile_options(jvalidate_mono_test PRIVATE -fprofile-instr-generate -fcoverage-mapping)
  target_link_options(jvalidate_mono_test PRIVATE -fprofile-instr-generate -fcoverage-mapping)

  # Add to the list of tests in order to perform the rest of the test setup...
  list(APPEND JVALIDATE_TESTS jvalidate_mono_test)
endif()

foreach(CASE IN LISTS JVALIDATE_TESTS)
  target_link_libraries(${CASE} GTest::gtest GTest::gmock jsoncpp_lib CURL::libcurl)
  target_compile_definitions(${CASE} PRIVATE JVALIDATE_USE_EXCEPTIONS)
  if (${json_schema_test_suite_POPULATED})
    target_compile_definitions(${CASE}
        PUBLIC
        JVALIDATE_JSON_SCHEMA_TEST_SUITE_DIR="${json_schema_test_suite_SOURCE_DIR}"
    )
  endif()

  if (ICU_FOUND)
    target_compile_definitions(${CASE} PUBLIC JVALIDATE_HAS_ICU=1)
    target_link_libraries(${CASE} ICU::uc ICU::i18n)
  else()
    target_compile_definitions(${CASE} PUBLIC JVALIDATE_HAS_ICU=0)
  endif()
endforeach()

string(
  JOIN ":"
  SelfValidateTest_Unsupported
  "*optional_content"
  "*optional_*ecmascript_regex"
  "*optional_zeroTerminatedFloats"
  "*optional_format*"
)
set(SelfValidateTest_Unsupported_Suites "")
set(SelfValidateTest_Unsupported_Cases "*leap second")

if (NOT ICU_FOUND)
  string(
    APPEND SelfValidateTest_Unsupported
    ":*optional_non_bmp_regex"
  )
endif()

foreach(CASE IN LISTS JVALIDATE_UNIT_TESTS)
  gtest_discover_tests(${CASE})
endforeach()
gtest_discover_tests(selfvalidate_test
                     EXTRA_ARGS --json_suite_filter=-${SelfValidateTest_Unsupported_Suites}
                                --json_case_filter=-${SelfValidateTest_Unsupported_Cases}
                     TEST_FILTER -${SelfValidateTest_Unsupported})
