#MIT License
#
#Copyright (c) 2017 Mindaugas Vinkelis
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.

cmake_minimum_required(VERSION 3.5)
project(bitsery_tests CXX)

find_package(GTest 1.8 REQUIRED)

if (NOT TARGET Bitsery::bitsery)
    message(FATAL_ERROR "Bitsery::bitsery alias not set. Please generate CMake from bitsery root directory.")
endif()

file(GLOB TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
    message(WARNING "extension tests for optional is disable for VS, because VS currenty doesn't have <optional>")
    list(REMOVE_ITEM TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/serialization_ext_std_optional.cpp)
endif()

enable_testing()

foreach(TestFile ${TestSourceFiles})
    get_filename_component(TestName ${TestFile} NAME_WE)
    set(TestName bitsery.test.${TestName})
    add_executable(${TestName} ${TestFile})
    target_link_libraries(${TestName} PRIVATE GTest::Main Bitsery::bitsery)
    add_test(NAME ${TestName} COMMAND $<TARGET_FILE:${TestName}>)
endforeach()

#======================= setup development environment ====================

# get all header files for IDE  (in my case Clion) and create dummy project that consumes theses files
get_directory_property(ParentDir PARENT_DIRECTORY)
if (ParentDir)
    # only include when working from root project (Bitsery)
    file(GLOB_RECURSE HeadersForIDE ${ParentDir}/include/bitsery/*.h)
    # create dummy target IDE
    file(WRITE ${CMAKE_BINARY_DIR}/dummy_for_ide.cpp "//generated by CMake to create dummy target with all includes for IDE.")
    add_library(bitsery.dummy_for_ide ${CMAKE_BINARY_DIR}/dummy_for_ide.cpp )
    # add headers so IDE correctly show them
    target_sources(bitsery.dummy_for_ide PRIVATE ${HeadersForIDE} serialization_test_utils.h)
    target_link_libraries(bitsery.dummy_for_ide PRIVATE GTest::Main Bitsery::bitsery)
endif()
