diff --git a/SimpleGame/CMakeLists.txt b/SimpleGame/CMakeLists.txt index a67f88a..4599843 100644 --- a/SimpleGame/CMakeLists.txt +++ b/SimpleGame/CMakeLists.txt @@ -20,3 +20,16 @@ set(CMAKE_CXX_STANDARD 23) add_subdirectory(src) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/releases/download/v1.16.0/googletest-1.16.0.tar.gz +) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +# enable_testing() must be in the source directory root (see cmake documentation at https://cmake.org/cmake/help/latest/command/enable_testing.html) +# Otherwise, Visual Studio test explorer does not see unit tests (See ticket https://developercommunity.visualstudio.com/t/No-tests-discovered-for-googletest-and-C/1148799#T-ND1150621) +include(GoogleTest) +enable_testing() diff --git a/SimpleGame/src/CMakeLists.txt b/SimpleGame/src/CMakeLists.txt index a7748a3..ed3be36 100644 --- a/SimpleGame/src/CMakeLists.txt +++ b/SimpleGame/src/CMakeLists.txt @@ -46,3 +46,5 @@ add_custom_command(TARGET simpleGame POST_BUILD "${FMOD_PATH}/api/core/lib/${CPU_THING}/${FMOD_DLL_NAME}" "$" ) + +add_subdirectory(Test) diff --git a/SimpleGame/src/Include/NoteTile.hpp b/SimpleGame/src/Include/NoteTile.hpp index 9920b8a..ab3b236 100644 --- a/SimpleGame/src/Include/NoteTile.hpp +++ b/SimpleGame/src/Include/NoteTile.hpp @@ -19,11 +19,24 @@ private: // Constructor is rivate, use create NoteTile(float play_time, float good_interval, NotePlaceEnum place); + // For unit testing + public: // Good key press for this note, is if player press good place, within // [play_time - good_interval/2, play_time + good_interval/2] static void create(float play_time, float good_interval, NotePlaceEnum place); + // Usefull in test, but don't use it anywhere + static void clear() { existing_tiles.clear(); }; // Should be called when one of arrows key pressed, return True if it's a good // press - bool CheckPress(float press_time, NotePlaceEnum key_pressed); + bool checkPress(float press_time, NotePlaceEnum key_pressed); + + // Some getters usefull in test : + float getPlayTime() const { return play_time; }; + float getGoodInterval() const { return good_interval; }; + NotePlaceEnum getPlace() const { return place; }; + NoteSprite getNodeSprite() const { return note_sprite; }; + static const std::vector> &getExistingTiles() { + return existing_tiles; + }; }; diff --git a/SimpleGame/src/Source/NoteTile.cpp b/SimpleGame/src/Source/NoteTile.cpp index 384cc23..2d3af91 100644 --- a/SimpleGame/src/Source/NoteTile.cpp +++ b/SimpleGame/src/Source/NoteTile.cpp @@ -1,9 +1,9 @@ #include "NoteTile.hpp" #include "NoteSprite.hpp" #include +#include -std::vector> NoteTile::existing_tiles; - +std::vector> NoteTile::existing_tiles{}; // private NoteTile::NoteTile(float play_time, float good_interval, NotePlaceEnum place) : play_time(play_time), good_interval(good_interval), place(place), @@ -11,16 +11,17 @@ NoteTile::NoteTile(float play_time, float good_interval, NotePlaceEnum place) // TODO do real note_sprite init, make it fall at good speed far enough... note_sprite.sprite.setPosition(sf::Vector2f(200, 0)); note_sprite.fall_speed = 1; - NoteTile::existing_tiles.push_back(std::unique_ptr(this)); } // public void NoteTile::create(float play_time, float good_interval, NotePlaceEnum place) { - NoteTile(play_time, good_interval, place); + // Can't use make_unique because constructor is private + existing_tiles.push_back( + std::unique_ptr(new NoteTile(play_time, good_interval, place))); } -bool NoteTile::CheckPress(float press_time, NotePlaceEnum) { +bool NoteTile::checkPress(float press_time, NotePlaceEnum) { for (const auto ¬e_tile : NoteTile::existing_tiles) { if (note_tile->play_time - note_tile->good_interval / 2 < press_time && note_tile->play_time + note_tile->good_interval / 2 > press_time) { diff --git a/SimpleGame/src/Test/CMakeLists.txt b/SimpleGame/src/Test/CMakeLists.txt new file mode 100644 index 0000000..40ba05f --- /dev/null +++ b/SimpleGame/src/Test/CMakeLists.txt @@ -0,0 +1,26 @@ + +# +# Lines inspired by https://google.github.io/googletest/quickstart-cmake.html +# + +# Note: include(GoogleTest)and enable_testing() already done in top CMakeLists.txt + +add_executable(unitTests + NoteTileTest.cpp + ../Include/NoteTile.hpp ../Source/NoteTile.cpp ../Source/NoteSprite.cpp +) +target_include_directories(unitTests + PRIVATE + ../Include +) +target_link_libraries(unitTests GTest::gtest_main sfml-graphics) +if (WIN32) + target_link_libraries(unitTests wsock32.lib ws2_32.lib) +else() # We are under UNIX + target_link_options(unitTests PRIVATE -pthread) +endif() + +# The next line enables CMake’s test runner to discover the tests included in the binary, +# using the GoogleTest CMake module (which was included in root CMakeLists.txt). +include(GoogleTest) +gtest_discover_tests(unitTests) diff --git a/SimpleGame/src/Test/NoteTileTest.cpp b/SimpleGame/src/Test/NoteTileTest.cpp new file mode 100644 index 0000000..60f983c --- /dev/null +++ b/SimpleGame/src/Test/NoteTileTest.cpp @@ -0,0 +1,18 @@ +#include + +#include "../Include/NoteTile.hpp" +#include "NoteSprite.hpp" + +namespace NoteTile_test { + +TEST(NoteFileTest, Yes) { EXPECT_EQ(0, 0); } + +TEST(NoteFileTest, No) { EXPECT_EQ(0, 1); } +TEST(NoteTileTest, CreateTile) { + NoteTile::clear(); + NoteTile::create(2.0f, 0.5f, NotePlaceEnum::Right); + EXPECT_FLOAT_EQ(NoteTile::getExistingTiles()[0]->getPlayTime(), 2.0f); + EXPECT_FLOAT_EQ(NoteTile::getExistingTiles()[0]->getGoodInterval(), 0.5f); + EXPECT_EQ(NoteTile::getExistingTiles()[0]->getPlace(), NotePlaceEnum::Right); +} +}; // namespace NoteTile_test