mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 06:53:41 +00:00
test(setup)
basic setup for googletest
This commit is contained in:
parent
d10afe1e07
commit
d3ebe5fea0
@ -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})
|
||||
|
||||
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)
|
||||
@ -30,7 +30,7 @@ public:
|
||||
|
||||
CollisionHandler(Manager& mManager) :
|
||||
manager(mManager) { };
|
||||
~CollisionHandler();
|
||||
~CollisionHandler() {};
|
||||
|
||||
static IntersectionBitSet getIntersection( // intersections relative to entityA
|
||||
Entity* entityA,
|
||||
|
||||
93
unittests.cc
Normal file
93
unittests.cc
Normal file
@ -0,0 +1,93 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock.h>
|
||||
#include <SDL_rect.h>
|
||||
|
||||
#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<typename T>
|
||||
bool hasComponent() const {
|
||||
return true; // Simplified for testing
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
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<ColliderComponent>().collider.x = 20;
|
||||
entityB.getComponent<ColliderComponent>().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<ColliderComponent>().collider.x = 5;
|
||||
entityB.getComponent<ColliderComponent>().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<ColliderComponent>().collider.x = 5;
|
||||
entityB.getComponent<ColliderComponent>().collider.y = 15;
|
||||
|
||||
auto intersections = collisionHandler.getIntersection(&entityA, &entityB, posModA, posModB);
|
||||
ASSERT_EQ(intersections.test(static_cast<size_t>(Direction::UP)), true);
|
||||
ASSERT_EQ(intersections.test(static_cast<size_t>(Direction::DOWN)), false);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user