aboutsummaryrefslogtreecommitdiff
path: root/src/test/ScriptEventTest.cpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-28 11:03:48 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-28 11:03:48 +0100
commit42d4cdbd12fbe8ddc77e4c6600fe8aae4e9298ad (patch)
tree369d1f950ae766162eae09fc0a97a8a7c3fb6e20 /src/test/ScriptEventTest.cpp
parent6fd7cec7d4bbf5aeb361b3f1337671bb0f9af61b (diff)
split up script tests
Diffstat (limited to 'src/test/ScriptEventTest.cpp')
-rw-r--r--src/test/ScriptEventTest.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/test/ScriptEventTest.cpp b/src/test/ScriptEventTest.cpp
new file mode 100644
index 0000000..ab7dd35
--- /dev/null
+++ b/src/test/ScriptEventTest.cpp
@@ -0,0 +1,71 @@
+#include <gtest/gtest.h>
+
+// stupid hack to allow access to private/protected members under test
+#define private public
+#define protected public
+
+#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/EventManager.h>
+#include <crepe/api/BehaviorScript.h>
+#include <crepe/api/Event.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Script.h>
+#include <crepe/api/Vector2.h>
+#include <crepe/system/ScriptSystem.h>
+
+using namespace std;
+using namespace crepe;
+using namespace testing;
+
+class ScriptEventTest : public Test {
+ Mediator m;
+public:
+ ComponentManager component_manager{m};
+ ScriptSystem system{m};
+ EventManager & event_manager = m.event_manager;
+
+ class MyEvent : public Event {};
+ class MyScript : public Script {};
+
+ OptionalRef<BehaviorScript> behaviorscript;
+ OptionalRef<MyScript> script;
+
+ void SetUp() override {
+ auto & mgr = this->component_manager;
+ GameObject entity = mgr.new_object("name");
+ BehaviorScript & component = entity.add_component<BehaviorScript>();
+
+ this->behaviorscript = component;
+ ASSERT_TRUE(this->behaviorscript);
+ EXPECT_EQ(component.script.get(), nullptr);
+ component.set_script<MyScript>();
+ ASSERT_NE(component.script.get(), nullptr);
+
+ this->script = *(MyScript *) component.script.get();
+ ASSERT_TRUE(this->script);
+ }
+};
+
+TEST_F(ScriptEventTest, Inactive) {
+ BehaviorScript & behaviorscript = this->behaviorscript;
+ MyScript & script = this->script;
+ EventManager & evmgr = this->event_manager;
+
+ unsigned event_count = 0;
+ script.subscribe<MyEvent>([&](const MyEvent &){
+ event_count++;
+ return true;
+ });
+
+ system.update();
+ behaviorscript.active = false;
+ EXPECT_EQ(0, event_count);
+
+ evmgr.trigger_event<MyEvent>();
+ EXPECT_EQ(0, event_count);
+
+ behaviorscript.active = true;
+ evmgr.trigger_event<MyEvent>();
+ EXPECT_EQ(1, event_count);
+}
+