Skip to content

Commit

Permalink
test: reactive mixin on_destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Sep 25, 2024
1 parent f9fd843 commit ea660aa
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions test/entt/entity/reactive_mixin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ void emplace(Type &storage, const typename Type::registry_type &, const typename
}
}

template<typename Type>
void remove(Type &storage, const typename Type::registry_type &, const typename Type::entity_type entity) {
storage.remove(entity);
}

template<typename Type>
struct ReactiveMixin: testing::Test {
using type = Type;
Expand Down Expand Up @@ -297,6 +302,82 @@ ENTT_DEBUG_TYPED_TEST(ReactiveMixinDeathTest, OnUpdate) {
ASSERT_DEATH(pool.template on_update<test::empty>(), "");
}

TYPED_TEST(ReactiveMixin, OnDestroy) {
using value_type = typename TestFixture::type;

entt::registry registry;
entt::reactive_mixin<entt::storage<value_type>> pool;
const entt::entity entity{registry.create()};

pool.bind(registry);
registry.emplace<test::empty>(entity);
registry.erase<test::empty>(entity);

ASSERT_FALSE(pool.contains(entity));

pool.template on_destroy<test::other_empty>();
registry.emplace<test::empty>(entity);
registry.erase<test::empty>(entity);

ASSERT_FALSE(pool.contains(entity));

registry.on_destroy<test::other_empty>().disconnect(&pool);
pool.template on_destroy<test::empty>();
registry.emplace<test::empty>(entity);
registry.erase<test::empty>(entity);

ASSERT_TRUE(pool.contains(entity));

registry.clear<test::empty>();

ASSERT_TRUE(pool.contains(entity));

registry.emplace<test::empty>(entity);
registry.erase<test::empty>(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<entt::storage<value_type>> pool;
const std::array entity{registry.create(), registry.create(entt::entity{3})};

pool.bind(registry);
pool.template on_destroy<test::empty, &emplace<entt::reactive_mixin<entt::storage<value_type>>, 3u>>();
registry.insert<test::empty>(entity.begin(), entity.end());
registry.erase<test::empty>(entity[0u]);

ASSERT_TRUE(pool.empty());

registry.erase<test::empty>(entity[1u]);

ASSERT_EQ(pool.size(), 1u);
ASSERT_TRUE(pool.contains(entity[1u]));

pool.clear();

ASSERT_TRUE(pool.empty());

registry.insert<test::empty>(entity.begin(), entity.end());
registry.erase<test::empty>(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<entt::storage<value_type>> pool;
ASSERT_DEATH(pool.template on_destroy<test::empty>(), "");
}

TYPED_TEST(ReactiveMixin, ThrowingAllocator) {
using value_type = typename TestFixture::type;
using storage_type = entt::reactive_mixin<entt::basic_storage<value_type, entt::entity, test::throwing_allocator<value_type>>>;
Expand Down

0 comments on commit ea660aa

Please sign in to comment.