# SPDX-FileCopyrightText: 2026 Jérôme SONRIER <jsid@emor3j.fr.eu.org>
# SPDX-License-Identifier: GPL-2.0-or-later

add_executable(test-core test_core.c)
target_link_libraries(test-core PRIVATE taylet-core)
set_target_properties(test-core PROPERTIES C_STANDARD 11)
if(TAYLET_WARNINGS AND CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
  target_compile_options(test-core PRIVATE -Wall)
endif()

add_test(NAME core COMMAND test-core)

# The SDL driver, tested on its own with SDL's dummy audio driver: no sound
# card is needed, so it also runs in a build chroot.
if(TAYLET_BUILD_SDL)
  add_executable(test-sdl
    test_sdl.c
    ${PROJECT_SOURCE_DIR}/src/sdl/drv-sdl.c)
  target_include_directories(test-sdl PRIVATE ${PROJECT_SOURCE_DIR}/src/core)
  target_link_libraries(test-sdl PRIVATE SDL2::SDL2)
  if(UNIX)
    target_link_libraries(test-sdl PRIVATE m)
  endif()
  set_target_properties(test-sdl PROPERTIES C_STANDARD 11)
  if(TAYLET_WARNINGS AND CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
    target_compile_options(test-sdl PRIVATE -Wall)
  endif()

  add_test(NAME sdl COMMAND test-sdl)
  set_tests_properties(sdl PROPERTIES
    ENVIRONMENT "SDL_AUDIODRIVER=dummy")
endif()

# The Qt part of taylet-kde (ring buffer, controller), without KDE. The null
# audio output plays much faster than real time and needs no sound card.
if(TAYLET_BUILD_KDE)
  add_executable(test-qt test_qt.cpp)
  target_link_libraries(test-qt PRIVATE taylet-qt)
  set_target_properties(test-qt PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED ON)

  add_test(NAME qt COMMAND test-qt)
  set_tests_properties(qt PROPERTIES
    ENVIRONMENT "TAYLET_AUDIO_NULL=50")
endif()

# The command-line players, run without any file to play (so no audio device
# is opened): --version, --help and -h print and exit with 0; a missing file
# name and an unknown option exit with 1 and explain why on stderr.
function(taylet_cli_test name target arg expect_rc expect_out expect_err)
  add_test(NAME ${name}
    COMMAND ${CMAKE_COMMAND}
      -DEXE=$<TARGET_FILE:${target}>
      -DARG=${arg}
      -DEXPECT_RC=${expect_rc}
      -DEXPECT_OUT=${expect_out}
      -DEXPECT_ERR=${expect_err}
      -P ${CMAKE_CURRENT_SOURCE_DIR}/check_cli.cmake)
endfunction()

foreach(player IN ITEMS taylet taylet-sdl)
  if(TARGET ${player})
    taylet_cli_test(${player}-version ${player} --version 0
      "${player} ${PROJECT_VERSION}" "")
    taylet_cli_test(${player}-help ${player} --help 0
      "usage: ${player}" "")
    taylet_cli_test(${player}-short-help ${player} -h 0
      "usage: ${player}" "")
    taylet_cli_test(${player}-no-file ${player} "" 1
      "" "you must specify the file")
    taylet_cli_test(${player}-bad-option ${player} --bogus 1
      "" "not recognised")
  endif()
endforeach()
