GFramework/GFramework.Core.Tests/Command/CommandExecutorTests.cs
gewuyou ff553977e3 chore(license): 补齐 Apache-2.0 文件头治理
- 新增许可证文件头检查与修复脚本

- 补充维护者手动修复 PR 工作流和 CI 校验

- 更新贡献指南中的文件头说明

- 补齐仓库维护源码和配置文件的许可证声明
2026-05-03 19:39:49 +08:00

126 lines
3.8 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.

// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using GFramework.Core.Command;
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>(() => _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>(() => _commandExecutor.SendAsync<int>(null!));
}
}