diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/test/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/test/ScriptEventTest.cpp | 28 | ||||
-rw-r--r-- | src/test/ScriptSceneTest.cpp | 31 | ||||
-rw-r--r-- | src/test/ScriptTest.cpp | 51 | ||||
-rw-r--r-- | src/test/ScriptTest.h | 29 |
5 files changed, 80 insertions, 60 deletions
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 7b03a5f..d3e27b0 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -13,4 +13,5 @@ target_sources(test_main PUBLIC DBTest.cpp Vector2Test.cpp ScriptEventTest.cpp + ScriptSceneTest.cpp ) diff --git a/src/test/ScriptEventTest.cpp b/src/test/ScriptEventTest.cpp index ab7dd35..7a9abbb 100644 --- a/src/test/ScriptEventTest.cpp +++ b/src/test/ScriptEventTest.cpp @@ -13,37 +13,17 @@ #include <crepe/api/Vector2.h> #include <crepe/system/ScriptSystem.h> +#include "ScriptTest.h" + using namespace std; using namespace crepe; using namespace testing; -class ScriptEventTest : public Test { - Mediator m; +class ScriptEventTest : public ScriptTest { public: - ComponentManager component_manager{m}; - ScriptSystem system{m}; - EventManager & event_manager = m.event_manager; + EventManager & event_manager = mediator.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) { diff --git a/src/test/ScriptSceneTest.cpp b/src/test/ScriptSceneTest.cpp new file mode 100644 index 0000000..f96ae8b --- /dev/null +++ b/src/test/ScriptSceneTest.cpp @@ -0,0 +1,31 @@ +#include <gtest/gtest.h> + +// stupid hack to allow access to private/protected members under test +#define private public +#define protected public + +#include <crepe/manager/SceneManager.h> +#include "ScriptTest.h" + +using namespace std; +using namespace crepe; +using namespace testing; + +class ScriptSceneTest : public ScriptTest { +public: + SceneManager scene_manager{mediator}; + + class MyScene : public Scene {}; +}; + +TEST_F(ScriptSceneTest, Inactive) { + BehaviorScript & behaviorscript = this->behaviorscript; + MyScript & script = this->script; + + const char * non_default_value = "foo"; + ASSERT_NE(non_default_value, scene_manager.next_scene); + + script.set_next_scene(non_default_value); + EXPECT_EQ(non_default_value, scene_manager.next_scene); +} + diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp index fabf127..6d0d5fb 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -5,48 +5,27 @@ #define private public #define protected public -#include <crepe/manager/ComponentManager.h> -#include <crepe/api/BehaviorScript.h> +#include "ScriptTest.h" #include <crepe/api/GameObject.h> -#include <crepe/api/Script.h> -#include <crepe/system/ScriptSystem.h> using namespace std; using namespace crepe; using namespace testing; -class ScriptTest : public Test { - Mediator m; -public: - ComponentManager component_manager{m}; - ScriptSystem system{m}; - EventManager & event_manager = m.event_manager; - - class MyScript : public Script { - // NOTE: explicitly stating `public:` is not required on actual scripts - public: - MOCK_METHOD(void, init, (), (override)); - MOCK_METHOD(void, update, (), (override)); - }; - - 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); - } -}; +void ScriptTest::SetUp() { + 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<NiceMock<MyScript>>(); + ASSERT_NE(component.script.get(), nullptr); + + this->script = *(MyScript *) component.script.get(); + ASSERT_TRUE(this->script); +} TEST_F(ScriptTest, Default) { MyScript & script = this->script; diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h new file mode 100644 index 0000000..9a71ba7 --- /dev/null +++ b/src/test/ScriptTest.h @@ -0,0 +1,29 @@ +#pragma once + +#include <gtest/gtest.h> +#include <gmock/gmock.h> + +#include <crepe/manager/ComponentManager.h> +#include <crepe/system/ScriptSystem.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Script.h> + +class ScriptTest : public testing::Test { +protected: + crepe::Mediator mediator; +public: + crepe::ComponentManager component_manager{mediator}; + crepe::ScriptSystem system{mediator}; + + class MyScript : public crepe::Script { + // NOTE: explicitly stating `public:` is not required on actual scripts + public: + MOCK_METHOD(void, init, (), (override)); + MOCK_METHOD(void, update, (), (override)); + }; + + crepe::OptionalRef<crepe::BehaviorScript> behaviorscript; + crepe::OptionalRef<MyScript> script; + + virtual void SetUp(); +}; |