Browse Source

more tests

Michele Caini 7 years ago
parent
commit
de9886e011
6 changed files with 93 additions and 19 deletions
  1. 0 3
      TODO
  2. 11 1
      test/lib/a_module.cpp
  3. 11 1
      test/lib/another_module.cpp
  4. 0 11
      test/lib/component.h
  5. 49 3
      test/lib/lib.cpp
  6. 22 0
      test/lib/types.h

+ 0 - 3
TODO

@@ -21,6 +21,3 @@
 * allow to sort groups (::respect can already work with begin/end instead of a whole sparse set)
 * README file: add section with contacts (gitter and the others)
 * cleanup - see https://github.com/skypjack/entt/commit/ad5cedc08c83e8cbcc8aaeac9634d44624ffe35a#commitcomment-32380903
-
-==> can we do more for shared libraries? who knows... see #144
-* test across boundaries dispatcher and emitter

+ 11 - 1
test/lib/a_module.cpp

@@ -1,5 +1,7 @@
 #include <entt/entity/registry.hpp>
-#include "component.h"
+#include <entt/signal/dispatcher.hpp>
+#include <entt/signal/emitter.hpp>
+#include "types.h"
 
 #ifndef LIB_EXPORT
 #if defined _WIN32 || defined __CYGWIN__
@@ -40,3 +42,11 @@ LIB_EXPORT void update_position(int delta, entt::registry<> &registry) {
         pos.y += delta * vel.dy;
     });
 }
+
+LIB_EXPORT void trigger_another_event(entt::dispatcher<> &dispatcher) {
+    dispatcher.trigger<another_event>();
+}
+
+LIB_EXPORT void emit_another_event(test_emitter &emitter) {
+    emitter.publish<another_event>();
+}

+ 11 - 1
test/lib/another_module.cpp

@@ -1,5 +1,7 @@
 #include <entt/entity/registry.hpp>
-#include "component.h"
+#include <entt/signal/dispatcher.hpp>
+#include <entt/signal/emitter.hpp>
+#include "types.h"
 
 #ifndef LIB_EXPORT
 #if defined _WIN32 || defined __CYGWIN__
@@ -45,3 +47,11 @@ LIB_EXPORT void assign_velocity(int vel, entt::registry<> &registry) {
         registry.assign<velocity>(entity, vel, vel);
     }
 }
+
+LIB_EXPORT void trigger_an_event(int payload, entt::dispatcher<> &dispatcher) {
+    dispatcher.trigger(an_event{payload});
+}
+
+LIB_EXPORT void emit_an_event(int payload, test_emitter &emitter) {
+    emitter.publish<an_event>(payload);
+}

+ 0 - 11
test/lib/component.h

@@ -1,11 +0,0 @@
-#include <entt/core/type_traits.hpp>
-
-ENTT_SHARED_STRUCT(position, {
-    int x;
-    int y;
-})
-
-ENTT_SHARED_STRUCT(velocity, {
-    int dx;
-    int dy;
-})

+ 49 - 3
test/lib/lib.cpp

@@ -1,6 +1,8 @@
 #include <entt/entity/registry.hpp>
+#include <entt/signal/dispatcher.hpp>
+#include <entt/signal/emitter.hpp>
 #include <gtest/gtest.h>
-#include "component.h"
+#include "types.h"
 
 extern typename entt::registry<>::component_type a_module_int_type();
 extern typename entt::registry<>::component_type a_module_char_type();
@@ -10,10 +12,20 @@ extern typename entt::registry<>::component_type another_module_char_type();
 extern void update_position(int delta, entt::registry<> &);
 extern void assign_velocity(int, entt::registry<> &);
 
+extern void trigger_an_event(int, entt::dispatcher<> &);
+extern void trigger_another_event(entt::dispatcher<> &);
+
+struct listener {
+    void on_an_event(an_event event) { value = event.payload; }
+    void on_another_event(another_event) {}
+
+    int value;
+};
+
 ENTT_SHARED_TYPE(int)
 ENTT_SHARED_TYPE(char)
 
-TEST(Lib, Shared) {
+TEST(Lib, Types) {
     entt::registry<> registry;
 
     ASSERT_EQ(registry.type<int>(), registry.type<const int>());
@@ -30,7 +42,7 @@ TEST(Lib, Shared) {
     ASSERT_EQ(registry.type<int>(), another_module_int_type());
 }
 
-TEST(Lib, PositionVelocity) {
+TEST(Lib, Registry) {
     entt::registry<> registry;
 
     for(auto i = 0; i < 3; ++i) {
@@ -50,3 +62,37 @@ TEST(Lib, PositionVelocity) {
         ASSERT_EQ(position.y, entity + 3);
     });
 }
+
+TEST(Lib, Dispatcher) {
+    entt::dispatcher<> dispatcher;
+    listener listener;
+
+    dispatcher.sink<an_event>().connect<&listener::on_an_event>(&listener);
+    dispatcher.sink<another_event>().connect<&listener::on_another_event>(&listener);
+
+    listener.value = 0;
+
+    trigger_another_event(dispatcher);
+    trigger_an_event(3, dispatcher);
+
+    ASSERT_EQ(listener.value, 3);
+}
+
+TEST(Lib, Emitter) {
+    test_emitter emitter;
+
+    emitter.once<another_event>([](another_event, test_emitter &) {});
+    emitter.once<an_event>([](an_event event, test_emitter &) {
+        ASSERT_EQ(event.payload, 3);
+    });
+
+    emitter.publish<an_event>(3);
+    emitter.publish<another_event>();
+
+    emitter.once<an_event>([](an_event event, test_emitter &) {
+        ASSERT_EQ(event.payload, 42);
+    });
+
+    emitter.publish<another_event>();
+    emitter.publish<an_event>(42);
+}

+ 22 - 0
test/lib/types.h

@@ -0,0 +1,22 @@
+#include <entt/core/type_traits.hpp>
+#include <entt/signal/emitter.hpp>
+
+struct test_emitter
+        : entt::emitter<test_emitter>
+{};
+
+ENTT_SHARED_STRUCT(position, {
+    int x;
+    int y;
+})
+
+ENTT_SHARED_STRUCT(velocity, {
+    int dx;
+    int dy;
+})
+
+ENTT_SHARED_STRUCT(an_event, {
+    int payload;
+})
+
+ENTT_SHARED_STRUCT(another_event, {})