CreatureLib/tests/BattleTests/EventHookTests.cpp

44 lines
1.9 KiB
C++

#ifdef TESTS_BUILD
#include "../../extern/catch.hpp"
#include "../../src/Battling/EventHooks/EventDataClasses.hpp"
#include "../../src/Battling/EventHooks/EventHook.hpp"
#include "../../src/Battling/Models/CreateCreature.hpp"
#include "../TestLibrary/TestLibrary.hpp"
using namespace CreatureLib::Battling;
TEST_CASE("Build and use event hook", "[Battling]") {
auto eventHook = EventHook();
std::vector<const EventData*> events;
eventHook.RegisterListener([&](const EventData* evt) mutable -> void { events.push_back(evt); });
for (size_t i = 0; i < 10; i++) {
eventHook.Trigger<DamageEvent>(nullptr, DamageSource::AttackDamage, 0, 0);
}
REQUIRE(events.size() == 10);
REQUIRE(events[0]->GetKind() == EventDataKind::Damage);
}
TEST_CASE("Build and use event hook a lot", "[Battling]") {
auto eventHook = EventHook();
std::vector<const EventData*> events;
eventHook.RegisterListener([&](const EventData* evt) mutable -> void { events.push_back(evt); });
for (size_t i = 0; i < 10000; i++) {
eventHook.Trigger<DamageEvent>(nullptr, DamageSource::AttackDamage, 0, 0);
}
REQUIRE(events.size() == 10000);
}
TEST_CASE("Build and use event hook with different types", "[Battling]") {
auto eventHook = EventHook();
std::vector<const EventData*> events;
eventHook.RegisterListener([&](const EventData* evt) mutable -> void { events.push_back(evt); });
eventHook.Trigger<DamageEvent>(nullptr, DamageSource::AttackDamage, 0, 0);
eventHook.Trigger<FaintEvent>(nullptr);
eventHook.Trigger<DamageEvent>(nullptr, DamageSource::AttackDamage, 0, 0);
eventHook.Trigger<FaintEvent>(nullptr);
eventHook.Trigger<DamageEvent>(nullptr, DamageSource::AttackDamage, 0, 0);
eventHook.Trigger<FaintEvent>(nullptr);
eventHook.Trigger<DamageEvent>(nullptr, DamageSource::AttackDamage, 0, 0);
eventHook.Trigger<FaintEvent>(nullptr);
}
#endif