# Check whether gtest is available. If not, build from source.
find_package(GTest QUIET)
find_package(ament_cmake_gtest QUIET)
if(NOT GTest_FOUND)
  include(FetchContent)
  FetchContent_Declare(
    googletest
    URL https://github.com/google/googletest/archive/refs/tags/v1.17.0.zip
  )
  # For Windows: Prevent overriding the parent project's compiler/linker settings
  set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  FetchContent_MakeAvailable(googletest)

  # gtest target is non-namespaced whe built via FetchContent,
  # so we will make an alias to correct this.
  if(TARGET gtest AND NOT TARGET GTest::gtest)
    add_library(GTest::gtest ALIAS gtest)
  endif()
endif()

# Now simply link against gtest or gtest_main as needed.
set(TEST_SOURCES
  gtest_main.cpp
  test_constraints.cpp
  test_solver.cpp
  test_poly_geometric_path.cpp
  test_hermite_spline.cpp
  test_cubic_spline.cpp
  test_algorithm.cpp
  test_parametrizer.cpp
  test_spline_parametrizer.cpp
  test_toppra_approach.cpp
  test_issue_198.cpp
  )

add_executable(all_tests ${TEST_SOURCES})
target_link_libraries(all_tests PUBLIC toppra GTest::gtest ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(all_tests PRIVATE .)

if(BUILD_WITH_PINOCCHIO)
  target_compile_definitions(all_tests PRIVATE -DBUILD_WITH_PINOCCHIO)
  target_link_libraries(all_tests PUBLIC pinocchio::pinocchio)
endif()
if(BUILD_WITH_qpOASES)
  target_compile_definitions(all_tests PRIVATE -DBUILD_WITH_qpOASES)
endif()
if(BUILD_WITH_GLPK)
  target_compile_definitions(all_tests PRIVATE -DBUILD_WITH_GLPK)
endif()
if (OPT_MSGPACK)
  target_compile_definitions(all_tests PRIVATE -DTOPPRA_OPT_MSGPACK)
endif()
if (TOPPRA_DEBUG_ON)
  target_compile_definitions(all_tests PRIVATE -DTOPPRA_DEBUG_ON)
endif()
if (TOPPRA_WARN_ON)
  target_compile_definitions(all_tests PRIVATE TOPPRA_WARN_ON)
endif()
add_test(NAME all_tests COMMAND all_tests)
