configured testing framework to build the project src files into the test binary so that the functions to be tested can be called from any src file

This commit is contained in:
2026-03-05 17:46:10 +01:00
parent 62214f0e81
commit e97390986b
4 changed files with 78 additions and 39 deletions

View File

@@ -22,50 +22,40 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/${OUTPUT_DIR_SUFFIX}
# ------------------------------------------------------------------------------
# Dependencies
# ------------------------------------------------------------------------------
# 1. SDL3
# Disable tests and examples before adding the subdirectory
set(SDL_TESTS OFF CACHE BOOL "Disable SDL3 tests" FORCE)
set(SDL_EXAMPLES OFF CACHE BOOL "Disable SDL3 examples" FORCE)
add_subdirectory(libs/sdl3)
# 2. spdlog
# Force compiled mode instead of header-only, and disable extras
set(SPDLOG_BUILD_COMPILED ON CACHE BOOL "Build spdlog as a compiled library" FORCE)
set(SPDLOG_BUILD_EXAMPLE OFF CACHE BOOL "Disable spdlog examples" FORCE)
set(SPDLOG_BUILD_TESTS OFF CACHE BOOL "Disable spdlog tests" FORCE)
add_subdirectory(libs/spdlog)
# 3. ImGui & ImGui Node Editor
# Assuming you have CMakeLists.txt inside these directories.
# If you don't, you would use add_library() to compile their .cpp files manually here.
add_subdirectory(libs/imgui)
add_subdirectory(libs/imgui-node-editor)
# 4. Catch2
# Testing framework
add_subdirectory(libs/catch2)
# These tests can use the Catch2-provided main
add_executable(tests SakuraVNE/test/test.cpp)
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
# These tests need their own main
# add_executable(custom-main-tests test.cpp test-main.cpp)
# target_link_libraries(custom-main-tests PRIVATE Catch2::Catch2)
# ------------------------------------------------------------------------------
# Source Gathering
# ------------------------------------------------------------------------------
# Gather all .cpp and .h files in the src folder
file(GLOB_RECURSE SAKURA_SRCS "SakuraVNE/src/*.cpp" "SakuraVNE/src/*.h")
# CRITICAL: Remove main.cpp from the list so Catch2 can provide its own main()
list(REMOVE_ITEM SAKURA_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/SakuraVNE/src/main.cpp")
# ------------------------------------------------------------------------------
# SakuraVNE Executable Target
# ------------------------------------------------------------------------------
# Gather all .cpp and .h files in the src folder
file(GLOB_RECURSE SAKURA_SRCS "SakuraVNE/src/*.cpp" "SakuraVNE/src/*.h")
# Create the ConsoleApp executable
add_executable(SakuraVNE ${SAKURA_SRCS})
# ------------------------------------------------------------------------------
# Include Directories & Linking
# ------------------------------------------------------------------------------
# Create the ConsoleApp executable (explicitly adding main.cpp back in here)
add_executable(SakuraVNE "SakuraVNE/src/main.cpp" ${SAKURA_SRCS})
# Tell the compiler where the headers are
target_include_directories(SakuraVNE PRIVATE
@@ -78,36 +68,72 @@ target_include_directories(SakuraVNE PRIVATE
"libs/sdl3/include"
)
# Link the libraries. CMake handles all the libdirs automatically!
# Link the libraries. CMake handles all the libdirs automatically! [cite: 5]
target_link_libraries(SakuraVNE PRIVATE
ImGui # (Depends on what your ImGui target is actually named)
ImGuiNodeEditor # (Depends on what your ImGuiNodeEditor target is actually named)
SDL3::SDL3 # The official SDL3 target name
spdlog::spdlog # The official spdlog target name
ImGui
ImGuiNodeEditor
SDL3::SDL3
spdlog::spdlog
)
# ------------------------------------------------------------------------------
# Tests Executable Target
# ------------------------------------------------------------------------------
# These tests can use the Catch2-provided main, plus ALL your engine source files
add_executable(tests "SakuraVNE/test/test.cpp" ${SAKURA_SRCS})
# The tests need the exact same include directories so your engine #includes work
target_include_directories(tests PRIVATE
"SakuraVNE/src"
"libs/spdlog/include"
"libs/imgui"
"libs/imgui/misc"
"libs/imgui/backends"
"libs/imgui-node-editor"
"libs/sdl3/include"
)
# Link Catch2 AND the exact same dependencies your game uses
target_link_libraries(tests PRIVATE
Catch2::Catch2WithMain
ImGui
ImGuiNodeEditor
SDL3::SDL3
spdlog::spdlog
)
# ------------------------------------------------------------------------------
# Platform & Configuration Filters
# ------------------------------------------------------------------------------
# Windows-specific settings
if(WIN32)
# Apply to Game
target_compile_definitions(SakuraVNE PRIVATE PLATFORM_WINDOWS)
# Ignore MSVCRT default library
target_link_options(SakuraVNE PRIVATE "/NODEFAULTLIB:MSVCRT")
# Linux-specific settings
elseif(UNIX AND NOT APPLE)
target_compile_definitions(SakuraVNE PRIVATE PLATFORM_LINUX)
# Apply to Tests
target_compile_definitions(tests PRIVATE PLATFORM_WINDOWS)
target_link_options(tests PRIVATE "/NODEFAULTLIB:MSVCRT")
# Link dl and pthread
elseif(UNIX AND NOT APPLE)
# Apply to Game
target_compile_definitions(SakuraVNE PRIVATE PLATFORM_LINUX)
target_link_libraries(SakuraVNE PRIVATE dl pthread)
# Apply to Tests
target_compile_definitions(tests PRIVATE PLATFORM_LINUX)
target_link_libraries(tests PRIVATE dl pthread)
endif()
# Debug vs Release definitions
# $<CONFIG:...> is a generator expression that injects the flag based on the active build type
# Debug vs Release definitions for Game
target_compile_definitions(SakuraVNE PRIVATE
$<$<CONFIG:Debug>:DEBUG>
$<$<CONFIG:Release>:RELEASE>
)
# Debug vs Release definitions for Tests
target_compile_definitions(tests PRIVATE
$<$<CONFIG:Debug>:DEBUG>
$<$<CONFIG:Release>:RELEASE>
)

View File

@@ -0,0 +1,3 @@
#include "factorial.h"
unsigned int Factorial(unsigned int number) { return number >= 1 ? Factorial(number - 1) * number : 1; }

View File

@@ -0,0 +1 @@
unsigned int Factorial(unsigned int number);

View File

@@ -1 +1,10 @@
#include "factorial.h"
#include <catch2/catch_test_macros.hpp>
TEST_CASE("Factorials are computed", "[factorial]") {
REQUIRE(Factorial(0) == 1);
REQUIRE(Factorial(1) == 1);
REQUIRE(Factorial(2) == 2);
REQUIRE(Factorial(3) == 6);
REQUIRE(Factorial(10) == 3628800);
}