Skip to content

Commit 9816376

Browse files
authored
event collector test + remove event fire bug (#29)
1 parent f2edda9 commit 9816376

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

ecsact/entt/runtime.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ private:
10611061
}
10621062

10631063
for(int i = 0; options.remove_components_length > i; ++i) {
1064-
const ecsact_entity_id& entity = options.update_components_entities[i];
1064+
const ecsact_entity_id& entity = options.remove_components_entities[i];
10651065
ecsact_component_id component_id = options.remove_components[i];
10661066

10671067
mp_for_each<typename package::components>([&]<typename C>(C) {

runtime/test/runtime_test.cc

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "gtest/gtest.h"
22

3+
#include <array>
34
#include <unordered_set>
45
#include <version>
56
#include <ranges>
@@ -301,6 +302,97 @@ TEST(Core, TrivialRemoveEvent) {
301302
EXPECT_TRUE(event_happened);
302303
}
303304

305+
TEST(Core, EventCollector) {
306+
auto reg = ecsact::core::registry{"EventCollector"};
307+
auto entity = reg.create_entity();
308+
309+
// Test if we receive an init, update, and remove event
310+
311+
static auto event_happened = false;
312+
313+
auto callback = //
314+
[](
315+
ecsact_event event,
316+
ecsact_entity_id entity_id,
317+
ecsact_component_id component_id,
318+
const void* component_data,
319+
void* callback_user_data
320+
) { event_happened = true; };
321+
322+
// Checking if we get the init event for a new component added
323+
{
324+
auto evc = ecsact_execution_events_collector{};
325+
evc.init_callback = callback;
326+
327+
auto test_comp = ComponentA{};
328+
test_comp.a = 10;
329+
330+
auto add_component_entities = std::array{entity};
331+
auto add_components = std::array{
332+
ecsact_component{
333+
.component_id = ComponentA::id,
334+
.component_data = &test_comp,
335+
},
336+
};
337+
338+
auto exec_options = ecsact_execution_options{};
339+
exec_options.add_components_length = add_components.size();
340+
exec_options.add_components_entities = add_component_entities.data();
341+
exec_options.add_components = add_components.data();
342+
343+
ecsact_execute_systems(reg.id(), 1, &exec_options, &evc);
344+
345+
EXPECT_TRUE(event_happened) << "Init event did not happen";
346+
event_happened = false;
347+
}
348+
349+
// Checking if we get the update event
350+
{
351+
auto evc = ecsact_execution_events_collector{};
352+
evc.update_callback = callback;
353+
354+
auto test_comp = ComponentA{};
355+
test_comp.a = 42;
356+
357+
auto update_component_entities = std::array{entity};
358+
auto update_components = std::array{
359+
ecsact_component{
360+
.component_id = ComponentA::id,
361+
.component_data = &test_comp,
362+
},
363+
};
364+
365+
auto exec_options = ecsact_execution_options{};
366+
exec_options.update_components_length = update_components.size();
367+
exec_options.update_components_entities = update_component_entities.data();
368+
exec_options.update_components = update_components.data();
369+
370+
ecsact_execute_systems(reg.id(), 1, &exec_options, &evc);
371+
372+
EXPECT_TRUE(event_happened) << "Update event did not happen";
373+
event_happened = false;
374+
}
375+
376+
// Checking if we get the remove_event
377+
{
378+
auto evc = ecsact_execution_events_collector{};
379+
evc.remove_callback = callback;
380+
381+
auto remove_component_entities = std::array{entity};
382+
auto remove_components = std::array{ComponentA::id};
383+
384+
auto exec_options = ecsact_execution_options{};
385+
exec_options.remove_components_length = remove_components.size();
386+
exec_options.remove_components_entities = remove_component_entities.data();
387+
exec_options.remove_components = remove_components.data();
388+
389+
ecsact_execute_systems(reg.id(), 1, &exec_options, &evc);
390+
391+
EXPECT_TRUE(event_happened) << "Remove event did not happen";
392+
event_happened = false;
393+
}
394+
}
395+
304396
TEST(Core, DynamicSystemImpl) {
305397
auto reg = ecsact::core::registry("DynamicSystemImpl");
306398
auto entity = reg.create_entity();

0 commit comments

Comments
 (0)