Просмотр исходного кода

test: signals on entity creation/destruction

Michele Caini 3 лет назад
Родитель
Сommit
47ea16f17c
2 измененных файлов с 41 добавлено и 3 удалено
  1. 1 3
      TODO
  2. 40 0
      test/entt/entity/registry.cpp

+ 1 - 3
TODO

@@ -16,12 +16,10 @@ TODO (high prio):
 * remove the static storage from the const assure in the registry
 * pop_if to improve further destroying entities (drastically)
 * improve sigh mixin for entity storage (split basic + entt/entt spec)
-* doc: exclude only views, storage entity, bump entities
+* doc: exclude only views, storage entity (and diff between iterator and each based iteration), bump entities, signals on entity creation/destruction
 * registry::destroy avoid pack if entity storage iterators already
 * registry: replace destroy with a drop-all method that doesn't care about validity
 * registry: review assign mechanism, maybe it's worth to drop it
-* test signals on entity creation/destruction/update
-* entity storage: clear the difference between iterator and each based iteration (see exclude-only view test)
 
 WIP:
 * get rid of observers, storage based views made them pointless - document alternatives

+ 40 - 0
test/entt/entity/registry.cpp

@@ -1541,6 +1541,46 @@ TEST(Registry, Signals) {
     ASSERT_EQ(listener.last, entities[0u]);
 }
 
+TEST(Registry, SignalsOnEntity) {
+    entt::registry registry;
+    listener listener;
+
+    registry.on_construct<entt::entity>().connect<&listener::incr>(listener);
+
+    entt::entity entity = registry.create();
+    entt::entity other = registry.create();
+
+    ASSERT_EQ(listener.counter, 2);
+    ASSERT_EQ(listener.last, other);
+
+    registry.destroy(other);
+    registry.destroy(entity);
+
+    ASSERT_EQ(listener.counter, 2);
+    ASSERT_EQ(listener.last, other);
+
+    registry.on_construct<entt::entity>().disconnect<&listener::incr>(listener);
+
+    other = registry.create();
+    entity = registry.create();
+
+    ASSERT_EQ(listener.counter, 2);
+    ASSERT_NE(listener.last, entity);
+    ASSERT_NE(listener.last, other);
+
+    registry.on_destroy<entt::entity>().connect<&listener::decr>(listener);
+
+    registry.destroy(entity);
+
+    ASSERT_EQ(listener.counter, 1);
+    ASSERT_EQ(listener.last, entity);
+
+    registry.storage<entt::entity>().erase(other);
+
+    ASSERT_EQ(listener.counter, 0);
+    ASSERT_EQ(listener.last, other);
+}
+
 TEST(Registry, SignalWhenDestroying) {
     entt::registry registry;
     const auto entity = registry.create();