helper.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  20. TEST(Helper, Dependency) {
  21. entt::registry registry;
  22. const auto entity = registry.create();
  23. entt::connect<double, float>(registry.construction<int>());
  24. ASSERT_FALSE(registry.has<double>(entity));
  25. ASSERT_FALSE(registry.has<float>(entity));
  26. registry.assign<char>(entity);
  27. ASSERT_FALSE(registry.has<double>(entity));
  28. ASSERT_FALSE(registry.has<float>(entity));
  29. registry.assign<int>(entity);
  30. ASSERT_TRUE(registry.has<double>(entity));
  31. ASSERT_TRUE(registry.has<float>(entity));
  32. ASSERT_EQ(registry.get<double>(entity), .0);
  33. ASSERT_EQ(registry.get<float>(entity), .0f);
  34. registry.get<double>(entity) = .3;
  35. registry.get<float>(entity) = .1f;
  36. registry.remove<int>(entity);
  37. registry.assign<int>(entity);
  38. ASSERT_EQ(registry.get<double>(entity), .3);
  39. ASSERT_EQ(registry.get<float>(entity), .1f);
  40. registry.remove<int>(entity);
  41. registry.remove<float>(entity);
  42. registry.assign<int>(entity);
  43. ASSERT_TRUE(registry.has<float>(entity));
  44. ASSERT_EQ(registry.get<double>(entity), .3);
  45. ASSERT_EQ(registry.get<float>(entity), .0f);
  46. registry.remove<int>(entity);
  47. registry.remove<double>(entity);
  48. registry.remove<float>(entity);
  49. entt::disconnect<double, float>(registry.construction<int>());
  50. registry.assign<int>(entity);
  51. ASSERT_FALSE(registry.has<double>(entity));
  52. ASSERT_FALSE(registry.has<float>(entity));
  53. }
  54. TEST(Dependency, MultipleListenersOnTheSameType) {
  55. entt::registry registry;
  56. entt::connect<double>(registry.construction<int>());
  57. entt::connect<char>(registry.construction<int>());
  58. const auto entity = registry.create();
  59. registry.assign<int>(entity);
  60. ASSERT_TRUE(registry.has<double>(entity));
  61. ASSERT_TRUE(registry.has<char>(entity));
  62. }
  63. TEST(Helper, Tag) {
  64. entt::registry registry;
  65. const auto entity = registry.create();
  66. registry.assign<entt::tag<"foobar"_hs>>(entity);
  67. registry.assign<int>(entity, 42);
  68. int counter{};
  69. ASSERT_FALSE(registry.has<entt::tag<"barfoo"_hs>>(entity));
  70. ASSERT_TRUE(registry.has<entt::tag<"foobar"_hs>>(entity));
  71. for(auto entity: registry.view<int, entt::tag<"foobar"_hs>>()) {
  72. (void)entity;
  73. ++counter;
  74. }
  75. ASSERT_NE(counter, 0);
  76. for(auto entity: registry.view<entt::tag<"foobar"_hs>>()) {
  77. (void)entity;
  78. --counter;
  79. }
  80. ASSERT_EQ(counter, 0);
  81. }