cmake_minimum_required(VERSION 3.20)
project(ninlight_host_tests C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(unity
  GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
  GIT_TAG v2.6.0)
FetchContent_MakeAvailable(unity)

enable_testing()

# cJSON is third-party code: build it without -Werror.
add_library(nl_cjson STATIC ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/cJSON/cJSON.c)
target_include_directories(nl_cjson PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/cJSON)
# C1: match the firmware's nesting limit so host tests exercise the same behaviour.
target_compile_definitions(nl_cjson PUBLIC CJSON_NESTING_LIMIT=8)

set(NL_SRC ${CMAKE_CURRENT_SOURCE_DIR}/../src)

function(nl_test name)
  add_executable(${name} ${ARGN})
  target_include_directories(${name} PRIVATE ${NL_SRC} ${CMAKE_CURRENT_SOURCE_DIR})
  target_compile_options(${name} PRIVATE -Wall -Wextra -Werror)
  target_compile_definitions(${name} PRIVATE NINLIGHT_HOST_TEST=1)
  target_link_libraries(${name} PRIVATE unity m)
  add_test(NAME ${name} COMMAND ${name})
endfunction()

nl_test(test_sha256 test_sha256.c ${NL_SRC}/util/nl_sha256.c)
nl_test(test_crc test_crc.c ${NL_SRC}/util/nl_crc.c)
nl_test(test_board test_board.c ${NL_SRC}/board/nl_board.c)
nl_test(test_color test_color.c ${NL_SRC}/light/nl_color.c)
nl_test(test_engine test_engine.c ${NL_SRC}/light/nl_engine.c ${NL_SRC}/light/nl_color.c)
nl_test(test_boot_state test_boot_state.c ${NL_SRC}/light/nl_boot_state.c ${NL_SRC}/board/nl_board.c)
nl_test(test_control test_control.c ${NL_SRC}/light/nl_control.c ${NL_SRC}/light/nl_boot_state.c
        ${NL_SRC}/light/nl_engine.c ${NL_SRC}/light/nl_color.c ${NL_SRC}/board/nl_board.c)
nl_test(test_settings test_settings.c fake_flash.c ${NL_SRC}/config/nl_settings.c ${NL_SRC}/util/nl_crc.c)
nl_test(test_state_log test_state_log.c fake_flash.c ${NL_SRC}/config/nl_state_log.c
        ${NL_SRC}/light/nl_boot_state.c ${NL_SRC}/util/nl_crc.c)
nl_test(test_reset_counter test_reset_counter.c fake_flash.c ${NL_SRC}/config/nl_reset_counter.c)
nl_test(test_console_cmd test_console_cmd.c ${NL_SRC}/sys/nl_console_cmd.c)
nl_test(test_names test_names.c ${NL_SRC}/net/nl_names.c)
nl_test(test_http test_http.c ${NL_SRC}/net/nl_http.c)
nl_test(test_api test_api.c fake_api_ctx.c ${NL_SRC}/net/nl_api.c ${NL_SRC}/board/nl_board.c
        ${NL_SRC}/light/nl_boot_state.c ${NL_SRC}/config/nl_settings.c ${NL_SRC}/util/nl_crc.c fake_flash.c)
target_link_libraries(test_api PRIVATE nl_cjson)
nl_test(test_wifi_sm test_wifi_sm.c ${NL_SRC}/net/nl_wifi_sm.c)
