diff --git a/test/entt/entity/reactive_mixin.cpp b/test/entt/entity/reactive_mixin.cpp index 74c5c2f6e..e427af31f 100644 --- a/test/entt/entity/reactive_mixin.cpp +++ b/test/entt/entity/reactive_mixin.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -8,10 +9,18 @@ #include #include #include +#include "../../common/config.h" #include "../../common/empty.h" #include "../../common/linter.hpp" #include "../../common/throwing_allocator.hpp" +template +void emplace(Type &storage, const typename Type::registry_type &, const typename Type::entity_type entity) { + if((entity == typename Type::entity_type{Value}) && !storage.contains(entity)) { + storage.emplace(entity); + } +} + template struct ReactiveMixin: testing::Test { using type = Type; @@ -138,6 +147,80 @@ TYPED_TEST(ReactiveMixin, Swap) { ASSERT_EQ(other.index(entity[1u]), 0u); } +TYPED_TEST(ReactiveMixin, OnConstruct) { + using value_type = typename TestFixture::type; + + entt::registry registry; + entt::reactive_mixin> pool; + const entt::entity entity{registry.create()}; + + pool.bind(registry); + registry.emplace(entity); + + ASSERT_FALSE(pool.contains(entity)); + + registry.clear(); + pool.template on_construct(); + registry.emplace(entity); + + ASSERT_FALSE(pool.contains(entity)); + + registry.on_construct().disconnect(&pool); + registry.clear(); + pool.template on_construct(); + registry.emplace(entity); + + ASSERT_TRUE(pool.contains(entity)); + + registry.clear(); + + ASSERT_TRUE(pool.contains(entity)); + + registry.emplace(entity); + registry.emplace_or_replace(entity); + + ASSERT_TRUE(pool.contains(entity)); + + registry.destroy(entity); + + ASSERT_TRUE(pool.contains(entity)); +} + +TYPED_TEST(ReactiveMixin, OnConstructCallback) { + using value_type = typename TestFixture::type; + + entt::registry registry; + entt::reactive_mixin> pool; + const std::array entity{registry.create(), registry.create(entt::entity{3})}; + + pool.bind(registry); + pool.template on_construct>, 3u>>(); + registry.emplace(entity[0u]); + + ASSERT_TRUE(pool.empty()); + + registry.emplace(entity[1u]); + + ASSERT_EQ(pool.size(), 1u); + ASSERT_TRUE(pool.contains(entity[1u])); + + pool.clear(); + registry.clear(); + + ASSERT_TRUE(pool.empty()); + + registry.insert(entity.begin(), entity.end()); + + ASSERT_EQ(pool.size(), 1u); + ASSERT_TRUE(pool.contains(entity[1u])); +} + +ENTT_DEBUG_TYPED_TEST(ReactiveMixinDeathTest, OnConstruct) { + using value_type = typename TestFixture::type; + entt::reactive_mixin> pool; + ASSERT_DEATH(pool.template on_construct(), ""); +} + TYPED_TEST(ReactiveMixin, ThrowingAllocator) { using value_type = typename TestFixture::type; using storage_type = entt::reactive_mixin>>;