prototype.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <gtest/gtest.h>
  2. #include <entt/entity/prototype.hpp>
  3. #include <entt/entity/registry.hpp>
  4. TEST(Prototype, SameRegistry) {
  5. entt::registry registry;
  6. entt::prototype prototype{registry};
  7. ASSERT_EQ(&registry, &prototype.backend());
  8. ASSERT_EQ(&registry, &std::as_const(prototype).backend());
  9. ASSERT_FALSE(registry.empty());
  10. ASSERT_FALSE((prototype.has<int, char>()));
  11. ASSERT_EQ(prototype.set<int>(2), 2);
  12. ASSERT_EQ(prototype.set<int>(3), 3);
  13. ASSERT_EQ(prototype.set<char>('c'), 'c');
  14. ASSERT_EQ(prototype.get<int>(), 3);
  15. ASSERT_EQ(std::as_const(prototype).get<char>(), 'c');
  16. ASSERT_EQ(std::get<0>(prototype.get<int, char>()), 3);
  17. ASSERT_EQ(std::get<1>(std::as_const(prototype).get<int, char>()), 'c');
  18. ASSERT_NE(prototype.try_get<int>(), nullptr);
  19. ASSERT_NE(prototype.try_get<char>(), nullptr);
  20. ASSERT_EQ(prototype.try_get<double>(), nullptr);
  21. ASSERT_EQ(*prototype.try_get<int>(), 3);
  22. ASSERT_EQ(*std::as_const(prototype).try_get<char>(), 'c');
  23. ASSERT_EQ(*std::get<0>(prototype.try_get<int, char, double>()), 3);
  24. ASSERT_EQ(*std::get<1>(std::as_const(prototype).try_get<int, char, double>()), 'c');
  25. const auto e0 = prototype.create();
  26. ASSERT_TRUE((prototype.has<int, char>()));
  27. ASSERT_FALSE(registry.orphan(e0));
  28. const auto e1 = prototype();
  29. prototype(e0);
  30. ASSERT_FALSE(registry.orphan(e0));
  31. ASSERT_FALSE(registry.orphan(e1));
  32. ASSERT_TRUE((registry.has<int, char>(e0)));
  33. ASSERT_TRUE((registry.has<int, char>(e1)));
  34. registry.remove<int>(e0);
  35. registry.remove<int>(e1);
  36. prototype.unset<int>();
  37. ASSERT_FALSE((prototype.has<int, char>()));
  38. ASSERT_FALSE((prototype.has<int>()));
  39. ASSERT_TRUE((prototype.has<char>()));
  40. prototype(e0);
  41. prototype(e1);
  42. ASSERT_FALSE(registry.has<int>(e0));
  43. ASSERT_FALSE(registry.has<int>(e1));
  44. ASSERT_EQ(registry.get<char>(e0), 'c');
  45. ASSERT_EQ(registry.get<char>(e1), 'c');
  46. registry.get<char>(e0) = '*';
  47. prototype.assign(e0);
  48. ASSERT_EQ(registry.get<char>(e0), '*');
  49. registry.get<char>(e1) = '*';
  50. prototype.assign_or_replace(e1);
  51. ASSERT_EQ(registry.get<char>(e1), 'c');
  52. }
  53. TEST(Prototype, OtherRegistry) {
  54. entt::registry registry;
  55. entt::registry repository;
  56. entt::prototype prototype{repository};
  57. ASSERT_TRUE(registry.empty());
  58. ASSERT_FALSE((prototype.has<int, char>()));
  59. ASSERT_EQ(prototype.set<int>(2), 2);
  60. ASSERT_EQ(prototype.set<int>(3), 3);
  61. ASSERT_EQ(prototype.set<char>('c'), 'c');
  62. ASSERT_EQ(prototype.get<int>(), 3);
  63. ASSERT_EQ(std::as_const(prototype).get<char>(), 'c');
  64. ASSERT_EQ(std::get<0>(prototype.get<int, char>()), 3);
  65. ASSERT_EQ(std::get<1>(std::as_const(prototype).get<int, char>()), 'c');
  66. const auto e0 = prototype.create(registry);
  67. ASSERT_TRUE((prototype.has<int, char>()));
  68. ASSERT_FALSE(registry.orphan(e0));
  69. const auto e1 = prototype(registry);
  70. prototype(registry, e0);
  71. ASSERT_FALSE(registry.orphan(e0));
  72. ASSERT_FALSE(registry.orphan(e1));
  73. ASSERT_TRUE((registry.has<int, char>(e0)));
  74. ASSERT_TRUE((registry.has<int, char>(e1)));
  75. registry.remove<int>(e0);
  76. registry.remove<int>(e1);
  77. prototype.unset<int>();
  78. ASSERT_FALSE((prototype.has<int, char>()));
  79. ASSERT_FALSE((prototype.has<int>()));
  80. ASSERT_TRUE((prototype.has<char>()));
  81. prototype(registry, e0);
  82. prototype(registry, e1);
  83. ASSERT_FALSE(registry.has<int>(e0));
  84. ASSERT_FALSE(registry.has<int>(e1));
  85. ASSERT_EQ(registry.get<char>(e0), 'c');
  86. ASSERT_EQ(registry.get<char>(e1), 'c');
  87. registry.get<char>(e0) = '*';
  88. prototype.assign(registry, e0);
  89. ASSERT_EQ(registry.get<char>(e0), '*');
  90. registry.get<char>(e1) = '*';
  91. prototype.assign_or_replace(registry, e1);
  92. ASSERT_EQ(registry.get<char>(e1), 'c');
  93. }
  94. TEST(Prototype, RAII) {
  95. entt::registry registry;
  96. {
  97. entt::prototype prototype{registry};
  98. prototype.set<int>(0);
  99. ASSERT_FALSE(registry.empty());
  100. }
  101. ASSERT_TRUE(registry.empty());
  102. }
  103. TEST(Prototype, MoveConstructionAssignment) {
  104. entt::registry registry;
  105. entt::prototype prototype{registry};
  106. prototype.set<int>(0);
  107. auto other{std::move(prototype)};
  108. const auto e0 = other();
  109. ASSERT_EQ(registry.size(), entt::registry::size_type{2});
  110. ASSERT_TRUE(registry.has<int>(e0));
  111. prototype = std::move(other);
  112. const auto e1 = prototype();
  113. ASSERT_EQ(registry.size(), entt::registry::size_type{3});
  114. ASSERT_TRUE(registry.has<int>(e1));
  115. }