🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.9 KiB

  1. #include <entt/entity/registry.hpp>
  2. #include <entt/signal/dispatcher.hpp>
  3. #include <entt/signal/emitter.hpp>
  4. #include <gtest/gtest.h>
  5. #include "types.h"
  6. extern typename entt::registry::component_type a_module_int_type();
  7. extern typename entt::registry::component_type a_module_char_type();
  8. extern typename entt::registry::component_type another_module_int_type();
  9. extern typename entt::registry::component_type another_module_char_type();
  10. extern void update_position(int delta, entt::registry &);
  11. extern void assign_velocity(int, entt::registry &);
  12. extern void trigger_an_event(int, entt::dispatcher &);
  13. extern void trigger_another_event(entt::dispatcher &);
  14. struct listener {
  15. void on_an_event(an_event event) { value = event.payload; }
  16. void on_another_event(another_event) {}
  17. int value;
  18. };
  19. ENTT_NAMED_TYPE(int)
  20. ENTT_NAMED_TYPE(char)
  21. TEST(Lib, Types) {
  22. entt::registry registry;
  23. ASSERT_EQ(registry.type<int>(), registry.type<const int>());
  24. ASSERT_EQ(registry.type<char>(), registry.type<const char>());
  25. ASSERT_EQ(registry.type<int>(), a_module_int_type());
  26. ASSERT_EQ(registry.type<char>(), a_module_char_type());
  27. ASSERT_EQ(registry.type<const int>(), a_module_int_type());
  28. ASSERT_EQ(registry.type<const char>(), a_module_char_type());
  29. ASSERT_EQ(registry.type<const char>(), another_module_char_type());
  30. ASSERT_EQ(registry.type<const int>(), another_module_int_type());
  31. ASSERT_EQ(registry.type<char>(), another_module_char_type());
  32. ASSERT_EQ(registry.type<int>(), another_module_int_type());
  33. }
  34. TEST(Lib, Registry) {
  35. entt::registry registry;
  36. for(auto i = 0; i < 3; ++i) {
  37. const auto entity = registry.create();
  38. registry.assign<position>(entity, i, i+1);
  39. }
  40. assign_velocity(2, registry);
  41. ASSERT_EQ(registry.size<position>(), entt::registry::size_type{3});
  42. ASSERT_EQ(registry.size<velocity>(), entt::registry::size_type{3});
  43. update_position(1, registry);
  44. registry.view<position>().each([](auto entity, auto &position) {
  45. ASSERT_EQ(position.x, entity + 2);
  46. ASSERT_EQ(position.y, entity + 3);
  47. });
  48. }
  49. TEST(Lib, Dispatcher) {
  50. entt::dispatcher dispatcher;
  51. listener listener;
  52. dispatcher.sink<an_event>().connect<&listener::on_an_event>(&listener);
  53. dispatcher.sink<another_event>().connect<&listener::on_another_event>(&listener);
  54. listener.value = 0;
  55. trigger_another_event(dispatcher);
  56. trigger_an_event(3, dispatcher);
  57. ASSERT_EQ(listener.value, 3);
  58. }
  59. TEST(Lib, Emitter) {
  60. test_emitter emitter;
  61. emitter.once<another_event>([](another_event, test_emitter &) {});
  62. emitter.once<an_event>([](an_event event, test_emitter &) {
  63. ASSERT_EQ(event.payload, 3);
  64. });
  65. emitter.publish<an_event>(3);
  66. emitter.publish<another_event>();
  67. emitter.once<an_event>([](an_event event, test_emitter &) {
  68. ASSERT_EQ(event.payload, 42);
  69. });
  70. emitter.publish<another_event>();
  71. emitter.publish<an_event>(42);
  72. }