diff --git a/CMakeLists.txt b/CMakeLists.txt index e64436a..64e08f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/tests/test_board.cpp b/tests/test_board.cpp index babc08a..3eb941c 100644 --- a/tests/test_board.cpp +++ b/tests/test_board.cpp @@ -1,31 +1,38 @@ #include "../src/logic/Board.h" -#include -#include +#include -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(); } diff --git a/vcpkg.json b/vcpkg.json index f93b924..b4a5264 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -8,6 +8,7 @@ }, "enet", "catch2", + "gtest", "cpr", "nlohmann-json", "ffmpeg"