mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 03:04:29 +08:00
- 在AsyncTestModel和TestModel中添加override关键字修复方法重写 - 调整GFramework.Core.Tests和GFramework.SourceGenerators.Tests的目标框架顺序为net10.0;net8.0 - 优化SyncArchitectureTests中的注释格式,统一添加前导空格 - 移除TestQuery相关代码文件 - 新增BindablePropertyTests测试绑定属性功能 - 新增CommandBusTests测试命令总线功能 - 新增EasyEventsTests和EventTests测试事件系统功能 - 新增IocContainerTests测试依赖注入容器功能 - 新增ObjectExtensionsTests测试对象扩展方法功能 - 新增ObjectPoolTests测试对象池功能 - 新增OrEventTests测试或事件功能 - 新增QueryBusTests测试查询总线功能 - [skip ci]
262 lines
7.2 KiB
C#
262 lines
7.2 KiB
C#
using System.Reflection;
|
|
using GFramework.Core.ioc;
|
|
using GFramework.Core.logging;
|
|
using GFramework.Core.Tests.system;
|
|
using NUnit.Framework;
|
|
|
|
namespace GFramework.Core.Tests.ioc;
|
|
|
|
[TestFixture]
|
|
public class IocContainerTests
|
|
{
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
// 初始化 LoggerFactoryResolver 以支持 IocContainer
|
|
LoggerFactoryResolver.Provider = new ConsoleLoggerFactoryProvider();
|
|
_container = new IocContainer();
|
|
|
|
// 直接初始化 logger 字段
|
|
var loggerField = typeof(IocContainer).GetField("_logger",
|
|
BindingFlags.NonPublic | BindingFlags.Instance);
|
|
loggerField?.SetValue(_container,
|
|
LoggerFactoryResolver.Provider.CreateLogger(nameof(IocContainer)));
|
|
}
|
|
|
|
private IocContainer _container = null!;
|
|
private readonly Dictionary<Type, object> _mockContextServices = new();
|
|
|
|
[Test]
|
|
public void RegisterSingleton_Should_Register_Instance()
|
|
{
|
|
var instance = new TestService();
|
|
|
|
Assert.DoesNotThrow(() => _container.RegisterSingleton(instance));
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterSingleton_WithDuplicate_Should_ThrowInvalidOperationException()
|
|
{
|
|
var instance1 = new TestService();
|
|
var instance2 = new TestService();
|
|
|
|
_container.RegisterSingleton(instance1);
|
|
|
|
Assert.Throws<InvalidOperationException>(() => _container.RegisterSingleton(instance2));
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterSingleton_AfterFreeze_Should_ThrowInvalidOperationException()
|
|
{
|
|
var instance = new TestService();
|
|
_container.Freeze();
|
|
|
|
Assert.Throws<InvalidOperationException>(() => _container.RegisterSingleton(instance));
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterPlurality_Should_Register_Instance_To_All_Types()
|
|
{
|
|
var instance = new TestService();
|
|
|
|
_container.RegisterPlurality(instance);
|
|
|
|
Assert.That(_container.Contains<TestService>(), Is.True);
|
|
Assert.That(_container.Contains<IService>(), Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterPlurality_AfterFreeze_Should_ThrowInvalidOperationException()
|
|
{
|
|
var instance = new TestService();
|
|
_container.Freeze();
|
|
|
|
Assert.Throws<InvalidOperationException>(() => _container.RegisterPlurality(instance));
|
|
}
|
|
|
|
[Test]
|
|
public void Register_Generic_Should_Register_Instance()
|
|
{
|
|
var instance = new TestService();
|
|
|
|
_container.Register(instance);
|
|
|
|
Assert.That(_container.Contains<TestService>(), Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void Register_Generic_AfterFreeze_Should_ThrowInvalidOperationException()
|
|
{
|
|
var instance = new TestService();
|
|
_container.Freeze();
|
|
|
|
Assert.Throws<InvalidOperationException>(() => _container.Register(instance));
|
|
}
|
|
|
|
[Test]
|
|
public void Register_Type_Should_Register_Instance()
|
|
{
|
|
var instance = new TestService();
|
|
|
|
_container.Register(typeof(TestService), instance);
|
|
|
|
Assert.That(_container.Contains<TestService>(), Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void Get_Should_Return_First_Instance()
|
|
{
|
|
var instance = new TestService();
|
|
_container.Register(instance);
|
|
|
|
var result = _container.Get<TestService>();
|
|
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result, Is.SameAs(instance));
|
|
}
|
|
|
|
[Test]
|
|
public void Get_WithNoInstances_Should_ReturnNull()
|
|
{
|
|
var result = _container.Get<TestService>();
|
|
|
|
Assert.That(result, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void GetRequired_Should_Return_Single_Instance()
|
|
{
|
|
var instance = new TestService();
|
|
_container.Register(instance);
|
|
|
|
var result = _container.GetRequired<TestService>();
|
|
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result, Is.SameAs(instance));
|
|
}
|
|
|
|
[Test]
|
|
public void GetRequired_WithNoInstances_Should_ThrowInvalidOperationException()
|
|
{
|
|
Assert.Throws<InvalidOperationException>(() => _container.GetRequired<TestService>());
|
|
}
|
|
|
|
[Test]
|
|
public void GetRequired_WithMultipleInstances_Should_ThrowInvalidOperationException()
|
|
{
|
|
_container.Register(new TestService());
|
|
_container.Register(new TestService());
|
|
|
|
Assert.Throws<InvalidOperationException>(() => _container.GetRequired<TestService>());
|
|
}
|
|
|
|
[Test]
|
|
public void GetAll_Should_Return_All_Instances()
|
|
{
|
|
var instance1 = new TestService();
|
|
var instance2 = new TestService();
|
|
|
|
_container.Register(instance1);
|
|
_container.Register(instance2);
|
|
|
|
var results = _container.GetAll<TestService>();
|
|
|
|
Assert.That(results.Count, Is.EqualTo(2));
|
|
Assert.That(results, Does.Contain(instance1));
|
|
Assert.That(results, Does.Contain(instance2));
|
|
}
|
|
|
|
[Test]
|
|
public void GetAll_WithNoInstances_Should_Return_Empty_Array()
|
|
{
|
|
var results = _container.GetAll<TestService>();
|
|
|
|
Assert.That(results.Count, Is.EqualTo(0));
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllSorted_Should_Return_Sorted_Instances()
|
|
{
|
|
_container.Register(new TestService { Priority = 3 });
|
|
_container.Register(new TestService { Priority = 1 });
|
|
_container.Register(new TestService { Priority = 2 });
|
|
|
|
var results = _container.GetAllSorted<TestService>((a, b) => a.Priority.CompareTo(b.Priority));
|
|
|
|
Assert.That(results.Count, Is.EqualTo(3));
|
|
Assert.That(results[0].Priority, Is.EqualTo(1));
|
|
Assert.That(results[1].Priority, Is.EqualTo(2));
|
|
Assert.That(results[2].Priority, Is.EqualTo(3));
|
|
}
|
|
|
|
[Test]
|
|
public void Contains_WithExistingInstance_Should_ReturnTrue()
|
|
{
|
|
var instance = new TestService();
|
|
_container.Register(instance);
|
|
|
|
Assert.That(_container.Contains<TestService>(), Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void Contains_WithNoInstances_Should_ReturnFalse()
|
|
{
|
|
Assert.That(_container.Contains<TestService>(), Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void ContainsInstance_WithExistingInstance_Should_ReturnTrue()
|
|
{
|
|
var instance = new TestService();
|
|
_container.Register(instance);
|
|
|
|
Assert.That(_container.ContainsInstance(instance), Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void ContainsInstance_WithNonExistingInstance_Should_ReturnFalse()
|
|
{
|
|
var instance = new TestService();
|
|
|
|
Assert.That(_container.ContainsInstance(instance), Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void Clear_Should_Remove_All_Instances()
|
|
{
|
|
var instance = new TestService();
|
|
_container.Register(instance);
|
|
|
|
_container.Clear();
|
|
|
|
Assert.That(_container.Contains<TestService>(), Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void Freeze_Should_Prevent_Further_Registrations()
|
|
{
|
|
var instance1 = new TestService();
|
|
_container.Register(instance1);
|
|
_container.Freeze();
|
|
|
|
var instance2 = new TestService();
|
|
Assert.Throws<InvalidOperationException>(() => _container.Register(instance2));
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterSystem_Should_Register_Instance()
|
|
{
|
|
var system = new TestSystem();
|
|
|
|
_container.RegisterSystem(system);
|
|
|
|
Assert.That(_container.Contains<TestSystem>(), Is.True);
|
|
}
|
|
}
|
|
|
|
public interface IService;
|
|
|
|
public sealed class TestService : IService
|
|
{
|
|
public int Priority { get; set; }
|
|
} |