# -*- cmake -*-
# Copyright 2021 Jean Pierre Cimalando
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

include(CTest)
include(CheckCSourceCompiles)
find_package(PkgConfig)

# sndfile
# ------------------------------------------------------------------------------

find_package(SndFile)
if(TARGET SndFile::sndfile)
    add_library(sndfile INTERFACE)
    target_link_libraries(sndfile INTERFACE SndFile::sndfile)
else()
    pkg_check_modules(SndFileViaPkgConfig "sndfile" IMPORTED_TARGET)
    if(TARGET PkgConfig::SndFileViaPkgConfig)
        add_library(sndfile INTERFACE)
        target_link_libraries(sndfile INTERFACE PkgConfig::SndFileViaPkgConfig)
    endif()
endif()

# extra check for macOS, which can fail if build is universal but library is not
function(ysfx_check_sndfile_works VAR)
    if(NOT TARGET sndfile)
        set("${VAR}" FALSE)
    endif()
    set(CMAKE_REQUIRED_LIBRARIES sndfile)
    check_c_source_compiles("#include <sndfile.h>
int main() { sf_close((SNDFILE *)0); }" "${VAR}")
endfunction()

ysfx_check_sndfile_works(YSFX_TESTS_HAVE_SNDFILE)

# catch
# ------------------------------------------------------------------------------

include(FetchContent)

FetchContent_Declare(catch
    URL "https://github.com/catchorg/Catch2/archive/refs/tags/v2.13.7.tar.gz"
    URL_HASH "SHA512=1c3cbdecc6a3b59360a97789c4784d79d027e1b63bdc42b0e152c3272f7bad647fcd1490aa5caf67f968a6311dc9624b5a70d5eb3fbc1d5179d520e09b76c9ed")

FetchContent_GetProperties(catch)
if(NOT catch_POPULATED)
  FetchContent_Populate(catch)
endif()

add_library(catch INTERFACE)
target_include_directories(catch INTERFACE "${catch_SOURCE_DIR}/single_include/catch2")

list(APPEND CMAKE_MODULE_PATH "${catch_SOURCE_DIR}/contrib")
include(Catch)

# tests
# ------------------------------------------------------------------------------

add_executable(ysfx_tests
    "tests/ysfx_test_parse.cpp"
    "tests/ysfx_test_serialization.cpp"
    "tests/ysfx_test_slider.cpp"
    "tests/ysfx_test_midi.cpp"
    "tests/ysfx_test_audio_wav.cpp"
    "tests/ysfx_test_audio_flac.cpp"
    "tests/ysfx_test_filesystem.cpp"
    "tests/ysfx_test_preset.cpp"
    "tests/ysfx_test_c_api.c"
    "tests/ysfx_test_utils.hpp"
    "tests/ysfx_test_utils.cpp"
    "tests/ysfx_test_main.cpp")
target_link_libraries(ysfx_tests
    PRIVATE
        ysfx-private
        eel2
        eel2nasm
        wdl-base
        catch)
if(YSFX_GFX)
    target_link_libraries(ysfx_tests PUBLIC lice)
endif()
if(YSFX_TESTS_HAVE_SNDFILE)
    target_compile_definitions(ysfx_tests PRIVATE "YSFX_TESTS_HAVE_SNDFILE")
    target_link_libraries(ysfx_tests PRIVATE sndfile)
endif()
catch_discover_tests(ysfx_tests)

# test tools
# ------------------------------------------------------------------------------

add_executable(ysfx_parse_menu "tests/tools/ysfx_parse_menu.cpp")
target_link_libraries(ysfx_parse_menu PRIVATE ysfx::ysfx)
