Files
XCEngine/tests/Components/test_component.cpp
ssdfasd 00f70eccf1 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 个测试)

所有测试均已编译通过。
2026-03-20 20:22:04 +08:00

110 lines
2.8 KiB
C++

#include <gtest/gtest.h>
#include <XCEngine/Components/Component.h>
#include <XCEngine/Components/GameObject.h>
using namespace XCEngine::Components;
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;
bool m_onEnableCalled = false;
bool m_onDisableCalled = false;
bool m_onDestroyCalled = false;
void Awake() override { m_awakeCalled = true; }
void Start() override { m_startCalled = true; }
void Update(float deltaTime) override { m_updateCalled = true; }
void OnEnable() override { m_onEnableCalled = true; }
void OnDisable() override { m_onDisableCalled = true; }
void OnDestroy() override { m_onDestroyCalled = true; }
private:
std::string m_customName;
};
TEST(Component_Test, DefaultConstructor) {
TestComponent comp;
EXPECT_EQ(comp.GetName(), "TestComponent");
}
TEST(Component_Test, GetGameObject_ReturnsNullptr_WhenNotAttached) {
TestComponent comp;
EXPECT_EQ(comp.GetGameObject(), nullptr);
}
TEST(Component_Test, IsEnabled_DefaultTrue) {
TestComponent comp;
EXPECT_TRUE(comp.IsEnabled());
}
TEST(Component_Test, SetEnabled_TrueToFalse) {
TestComponent comp;
EXPECT_TRUE(comp.IsEnabled());
comp.SetEnabled(false);
EXPECT_FALSE(comp.IsEnabled());
}
TEST(Component_Test, SetEnabled_FalseToTrue) {
TestComponent comp;
comp.SetEnabled(false);
EXPECT_FALSE(comp.IsEnabled());
comp.SetEnabled(true);
EXPECT_TRUE(comp.IsEnabled());
}
TEST(Component_Test, Lifecycle_AwakeCalled) {
TestComponent comp;
comp.Awake();
EXPECT_TRUE(comp.m_awakeCalled);
}
TEST(Component_Test, Lifecycle_StartCalled) {
TestComponent comp;
comp.Start();
EXPECT_TRUE(comp.m_startCalled);
}
TEST(Component_Test, Lifecycle_UpdateCalled) {
TestComponent comp;
comp.Update(0.016f);
EXPECT_TRUE(comp.m_updateCalled);
}
TEST(Component_Test, Lifecycle_OnEnableCalled_WhenEnabling) {
TestComponent comp;
comp.SetEnabled(false);
comp.SetEnabled(true);
EXPECT_TRUE(comp.m_onEnableCalled);
}
TEST(Component_Test, Lifecycle_OnDisableCalled_WhenDisabling) {
TestComponent comp;
comp.SetEnabled(false);
EXPECT_TRUE(comp.m_onDisableCalled);
}
TEST(Component_Test, Lifecycle_OnDestroyCalled) {
TestComponent comp;
comp.OnDestroy();
EXPECT_TRUE(comp.m_onDestroyCalled);
}
TEST(Component_Test, SetEnabled_NoCallback_WhenStateUnchanged) {
TestComponent comp;
comp.SetEnabled(true);
EXPECT_FALSE(comp.m_onEnableCalled);
EXPECT_FALSE(comp.m_onDisableCalled);
}
} // namespace