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(
  IDNA
  GIT_REPOSITORY https://github.com/ada-url/idna.git
  GIT_TAG        0.5.0
)
FetchContent_MakeAvailable(IDNA)

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)

add_executable(annotation_test annotation_test.cxx)
target_link_libraries(annotation_test GTest::gtest GTest::gmock jsoncpp_lib)
gtest_discover_tests(annotation_test)

add_executable(extension_test extension_test.cxx)
target_link_libraries(extension_test GTest::gtest GTest::gmock jsoncpp_lib)
gtest_discover_tests(extension_test)

add_executable(selfvalidate_test selfvalidate_test.cxx)
target_compile_definitions(selfvalidate_test PUBLIC JVALIDATE_USE_EXCEPTIONS)
if (${json_schema_test_suite_POPULATED})
target_compile_definitions(selfvalidate_test
    PUBLIC
    JVALIDATE_JSON_SCHEMA_TEST_SUITE_DIR="${json_schema_test_suite_SOURCE_DIR}"
)
endif()
target_link_libraries(selfvalidate_test GTest::gtest GTest::gmock jsoncpp_lib CURL::libcurl)

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

if (ICU_FOUND)
  target_link_libraries(selfvalidate_test ICU::uc ICU::i18n)
else()
  string(
    APPEND SelfValidateTest_Unsupported
    ":*optional_non_bmp_regex"
  )
endif()

if (IDNA_POPULATED)
  target_link_libraries(selfvalidate_test ada-idna)
else()
  string(
    APPEND SelfValidateTest_Unsupported
    ":*optional_*iri*"
    ":*optional_*idn*"
    ":*optional_*uri_template*"
  )
  string(
    APPEND SelfValidateTest_Unsupported_Suites
    ":*punycode*"
    ":*puny-code"
  )
  string(
    APPEND SelfValidateTest_Unsupported_Cases
    ":*punycode*"
    ":*puny-code"
  )
endif()

gtest_discover_tests(selfvalidate_test
                     EXTRA_ARGS --json_suite_filter=-${SelfValidateTest_Unsupported_Suites}
                                --json_case_filter=-${SelfValidateTest_Unsupported_Cases}
                     TEST_FILTER -${SelfValidateTest_Unsupported})
