cmake_minimum_required(VERSION 3.6.0)
project(TestCTestProcess VERSION 0.1.0)

add_library(TestUtils SHARED)
target_sources(TestUtils PRIVATE test_utils.cpp)
target_include_directories(TestUtils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
set_target_properties(TestUtils PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED
                                                           ON
                                           WINDOWS_EXPORT_ALL_SYMBOLS ON)

set(TESTS_DIR
    ""
    CACHE STRING "Directory where the files generated by tests will be written")
# Will generate a file get_test_dir.h with the content of get_test_dir.h.in It's
# goal is to provide a function that returns the directory where the tests will
# be executed
file(TO_CMAKE_PATH "${TESTS_DIR}" TESTS_DIR_FOR_CPP)
configure_file("get_test_dir.h.in" "get_test_dir.h" @ONLY)
add_library(GetTestDir INTERFACE)
target_include_directories(GetTestDir INTERFACE ${CMAKE_CURRENT_BINARY_DIR})

# Adds an executable that will generate an output file by concatenating the
# content of each file in the test directory
add_executable(GenerateOutputFile generate_output_file.cpp)
target_link_libraries(GenerateOutputFile PRIVATE GetTestDir)
set_target_properties(GenerateOutputFile PROPERTIES CXX_STANDARD 17
                                                    CXX_STANDARD_REQUIRED ON)

include(CTest)

# The generation of json readable output file must occur after each test run
# Declares a fixture that will be executed after each test run
add_test(NAME "Generate_Output_File" COMMAND GenerateOutputFile)
set_tests_properties("Generate_Output_File" PROPERTIES FIXTURES_CLEANUP GENOUT)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(RegisterTest)

set(TESTS_OUTPUT_FILES
    ""
    CACHE STRING "Output files generated by tests")
set(TESTS_NAMES
    ""
    CACHE STRING "Names of the tests")
set(TESTS_SUCCESS
    ""
    CACHE BOOL "Success of the tests")

register_tests(
  TEST_DIRECTORY
  ${TESTS_DIR}
  TEST_NAME_LIST
  ${TESTS_NAMES}
  TEST_OUTPUT_FILE_LIST
  ${TESTS_OUTPUT_FILES}
  TEST_SUCCESS_LIST
  ${TESTS_SUCCESS})
