diff options
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/test/DBTest.cpp | 3 | ||||
| -rw-r--r-- | src/test/ECSTest.cpp | 6 | ||||
| -rw-r--r-- | src/test/EventTest.cpp | 16 | ||||
| -rw-r--r-- | src/test/ParticleTest.cpp | 10 | ||||
| -rw-r--r-- | src/test/PhysicsTest.cpp | 8 | ||||
| -rw-r--r-- | src/test/RenderSystemTest.cpp | 11 | ||||
| -rw-r--r-- | src/test/SceneManagerTest.cpp | 24 | ||||
| -rw-r--r-- | src/test/ScriptEventTest.cpp | 50 | ||||
| -rw-r--r-- | src/test/ScriptSceneTest.cpp | 30 | ||||
| -rw-r--r-- | src/test/ScriptTest.cpp | 150 | ||||
| -rw-r--r-- | src/test/ScriptTest.h | 31 | 
12 files changed, 207 insertions, 134 deletions
| diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index d310f6a..d3e27b0 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -12,4 +12,6 @@ target_sources(test_main PUBLIC  	ValueBrokerTest.cpp  	DBTest.cpp  	Vector2Test.cpp +	ScriptEventTest.cpp +	ScriptSceneTest.cpp  ) diff --git a/src/test/DBTest.cpp b/src/test/DBTest.cpp index e80814c..99dedff 100644 --- a/src/test/DBTest.cpp +++ b/src/test/DBTest.cpp @@ -1,6 +1,7 @@ -#include <crepe/facade/DB.h>  #include <gtest/gtest.h> +#include <crepe/facade/DB.h> +  using namespace std;  using namespace crepe;  using namespace testing; diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index af9d6f2..3e6c61c 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -2,18 +2,20 @@  #define protected public -#include <crepe/ComponentManager.h>  #include <crepe/api/GameObject.h>  #include <crepe/api/Metadata.h>  #include <crepe/api/Transform.h>  #include <crepe/api/Vector2.h> +#include <crepe/manager/ComponentManager.h>  using namespace std;  using namespace crepe;  class ECSTest : public ::testing::Test { +	Mediator m; +  public: -	ComponentManager mgr{}; +	ComponentManager mgr{m};  };  TEST_F(ECSTest, createGameObject) { diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index b0e6c9c..dccd554 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -1,10 +1,11 @@ - -#include "api/Event.h" -#include "api/EventManager.h" -#include "api/IKeyListener.h" -#include "api/IMouseListener.h"  #include <gmock/gmock.h>  #include <gtest/gtest.h> + +#include <crepe/api/Event.h> +#include <crepe/api/IKeyListener.h> +#include <crepe/api/IMouseListener.h> +#include <crepe/manager/EventManager.h> +  using namespace std;  using namespace std::chrono_literals;  using namespace crepe; @@ -36,10 +37,7 @@ public:  };  TEST_F(EventManagerTest, EventSubscription) { -	EventHandler<KeyPressEvent> key_handler = [](const KeyPressEvent & e) { -		std::cout << "Key Event Triggered" << std::endl; -		return true; -	}; +	EventHandler<KeyPressEvent> key_handler = [](const KeyPressEvent & e) { return true; };  	// Subscribe to KeyPressEvent  	EventManager::get_instance().subscribe<KeyPressEvent>(key_handler, 1); diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 976f9a1..a659fe5 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,12 +1,12 @@ -#include "api/Texture.h" -#include <crepe/ComponentManager.h>  #include <crepe/Particle.h>  #include <crepe/api/Config.h>  #include <crepe/api/GameObject.h>  #include <crepe/api/ParticleEmitter.h>  #include <crepe/api/Rigidbody.h>  #include <crepe/api/Sprite.h> +#include <crepe/api/Texture.h>  #include <crepe/api/Transform.h> +#include <crepe/manager/ComponentManager.h>  #include <crepe/system/ParticleSystem.h>  #include <gtest/gtest.h>  #include <math.h> @@ -16,9 +16,11 @@ using namespace std::chrono_literals;  using namespace crepe;  class ParticlesTest : public ::testing::Test { +	Mediator m; +  public: -	ComponentManager component_manager; -	ParticleSystem particle_system{component_manager}; +	ComponentManager component_manager{m}; +	ParticleSystem particle_system{m};  	void SetUp() override {  		ComponentManager & mgr = this->component_manager; diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 33b6020..43af8e4 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -1,8 +1,8 @@ -#include <crepe/ComponentManager.h>  #include <crepe/api/Config.h>  #include <crepe/api/GameObject.h>  #include <crepe/api/Rigidbody.h>  #include <crepe/api/Transform.h> +#include <crepe/manager/ComponentManager.h>  #include <crepe/system/PhysicsSystem.h>  #include <gtest/gtest.h> @@ -11,9 +11,11 @@ using namespace std::chrono_literals;  using namespace crepe;  class PhysicsTest : public ::testing::Test { +	Mediator m; +  public: -	ComponentManager component_manager; -	PhysicsSystem system{component_manager}; +	ComponentManager component_manager{m}; +	PhysicsSystem system{m};  	void SetUp() override {  		ComponentManager & mgr = this->component_manager; diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index bb5b81a..c105dcb 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -1,4 +1,3 @@ -#include "types.h"  #include <functional>  #include <gtest/gtest.h>  #include <memory> @@ -7,12 +6,12 @@  #define private public  #define protected public -#include "crepe/api/Camera.h" -#include <crepe/ComponentManager.h> +#include <crepe/api/Camera.h>  #include <crepe/api/Color.h>  #include <crepe/api/GameObject.h>  #include <crepe/api/Sprite.h>  #include <crepe/api/Texture.h> +#include <crepe/manager/ComponentManager.h>  #include <crepe/system/RenderSystem.h> @@ -21,9 +20,11 @@ using namespace crepe;  using namespace testing;  class RenderSystemTest : public Test { +	Mediator m; +  public: -	ComponentManager mgr{}; -	RenderSystem sys{mgr}; +	ComponentManager mgr{m}; +	RenderSystem sys{m};  	GameObject entity1 = this->mgr.new_object("name");  	GameObject entity2 = this->mgr.new_object("name");  	GameObject entity3 = this->mgr.new_object("name"); diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp index 62b7d33..9bb260c 100644 --- a/src/test/SceneManagerTest.cpp +++ b/src/test/SceneManagerTest.cpp @@ -1,12 +1,13 @@ -#include "types.h" -#include <crepe/ComponentManager.h> +#include <gtest/gtest.h> +  #include <crepe/api/GameObject.h>  #include <crepe/api/Metadata.h>  #include <crepe/api/Scene.h> -#include <crepe/api/SceneManager.h>  #include <crepe/api/Transform.h>  #include <crepe/api/Vector2.h> -#include <gtest/gtest.h> +#include <crepe/manager/ComponentManager.h> +#include <crepe/manager/SceneManager.h> +#include <crepe/types.h>  using namespace std;  using namespace crepe; @@ -14,7 +15,8 @@ using namespace crepe;  class ConcreteScene1 : public Scene {  public:  	void load_scene() { -		ComponentManager & mgr = this->component_manager; +		Mediator & mediator = this->mediator; +		ComponentManager & mgr = mediator.component_manager;  		GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", vec2{0, 0}, 0, 1);  		GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", vec2{1, 0}, 0, 1);  		GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", vec2{2, 0}, 0, 1); @@ -26,7 +28,8 @@ public:  class ConcreteScene2 : public Scene {  public:  	void load_scene() { -		ComponentManager & mgr = this->component_manager; +		Mediator & mediator = this->mediator; +		ComponentManager & mgr = mediator.component_manager;  		GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 0}, 0, 1);  		GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 1}, 0, 1);  		GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 2}, 0, 1); @@ -41,7 +44,8 @@ public:  	ConcreteScene3(const string & name) : name(name) {}  	void load_scene() { -		ComponentManager & mgr = this->component_manager; +		Mediator & mediator = this->mediator; +		ComponentManager & mgr = mediator.component_manager;  		GameObject object1 = mgr.new_object("scene_3", "tag_scene_3", vec2{0, 0}, 0, 1);  	} @@ -52,9 +56,11 @@ private:  };  class SceneManagerTest : public ::testing::Test { +	Mediator m; +  public: -	ComponentManager component_mgr{}; -	SceneManager scene_mgr{component_mgr}; +	ComponentManager component_mgr{m}; +	SceneManager scene_mgr{m};  };  TEST_F(SceneManagerTest, loadScene) { diff --git a/src/test/ScriptEventTest.cpp b/src/test/ScriptEventTest.cpp new file mode 100644 index 0000000..5da31e7 --- /dev/null +++ b/src/test/ScriptEventTest.cpp @@ -0,0 +1,50 @@ +#include <gtest/gtest.h> + +// stupid hack to allow access to private/protected members under test +#define private public +#define protected public + +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Event.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Script.h> +#include <crepe/api/Vector2.h> +#include <crepe/manager/ComponentManager.h> +#include <crepe/manager/EventManager.h> +#include <crepe/system/ScriptSystem.h> + +#include "ScriptTest.h" + +using namespace std; +using namespace crepe; +using namespace testing; + +class ScriptEventTest : public ScriptTest { +public: +	EventManager & event_manager = mediator.event_manager; + +	class MyEvent : public Event {}; +}; + +TEST_F(ScriptEventTest, Inactive) { +	BehaviorScript & behaviorscript = this->behaviorscript; +	MyScript & script = this->script; +	EventManager & evmgr = this->event_manager; + +	unsigned event_count = 0; +	script.subscribe<MyEvent>([&](const MyEvent &) { +		event_count++; +		return true; +	}); + +	system.update(); +	behaviorscript.active = false; +	EXPECT_EQ(0, event_count); + +	evmgr.trigger_event<MyEvent>(); +	EXPECT_EQ(0, event_count); + +	behaviorscript.active = true; +	evmgr.trigger_event<MyEvent>(); +	EXPECT_EQ(1, event_count); +} diff --git a/src/test/ScriptSceneTest.cpp b/src/test/ScriptSceneTest.cpp new file mode 100644 index 0000000..9ee1e52 --- /dev/null +++ b/src/test/ScriptSceneTest.cpp @@ -0,0 +1,30 @@ +#include <gtest/gtest.h> + +// stupid hack to allow access to private/protected members under test +#define private public +#define protected public + +#include "ScriptTest.h" +#include <crepe/manager/SceneManager.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 78d5061..1d2d6dd 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -1,129 +1,77 @@ +#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/Event.h> -#include <crepe/api/EventManager.h> +#include "ScriptTest.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 MyEvent : public Event {}; - -class ScriptTest : public Test { -public: -	ComponentManager component_manager{}; -	ScriptSystem system{component_manager}; -	EventManager & event_manager = EventManager::get_instance(); - -	class MyScript : public Script { -		// NOTE: default (private) visibility of init and update shouldn't cause -		// issues! -		void init() { -			this->init_count++; - -			subscribe<MyEvent>([this](const MyEvent &) { -				this->event_count++; -				return true; -			}); - -			// init should never be called more than once -			EXPECT_LE(this->init_count, 1); -		} -		void update() { this->update_count++; } - -	public: -		unsigned init_count = 0; -		unsigned update_count = 0; -		unsigned event_count = 0; -	}; - -	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); - -		// sanity -		MyScript & script = this->script; -		ASSERT_EQ(script.init_count, 0); -		ASSERT_EQ(script.update_count, 0); -		ASSERT_EQ(script.event_count, 0); -	} -}; +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; -	EXPECT_EQ(0, script.init_count); -	EXPECT_EQ(0, script.update_count); -	EXPECT_EQ(0, script.event_count); +	EXPECT_CALL(script, init()).Times(0); +	EXPECT_CALL(script, update()).Times(0);  }  TEST_F(ScriptTest, UpdateOnce) {  	MyScript & script = this->script; -	system.update(); -	EXPECT_EQ(1, script.init_count); -	EXPECT_EQ(1, script.update_count); -	EXPECT_EQ(0, script.event_count); +	{ +		InSequence seq; + +		EXPECT_CALL(script, init()).Times(1); +		EXPECT_CALL(script, update()).Times(1); +		system.update(); +	} + +	{ +		InSequence seq; + +		EXPECT_CALL(script, init()).Times(0); +		EXPECT_CALL(script, update()).Times(1); +		system.update(); +	}  }  TEST_F(ScriptTest, UpdateInactive) {  	BehaviorScript & behaviorscript = this->behaviorscript;  	MyScript & script = this->script; -	behaviorscript.active = false; -	system.update(); -	EXPECT_EQ(0, script.init_count); -	EXPECT_EQ(0, script.update_count); -	EXPECT_EQ(0, script.event_count); - -	behaviorscript.active = true; -	system.update(); -	EXPECT_EQ(1, script.init_count); -	EXPECT_EQ(1, script.update_count); -	EXPECT_EQ(0, script.event_count); -} +	{ +		InSequence seq; -TEST_F(ScriptTest, EventInactive) { -	BehaviorScript & behaviorscript = this->behaviorscript; -	MyScript & script = this->script; -	EventManager & evmgr = this->event_manager; - -	system.update(); -	behaviorscript.active = false; -	EXPECT_EQ(1, script.init_count); -	EXPECT_EQ(1, script.update_count); -	EXPECT_EQ(0, script.event_count); - -	evmgr.trigger_event<MyEvent>(); -	EXPECT_EQ(1, script.init_count); -	EXPECT_EQ(1, script.update_count); -	EXPECT_EQ(0, script.event_count); - -	behaviorscript.active = true; -	evmgr.trigger_event<MyEvent>(); -	EXPECT_EQ(1, script.init_count); -	EXPECT_EQ(1, script.update_count); -	EXPECT_EQ(1, script.event_count); +		EXPECT_CALL(script, init()).Times(0); +		EXPECT_CALL(script, update()).Times(0); +		behaviorscript.active = false; +		system.update(); +	} + +	{ +		InSequence seq; + +		EXPECT_CALL(script, init()).Times(1); +		EXPECT_CALL(script, update()).Times(1); +		behaviorscript.active = true; +		system.update(); +	}  } diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h new file mode 100644 index 0000000..1bbfdd3 --- /dev/null +++ b/src/test/ScriptTest.h @@ -0,0 +1,31 @@ +#pragma once + +#include <gmock/gmock.h> +#include <gtest/gtest.h> + +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Script.h> +#include <crepe/manager/ComponentManager.h> +#include <crepe/system/ScriptSystem.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(); +}; |