aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/AudioTest.cpp2
-rw-r--r--src/test/CMakeLists.txt6
-rw-r--r--src/test/ParticleTest.cpp21
-rw-r--r--src/test/PhysicsTest.cpp56
-rw-r--r--src/test/ScriptTest.cpp72
-rw-r--r--src/test/main.cpp15
6 files changed, 137 insertions, 35 deletions
diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp
index a9d5e6c..6e2706c 100644
--- a/src/test/AudioTest.cpp
+++ b/src/test/AudioTest.cpp
@@ -3,6 +3,7 @@
#include <crepe/ComponentManager.h>
#include <crepe/api/AudioSource.h>
+#include <crepe/api/GameObject.h>
using namespace std;
using namespace crepe;
@@ -16,6 +17,7 @@ public:
void SetUp() override {
auto & mgr = this->component_manager;
GameObject entity = mgr.new_object("name");
+ entity.add_component<AudioSource>("../mwe/audio/sfx1.wav");
}
};
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt
index f63a733..1a986bd 100644
--- a/src/test/CMakeLists.txt
+++ b/src/test/CMakeLists.txt
@@ -1,6 +1,8 @@
target_sources(test_main PUBLIC
- # PhysicsTest.cpp
- # ParticleTest.cpp
+ main.cpp
+ PhysicsTest.cpp
+ ScriptTest.cpp
+ ParticleTest.cpp
AudioTest.cpp
)
diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp
index 6fe3133..1a89e3a 100644
--- a/src/test/ParticleTest.cpp
+++ b/src/test/ParticleTest.cpp
@@ -16,15 +16,16 @@ using namespace std::chrono_literals;
using namespace crepe;
class ParticlesTest : public ::testing::Test {
-protected:
- ParticleSystem particle_system;
+public:
+ ComponentManager component_manager;
+ ParticleSystem particle_system{component_manager};
+
void SetUp() override {
- ComponentManager & mgr = ComponentManager::get_instance();
+ ComponentManager & mgr = this->component_manager;
std::vector<std::reference_wrapper<Transform>> transforms
= mgr.get_components_by_id<Transform>(0);
if (transforms.empty()) {
-
- GameObject game_object(0, "", "", Vector2{0, 0}, 0, 0);
+ GameObject game_object = mgr.new_object("", "", Vector2{0, 0}, 0, 0);
Color color(0, 0, 0, 0);
Sprite test_sprite = game_object.add_component<Sprite>(
@@ -77,7 +78,7 @@ protected:
TEST_F(ParticlesTest, spawnParticle) {
Config::get_instance().physics.gravity = 1;
- ComponentManager & mgr = ComponentManager::get_instance();
+ ComponentManager & mgr = this->component_manager;
ParticleEmitter & emitter
= mgr.get_components_by_id<ParticleEmitter>(0).front().get();
emitter.data.end_lifespan = 5;
@@ -122,7 +123,7 @@ TEST_F(ParticlesTest, spawnParticle) {
TEST_F(ParticlesTest, moveParticleHorizontal) {
Config::get_instance().physics.gravity = 1;
- ComponentManager & mgr = ComponentManager::get_instance();
+ ComponentManager & mgr = this->component_manager;
ParticleEmitter & emitter
= mgr.get_components_by_id<ParticleEmitter>(0).front().get();
emitter.data.end_lifespan = 100;
@@ -140,7 +141,7 @@ TEST_F(ParticlesTest, moveParticleHorizontal) {
TEST_F(ParticlesTest, moveParticleVertical) {
Config::get_instance().physics.gravity = 1;
- ComponentManager & mgr = ComponentManager::get_instance();
+ ComponentManager & mgr = this->component_manager;
ParticleEmitter & emitter
= mgr.get_components_by_id<ParticleEmitter>(0).front().get();
emitter.data.end_lifespan = 100;
@@ -159,7 +160,7 @@ TEST_F(ParticlesTest, moveParticleVertical) {
TEST_F(ParticlesTest, boundaryParticleReset) {
Config::get_instance().physics.gravity = 1;
- ComponentManager & mgr = ComponentManager::get_instance();
+ ComponentManager & mgr = this->component_manager;
ParticleEmitter & emitter
= mgr.get_components_by_id<ParticleEmitter>(0).front().get();
emitter.data.end_lifespan = 100;
@@ -179,7 +180,7 @@ TEST_F(ParticlesTest, boundaryParticleReset) {
TEST_F(ParticlesTest, boundaryParticleStop) {
Config::get_instance().physics.gravity = 1;
- ComponentManager & mgr = ComponentManager::get_instance();
+ ComponentManager & mgr = this->component_manager;
ParticleEmitter & emitter
= mgr.get_components_by_id<ParticleEmitter>(0).front().get();
emitter.data.end_lifespan = 100;
diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp
index 5385962..edb76e9 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..19fef6d
--- /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/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();
+}