|
1 | 1 | #include "gtest/gtest.h"
|
2 | 2 |
|
| 3 | +#include <array> |
3 | 4 | #include <unordered_set>
|
4 | 5 | #include <version>
|
5 | 6 | #include <ranges>
|
@@ -301,6 +302,97 @@ TEST(Core, TrivialRemoveEvent) {
|
301 | 302 | EXPECT_TRUE(event_happened);
|
302 | 303 | }
|
303 | 304 |
|
| 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 | + |
304 | 396 | TEST(Core, DynamicSystemImpl) {
|
305 | 397 | auto reg = ecsact::core::registry("DynamicSystemImpl");
|
306 | 398 | auto entity = reg.create_entity();
|
|
0 commit comments