helper.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <gtest/gtest.h>
  2. #include <entt/core/hashed_string.hpp>
  3. #include <entt/entity/helper.hpp>
  4. #include <entt/entity/registry.hpp>
  5. #include <entt/core/type_traits.hpp>
  6. TEST(Helper, AsView) {
  7. entt::registry registry;
  8. const entt::registry cregistry;
  9. ([](entt::view<int, char>) {})(entt::as_view{registry});
  10. ([](entt::view<const int, char>) {})(entt::as_view{registry});
  11. ([](entt::view<const double>) {})(entt::as_view{cregistry});
  12. }
  13. TEST(Helper, AsGroup) {
  14. entt::registry registry;
  15. const entt::registry cregistry;
  16. ([](entt::group<entt::get_t<>, double, float>) {})(entt::as_group{registry});
  17. ([](entt::group<entt::get_t<>, const double, float>) {})(entt::as_group{registry});
  18. ([](entt::group<entt::get_t<>, const double, const float>) {})(entt::as_group{cregistry});
  19. ([](entt::group<entt::get_t<int>, char>) {})(entt::as_group{registry});
  20. ([](entt::group<entt::get_t<const int>, char>) {})(entt::as_group{registry});
  21. ([](entt::group<entt::get_t<const int>, const char>) {})(entt::as_group{cregistry});
  22. }
  23. TEST(Helper, Dependency) {
  24. entt::registry registry;
  25. const auto entity = registry.create();
  26. entt::connect<double, float>(registry.on_construct<int>());
  27. ASSERT_FALSE(registry.has<double>(entity));
  28. ASSERT_FALSE(registry.has<float>(entity));
  29. registry.assign<char>(entity);
  30. ASSERT_FALSE(registry.has<double>(entity));
  31. ASSERT_FALSE(registry.has<float>(entity));
  32. registry.assign<int>(entity);
  33. ASSERT_TRUE(registry.has<double>(entity));
  34. ASSERT_TRUE(registry.has<float>(entity));
  35. ASSERT_EQ(registry.get<double>(entity), .0);
  36. ASSERT_EQ(registry.get<float>(entity), .0f);
  37. registry.get<double>(entity) = .3;
  38. registry.get<float>(entity) = .1f;
  39. registry.remove<int>(entity);
  40. registry.assign<int>(entity);
  41. ASSERT_EQ(registry.get<double>(entity), .3);
  42. ASSERT_EQ(registry.get<float>(entity), .1f);
  43. registry.remove<int>(entity);
  44. registry.remove<float>(entity);
  45. registry.assign<int>(entity);
  46. ASSERT_TRUE(registry.has<float>(entity));
  47. ASSERT_EQ(registry.get<double>(entity), .3);
  48. ASSERT_EQ(registry.get<float>(entity), .0f);
  49. registry.remove<int>(entity);
  50. registry.remove<double>(entity);
  51. registry.remove<float>(entity);
  52. entt::disconnect<double, float>(registry.on_construct<int>());
  53. registry.assign<int>(entity);
  54. ASSERT_FALSE(registry.has<double>(entity));
  55. ASSERT_FALSE(registry.has<float>(entity));
  56. }
  57. TEST(Dependency, MultipleListenersOnTheSameType) {
  58. entt::registry registry;
  59. entt::connect<double>(registry.on_construct<int>());
  60. entt::connect<char>(registry.on_construct<int>());
  61. const auto entity = registry.create();
  62. registry.assign<int>(entity);
  63. ASSERT_TRUE(registry.has<double>(entity));
  64. ASSERT_TRUE(registry.has<char>(entity));
  65. }
  66. TEST(Helper, Tag) {
  67. entt::registry registry;
  68. const auto entity = registry.create();
  69. registry.assign<entt::tag<"foobar"_hs>>(entity);
  70. registry.assign<int>(entity, 42);
  71. int counter{};
  72. ASSERT_FALSE(registry.has<entt::tag<"barfoo"_hs>>(entity));
  73. ASSERT_TRUE(registry.has<entt::tag<"foobar"_hs>>(entity));
  74. for(auto entt: registry.view<int, entt::tag<"foobar"_hs>>()) {
  75. (void)entt;
  76. ++counter;
  77. }
  78. ASSERT_NE(counter, 0);
  79. for(auto entt: registry.view<entt::tag<"foobar"_hs>>()) {
  80. (void)entt;
  81. --counter;
  82. }
  83. ASSERT_EQ(counter, 0);
  84. }