aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/test/CMakeLists.txt1
-rw-r--r--src/test/EventTest.cpp1
-rw-r--r--src/test/ScriptEventTest.cpp71
-rw-r--r--src/test/ScriptTest.cpp107
5 files changed, 111 insertions, 70 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index c3f29da..97b21f0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -40,5 +40,6 @@ install(
target_link_libraries(test_main
PRIVATE gtest
+ PRIVATE gmock
PUBLIC crepe
)
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt
index d310f6a..7b03a5f 100644
--- a/src/test/CMakeLists.txt
+++ b/src/test/CMakeLists.txt
@@ -12,4 +12,5 @@ target_sources(test_main PUBLIC
ValueBrokerTest.cpp
DBTest.cpp
Vector2Test.cpp
+ ScriptEventTest.cpp
)
diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp
index f232027..350dd07 100644
--- a/src/test/EventTest.cpp
+++ b/src/test/EventTest.cpp
@@ -38,7 +38,6 @@ public:
TEST_F(EventManagerTest, EventSubscription) {
EventHandler<KeyPressEvent> key_handler = [](const KeyPressEvent & e) {
- std::cout << "Key Event Triggered" << std::endl;
return true;
};
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);
+}
+
diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp
index 3187ed2..fabf127 100644
--- a/src/test/ScriptTest.cpp
+++ b/src/test/ScriptTest.cpp
@@ -1,24 +1,20 @@
#include <gtest/gtest.h>
+#include <gmock/gmock.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 MyEvent : public Event {};
-
class ScriptTest : public Test {
Mediator m;
public:
@@ -27,25 +23,10 @@ public:
EventManager & event_manager = m.event_manager;
class MyScript : public Script {
- // NOTE: default (private) visibility of init and update shouldn't cause
- // issues!
- 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++; }
-
+ // NOTE: explicitly stating `public:` is not required on actual scripts
public:
- unsigned init_count = 0;
- unsigned update_count = 0;
- unsigned event_count = 0;
+ MOCK_METHOD(void, init, (), (override));
+ MOCK_METHOD(void, update, (), (override));
};
OptionalRef<BehaviorScript> behaviorscript;
@@ -64,67 +45,55 @@ public:
this->script = *(MyScript *) component.script.get();
ASSERT_TRUE(this->script);
-
- // sanity
- MyScript & script = this->script;
- ASSERT_EQ(script.init_count, 0);
- ASSERT_EQ(script.update_count, 0);
- ASSERT_EQ(script.event_count, 0);
}
};
TEST_F(ScriptTest, Default) {
MyScript & script = this->script;
- EXPECT_EQ(0, script.init_count);
- EXPECT_EQ(0, script.update_count);
- EXPECT_EQ(0, script.event_count);
+ EXPECT_CALL(script, init()).Times(0);
+ EXPECT_CALL(script, update()).Times(0);
}
TEST_F(ScriptTest, UpdateOnce) {
MyScript & script = this->script;
- system.update();
- EXPECT_EQ(1, script.init_count);
- EXPECT_EQ(1, script.update_count);
- EXPECT_EQ(0, script.event_count);
+ {
+ InSequence seq;
+
+ EXPECT_CALL(script, init()).Times(1);
+ EXPECT_CALL(script, update()).Times(1);
+ system.update();
+ }
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(script, init()).Times(0);
+ EXPECT_CALL(script, update()).Times(1);
+ system.update();
+ }
}
TEST_F(ScriptTest, UpdateInactive) {
BehaviorScript & behaviorscript = this->behaviorscript;
MyScript & script = this->script;
- behaviorscript.active = false;
- system.update();
- EXPECT_EQ(0, script.init_count);
- EXPECT_EQ(0, script.update_count);
- EXPECT_EQ(0, script.event_count);
-
- behaviorscript.active = true;
- system.update();
- EXPECT_EQ(1, script.init_count);
- EXPECT_EQ(1, script.update_count);
- EXPECT_EQ(0, script.event_count);
-}
+ {
+ InSequence seq;
-TEST_F(ScriptTest, EventInactive) {
- BehaviorScript & behaviorscript = this->behaviorscript;
- MyScript & script = this->script;
- EventManager & evmgr = this->event_manager;
-
- system.update();
- behaviorscript.active = false;
- EXPECT_EQ(1, script.init_count);
- EXPECT_EQ(1, script.update_count);
- EXPECT_EQ(0, script.event_count);
-
- evmgr.trigger_event<MyEvent>();
- EXPECT_EQ(1, script.init_count);
- EXPECT_EQ(1, script.update_count);
- EXPECT_EQ(0, script.event_count);
-
- behaviorscript.active = true;
- evmgr.trigger_event<MyEvent>();
- EXPECT_EQ(1, script.init_count);
- EXPECT_EQ(1, script.update_count);
- EXPECT_EQ(1, script.event_count);
+ EXPECT_CALL(script, init()).Times(0);
+ EXPECT_CALL(script, update()).Times(0);
+ behaviorscript.active = false;
+ system.update();
+ }
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(script, init()).Times(1);
+ EXPECT_CALL(script, update()).Times(1);
+ behaviorscript.active = true;
+ system.update();
+ }
}
+