From e97390986b3823e74efb15e815b4dae1a6eb07a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hatvani=20Tam=C3=A1s?= Date: Thu, 5 Mar 2026 17:46:10 +0100 Subject: [PATCH] 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 --- CMakeLists.txt | 104 ++++++++++++++++++++++-------------- SakuraVNE/src/factorial.cpp | 3 ++ SakuraVNE/src/factorial.h | 1 + SakuraVNE/test/test.cpp | 9 ++++ 4 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 SakuraVNE/src/factorial.cpp create mode 100644 SakuraVNE/src/factorial.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0884798..25a4817 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -# Require a modern version of CMake +# Require a modern version of CMake cmake_minimum_required(VERSION 3.15) # Define the workspace/project @@ -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) - # Link dl and pthread + # Apply to Tests + target_compile_definitions(tests PRIVATE PLATFORM_WINDOWS) + target_link_options(tests PRIVATE "/NODEFAULTLIB:MSVCRT") + +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 -# $ 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 $<$:DEBUG> $<$:RELEASE> ) + +# Debug vs Release definitions for Tests +target_compile_definitions(tests PRIVATE + $<$:DEBUG> + $<$:RELEASE> +) diff --git a/SakuraVNE/src/factorial.cpp b/SakuraVNE/src/factorial.cpp new file mode 100644 index 0000000..9fe809d --- /dev/null +++ b/SakuraVNE/src/factorial.cpp @@ -0,0 +1,3 @@ +#include "factorial.h" + +unsigned int Factorial(unsigned int number) { return number >= 1 ? Factorial(number - 1) * number : 1; } diff --git a/SakuraVNE/src/factorial.h b/SakuraVNE/src/factorial.h new file mode 100644 index 0000000..b57c988 --- /dev/null +++ b/SakuraVNE/src/factorial.h @@ -0,0 +1 @@ +unsigned int Factorial(unsigned int number); diff --git a/SakuraVNE/test/test.cpp b/SakuraVNE/test/test.cpp index 8b13789..757fb21 100644 --- a/SakuraVNE/test/test.cpp +++ b/SakuraVNE/test/test.cpp @@ -1 +1,10 @@ +#include "factorial.h" +#include +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); +}