aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-21 09:10:40 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-21 09:10:40 +0100
commit9619f340827b00d4836406ded073a73b6df9386f (patch)
treee6b00c53cc0ec28dcf3e189d90b0cb4ce4cbe144 /src/test
parent9a5967006c4b8cd202142517b402b4c75dc8b90b (diff)
do not call event handlers when behaviorscript is not active + add doxygenloek/collision-system
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ScriptTest.cpp60
1 files changed, 58 insertions, 2 deletions
diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp
index 19fef6d..875c422 100644
--- a/src/test/ScriptTest.cpp
+++ b/src/test/ScriptTest.cpp
@@ -9,26 +9,44 @@
#include <crepe/api/GameObject.h>
#include <crepe/api/Script.h>
#include <crepe/api/Vector2.h>
+#include <crepe/api/Event.h>
+#include <crepe/api/EventManager.h>
#include <crepe/system/ScriptSystem.h>
using namespace std;
using namespace crepe;
using namespace testing;
+class MyEvent : public Event { };
+
class ScriptTest : public Test {
public:
ComponentManager component_manager{};
ScriptSystem system{component_manager};
+ EventManager & evmgr = EventManager::get_instance();
class MyScript : public Script {
// NOTE: default (private) visibility of init and update shouldn't cause
// issues!
- void init() { this->init_count++; }
- void update() { this->update_count++; }
+ void init() {
+ this->init_count++;
+
+ subscribe<MyEvent>([this](const MyEvent &) {
+ this->event_count++;
+ return true;
+ });
+
+ // init should never be called more than once
+ EXPECT_LE(this->init_count, 1);
+ }
+ void update() {
+ this->update_count++;
+ }
public:
unsigned init_count = 0;
unsigned update_count = 0;
+ unsigned event_count = 0;
};
BehaviorScript * behaviorscript_ref = nullptr;
@@ -46,21 +64,58 @@ public:
this->script_ref = (MyScript *) this->behaviorscript_ref->script.get();
ASSERT_NE(this->script_ref, nullptr);
+
+ // sanity
+ ASSERT_EQ(script_ref->init_count, 0);
+ ASSERT_EQ(script_ref->update_count, 0);
+ ASSERT_EQ(script_ref->event_count, 0);
}
};
TEST_F(ScriptTest, Default) {
EXPECT_EQ(0, this->script_ref->init_count);
EXPECT_EQ(0, this->script_ref->update_count);
+ EXPECT_EQ(0, this->script_ref->event_count);
}
TEST_F(ScriptTest, UpdateOnce) {
+ this->system.update();
+ EXPECT_EQ(1, this->script_ref->init_count);
+ EXPECT_EQ(1, this->script_ref->update_count);
+ EXPECT_EQ(0, this->script_ref->event_count);
+}
+
+TEST_F(ScriptTest, UpdateInactive) {
+ this->behaviorscript_ref->active = false;
+ this->system.update();
EXPECT_EQ(0, this->script_ref->init_count);
EXPECT_EQ(0, this->script_ref->update_count);
+ EXPECT_EQ(0, this->script_ref->event_count);
+
+ this->behaviorscript_ref->active = true;
+ this->system.update();
+ EXPECT_EQ(1, this->script_ref->init_count);
+ EXPECT_EQ(1, this->script_ref->update_count);
+ EXPECT_EQ(0, this->script_ref->event_count);
+}
+TEST_F(ScriptTest, EventInactive) {
this->system.update();
+ this->behaviorscript_ref->active = false;
EXPECT_EQ(1, this->script_ref->init_count);
EXPECT_EQ(1, this->script_ref->update_count);
+ EXPECT_EQ(0, this->script_ref->event_count);
+
+ this->evmgr.trigger_event<MyEvent>();
+ EXPECT_EQ(1, this->script_ref->init_count);
+ EXPECT_EQ(1, this->script_ref->update_count);
+ EXPECT_EQ(0, this->script_ref->event_count);
+
+ this->behaviorscript_ref->active = true;
+ this->evmgr.trigger_event<MyEvent>();
+ EXPECT_EQ(1, this->script_ref->init_count);
+ EXPECT_EQ(1, this->script_ref->update_count);
+ EXPECT_EQ(1, this->script_ref->event_count);
}
TEST_F(ScriptTest, ListScripts) {
@@ -70,3 +125,4 @@ TEST_F(ScriptTest, ListScripts) {
}
ASSERT_EQ(1, script_count);
}
+