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:
@ -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")
|
target_include_directories(spacetris_tests PRIVATE "${CMAKE_SOURCE_DIR}/vcpkg_installed/x64-windows/include")
|
||||||
endif()
|
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
|
# Add new src subfolders to include path so old #includes continue to work
|
||||||
target_include_directories(spacetris PRIVATE
|
target_include_directories(spacetris PRIVATE
|
||||||
${CMAKE_SOURCE_DIR}/src
|
${CMAKE_SOURCE_DIR}/src
|
||||||
|
|||||||
@ -1,31 +1,38 @@
|
|||||||
#include "../src/logic/Board.h"
|
#include "../src/logic/Board.h"
|
||||||
#include <cassert>
|
#include <gtest/gtest.h>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
int main()
|
using logic::Board;
|
||||||
|
|
||||||
|
TEST(BoardTests, InitiallyEmpty)
|
||||||
{
|
{
|
||||||
using logic::Board;
|
|
||||||
Board b;
|
Board b;
|
||||||
// board starts empty
|
|
||||||
for (int y = 0; y < Board::Height; ++y)
|
for (int y = 0; y < Board::Height; ++y)
|
||||||
for (int x = 0; x < Board::Width; ++x)
|
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;
|
int y = Board::Height - 1;
|
||||||
for (int x = 0; x < Board::Width; ++x) b.set(x, y, Board::Cell::Filled);
|
for (int x = 0; x < Board::Width; ++x) b.set(x, y, Board::Cell::Filled);
|
||||||
int cleared = b.clearFullLines();
|
int cleared = b.clearFullLines();
|
||||||
assert(cleared == 1);
|
EXPECT_EQ(cleared, 1);
|
||||||
// bottom row should be empty after clear
|
for (int x = 0; x < Board::Width; ++x) EXPECT_EQ(b.at(x, Board::Height - 1), Board::Cell::Empty);
|
||||||
for (int x = 0; x < Board::Width; ++x) assert(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 y1 = Board::Height - 1;
|
||||||
int y2 = Board::Height - 3;
|
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); }
|
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();
|
int cleared = b.clearFullLines();
|
||||||
assert(cleared == 2);
|
EXPECT_EQ(cleared, 2);
|
||||||
|
}
|
||||||
std::cout << "tests/test_board: all assertions passed\n";
|
|
||||||
return 0;
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
|
return RUN_ALL_TESTS();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
},
|
},
|
||||||
"enet",
|
"enet",
|
||||||
"catch2",
|
"catch2",
|
||||||
|
"gtest",
|
||||||
"cpr",
|
"cpr",
|
||||||
"nlohmann-json",
|
"nlohmann-json",
|
||||||
"ffmpeg"
|
"ffmpeg"
|
||||||
|
|||||||
Reference in New Issue
Block a user