include_directories(${CMAKE_CURRENT_LIST_DIR})

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-Wno-deprecated-copy HAS_NO_DEPRECATED_COPY)

file(GLOB_RECURSE all_tests RELATIVE ${CMAKE_CURRENT_LIST_DIR} *.cpp)

foreach(test ${all_tests})
  get_filename_component(test_name ${test} NAME_WE)
  get_filename_component(test_dir ${test} DIRECTORY)
  add_executable(${test_name} ${test})
  if(WIN32)
    target_compile_options(${test_name} PUBLIC /Wall /bigobj)
  else()
    target_compile_options(${test_name} PUBLIC -Wall -pedantic -Wextra -Werror)
    if(HAS_NO_DEPRECATED_COPY)
      target_compile_options(${test_name} PUBLIC -Wno-deprecated-copy)
    endif()
  endif()
  set_property(TARGET ${test_name} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${test_dir})
  add_test(NAME ${test_name} COMMAND ${test_name} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${test_dir})
endforeach()
