From d3ebe5fea047001840b8c4c21893ad36fc7ad2d4 Mon Sep 17 00:00:00 2001 From: Nimac0 Date: Mon, 29 Apr 2024 20:33:59 +0200 Subject: [PATCH] test(setup) basic setup for googletest --- CMakeLists.txt | 35 +++++++++++++- include/CollisionHandler.h | 2 +- unittests.cc | 93 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 unittests.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 1905143..3b69e85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,15 @@ set(BUILD_SHARED_LIBS FALSE) set(SDL2MIXER_VENDORED ON) set(SDL2TTF_VENDORED ON) +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip +) + +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + add_subdirectory(extern/SDL EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_image EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL) @@ -38,5 +47,29 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug") target_link_libraries(${PROJECT_NAME} PRIVATE "-fsanitize=address") endif() +file(COPY ${PROJECT_SOURCE_DIR}/assets DESTINATION ${PROJECT_BINARY_DIR}) -file(COPY ${PROJECT_SOURCE_DIR}/assets DESTINATION ${PROJECT_BINARY_DIR}) \ No newline at end of file + +enable_testing() + +add_executable( + unittests + unittests.cc + ${SOURCES} +) + +target_include_directories(unittests PRIVATE ${PROJECT_INCLUDE_DIR} +) + +target_link_libraries( + unittests + GTest::gtest_main + GTest::gmock_main + SDL2::SDL2-static + SDL2_image::SDL2_image-static + SDL2_mixer::SDL2_mixer-static + SDL2_ttf::SDL2_ttf-static +) + +include(GoogleTest) +gtest_discover_tests(unittests) \ No newline at end of file diff --git a/include/CollisionHandler.h b/include/CollisionHandler.h index 1fdba31..0412cf5 100644 --- a/include/CollisionHandler.h +++ b/include/CollisionHandler.h @@ -30,7 +30,7 @@ public: CollisionHandler(Manager& mManager) : manager(mManager) { }; - ~CollisionHandler(); + ~CollisionHandler() {}; static IntersectionBitSet getIntersection( // intersections relative to entityA Entity* entityA, diff --git a/unittests.cc b/unittests.cc new file mode 100644 index 0000000..fb1d94f --- /dev/null +++ b/unittests.cc @@ -0,0 +1,93 @@ +#include +#include +#include + +#include "CollisionHandler.h" +#include "Entity.h" +#include "ColliderComponent.h" +#include "Vector2D.h" + +class MockColliderComponent : public ColliderComponent { +public: + MockColliderComponent() : ColliderComponent("") { + + }; + + SDL_Rect collider; +}; + +class MockManager : public Manager { + // do nothing +}; + +class MockEntity : public Entity { +public: + MockEntity() : Entity(*(new MockManager())) { + this->comp = MockColliderComponent(); + }; + + MockColliderComponent comp; + + template + bool hasComponent() const { + return true; // Simplified for testing + } + + template + T& getComponent() { + return comp; + } +}; + +// Test fixture +class CollisionHandlerTest : public::testing::Test { +public: + CollisionHandlerTest() {}; + CollisionHandler collisionHandler = CollisionHandler(*(new MockManager())); + MockEntity entityA, entityB; + Vector2D posModA, posModB; + + void SetUp() override { + // Initialize entities + //MockEntity entityA; + //MockEntity entityB; + + // Set up colliders for testing + entityA.comp.collider = {0, 0, 10, 10}; + entityB.comp.collider = {15, 15, 10, 10}; + + posModA = {0, 0}; + posModB = {0, 0}; + } +}; + +TEST_F(CollisionHandlerTest, NoIntersection) { + // Test when entities do not intersect + // Adjust collider positions so they do not intersect + entityB.getComponent().collider.x = 20; + entityB.getComponent().collider.y = 20; + + auto intersections = collisionHandler.getIntersection(&entityA, &entityB, posModA, posModB); + ASSERT_EQ(intersections.none(), true); +} + +TEST_F(CollisionHandlerTest, IntersectionInAllDirections) { + // Test when entities intersect in all directions + // Adjust collider positions so they intersect in all directions + entityB.getComponent().collider.x = 5; + entityB.getComponent().collider.y = 5; + + auto intersections = collisionHandler.getIntersection(&entityA, &entityB, posModA, posModB); + ASSERT_EQ(intersections.all(), true); +} + +TEST_F(CollisionHandlerTest, IntersectionInSomeDirections) { + // Test when entities intersect in some directions + // Adjust collider positions so they intersect in some directions + entityB.getComponent().collider.x = 5; + entityB.getComponent().collider.y = 15; + + auto intersections = collisionHandler.getIntersection(&entityA, &entityB, posModA, posModB); + ASSERT_EQ(intersections.test(static_cast(Direction::UP)), true); + ASSERT_EQ(intersections.test(static_cast(Direction::DOWN)), false); +} \ No newline at end of file