diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-13 16:26:38 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-13 16:26:38 +0100 |
commit | b63f4700f7bda696afb14cc3111be0f8b0eed458 (patch) | |
tree | 08c4b00b249a4fa672d97a5bc79927adac0a0257 /src/test | |
parent | 2933655dea64f11f200f42fe51e58dacc5f160eb (diff) | |
parent | 9e87a556a5f68c5f9bb04bef9a66880536ccd6e8 (diff) |
merge `loek/tests` into `loek/cleanup` (close #32)
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/test/PhysicsTest.cpp | 56 | ||||
-rw-r--r-- | src/test/ScriptTest.cpp | 72 | ||||
-rw-r--r-- | src/test/audio.cpp | 10 | ||||
-rw-r--r-- | src/test/dummy.cpp | 3 | ||||
-rw-r--r-- | src/test/main.cpp | 15 |
6 files changed, 122 insertions, 38 deletions
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 0e4eaed..4dd5010 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,6 +1,6 @@ target_sources(test_main PUBLIC - dummy.cpp - # audio.cpp + main.cpp PhysicsTest.cpp + ScriptTest.cpp ) diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 5385962..9ac99aa 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -11,16 +11,17 @@ using namespace std::chrono_literals; using namespace crepe; class PhysicsTest : public ::testing::Test { -protected: - GameObject * game_object; - PhysicsSystem physics_system; +public: + ComponentManager component_manager; + PhysicsSystem system{component_manager}; + void SetUp() override { - ComponentManager & mgr = ComponentManager::get_instance(); - std::vector<std::reference_wrapper<Transform>> transforms + ComponentManager & mgr = this->component_manager; + vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0); if (transforms.empty()) { - game_object = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); - game_object->add_component<Rigidbody>(Rigidbody::Data{ + auto & entity = mgr.new_object("", "", Vector2{0, 0}, 0, 0); + entity.add_component<Rigidbody>(Rigidbody::Data{ .mass = 1, .gravity_scale = 1, .body_type = Rigidbody::BodyType::DYNAMIC, @@ -36,7 +37,7 @@ protected: transform.position.x = 0.0; transform.position.y = 0.0; transform.rotation = 0.0; - std::vector<std::reference_wrapper<Rigidbody>> rigidbodies + vector<reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_id<Rigidbody>(0); Rigidbody & rigidbody = rigidbodies.front().get(); rigidbody.data.angular_velocity = 0; @@ -47,34 +48,38 @@ protected: TEST_F(PhysicsTest, gravity) { Config::get_instance().physics.gravity = 1; - ComponentManager & mgr = ComponentManager::get_instance(); - std::vector<std::reference_wrapper<Transform>> transforms + ComponentManager & mgr = this->component_manager; + vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0); const Transform & transform = transforms.front().get(); ASSERT_FALSE(transforms.empty()); EXPECT_EQ(transform.position.y, 0); - physics_system.update(); + + system.update(); EXPECT_EQ(transform.position.y, 1); - physics_system.update(); + + system.update(); EXPECT_EQ(transform.position.y, 3); } TEST_F(PhysicsTest, max_velocity) { - ComponentManager & mgr = ComponentManager::get_instance(); - std::vector<std::reference_wrapper<Rigidbody>> rigidbodies + ComponentManager & mgr = this->component_manager; + vector<reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_id<Rigidbody>(0); Rigidbody & rigidbody = rigidbodies.front().get(); ASSERT_FALSE(rigidbodies.empty()); EXPECT_EQ(rigidbody.data.linear_velocity.y, 0); + rigidbody.add_force_linear({100, 100}); rigidbody.add_force_angular(100); - physics_system.update(); + system.update(); EXPECT_EQ(rigidbody.data.linear_velocity.y, 10); EXPECT_EQ(rigidbody.data.linear_velocity.x, 10); EXPECT_EQ(rigidbody.data.angular_velocity, 10); + rigidbody.add_force_linear({-100, -100}); rigidbody.add_force_angular(-100); - physics_system.update(); + system.update(); EXPECT_EQ(rigidbody.data.linear_velocity.y, -10); EXPECT_EQ(rigidbody.data.linear_velocity.x, -10); EXPECT_EQ(rigidbody.data.angular_velocity, -10); @@ -82,39 +87,44 @@ TEST_F(PhysicsTest, max_velocity) { TEST_F(PhysicsTest, movement) { Config::get_instance().physics.gravity = 0; - ComponentManager & mgr = ComponentManager::get_instance(); - std::vector<std::reference_wrapper<Rigidbody>> rigidbodies + ComponentManager & mgr = this->component_manager; + vector<reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_id<Rigidbody>(0); Rigidbody & rigidbody = rigidbodies.front().get(); - std::vector<std::reference_wrapper<Transform>> transforms + vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0); const Transform & transform = transforms.front().get(); ASSERT_FALSE(rigidbodies.empty()); ASSERT_FALSE(transforms.empty()); + rigidbody.add_force_linear({1, 1}); rigidbody.add_force_angular(1); - physics_system.update(); + system.update(); EXPECT_EQ(transform.position.x, 1); EXPECT_EQ(transform.position.y, 1); EXPECT_EQ(transform.rotation, 1); + rigidbody.data.constraints = {1, 1, 1}; EXPECT_EQ(transform.position.x, 1); EXPECT_EQ(transform.position.y, 1); EXPECT_EQ(transform.rotation, 1); + rigidbody.data.linear_damping.x = 0.5; rigidbody.data.linear_damping.y = 0.5; rigidbody.data.angular_damping = 0.5; - physics_system.update(); + system.update(); EXPECT_EQ(rigidbody.data.linear_velocity.x, 0.5); EXPECT_EQ(rigidbody.data.linear_velocity.y, 0.5); EXPECT_EQ(rigidbody.data.angular_velocity, 0.5); + rigidbody.data.constraints = {1, 1, 0}; rigidbody.data.angular_damping = 0; rigidbody.data.max_angular_velocity = 1000; rigidbody.data.angular_velocity = 360; - physics_system.update(); + system.update(); EXPECT_EQ(transform.rotation, 1); + rigidbody.data.angular_velocity = -360; - physics_system.update(); + system.update(); EXPECT_EQ(transform.rotation, 1); } diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp new file mode 100644 index 0000000..bab9e52 --- /dev/null +++ b/src/test/ScriptTest.cpp @@ -0,0 +1,72 @@ +#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> + +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); + } +}; + +TEST_F(ScriptTest, Default) { + EXPECT_EQ(0, this->script_ref->init_count); + EXPECT_EQ(0, this->script_ref->update_count); +} + +TEST_F(ScriptTest, UpdateOnce) { + EXPECT_EQ(0, this->script_ref->init_count); + EXPECT_EQ(0, this->script_ref->update_count); + + this->system.update(); + EXPECT_EQ(1, this->script_ref->init_count); + EXPECT_EQ(1, this->script_ref->update_count); +} + +TEST_F(ScriptTest, ListScripts) { + size_t script_count = 0; + for (auto & _ : this->system.get_scripts()) { + script_count++; + } + ASSERT_EQ(1, script_count); +} diff --git a/src/test/audio.cpp b/src/test/audio.cpp deleted file mode 100644 index d6ff689..0000000 --- a/src/test/audio.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include <gtest/gtest.h> - -using namespace std; -using namespace std::chrono_literals; - -// using namespace crepe; - -// TODO: mock internal audio class - -TEST(audio, play) { ASSERT_TRUE(true); } diff --git a/src/test/dummy.cpp b/src/test/dummy.cpp deleted file mode 100644 index a00a9c6..0000000 --- a/src/test/dummy.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include <gtest/gtest.h> - -TEST(dummy, foo) { ASSERT_TRUE(1); } diff --git a/src/test/main.cpp b/src/test/main.cpp new file mode 100644 index 0000000..241015d --- /dev/null +++ b/src/test/main.cpp @@ -0,0 +1,15 @@ +#include <crepe/api/Config.h> + +#include <gtest/gtest.h> + +using namespace crepe; +using namespace testing; + +int main(int argc, char ** argv) { + InitGoogleTest(&argc, argv); + + auto & cfg = Config::get_instance(); + cfg.log.level = Log::Level::ERROR; + + return RUN_ALL_TESTS(); +} |