using Arch.Core;
using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Architectures;
using GFramework.Core.Ioc;
using GFramework.Ecs.Arch.Abstractions;
using GFramework.Ecs.Arch.Extensions;
namespace GFramework.Ecs.Arch.Tests.Integration;
///
/// 显式注册集成测试
///
[TestFixture]
public class ExplicitRegistrationTests
{
[SetUp]
public void Setup()
{
_container = new MicrosoftDiContainer();
_context = new ArchitectureContext(_container);
// 清空注册表,确保测试隔离
ArchitectureModuleRegistry.Clear();
}
[TearDown]
public void TearDown()
{
_container?.Clear();
_context = null;
ArchitectureModuleRegistry.Clear();
GameContext.Clear();
}
private MicrosoftDiContainer? _container;
private ArchitectureContext? _context;
///
/// 测试 Arch ECS 模块显式注册
///
[Test]
public void ArchEcsModule_Should_Be_Explicitly_Registered()
{
// Arrange
var architecture = new TestArchitecture();
// Act - 显式注册
architecture.UseArch();
architecture.Initialize();
// Assert - 验证 World 已注册(由 ArchEcsModule 注册)
var world = architecture.Context.GetService();
Assert.That(world, Is.Not.Null, "World should be registered by ArchEcsModule");
// 验证 IArchEcsModule 已注册
var ecsModule = architecture.Context.GetService();
Assert.That(ecsModule, Is.Not.Null, "IArchEcsModule should be registered");
}
///
/// 测试 World 是否正确注册到容器
///
[Test]
public void World_Should_Be_Registered_In_Container()
{
// Arrange
var architecture = new TestArchitecture();
// Act - 显式注册
architecture.UseArch();
architecture.Initialize();
// Assert
var world = architecture.Context.GetService();
Assert.That(world, Is.Not.Null, "World should be registered in container");
}
///
/// 测试带配置的注册
///
[Test]
public void UseArch_Should_Accept_Configuration()
{
// Arrange
var architecture = new TestArchitecture();
var configCalled = false;
// Act
architecture.UseArch(options =>
{
options.WorldCapacity = 2000;
options.EnableStatistics = true;
configCalled = true;
});
// Assert
Assert.That(configCalled, Is.True, "Configuration delegate should be called");
}
///
/// 测试链式调用
///
[Test]
public void UseArch_Should_Support_Chaining()
{
// Arrange & Act
var architecture = new TestArchitecture()
.UseArch()
.UseArch(options => options.WorldCapacity = 2000);
architecture.Initialize();
// Assert - 验证模块已注册
var world = architecture.Context.GetService();
Assert.That(world, Is.Not.Null, "World should be registered");
var ecsModule = architecture.Context.GetService();
Assert.That(ecsModule, Is.Not.Null, "IArchEcsModule should be registered");
}
///
/// 测试架构类,用于测试
///
private class TestArchitecture : Architecture
{
public TestArchitecture() : base(new ArchitectureConfiguration())
{
}
protected override void OnInitialize()
{
// 测试架构,无需额外初始化
}
}
}