aboutsummaryrefslogtreecommitdiff
path: root/src/test/ScriptTest.cpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-13 16:26:38 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-13 16:26:38 +0100
commitb63f4700f7bda696afb14cc3111be0f8b0eed458 (patch)
tree08c4b00b249a4fa672d97a5bc79927adac0a0257 /src/test/ScriptTest.cpp
parent2933655dea64f11f200f42fe51e58dacc5f160eb (diff)
parent9e87a556a5f68c5f9bb04bef9a66880536ccd6e8 (diff)
merge `loek/tests` into `loek/cleanup` (close #32)
Diffstat (limited to 'src/test/ScriptTest.cpp')
-rw-r--r--src/test/ScriptTest.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp
new file mode 100644
index 0000000..bab9e52
--- /dev/null
+++ b/src/test/ScriptTest.cpp
@@ -0,0 +1,72 @@
+#include <gtest/gtest.h>
+
+// stupid hack to allow access to private/protected members under test
+#define private public
+#define protected public
+
+#include <crepe/ComponentManager.h>
+#include <crepe/api/BehaviorScript.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 ScriptTest : public Test {
+public:
+ ComponentManager component_manager{};
+ ScriptSystem system{component_manager};
+
+ 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++; }
+
+ public:
+ unsigned init_count = 0;
+ unsigned update_count = 0;
+ };
+
+ BehaviorScript * behaviorscript_ref = nullptr;
+ MyScript * script_ref = nullptr;
+
+ void SetUp() override {
+ auto & mgr = this->component_manager;
+ GameObject & entity = mgr.new_object("name");
+ BehaviorScript & component = entity.add_component<BehaviorScript>();
+
+ this->behaviorscript_ref = &component;
+ EXPECT_EQ(this->behaviorscript_ref->script.get(), nullptr);
+ component.set_script<MyScript>();
+ ASSERT_NE(this->behaviorscript_ref->script.get(), nullptr);
+
+ this->script_ref = (MyScript *) this->behaviorscript_ref->script.get();
+ ASSERT_NE(this->script_ref, nullptr);
+ }
+};
+
+TEST_F(ScriptTest, Default) {
+ EXPECT_EQ(0, this->script_ref->init_count);
+ EXPECT_EQ(0, this->script_ref->update_count);
+}
+
+TEST_F(ScriptTest, UpdateOnce) {
+ EXPECT_EQ(0, this->script_ref->init_count);
+ EXPECT_EQ(0, this->script_ref->update_count);
+
+ this->system.update();
+ EXPECT_EQ(1, this->script_ref->init_count);
+ EXPECT_EQ(1, this->script_ref->update_count);
+}
+
+TEST_F(ScriptTest, ListScripts) {
+ size_t script_count = 0;
+ for (auto & _ : this->system.get_scripts()) {
+ script_count++;
+ }
+ ASSERT_EQ(1, script_count);
+}