GeWuYou b49079de3e style(coding-style): 统一代码风格并优化文档格式
- 移除多余using语句和空行,统一代码缩进格式
- 优化注释文档中的缩进和对齐格式
- 简化条件语句和方法实现,提升代码可读性
- 重构协程系统相关类的字段和方法定义格式
- 更新架构服务中容器访问方式的实现
- 调整异步操作类的属性和方法组织结构
- [skip ci]
2026-01-27 20:30:04 +08:00

164 lines
4.3 KiB
C#

using GFramework.Core.events;
using NUnit.Framework;
namespace GFramework.Core.Tests.events;
/// <summary>
/// 测试事件系统功能的测试类
/// </summary>
[TestFixture]
public class EventTests
{
/// <summary>
/// 在每个测试方法执行前进行初始化设置
/// </summary>
[SetUp]
public void SetUp()
{
_easyEvent = new EasyEvent();
_eventInt = new Event<int>();
_eventIntString = new Event<int, string>();
}
private EasyEvent _easyEvent = null!;
private Event<int> _eventInt = null!;
private Event<int, string> _eventIntString = null!;
/// <summary>
/// 测试EasyEvent注册功能是否正确添加处理器
/// </summary>
[Test]
public void EasyEvent_Register_Should_Add_Handler()
{
var called = false;
_easyEvent.Register(() => { called = true; });
_easyEvent.Trigger();
Assert.That(called, Is.True);
}
/// <summary>
/// 测试EasyEvent取消注册功能是否正确移除处理器
/// </summary>
[Test]
public void EasyEvent_UnRegister_Should_Remove_Handler()
{
var count = 0;
var handler = () => { count++; };
_easyEvent.Register(handler);
_easyEvent.Trigger();
Assert.That(count, Is.EqualTo(1));
_easyEvent.UnRegister(handler);
_easyEvent.Trigger();
Assert.That(count, Is.EqualTo(1));
}
/// <summary>
/// 测试EasyEvent多个处理器是否都能被调用
/// </summary>
[Test]
public void EasyEvent_Multiple_Handlers_Should_All_Be_Called()
{
var count1 = 0;
var count2 = 0;
_easyEvent.Register(() => { count1++; });
_easyEvent.Register(() => { count2++; });
_easyEvent.Trigger();
Assert.That(count1, Is.EqualTo(1));
Assert.That(count2, Is.EqualTo(1));
}
/// <summary>
/// 测试带泛型参数的事件注册功能是否正确添加处理器
/// </summary>
[Test]
public void EventT_Register_Should_Add_Handler()
{
var receivedValue = 0;
_eventInt.Register(value => { receivedValue = value; });
_eventInt.Trigger(42);
Assert.That(receivedValue, Is.EqualTo(42));
}
/// <summary>
/// 测试带泛型参数的事件取消注册功能是否正确移除处理器
/// </summary>
[Test]
public void EventT_UnRegister_Should_Remove_Handler()
{
var count = 0;
Action<int> handler = value => { count++; };
_eventInt.Register(handler);
_eventInt.Trigger(1);
Assert.That(count, Is.EqualTo(1));
_eventInt.UnRegister(handler);
_eventInt.Trigger(2);
Assert.That(count, Is.EqualTo(1));
}
/// <summary>
/// 测试带泛型参数的事件多个处理器是否都能被调用
/// </summary>
[Test]
public void EventT_Multiple_Handlers_Should_All_Be_Called()
{
var values = new List<int>();
_eventInt.Register(value => { values.Add(value); });
_eventInt.Register(value => { values.Add(value * 2); });
_eventInt.Trigger(5);
Assert.That(values.Count, Is.EqualTo(2));
Assert.That(values, Does.Contain(5));
Assert.That(values, Does.Contain(10));
}
/// <summary>
/// 测试带两个泛型参数的事件注册功能是否正确添加处理器
/// </summary>
[Test]
public void EventTTK_Register_Should_Add_Handler()
{
var receivedInt = 0;
var receivedString = string.Empty;
_eventIntString.Register((i, s) =>
{
receivedInt = i;
receivedString = s;
});
_eventIntString.Trigger(42, "test");
Assert.That(receivedInt, Is.EqualTo(42));
Assert.That(receivedString, Is.EqualTo("test"));
}
/// <summary>
/// 测试带两个泛型参数的事件取消注册功能是否正确移除处理器
/// </summary>
[Test]
public void EventTTK_UnRegister_Should_Remove_Handler()
{
var count = 0;
Action<int, string> handler = (i, s) => { count++; };
_eventIntString.Register(handler);
_eventIntString.Trigger(1, "a");
Assert.That(count, Is.EqualTo(1));
_eventIntString.UnRegister(handler);
_eventIntString.Trigger(2, "b");
Assert.That(count, Is.EqualTo(1));
}
}