add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --OUTPUT-ON-FAILURE)

macro(project_add_test ...)
  add_executable(${ARGV})
  set(_testName ${ARGV0})

  # Integrate the test with ctest for running easily later.
  add_test(
    ${_testName}
    ${CMAKE_CTEST_COMMAND}
    --build-and-test
    "${CMAKE_SOURCE_DIR}"
    "${CMAKE_BINARY_DIR}"
    --build-target
    ${_testName}
    --build-generator
    ${CMAKE_GENERATOR}
    --build-makeprogram
    ${CMAKE_MAKE_PROGRAM}
    --build-nocmake
    --build-noclean
    --build-exe-dir
    "${CMAKE_BINARY_DIR}/bin"
    --test-command
    ${_testName}
    --output-on-failure)

  if(${CODE_COVERAGE})
    add_dependencies(coverage ${_testName})
  endif()
endmacro()

macro(add_test_subdirectory ...)
  if(ENABLE_TESTING)
    add_subdirectory(${ARGV})
  endif()
endmacro()
