lib.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <gtest/gtest.h>
  2. #include <entt/entity/entity.hpp>
  3. #include <entt/entity/registry.hpp>
  4. #include <entt/meta/factory.hpp>
  5. #include <entt/signal/dispatcher.hpp>
  6. #include <entt/signal/emitter.hpp>
  7. #include "types.h"
  8. extern typename entt::component a_module_int_type();
  9. extern typename entt::component a_module_char_type();
  10. extern typename entt::component another_module_int_type();
  11. extern typename entt::component another_module_char_type();
  12. extern void update_position(int delta, entt::registry &);
  13. extern void assign_velocity(int, entt::registry &);
  14. extern void trigger_an_event(int, entt::dispatcher &);
  15. extern void trigger_another_event(entt::dispatcher &);
  16. extern void emit_an_event(int, test_emitter &);
  17. extern void emit_another_event(test_emitter &);
  18. extern void a_module_bind_ctx(entt::meta_ctx);
  19. extern void another_module_bind_ctx(entt::meta_ctx);
  20. extern void a_module_meta_init();
  21. extern void another_module_meta_init();
  22. extern void a_module_meta_deinit();
  23. extern void another_module_meta_deinit();
  24. struct listener {
  25. void on_an_event(an_event event) { value = event.payload; }
  26. void on_another_event(another_event) {}
  27. int value;
  28. };
  29. TEST(Lib, Types) {
  30. entt::registry registry;
  31. ASSERT_EQ(registry.type<int>(), registry.type<const int>());
  32. ASSERT_EQ(registry.type<char>(), registry.type<const char>());
  33. ASSERT_EQ(registry.type<int>(), a_module_int_type());
  34. ASSERT_EQ(registry.type<char>(), a_module_char_type());
  35. ASSERT_EQ(registry.type<const int>(), a_module_int_type());
  36. ASSERT_EQ(registry.type<const char>(), a_module_char_type());
  37. ASSERT_EQ(registry.type<const char>(), another_module_char_type());
  38. ASSERT_EQ(registry.type<const int>(), another_module_int_type());
  39. ASSERT_EQ(registry.type<char>(), another_module_char_type());
  40. ASSERT_EQ(registry.type<int>(), another_module_int_type());
  41. }
  42. TEST(Lib, Registry) {
  43. entt::registry registry;
  44. for(auto i = 0; i < 3; ++i) {
  45. const auto entity = registry.create();
  46. registry.assign<position>(entity, i, i+1);
  47. }
  48. assign_velocity(2, registry);
  49. ASSERT_EQ(registry.size<position>(), entt::registry::size_type{3});
  50. ASSERT_EQ(registry.size<velocity>(), entt::registry::size_type{3});
  51. update_position(1, registry);
  52. registry.view<position>().each([](auto entity, auto &position) {
  53. ASSERT_EQ(position.x, entt::to_integer(entity) + 2);
  54. ASSERT_EQ(position.y, entt::to_integer(entity) + 3);
  55. });
  56. }
  57. TEST(Lib, Dispatcher) {
  58. entt::dispatcher dispatcher;
  59. listener listener;
  60. dispatcher.sink<an_event>().connect<&listener::on_an_event>(listener);
  61. dispatcher.sink<another_event>().connect<&listener::on_another_event>(listener);
  62. listener.value = 0;
  63. trigger_an_event(3, dispatcher);
  64. trigger_another_event(dispatcher);
  65. ASSERT_EQ(listener.value, 3);
  66. }
  67. TEST(Lib, Emitter) {
  68. test_emitter emitter;
  69. emitter.once<another_event>([](another_event, test_emitter &) {});
  70. emitter.once<an_event>([](an_event event, test_emitter &) {
  71. ASSERT_EQ(event.payload, 3);
  72. });
  73. emit_an_event(3, emitter);
  74. emit_another_event(emitter);
  75. emitter.once<an_event>([](an_event event, test_emitter &) {
  76. ASSERT_EQ(event.payload, 42);
  77. });
  78. emit_an_event(42, emitter);
  79. emit_another_event(emitter);
  80. }
  81. TEST(Lib, Meta) {
  82. ASSERT_FALSE(entt::resolve("double"_hs));
  83. ASSERT_FALSE(entt::resolve("char"_hs));
  84. ASSERT_FALSE(entt::resolve("int"_hs));
  85. ASSERT_FALSE(entt::resolve<int>().data("0"_hs));
  86. ASSERT_FALSE(entt::resolve<char>().data("c"_hs));
  87. a_module_bind_ctx(entt::meta_ctx{});
  88. another_module_bind_ctx(entt::meta_ctx{});
  89. entt::meta<double>().type("double"_hs).conv<int>();
  90. another_module_meta_init();
  91. a_module_meta_init();
  92. ASSERT_TRUE(entt::resolve("double"_hs));
  93. ASSERT_TRUE(entt::resolve("char"_hs));
  94. ASSERT_TRUE(entt::resolve("int"_hs));
  95. ASSERT_TRUE(entt::resolve<int>().data("0"_hs));
  96. ASSERT_TRUE(entt::resolve<char>().data("c"_hs));
  97. a_module_meta_deinit();
  98. entt::meta<double>().reset();
  99. another_module_meta_deinit();
  100. ASSERT_FALSE(entt::resolve("double"_hs));
  101. ASSERT_FALSE(entt::resolve("char"_hs));
  102. ASSERT_FALSE(entt::resolve("int"_hs));
  103. ASSERT_FALSE(entt::resolve<int>().data("0"_hs));
  104. ASSERT_FALSE(entt::resolve<char>().data("c"_hs));
  105. }