Création Test

This commit is contained in:
Crizomb 2025-06-13 01:25:45 +02:00
parent 611ff37653
commit dd67b76d1f
6 changed files with 79 additions and 6 deletions

View file

@ -20,3 +20,16 @@ set(CMAKE_CXX_STANDARD 23)
add_subdirectory(src) 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()

View file

@ -46,3 +46,5 @@ add_custom_command(TARGET simpleGame POST_BUILD
"${FMOD_PATH}/api/core/lib/${CPU_THING}/${FMOD_DLL_NAME}" "${FMOD_PATH}/api/core/lib/${CPU_THING}/${FMOD_DLL_NAME}"
"$<TARGET_FILE_DIR:simpleGame>" "$<TARGET_FILE_DIR:simpleGame>"
) )
add_subdirectory(Test)

View file

@ -19,11 +19,24 @@ private:
// Constructor is rivate, use create // Constructor is rivate, use create
NoteTile(float play_time, float good_interval, NotePlaceEnum place); NoteTile(float play_time, float good_interval, NotePlaceEnum place);
// For unit testing
public: public:
// Good key press for this note, is if player press good place, within // Good key press for this note, is if player press good place, within
// [play_time - good_interval/2, play_time + good_interval/2] // [play_time - good_interval/2, play_time + good_interval/2]
static void create(float play_time, float good_interval, NotePlaceEnum place); 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 // Should be called when one of arrows key pressed, return True if it's a good
// press // 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<std::unique_ptr<NoteTile>> &getExistingTiles() {
return existing_tiles;
};
}; };

View file

@ -1,9 +1,9 @@
#include "NoteTile.hpp" #include "NoteTile.hpp"
#include "NoteSprite.hpp" #include "NoteSprite.hpp"
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <memory>
std::vector<std::unique_ptr<NoteTile>> NoteTile::existing_tiles; std::vector<std::unique_ptr<NoteTile>> NoteTile::existing_tiles{};
// private // private
NoteTile::NoteTile(float play_time, float good_interval, NotePlaceEnum place) NoteTile::NoteTile(float play_time, float good_interval, NotePlaceEnum place)
: play_time(play_time), good_interval(good_interval), place(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... // TODO do real note_sprite init, make it fall at good speed far enough...
note_sprite.sprite.setPosition(sf::Vector2f(200, 0)); note_sprite.sprite.setPosition(sf::Vector2f(200, 0));
note_sprite.fall_speed = 1; note_sprite.fall_speed = 1;
NoteTile::existing_tiles.push_back(std::unique_ptr<NoteTile>(this));
} }
// public // public
void NoteTile::create(float play_time, float good_interval, void NoteTile::create(float play_time, float good_interval,
NotePlaceEnum place) { NotePlaceEnum place) {
NoteTile(play_time, good_interval, place); // Can't use make_unique because constructor is private
existing_tiles.push_back(
std::unique_ptr<NoteTile>(new NoteTile(play_time, good_interval, place)));
} }
bool NoteTile::CheckPress(float press_time, NotePlaceEnum) { bool NoteTile::checkPress(float press_time, NotePlaceEnum) {
for (const auto &note_tile : NoteTile::existing_tiles) { for (const auto &note_tile : NoteTile::existing_tiles) {
if (note_tile->play_time - note_tile->good_interval / 2 < press_time && if (note_tile->play_time - note_tile->good_interval / 2 < press_time &&
note_tile->play_time + note_tile->good_interval / 2 > press_time) { note_tile->play_time + note_tile->good_interval / 2 > press_time) {

View file

@ -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 CMakes 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)

View file

@ -0,0 +1,18 @@
#include <gtest/gtest.h>
#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