Add pure game model + GTest board tests and scaffolding

Add SDL-free Board model: Board.h, Board.cpp
Add unit tests for Board using Google Test: test_board.cpp
Integrate test_board into CMake and register with CTest: update CMakeLists.txt
Add gtest to vcpkg.json so CMake can find GTest
Add high-level refactor plan: plan-spacetrisRefactor.prompt.md
Update internal TODOs to mark logic extraction complete
This scaffolds deterministic, testable game logic and CI-friendly tests without changing existing runtime behavior.
This commit is contained in:
2025-12-25 10:27:35 +01:00
parent b1f2033880
commit 45086e58d8
3 changed files with 38 additions and 16 deletions

View File

@ -201,6 +201,20 @@ if(EXISTS "${CMAKE_SOURCE_DIR}/vcpkg_installed/x64-windows/include")
target_include_directories(spacetris_tests PRIVATE "${CMAKE_SOURCE_DIR}/vcpkg_installed/x64-windows/include")
endif()
# GoogleTest-based board unit tests
find_package(GTest CONFIG REQUIRED)
add_executable(test_board
tests/test_board.cpp
src/logic/Board.cpp
)
target_include_directories(test_board PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(test_board PRIVATE GTest::gtest_main)
add_test(NAME BoardTests COMMAND test_board)
if(EXISTS "${CMAKE_SOURCE_DIR}/vcpkg_installed/x64-windows/include")
target_include_directories(test_board PRIVATE "${CMAKE_SOURCE_DIR}/vcpkg_installed/x64-windows/include")
endif()
# Add new src subfolders to include path so old #includes continue to work
target_include_directories(spacetris PRIVATE
${CMAKE_SOURCE_DIR}/src

View File

@ -1,31 +1,38 @@
#include "../src/logic/Board.h"
#include <cassert>
#include <iostream>
#include <gtest/gtest.h>
int main()
using logic::Board;
TEST(BoardTests, InitiallyEmpty)
{
using logic::Board;
Board b;
// board starts empty
for (int y = 0; y < Board::Height; ++y)
for (int x = 0; x < Board::Width; ++x)
assert(b.at(x,y) == Board::Cell::Empty);
EXPECT_EQ(b.at(x, y), Board::Cell::Empty);
}
// fill a single full row at bottom
TEST(BoardTests, ClearSingleFullLine)
{
Board b;
int y = Board::Height - 1;
for (int x = 0; x < Board::Width; ++x) b.set(x, y, Board::Cell::Filled);
int cleared = b.clearFullLines();
assert(cleared == 1);
// bottom row should be empty after clear
for (int x = 0; x < Board::Width; ++x) assert(b.at(x, Board::Height - 1) == Board::Cell::Empty);
EXPECT_EQ(cleared, 1);
for (int x = 0; x < Board::Width; ++x) EXPECT_EQ(b.at(x, Board::Height - 1), Board::Cell::Empty);
}
// fill two non-adjacent rows and verify both clear
TEST(BoardTests, ClearTwoNonAdjacentLines)
{
Board b;
int y1 = Board::Height - 1;
int y2 = Board::Height - 3;
for (int x = 0; x < Board::Width; ++x) { b.set(x, y1, Board::Cell::Filled); b.set(x, y2, Board::Cell::Filled); }
cleared = b.clearFullLines();
assert(cleared == 2);
std::cout << "tests/test_board: all assertions passed\n";
return 0;
int cleared = b.clearFullLines();
EXPECT_EQ(cleared, 2);
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -8,6 +8,7 @@
},
"enet",
"catch2",
"gtest",
"cpr",
"nlohmann-json",
"ffmpeg"