# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT

cmake_minimum_required (VERSION 3.13)

set(azure-core-test)

# Create test data for FileUpload test (100K) by writing 1K * 100 times
set(RANGE 0)
set(1K "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
file(WRITE ${CMAKE_BINARY_DIR}/fileData "")
while(RANGE LESS 100)
     file(APPEND ${CMAKE_BINARY_DIR}/fileData "${1K}")
     MATH(EXPR RANGE "${RANGE}+1")
endwhile()
add_compile_definitions(AZURE_TEST_DATA_PATH="${CMAKE_BINARY_DIR}")

project (azure-core-test LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

if(BUILD_TRANSPORT_CURL)
  SET(CURL_OPTIONS_TESTS curl_options.cpp)
  SET(CURL_SESSION_TESTS curl_session_test.cpp)
  SET(CURL_CONNECTION_POOL_TESTS curl_connection_pool.cpp)
endif()

if(RUN_LONG_UNIT_TESTS)
  add_compile_definitions(RUN_LONG_UNIT_TESTS)
endif()

include(GoogleTest)

add_executable (
  azure-core-test
    base64.cpp
    bearer_token_authentication_policy.cpp
    bodystream.cpp
    case_insensitive_containers.cpp
    client_options.cpp
    context.cpp
    ${CURL_CONNECTION_POOL_TESTS}
    ${CURL_OPTIONS_TESTS}
    ${CURL_SESSION_TESTS}
    datetime.cpp
    etag.cpp
    http.cpp
    json.cpp
    log_policy.cpp
    logging.cpp
    macro_guard.cpp
    main.cpp
    match_conditions.cpp
    md5.cpp
    modified_conditions.cpp
    nullable.cpp
    operation.cpp
    operation_status.cpp
    pipeline.cpp
    policy.cpp
    request_id_policy.cpp
    response_t.cpp
    retry_policy.cpp
    simplified_header.cpp
    string.cpp
    telemetry_policy.cpp
    transport_adapter_base.cpp
    transport_adapter_implementation.cpp
    url.cpp
    uuid.cpp
)

if (MSVC)
  # Disable warnings:
  # - C26495: Variable
  #             - 'testing::internal::Mutex::critical_section_'
  #             - 'testing::internal::Mutex::critical_section_init_phase_'
  #             - 'testing::internal::Mutex::owner_thread_id_'
  #             - 'testing::internal::Mutex::type_'
  #           is uninitialized. Always initialize member variables (type.6).
  # - C26812: The enum type
  #             - 'testing::internal::Mutex::StaticConstructorSelector'
  #             - 'testing::TestPartResult::Type'
  #           is unscoped. Prefer 'enum class' over 'enum' (Enum.3)
  # - C6326: Google comparisons 
  target_compile_options(azure-core-test PUBLIC /wd26495 /wd26812 /wd6326 /wd28204 /wd28020 /wd6330 /wd4389)
endif()

# Adding private headers from CORE to the tests so we can test the private APIs with no relative paths include.
target_include_directories (azure-core-test PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../src>)

target_link_libraries(azure-core-test PRIVATE azure-core gtest gmock)

## Global context test
add_executable (
  azure-core-global-context-test
  global_context.cpp
)
if (MSVC)
  # Disable gtest warnings for msvc
  target_compile_options(azure-core-global-context-test PUBLIC /wd26495 /wd26812 /wd6326 /wd28204 /wd28020 /wd6330 /wd4389)
endif()
target_link_libraries(azure-core-global-context-test PRIVATE azure-core gtest_main)

# gtest_discover_tests will scan the test from azure-core-test and call add_test
# for each test to ctest. This enables `ctest -r` to run specific tests directly.
gtest_discover_tests(azure-core-test
     TEST_PREFIX azure-core.
     NO_PRETTY_TYPES
     NO_PRETTY_VALUES)

if(BUILD_TRANSPORT_CURL)
  ################## Azure Libcurl Core Test #################################
  # Creating one exe file alone for this test since it requires the full control over the connection pool.
  # This test will check that end-user can call `curl_global_cleanup()` with active handlers in the connection pool
  # without getting errors.
  add_executable (
    azure-core-libcurl-test
    azure_libcurl_core_main.cpp
  )

  if (MSVC)
    # warning C4389: '==': signed/unsigned mismatch
    # warning C6326: Google comparisons 
    target_compile_options(azure-core-libcurl-test PUBLIC /wd4389 /wd6326 )
  endif()
  
  # Adding private headers from CORE to the tests so we can test the private APIs with no relative paths include.
  target_include_directories (azure-core-libcurl-test PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../src>)

  target_link_libraries(azure-core-libcurl-test PRIVATE azure-core gtest gmock)

  # Use the same prefix to run this test
  gtest_discover_tests(azure-core-libcurl-test
        TEST_PREFIX azure-core.)

endif()
gtest_discover_tests(azure-core-global-context-test
     TEST_PREFIX azure-core.
     NO_PRETTY_TYPES
     NO_PRETTY_VALUES)
