From ea660aa4663e2a2a20dd62fa6a6c7454baa32a3f Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Wed, 25 Sep 2024 17:19:04 +0200 Subject: [PATCH] test: reactive mixin on_destroy --- test/entt/entity/reactive_mixin.cpp | 81 +++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/test/entt/entity/reactive_mixin.cpp b/test/entt/entity/reactive_mixin.cpp index 8609818f4..25fff7931 100644 --- a/test/entt/entity/reactive_mixin.cpp +++ b/test/entt/entity/reactive_mixin.cpp @@ -21,6 +21,11 @@ void emplace(Type &storage, const typename Type::registry_type &, const typename } } +template +void remove(Type &storage, const typename Type::registry_type &, const typename Type::entity_type entity) { + storage.remove(entity); +} + template struct ReactiveMixin: testing::Test { using type = Type; @@ -297,6 +302,82 @@ ENTT_DEBUG_TYPED_TEST(ReactiveMixinDeathTest, OnUpdate) { ASSERT_DEATH(pool.template on_update(), ""); } +TYPED_TEST(ReactiveMixin, OnDestroy) { + 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); + registry.erase(entity); + + ASSERT_FALSE(pool.contains(entity)); + + pool.template on_destroy(); + registry.emplace(entity); + registry.erase(entity); + + ASSERT_FALSE(pool.contains(entity)); + + registry.on_destroy().disconnect(&pool); + pool.template on_destroy(); + registry.emplace(entity); + registry.erase(entity); + + ASSERT_TRUE(pool.contains(entity)); + + registry.clear(); + + ASSERT_TRUE(pool.contains(entity)); + + registry.emplace(entity); + registry.erase(entity); + + ASSERT_TRUE(pool.contains(entity)); + + registry.destroy(entity); + + ASSERT_TRUE(pool.contains(entity)); +} + +TYPED_TEST(ReactiveMixin, OnDestroyCallback) { + 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_destroy>, 3u>>(); + registry.insert(entity.begin(), entity.end()); + registry.erase(entity[0u]); + + ASSERT_TRUE(pool.empty()); + + registry.erase(entity[1u]); + + ASSERT_EQ(pool.size(), 1u); + ASSERT_TRUE(pool.contains(entity[1u])); + + pool.clear(); + + ASSERT_TRUE(pool.empty()); + + registry.insert(entity.begin(), entity.end()); + registry.erase(entity.begin(), entity.end()); + + ASSERT_EQ(pool.size(), 1u); + ASSERT_TRUE(pool.contains(entity[1u])); +} + +ENTT_DEBUG_TYPED_TEST(ReactiveMixinDeathTest, OnDestroy) { + using value_type = typename TestFixture::type; + entt::reactive_mixin> pool; + ASSERT_DEATH(pool.template on_destroy(), ""); +} + TYPED_TEST(ReactiveMixin, ThrowingAllocator) { using value_type = typename TestFixture::type; using storage_type = entt::reactive_mixin>>;