GFramework/GFramework.Core.Tests/Command/CommandExecutorTests.cs
GeWuYou fb14d7122c docs(style): 更新文档中的命名空间导入格式
- 将所有小写的命名空间导入更正为首字母大写格式
- 统一 GFramework 框架的命名空间引用规范
- 修复 core、ecs、godot 等模块的命名空间导入错误
- 标准化文档示例代码中的 using 语句格式
- 确保所有文档中的命名空间引用保持一致性
- 更新 global using 语句以匹配正确的命名空间格式
2026-03-10 07:18:49 +08:00

265 lines
7.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GFramework.Core.Abstractions.CQRS.Command;
using GFramework.Core.Command;
using NUnit.Framework;
namespace GFramework.Core.Tests.Command;
/// <summary>
/// CommandBus类的单元测试
/// 测试内容包括:
/// - Send方法执行命令
/// - Send方法处理null命令
/// - Send方法带返回值返回值
/// - Send方法带返回值处理null命令
/// - SendAsync方法执行异步命令
/// - SendAsync方法处理null异步命令
/// - SendAsync方法带返回值返回值
/// - SendAsync方法带返回值处理null异步命令
/// </summary>
[TestFixture]
public class CommandExecutorTests
{
[SetUp]
public void SetUp()
{
_commandExecutor = new CommandExecutor();
}
private CommandExecutor _commandExecutor = null!;
/// <summary>
/// 测试Send方法执行命令
/// </summary>
[Test]
public void Send_Should_Execute_Command()
{
var input = new TestCommandInput { Value = 42 };
var command = new TestCommand(input);
Assert.DoesNotThrow(() => _commandExecutor.Send(command));
Assert.That(command.Executed, Is.True);
Assert.That(command.ExecutedValue, Is.EqualTo(42));
}
/// <summary>
/// 测试Send方法处理null命令时抛出ArgumentNullException异常
/// </summary>
[Test]
public void Send_WithNullCommand_Should_ThrowArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => _commandExecutor.Send(null!));
}
/// <summary>
/// 测试Send方法带返回值正确返回值
/// </summary>
[Test]
public void Send_WithResult_Should_Return_Value()
{
var input = new TestCommandInput { Value = 100 };
var command = new TestCommandWithResult(input);
var result = _commandExecutor.Send(command);
Assert.That(command.Executed, Is.True);
Assert.That(result, Is.EqualTo(200));
}
/// <summary>
/// 测试Send方法带返回值处理null命令时抛出ArgumentNullException异常
/// </summary>
[Test]
public void Send_WithResult_AndNullCommand_Should_ThrowArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => _commandExecutor.Send<int>(null!));
}
/// <summary>
/// 测试SendAsync方法执行异步命令
/// </summary>
[Test]
public async Task SendAsync_Should_Execute_AsyncCommand()
{
var input = new TestCommandInput { Value = 42 };
var command = new TestAsyncCommand(input);
await _commandExecutor.SendAsync(command);
Assert.That(command.Executed, Is.True);
Assert.That(command.ExecutedValue, Is.EqualTo(42));
}
/// <summary>
/// 测试SendAsync方法处理null异步命令时抛出ArgumentNullException异常
/// </summary>
[Test]
public void SendAsync_WithNullCommand_Should_ThrowArgumentNullException()
{
Assert.ThrowsAsync<ArgumentNullException>(async () => await _commandExecutor.SendAsync(null!));
}
/// <summary>
/// 测试SendAsync方法带返回值正确返回值
/// </summary>
[Test]
public async Task SendAsync_WithResult_Should_Return_Value()
{
var input = new TestCommandInput { Value = 100 };
var command = new TestAsyncCommandWithResult(input);
var result = await _commandExecutor.SendAsync(command);
Assert.That(command.Executed, Is.True);
Assert.That(result, Is.EqualTo(200));
}
/// <summary>
/// 测试SendAsync方法带返回值处理null异步命令时抛出ArgumentNullException异常
/// </summary>
[Test]
public void SendAsync_WithResult_AndNullCommand_Should_ThrowArgumentNullException()
{
Assert.ThrowsAsync<ArgumentNullException>(async () => await _commandExecutor.SendAsync<int>(null!));
}
}
/// <summary>
/// 测试用命令输入类实现ICommandInput接口
/// </summary>
public sealed class TestCommandInput : ICommandInput
{
/// <summary>
/// 获取或设置值
/// </summary>
public int Value { get; init; }
}
/// <summary>
/// 测试用命令类继承AbstractCommand
/// </summary>
public sealed class TestCommand : AbstractCommand<TestCommandInput>
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="input">命令输入</param>
public TestCommand(TestCommandInput input) : base(input)
{
}
/// <summary>
/// 获取命令是否已执行
/// </summary>
public bool Executed { get; private set; }
/// <summary>
/// 获取执行的值
/// </summary>
public int ExecutedValue { get; private set; }
/// <summary>
/// 执行命令的重写方法
/// </summary>
/// <param name="input">命令输入</param>
protected override void OnExecute(TestCommandInput input)
{
Executed = true;
ExecutedValue = 42;
}
}
/// <summary>
/// 测试用带返回值的命令类继承AbstractCommand
/// </summary>
public sealed class TestCommandWithResult : AbstractCommand<TestCommandInput, int>
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="input">命令输入</param>
public TestCommandWithResult(TestCommandInput input) : base(input)
{
}
/// <summary>
/// 获取命令是否已执行
/// </summary>
public bool Executed { get; private set; }
/// <summary>
/// 执行命令并返回结果的重写方法
/// </summary>
/// <param name="input">命令输入</param>
/// <returns>执行结果</returns>
protected override int OnExecute(TestCommandInput input)
{
Executed = true;
return input.Value * 2;
}
}
/// <summary>
/// 测试用异步命令类继承AbstractAsyncCommand
/// </summary>
public sealed class TestAsyncCommand : AbstractAsyncCommand<TestCommandInput>
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="input">命令输入</param>
public TestAsyncCommand(TestCommandInput input) : base(input)
{
}
/// <summary>
/// 获取命令是否已执行
/// </summary>
public bool Executed { get; private set; }
/// <summary>
/// 获取执行的值
/// </summary>
public int ExecutedValue { get; private set; }
/// <summary>
/// 执行异步命令的重写方法
/// </summary>
/// <param name="input">命令输入</param>
/// <returns>表示异步操作的任务</returns>
protected override Task OnExecuteAsync(TestCommandInput input)
{
Executed = true;
ExecutedValue = 42;
return Task.CompletedTask;
}
}
/// <summary>
/// 测试用带返回值的异步命令类继承AbstractAsyncCommand
/// </summary>
public sealed class TestAsyncCommandWithResult : AbstractAsyncCommand<TestCommandInput, int>
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="input">命令输入</param>
public TestAsyncCommandWithResult(TestCommandInput input) : base(input)
{
}
/// <summary>
/// 获取命令是否已执行
/// </summary>
public bool Executed { get; private set; }
/// <summary>
/// 执行异步命令并返回结果的重写方法
/// </summary>
/// <param name="input">命令输入</param>
/// <returns>执行结果的异步任务</returns>
protected override Task<int> OnExecuteAsync(TestCommandInput input)
{
Executed = true;
return Task.FromResult(input.Value * 2);
}
}