using GFramework.Core.Abstractions.architecture;
using GFramework.Core.Abstractions.command;
using GFramework.Core.Abstractions.environment;
using GFramework.Core.Abstractions.events;
using GFramework.Core.Abstractions.ioc;
using GFramework.Core.Abstractions.model;
using GFramework.Core.Abstractions.query;
using GFramework.Core.Abstractions.system;
using GFramework.Core.Abstractions.utility;
using GFramework.Core.architecture;
using GFramework.Core.command;
using GFramework.Core.environment;
using GFramework.Core.events;
using GFramework.Core.ioc;
using GFramework.Core.query;
using Mediator;
using NUnit.Framework;
using ICommand = GFramework.Core.Abstractions.command.ICommand;
namespace GFramework.Core.Tests.architecture;
///
/// GameContext类的单元测试
/// 测试内容包括:
/// - ArchitectureReadOnlyDictionary在启动时为空
/// - Bind方法添加上下文到字典
/// - Bind重复类型时抛出异常
/// - GetByType返回正确的上下文
/// - GetByType未找到时抛出异常
/// - Get泛型方法返回正确的上下文
/// - TryGet方法在找到时返回true
/// - TryGet方法在未找到时返回false
/// - GetFirstArchitectureContext在存在时返回
/// - GetFirstArchitectureContext为空时抛出异常
/// - Unbind移除上下文
/// - Clear移除所有上下文
///
[TestFixture]
public class GameContextTests
{
///
/// 测试初始化方法,在每个测试方法执行前清空GameContext
///
[SetUp]
public void SetUp()
{
GameContext.Clear();
}
///
/// 测试清理方法,在每个测试方法执行后清空GameContext
///
[TearDown]
public void TearDown()
{
GameContext.Clear();
}
///
/// 测试ArchitectureReadOnlyDictionary在启动时返回空字典
///
[Test]
public void ArchitectureReadOnlyDictionary_Should_Return_Empty_At_Start()
{
var dict = GameContext.ArchitectureReadOnlyDictionary;
Assert.That(dict.Count, Is.EqualTo(0));
}
///
/// 测试Bind方法是否正确将上下文添加到字典中
///
[Test]
public void Bind_Should_Add_Context_To_Dictionary()
{
var context = new TestArchitectureContext();
GameContext.Bind(typeof(TestArchitecture), context);
Assert.That(GameContext.ArchitectureReadOnlyDictionary.Count, Is.EqualTo(1));
}
///
/// 测试Bind方法在绑定重复类型时是否抛出InvalidOperationException异常
///
[Test]
public void Bind_WithDuplicateType_Should_ThrowInvalidOperationException()
{
var context1 = new TestArchitectureContext();
var context2 = new TestArchitectureContext();
GameContext.Bind(typeof(TestArchitecture), context1);
Assert.Throws(() =>
GameContext.Bind(typeof(TestArchitecture), context2));
}
///
/// 测试GetByType方法是否返回正确的上下文
///
[Test]
public void GetByType_Should_Return_Correct_Context()
{
var context = new TestArchitectureContext();
GameContext.Bind(typeof(TestArchitecture), context);
var result = GameContext.GetByType(typeof(TestArchitecture));
Assert.That(result, Is.SameAs(context));
}
///
/// 测试GetByType方法在未找到对应类型时是否抛出InvalidOperationException异常
///
[Test]
public void GetByType_Should_Throw_When_Not_Found()
{
Assert.Throws(() =>
GameContext.GetByType(typeof(TestArchitecture)));
}
///
/// 测试Get泛型方法是否返回正确的上下文
///
[Test]
public void GetGeneric_Should_Return_Correct_Context()
{
var context = new TestArchitectureContext();
GameContext.Bind(typeof(TestArchitectureContext), context);
var result = GameContext.Get();
Assert.That(result, Is.SameAs(context));
}
///
/// 测试TryGet方法在找到上下文时是否返回true并正确设置输出参数
///
[Test]
public void TryGet_Should_ReturnTrue_When_Found()
{
var context = new TestArchitectureContext();
GameContext.Bind(typeof(TestArchitectureContext), context);
var result = GameContext.TryGet(out TestArchitectureContext? foundContext);
Assert.That(result, Is.True);
Assert.That(foundContext, Is.SameAs(context));
}
///
/// 测试TryGet方法在未找到上下文时是否返回false且输出参数为null
///
[Test]
public void TryGet_Should_ReturnFalse_When_Not_Found()
{
var result = GameContext.TryGet(out TestArchitectureContext? foundContext);
Assert.That(result, Is.False);
Assert.That(foundContext, Is.Null);
}
///
/// 测试GetFirstArchitectureContext方法在存在上下文时是否返回正确的上下文
///
[Test]
public void GetFirstArchitectureContext_Should_Return_When_Exists()
{
var context = new TestArchitectureContext();
GameContext.Bind(typeof(TestArchitecture), context);
var result = GameContext.GetFirstArchitectureContext();
Assert.That(result, Is.SameAs(context));
}
///
/// 测试GetFirstArchitectureContext方法在没有上下文时是否抛出InvalidOperationException异常
///
[Test]
public void GetFirstArchitectureContext_Should_Throw_When_Empty()
{
Assert.Throws(() =>
GameContext.GetFirstArchitectureContext());
}
///
/// 测试Unbind方法是否正确移除指定类型的上下文
///
[Test]
public void Unbind_Should_Remove_Context()
{
var context = new TestArchitectureContext();
GameContext.Bind(typeof(TestArchitecture), context);
GameContext.Unbind(typeof(TestArchitecture));
Assert.That(GameContext.ArchitectureReadOnlyDictionary.Count, Is.EqualTo(0));
}
///
/// 测试Clear方法是否正确移除所有上下文
///
[Test]
public void Clear_Should_Remove_All_Contexts()
{
GameContext.Bind(typeof(TestArchitecture), new TestArchitectureContext());
GameContext.Bind(typeof(TestArchitectureContext), new TestArchitectureContext());
GameContext.Clear();
Assert.That(GameContext.ArchitectureReadOnlyDictionary.Count, Is.EqualTo(0));
}
}
///
/// 测试用的架构类,继承自Architecture
///
public class TestArchitecture : Architecture
{
///
/// 初始化方法,当前为空实现
///
protected override void Init()
{
}
}
///
/// 测试用的架构上下文类,实现了IArchitectureContext接口
///
public class TestArchitectureContext : IArchitectureContext
{
private readonly MicrosoftDiContainer _container = new();
///
/// 获取依赖注入容器
///
public IIocContainer Container => _container;
///
/// 获取事件总线
///
public IEventBus EventBus => new EventBus();
///
/// 获取命令总线
///
public ICommandExecutor CommandExecutor => new CommandExecutor();
///
/// 获取查询总线
///
public IQueryExecutor QueryExecutor => new QueryExecutor();
///
/// 获取环境对象
///
public IEnvironment Environment => new DefaultEnvironment();
///
/// 获取指定类型的服务
///
/// 服务类型
/// 服务实例或null
public TService? GetService() where TService : class
{
return _container.Get();
}
///
/// 获取指定类型的模型
///
/// 模型类型
/// 模型实例或null
public TModel? GetModel() where TModel : class, IModel
{
return _container.Get();
}
///
/// 获取指定类型的系统
///
/// 系统类型
/// 系统实例或null
public TSystem? GetSystem() where TSystem : class, ISystem
{
return _container.Get();
}
///
/// 获取指定类型的工具
///
/// 工具类型
/// 工具实例或null
public TUtility? GetUtility() where TUtility : class, IUtility
{
return _container.Get();
}
///
/// 发送事件
///
/// 事件类型
public void SendEvent() where TEvent : new()
{
}
///
/// 发送事件
///
/// 事件类型
/// 事件实例
public void SendEvent(TEvent e) where TEvent : class
{
}
///
/// 注册事件处理器
///
/// 事件类型
/// 事件处理委托
/// 取消注册接口
public IUnRegister RegisterEvent(Action handler)
{
return new DefaultUnRegister(() => { });
}
///
/// 取消注册事件处理器
///
/// 事件类型
/// 事件处理委托
public void UnRegisterEvent(Action onEvent)
{
}
public ValueTask SendRequestAsync(IRequest request,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public TResponse SendRequest(IRequest request)
{
throw new NotImplementedException();
}
public ValueTask SendCommandAsync(Mediator.ICommand command,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public TResponse SendCommand(Mediator.ICommand command)
{
throw new NotImplementedException();
}
public ValueTask SendQueryAsync(Mediator.IQuery command,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public TResponse SendQuery(Mediator.IQuery command)
{
throw new NotImplementedException();
}
public ValueTask PublishAsync(TNotification notification,
CancellationToken cancellationToken = default) where TNotification : INotification
{
throw new NotImplementedException();
}
public IAsyncEnumerable CreateStream(IStreamRequest request,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public ValueTask SendAsync(TCommand command, CancellationToken cancellationToken = default)
where TCommand : IRequest
{
throw new NotImplementedException();
}
public ValueTask SendAsync(IRequest command,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public ValueTask QueryAsync(IRequest query,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public ValueTask PublishEventAsync(TNotification notification,
CancellationToken cancellationToken = default) where TNotification : INotification
{
throw new NotImplementedException();
}
///
/// 发送命令
///
/// 命令对象
public void SendCommand(ICommand command)
{
}
///
/// 发送带返回值的命令
///
/// 返回值类型
/// 命令对象
/// 命令执行结果
public TResult SendCommand(Abstractions.command.ICommand command)
{
return default!;
}
public Task SendCommandAsync(IAsyncCommand command)
{
return Task.CompletedTask;
}
public Task SendCommandAsync(IAsyncCommand command)
{
return (Task)Task.CompletedTask;
}
///
/// 发送查询请求
///
/// 查询结果类型
/// 查询对象
/// 查询结果
public TResult SendQuery(Abstractions.query.IQuery query)
{
return default!;
}
///
/// 异步发送查询请求
///
/// 查询结果类型
/// 异步查询对象
/// 查询结果
public Task SendQueryAsync(IAsyncQuery query)
{
return (Task)Task.CompletedTask;
}
///
/// 获取环境对象
///
/// 环境对象
public IEnvironment GetEnvironment()
{
return Environment;
}
}