Engine: 实现 Components 和 Scene 模块,包含完整单元测试
新增 Components 模块: - Component 基类 (生命周期、启用状态管理) - TransformComponent (本地/世界空间变换、矩阵缓存、父子层级) - GameObject (组件管理、父子层级、激活状态、静态查找) 新增 Scene 模块: - Scene (场景管理、对象创建销毁、查找、生命周期) - SceneManager (单例模式、多场景管理、场景切换) 新增测试: - test_component.cpp (12 个测试) - test_transform_component.cpp (35 个测试) - test_game_object.cpp (26 个测试) - test_scene.cpp (20 个测试) - test_scene_manager.cpp (17 个测试) 所有测试均已编译通过。
This commit is contained in:
277
tests/Components/test_game_object.cpp
Normal file
277
tests/Components/test_game_object.cpp
Normal file
@@ -0,0 +1,277 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <XCEngine/Components/GameObject.h>
|
||||
#include <XCEngine/Components/TransformComponent.h>
|
||||
#include <XCEngine/Scene/Scene.h>
|
||||
#include <XCEngine/Math/Vector3.h>
|
||||
|
||||
using namespace XCEngine::Components;
|
||||
using namespace XCEngine::Math;
|
||||
|
||||
namespace {
|
||||
|
||||
class TestComponent : public Component {
|
||||
public:
|
||||
TestComponent() = default;
|
||||
explicit TestComponent(const std::string& name) : m_customName(name) {}
|
||||
|
||||
std::string GetName() const override {
|
||||
return m_customName.empty() ? "TestComponent" : m_customName;
|
||||
}
|
||||
|
||||
bool m_awakeCalled = false;
|
||||
bool m_startCalled = false;
|
||||
bool m_updateCalled = false;
|
||||
|
||||
void Awake() override { m_awakeCalled = true; }
|
||||
void Start() override { m_startCalled = true; }
|
||||
void Update(float deltaTime) override { m_updateCalled = true; }
|
||||
|
||||
private:
|
||||
std::string m_customName;
|
||||
};
|
||||
|
||||
class GameObjectTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
testScene = std::make_unique<Scene>("TestScene");
|
||||
}
|
||||
|
||||
std::unique_ptr<Scene> testScene;
|
||||
};
|
||||
|
||||
TEST(GameObject_Test, DefaultConstructor_DefaultValues) {
|
||||
GameObject go;
|
||||
|
||||
EXPECT_EQ(go.GetName(), "GameObject");
|
||||
EXPECT_TRUE(go.IsActive());
|
||||
EXPECT_EQ(go.GetID(), GameObject::INVALID_ID);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, NamedConstructor) {
|
||||
GameObject go("TestObject");
|
||||
|
||||
EXPECT_EQ(go.GetName(), "TestObject");
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, AddComponent_Single) {
|
||||
GameObject go;
|
||||
|
||||
TestComponent* comp = go.AddComponent<TestComponent>();
|
||||
|
||||
EXPECT_NE(comp, nullptr);
|
||||
EXPECT_EQ(go.GetComponent<TestComponent>(), comp);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, AddComponent_Multiple) {
|
||||
GameObject go;
|
||||
|
||||
TestComponent* comp1 = go.AddComponent<TestComponent>();
|
||||
TestComponent* comp2 = go.AddComponent<TestComponent>();
|
||||
|
||||
EXPECT_NE(comp1, nullptr);
|
||||
EXPECT_NE(comp2, nullptr);
|
||||
EXPECT_NE(comp1, comp2);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, AddComponent_TransformComponent) {
|
||||
GameObject go;
|
||||
|
||||
TransformComponent* tc = go.GetTransform();
|
||||
|
||||
EXPECT_NE(tc, nullptr);
|
||||
EXPECT_EQ(tc->GetName(), "Transform");
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, GetComponent_Exists) {
|
||||
GameObject go;
|
||||
TestComponent* comp = go.AddComponent<TestComponent>();
|
||||
|
||||
TestComponent* found = go.GetComponent<TestComponent>();
|
||||
|
||||
EXPECT_EQ(found, comp);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, GetComponent_NotExists) {
|
||||
GameObject go;
|
||||
|
||||
TestComponent* found = go.GetComponent<TestComponent>();
|
||||
|
||||
EXPECT_EQ(found, nullptr);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, GetComponents_Multiple) {
|
||||
GameObject go;
|
||||
go.AddComponent<TestComponent>();
|
||||
go.AddComponent<TestComponent>();
|
||||
go.AddComponent<TestComponent>();
|
||||
|
||||
std::vector<TestComponent*> comps = go.GetComponents<TestComponent>();
|
||||
|
||||
EXPECT_EQ(comps.size(), 3u);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, GetTransform_ReturnsTransform) {
|
||||
GameObject go;
|
||||
|
||||
TransformComponent* tc = go.GetTransform();
|
||||
|
||||
EXPECT_NE(tc, nullptr);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, SetParent_WithWorldPosition) {
|
||||
GameObject parent("Parent");
|
||||
GameObject child("Child");
|
||||
|
||||
parent.GetTransform()->SetLocalPosition(Vector3(1.0f, 0.0f, 0.0f));
|
||||
child.GetTransform()->SetLocalPosition(Vector3(2.0f, 0.0f, 0.0f));
|
||||
|
||||
child.SetParent(&parent, true);
|
||||
|
||||
Vector3 childWorldPos = child.GetTransform()->GetPosition();
|
||||
EXPECT_NEAR(childWorldPos.x, 2.0f, 0.001f);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, SetParent_WithoutWorldPosition) {
|
||||
GameObject parent("Parent");
|
||||
GameObject child("Child");
|
||||
|
||||
parent.GetTransform()->SetLocalPosition(Vector3(1.0f, 0.0f, 0.0f));
|
||||
child.GetTransform()->SetLocalPosition(Vector3(2.0f, 0.0f, 0.0f));
|
||||
|
||||
child.SetParent(&parent, false);
|
||||
|
||||
Vector3 childWorldPos = child.GetTransform()->GetPosition();
|
||||
EXPECT_NEAR(childWorldPos.x, 3.0f, 0.001f);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, GetChild_ValidIndex) {
|
||||
GameObject parent("Parent");
|
||||
GameObject child("Child");
|
||||
|
||||
child.SetParent(&parent);
|
||||
|
||||
GameObject* found = parent.GetChild(0);
|
||||
|
||||
EXPECT_EQ(found, &child);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, GetChild_InvalidIndex) {
|
||||
GameObject parent("Parent");
|
||||
|
||||
GameObject* found = parent.GetChild(0);
|
||||
|
||||
EXPECT_EQ(found, nullptr);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, GetChildren_ReturnsAllChildren) {
|
||||
GameObject parent("Parent");
|
||||
GameObject child1("Child1");
|
||||
GameObject child2("Child2");
|
||||
|
||||
child1.SetParent(&parent);
|
||||
child2.SetParent(&parent);
|
||||
|
||||
std::vector<GameObject*> children = parent.GetChildren();
|
||||
|
||||
EXPECT_EQ(children.size(), 2u);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, DetachChildren) {
|
||||
GameObject parent("Parent");
|
||||
GameObject child("Child");
|
||||
|
||||
child.SetParent(&parent);
|
||||
EXPECT_EQ(parent.GetChildCount(), 1u);
|
||||
|
||||
parent.DetachChildren();
|
||||
|
||||
EXPECT_EQ(parent.GetChildCount(), 0u);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, IsActive_DefaultTrue) {
|
||||
GameObject go;
|
||||
|
||||
EXPECT_TRUE(go.IsActive());
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, SetActive_False) {
|
||||
GameObject go;
|
||||
|
||||
go.SetActive(false);
|
||||
|
||||
EXPECT_FALSE(go.IsActive());
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, IsActiveInHierarchy_True) {
|
||||
GameObject parent("Parent");
|
||||
GameObject child("Child");
|
||||
|
||||
child.SetParent(&parent);
|
||||
|
||||
EXPECT_TRUE(child.IsActiveInHierarchy());
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, IsActiveInHierarchy_FalseWhenParentInactive) {
|
||||
GameObject parent("Parent");
|
||||
GameObject child("Child");
|
||||
|
||||
child.SetParent(&parent);
|
||||
parent.SetActive(false);
|
||||
|
||||
EXPECT_FALSE(child.IsActiveInHierarchy());
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, Lifecycle_Awake) {
|
||||
GameObject go;
|
||||
TestComponent* comp = go.AddComponent<TestComponent>();
|
||||
|
||||
go.Awake();
|
||||
|
||||
EXPECT_TRUE(comp->m_awakeCalled);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, Lifecycle_Start) {
|
||||
GameObject go;
|
||||
TestComponent* comp = go.AddComponent<TestComponent>();
|
||||
|
||||
go.Start();
|
||||
|
||||
EXPECT_TRUE(comp->m_startCalled);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, Lifecycle_Update) {
|
||||
GameObject go;
|
||||
TestComponent* comp = go.AddComponent<TestComponent>();
|
||||
|
||||
go.Update(0.016f);
|
||||
|
||||
EXPECT_TRUE(comp->m_updateCalled);
|
||||
}
|
||||
|
||||
TEST_F(GameObjectTest, Find_Exists) {
|
||||
GameObject* go = testScene->CreateGameObject("TestObject");
|
||||
|
||||
GameObject* found = GameObject::Find("TestObject");
|
||||
|
||||
EXPECT_NE(found, nullptr);
|
||||
EXPECT_EQ(found->GetName(), "TestObject");
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, Find_NotExists) {
|
||||
GameObject* found = GameObject::Find("NonExistent");
|
||||
|
||||
EXPECT_EQ(found, nullptr);
|
||||
}
|
||||
|
||||
TEST(GameObject_Test, GetChildCount) {
|
||||
GameObject parent("Parent");
|
||||
GameObject child1("Child1");
|
||||
GameObject child2("Child2");
|
||||
|
||||
child1.SetParent(&parent);
|
||||
child2.SetParent(&parent);
|
||||
|
||||
EXPECT_EQ(parent.GetChildCount(), 2u);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user