#
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
# Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

# append all the test cases here (file name without the ".cpp" suffix)
set(test_cases
    test_data_type
    test_configuration
    test_connection
    test_entity
    test_file_transmission
    test_info
    test_issues
    test_list_properties
    test_protobuf
    test_transaction
    test_utility
    test_value
    test_ccaosdb
    )

IF(BUILD_ACM)
    list(APPEND test_cases
         test_user
         )
ENDIF()

###################################################
### Set up tests using GoogleTest (GTest)
###################################################

# special linting for tests
set(_CMAKE_CXX_CLANG_TIDY_TEST_CHECKS
  "${_CMAKE_CXX_CLANG_TIDY_CHECKS},-cert-err58-cpp,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-modernize-use-trailing-return-type,-google-readability-avoid-underscore-in-googletest-name,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-cppcoreguidelines-avoid-goto,-hicpp-avoid-goto,-readability-function-cognitive-complexity,-cppcoreguidelines-non-private-member-variables-in-classes,-misc-non-private-member-variables-in-classes,-clang-analyzer-cplusplus.Move,-clang-diagnostic-unused-result"
)

# add special cmake functions for gtest
find_package(GTest REQUIRED)
include(GoogleTest)

message(DEBUG "CMAKE_INCLUDE_PATH: ${CMAKE_INCLUDE_PATH}")

# loop over all test cases and add them to the test runner
list(LENGTH test_cases len_test_cases)
math(EXPR len_test_cases "${len_test_cases} - 1")
foreach (i RANGE "${len_test_cases}")
    list(GET test_cases ${i} test_case_name)
    add_executable(${test_case_name} ${test_case_name}.cpp)
    set(libcaosdb_TEST_SRC "${CMAKE_CURRENT_SOURCE_DIR}/${test_case_name}.cpp
        ${libcaosdb_TEST_SRC}")
    target_link_libraries(${test_case_name}
        PRIVATE GTest::gtest_main ${LIBCAOSDB} ccaosdb  gtest::gtest)
    target_include_directories(${test_case_name}
      PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
    if(_LINTING)
        message(STATUS "linting for tests: ${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}")
        set_target_properties(${test_case_name}
            PROPERTIES
            CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY};${_CMAKE_CXX_CLANG_TIDY_TEST_CHECKS}"
            CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}")
    endif()
    gtest_discover_tests(${test_case_name}
        WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
        PROPERTIES
            LABELS "caosdb-cpplib-unit-tests"
        )
endforeach ()

# copy test data to build dir
set(TEST_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/test_data")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/caosdb_test_utility.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/caosdb_test_utility.h)




###################################################
### Set up test coverage repor (Gcov + Lcov)
###################################################

include(CodeCoverage)

if (LCOV_PATH)
    message(STATUS "Found LCOV: ${LCOV_PATH}")
    target_link_libraries(caosdb gcov)
    target_link_libraries(ccaosdb gcov)
    setup_target_for_coverage_lcov(
        NAME unit_test_coverage
        EXECUTABLE ctest -L caosdb-cpplib-unit-tests
        EXCLUDE "${CMAKE_BINARY_DIR}/*"
        DEPENDENCIES caosdb ccaosdb ${test_cases}
        LCOV_ARGS --rc lcov_branch_coverage=1 --no-external
        GENHTML_ARGS --rc lcov_branch_coverage=1
        )
    message(STATUS "Adding COMPILE_FLAGS for coverage: ${COVERAGE_COMPILER_FLAGS}")
    set(TARGET_CAOSDB_COMPILE_FLAGS "${TARGET_CAOSDB_COMPILE_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
    set(TARGET_CCAOSDB_COMPILE_FLAGS "${TARGET_CCAOSDB_COMPILE_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
    set(TARGET_CAOSDB_COMPILE_FLAGS ${TARGET_CAOSDB_COMPILE_FLAGS} PARENT_SCOPE)
    set(TARGET_CCAOSDB_COMPILE_FLAGS ${TARGET_CCAOSDB_COMPILE_FLAGS} PARENT_SCOPE)
    set_target_properties(caosdb PROPERTIES
        COMPILE_FLAGS "${TARGET_CAOSDB_COMPILE_FLAGS}")
    set_target_properties(ccaosdb PROPERTIES
        COMPILE_FLAGS "${TARGET_CCAOSDB_COMPILE_FLAGS}")
else ()
    if (NOT SKIP_CODE_COVERAGE)
        message(WARNING "Could not generate code coverage report. Please install lcov.")
        add_custom_target(unit_test_coverage ctest -L caosdb-cpplib-unit-tests
            DEPENDS caosdb ccaosdb ${test_cases}
        )
    endif()
endif ()
