From 42d4cdbd12fbe8ddc77e4c6600fe8aae4e9298ad Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 28 Nov 2024 11:03:48 +0100 Subject: split up script tests --- src/test/ScriptEventTest.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/test/ScriptEventTest.cpp (limited to 'src/test/ScriptEventTest.cpp') 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 + +// stupid hack to allow access to private/protected members under test +#define private public +#define protected public + +#include +#include +#include +#include +#include +#include +#include +#include + +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; + 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) { + BehaviorScript & behaviorscript = this->behaviorscript; + MyScript & script = this->script; + EventManager & evmgr = this->event_manager; + + unsigned event_count = 0; + script.subscribe([&](const MyEvent &){ + event_count++; + return true; + }); + + system.update(); + behaviorscript.active = false; + EXPECT_EQ(0, event_count); + + evmgr.trigger_event(); + EXPECT_EQ(0, event_count); + + behaviorscript.active = true; + evmgr.trigger_event(); + EXPECT_EQ(1, event_count); +} + -- cgit v1.2.3 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 (limited to 'src/test/ScriptEventTest.cpp') 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 From 2d5721820063031b65d64b1b2b1b4797b71935af Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 30 Nov 2024 16:41:22 +0100 Subject: `make format` --- src/crepe/api/LoopManager.cpp | 5 +---- src/crepe/api/LoopManager.h | 2 +- src/crepe/api/Scene.h | 3 ++- src/crepe/api/Script.cpp | 1 - src/crepe/api/Script.h | 4 ++-- src/crepe/manager/ComponentManager.cpp | 2 +- src/crepe/manager/Manager.cpp | 3 +-- src/crepe/manager/Manager.h | 3 +-- src/crepe/manager/Mediator.h | 4 ++-- src/crepe/manager/SaveManager.cpp | 4 ++-- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 2 +- src/crepe/system/ScriptSystem.cpp | 2 +- src/test/ECSTest.cpp | 3 ++- src/test/EventTest.cpp | 6 ++---- src/test/ParticleTest.cpp | 3 ++- src/test/PhysicsTest.cpp | 3 ++- src/test/RenderSystemTest.cpp | 3 ++- src/test/SceneManagerTest.cpp | 7 ++++--- src/test/ScriptEventTest.cpp | 7 +++---- src/test/ScriptSceneTest.cpp | 3 +-- src/test/ScriptTest.cpp | 3 +-- src/test/ScriptTest.h | 8 +++++--- 23 files changed, 40 insertions(+), 43 deletions(-) (limited to 'src/test/ScriptEventTest.cpp') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index b277185..731cfb7 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -22,9 +22,7 @@ LoopManager::LoopManager() { this->load_system(); } -void LoopManager::process_input() { - this->sdl_context.handle_events(this->game_running); -} +void LoopManager::process_input() { this->sdl_context.handle_events(this->game_running); } void LoopManager::start() { this->setup(); @@ -69,4 +67,3 @@ void LoopManager::render() { } void LoopManager::update() {} - diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 6ea5ccc..d8910a0 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -2,10 +2,10 @@ #include +#include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/SceneManager.h" #include "../system/System.h" -#include "../facade/SDLContext.h" #include "LoopTimer.h" diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index 66dad17..9f1e8ce 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -2,8 +2,8 @@ #include -#include "../util/OptionalRef.h" #include "../manager/Mediator.h" +#include "../util/OptionalRef.h" namespace crepe { @@ -37,6 +37,7 @@ public: // TODO: Late references should ALWAYS be private! This is currently kept as-is so unit tests // keep passing, but this reference should not be directly accessible by the user!!! + protected: /** * \name Late references diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index a27838e..4091fd4 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -25,4 +25,3 @@ void Script::set_next_scene(const string & name) { SceneManager & mgr = mediator.scene_manager; mgr.set_next_scene(name); } - diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index e1f86b2..1b339b0 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -2,10 +2,10 @@ #include +#include "../manager/EventManager.h" +#include "../manager/Mediator.h" #include "../types.h" #include "../util/OptionalRef.h" -#include "../manager/Mediator.h" -#include "../manager/EventManager.h" namespace crepe { diff --git a/src/crepe/manager/ComponentManager.cpp b/src/crepe/manager/ComponentManager.cpp index 5a96158..80cf8b4 100644 --- a/src/crepe/manager/ComponentManager.cpp +++ b/src/crepe/manager/ComponentManager.cpp @@ -1,6 +1,6 @@ #include "../api/GameObject.h" -#include "../util/Log.h" #include "../types.h" +#include "../util/Log.h" #include "ComponentManager.h" diff --git a/src/crepe/manager/Manager.cpp b/src/crepe/manager/Manager.cpp index fe7c936..1182785 100644 --- a/src/crepe/manager/Manager.cpp +++ b/src/crepe/manager/Manager.cpp @@ -2,5 +2,4 @@ using namespace crepe; -Manager::Manager(Mediator & mediator) : mediator(mediator) { } - +Manager::Manager(Mediator & mediator) : mediator(mediator) {} diff --git a/src/crepe/manager/Manager.h b/src/crepe/manager/Manager.h index 9adfd0b..4f21ef4 100644 --- a/src/crepe/manager/Manager.h +++ b/src/crepe/manager/Manager.h @@ -13,5 +13,4 @@ protected: Mediator & mediator; }; -} - +} // namespace crepe diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index a91509e..71bd1c9 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -3,8 +3,8 @@ #include "../util/OptionalRef.h" // TODO: remove these singletons: -#include "SaveManager.h" #include "EventManager.h" +#include "SaveManager.h" namespace crepe { @@ -30,4 +30,4 @@ struct Mediator { OptionalRef event_manager = EventManager::get_instance(); }; -} +} // namespace crepe diff --git a/src/crepe/manager/SaveManager.cpp b/src/crepe/manager/SaveManager.cpp index 121d017..d4ed1c1 100644 --- a/src/crepe/manager/SaveManager.cpp +++ b/src/crepe/manager/SaveManager.cpp @@ -1,7 +1,7 @@ +#include "../ValueBroker.h" +#include "../api/Config.h" #include "../facade/DB.h" #include "../util/Log.h" -#include "../api/Config.h" -#include "../ValueBroker.h" #include "SaveManager.h" diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index eba9dfa..bebcf3d 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -1,10 +1,10 @@ #include -#include "../manager/ComponentManager.h" #include "../api/Config.h" #include "../api/Rigidbody.h" #include "../api/Transform.h" #include "../api/Vector2.h" +#include "../manager/ComponentManager.h" #include "PhysicsSystem.h" diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 4e97b3e..0ad685c 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -5,12 +5,12 @@ #include #include -#include "../manager/ComponentManager.h" #include "../api/Camera.h" #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" +#include "../manager/ComponentManager.h" #include "RenderSystem.h" diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 2e16eb0..d6b2ca1 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -1,6 +1,6 @@ -#include "../manager/ComponentManager.h" #include "../api/BehaviorScript.h" #include "../api/Script.h" +#include "../manager/ComponentManager.h" #include "ScriptSystem.h" diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index 22c4fe7..3e6c61c 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -2,17 +2,18 @@ #define protected public -#include #include #include #include #include +#include using namespace std; using namespace crepe; class ECSTest : public ::testing::Test { Mediator m; + public: ComponentManager mgr{m}; }; diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index 350dd07..dccd554 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -2,9 +2,9 @@ #include #include -#include #include #include +#include using namespace std; using namespace std::chrono_literals; @@ -37,9 +37,7 @@ public: }; TEST_F(EventManagerTest, EventSubscription) { - EventHandler key_handler = [](const KeyPressEvent & e) { - return true; - }; + EventHandler key_handler = [](const KeyPressEvent & e) { return true; }; // Subscribe to KeyPressEvent EventManager::get_instance().subscribe(key_handler, 1); diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 4e9fa4e..a659fe5 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -7,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +17,7 @@ using namespace crepe; class ParticlesTest : public ::testing::Test { Mediator m; + public: ComponentManager component_manager{m}; ParticleSystem particle_system{m}; diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 01b7c51..43af8e4 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -1,8 +1,8 @@ -#include #include #include #include #include +#include #include #include @@ -12,6 +12,7 @@ using namespace crepe; class PhysicsTest : public ::testing::Test { Mediator m; + public: ComponentManager component_manager{m}; PhysicsSystem system{m}; diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index 3528e46..c105dcb 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -7,11 +7,11 @@ #define protected public #include -#include #include #include #include #include +#include #include @@ -21,6 +21,7 @@ using namespace testing; class RenderSystemTest : public Test { Mediator m; + public: ComponentManager mgr{m}; RenderSystem sys{m}; diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp index d027d89..9bb260c 100644 --- a/src/test/SceneManagerTest.cpp +++ b/src/test/SceneManagerTest.cpp @@ -1,13 +1,13 @@ #include -#include -#include -#include #include #include #include #include #include +#include +#include +#include using namespace std; using namespace crepe; @@ -57,6 +57,7 @@ private: class SceneManagerTest : public ::testing::Test { Mediator m; + public: ComponentManager component_mgr{m}; SceneManager scene_mgr{m}; diff --git a/src/test/ScriptEventTest.cpp b/src/test/ScriptEventTest.cpp index 7a9abbb..5da31e7 100644 --- a/src/test/ScriptEventTest.cpp +++ b/src/test/ScriptEventTest.cpp @@ -4,13 +4,13 @@ #define private public #define protected public -#include -#include #include #include #include #include #include +#include +#include #include #include "ScriptTest.h" @@ -32,7 +32,7 @@ TEST_F(ScriptEventTest, Inactive) { EventManager & evmgr = this->event_manager; unsigned event_count = 0; - script.subscribe([&](const MyEvent &){ + script.subscribe([&](const MyEvent &) { event_count++; return true; }); @@ -48,4 +48,3 @@ TEST_F(ScriptEventTest, Inactive) { evmgr.trigger_event(); EXPECT_EQ(1, event_count); } - diff --git a/src/test/ScriptSceneTest.cpp b/src/test/ScriptSceneTest.cpp index f96ae8b..9ee1e52 100644 --- a/src/test/ScriptSceneTest.cpp +++ b/src/test/ScriptSceneTest.cpp @@ -4,8 +4,8 @@ #define private public #define protected public -#include #include "ScriptTest.h" +#include using namespace std; using namespace crepe; @@ -28,4 +28,3 @@ TEST_F(ScriptSceneTest, Inactive) { 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 6d0d5fb..1d2d6dd 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -1,5 +1,5 @@ -#include #include +#include // stupid hack to allow access to private/protected members under test #define private public @@ -75,4 +75,3 @@ TEST_F(ScriptTest, UpdateInactive) { system.update(); } } - diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h index 9a71ba7..1bbfdd3 100644 --- a/src/test/ScriptTest.h +++ b/src/test/ScriptTest.h @@ -1,22 +1,24 @@ #pragma once -#include #include +#include -#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)); -- cgit v1.2.3