aboutsummaryrefslogtreecommitdiff
path: root/src/test/ScriptTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/ScriptTest.cpp')
-rw-r--r--src/test/ScriptTest.cpp125
1 files changed, 74 insertions, 51 deletions
diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp
index 19fef6d..40aa25c 100644
--- a/src/test/ScriptTest.cpp
+++ b/src/test/ScriptTest.cpp
@@ -1,72 +1,95 @@
+#include <gmock/gmock.h>
#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>
+#include "ScriptTest.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);
- }
-};
+void ScriptTest::SetUp() {
+ auto & mgr = this->component_manager;
+ BehaviorScript & component = entity.add_component<BehaviorScript>();
+
+ this->behaviorscript = component;
+ ASSERT_TRUE(this->behaviorscript);
+ EXPECT_EQ(component.script.get(), nullptr);
+ component.set_script<NiceMock<MyScript>>();
+ ASSERT_NE(component.script.get(), nullptr);
+
+ this->script = *(MyScript *) component.script.get();
+ ASSERT_TRUE(this->script);
+}
TEST_F(ScriptTest, Default) {
- EXPECT_EQ(0, this->script_ref->init_count);
- EXPECT_EQ(0, this->script_ref->update_count);
+ MyScript & script = this->script;
+ EXPECT_CALL(script, init()).Times(0);
+ EXPECT_CALL(script, fixed_update(_)).Times(0);
+ EXPECT_CALL(script, frame_update(_)).Times(0);
}
TEST_F(ScriptTest, UpdateOnce) {
- EXPECT_EQ(0, this->script_ref->init_count);
- EXPECT_EQ(0, this->script_ref->update_count);
+ MyScript & script = this->script;
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(script, init()).Times(1);
+ EXPECT_CALL(script, fixed_update(_)).Times(1);
+ system.fixed_update();
+ }
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(script, init()).Times(0);
+ EXPECT_CALL(script, fixed_update(_)).Times(1);
+ system.fixed_update();
+ }
+
+ {
+ InSequence seq;
- this->system.update();
- EXPECT_EQ(1, this->script_ref->init_count);
- EXPECT_EQ(1, this->script_ref->update_count);
+ EXPECT_CALL(script, frame_update(_)).Times(1);
+ system.frame_update();
+ }
}
-TEST_F(ScriptTest, ListScripts) {
- size_t script_count = 0;
- for (auto & _ : this->system.get_scripts()) {
- script_count++;
+TEST_F(ScriptTest, UpdateInactive) {
+ BehaviorScript & behaviorscript = this->behaviorscript;
+ MyScript & script = this->script;
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(script, init()).Times(0);
+ EXPECT_CALL(script, fixed_update(_)).Times(0);
+ behaviorscript.active = false;
+ system.fixed_update();
+ }
+
+ {
+ InSequence seq;
+
+ EXPECT_CALL(script, init()).Times(1);
+ EXPECT_CALL(script, fixed_update(_)).Times(1);
+ behaviorscript.active = true;
+ system.fixed_update();
}
- ASSERT_EQ(1, script_count);
+}
+
+TEST_F(ScriptTest, SaveManager) {
+ MyScript & script = this->script;
+
+ EXPECT_EQ(&script.get_save_manager(), &this->save_manager);
+}
+
+TEST_F(ScriptTest, LoopTimerManager) {
+ MyScript & script = this->script;
+
+ EXPECT_EQ(&script.get_loop_timer(), &this->loop_timer);
}