From 2a2a3391ff0b449602825c3182af33c2ff52abc0 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 28 Nov 2024 11:19:33 +0100 Subject: add test for Script::set_next_scene --- src/test/CMakeLists.txt | 1 + src/test/ScriptEventTest.cpp | 28 ++++-------------------- src/test/ScriptSceneTest.cpp | 31 +++++++++++++++++++++++++++ src/test/ScriptTest.cpp | 51 +++++++++++++------------------------------- src/test/ScriptTest.h | 29 +++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 60 deletions(-) create mode 100644 src/test/ScriptSceneTest.cpp create mode 100644 src/test/ScriptTest.h 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 #include +#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; - OptionalRef script; - - void SetUp() override { - auto & mgr = this->component_manager; - GameObject entity = mgr.new_object("name"); - BehaviorScript & component = entity.add_component(); - - this->behaviorscript = component; - ASSERT_TRUE(this->behaviorscript); - EXPECT_EQ(component.script.get(), nullptr); - component.set_script(); - 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 + +// stupid hack to allow access to private/protected members under test +#define private public +#define protected public + +#include +#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 -#include +#include "ScriptTest.h" #include -#include -#include 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; - OptionalRef script; - - void SetUp() override { - auto & mgr = this->component_manager; - GameObject entity = mgr.new_object("name"); - BehaviorScript & component = entity.add_component(); - - this->behaviorscript = component; - ASSERT_TRUE(this->behaviorscript); - EXPECT_EQ(component.script.get(), nullptr); - component.set_script(); - 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(); + + this->behaviorscript = component; + ASSERT_TRUE(this->behaviorscript); + EXPECT_EQ(component.script.get(), nullptr); + component.set_script>(); + 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 +#include + +#include +#include +#include +#include + +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 behaviorscript; + crepe::OptionalRef script; + + virtual void SetUp(); +}; -- cgit v1.2.3