mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 移除多余using语句和空行,统一代码缩进格式 - 优化注释文档中的缩进和对齐格式 - 简化条件语句和方法实现,提升代码可读性 - 重构协程系统相关类的字段和方法定义格式 - 更新架构服务中容器访问方式的实现 - 调整异步操作类的属性和方法组织结构 - [skip ci]
114 lines
3.4 KiB
C#
114 lines
3.4 KiB
C#
using GFramework.Core.events;
|
||
using GFramework.Core.property;
|
||
using NUnit.Framework;
|
||
|
||
namespace GFramework.Core.Tests.events;
|
||
|
||
/// <summary>
|
||
/// 注销功能测试类,用于测试不同类型的注销行为
|
||
/// </summary>
|
||
[TestFixture]
|
||
public class UnRegisterTests
|
||
{
|
||
/// <summary>
|
||
/// 测试DefaultUnRegister在调用注销时是否正确触发回调函数
|
||
/// </summary>
|
||
[Test]
|
||
public void DefaultUnRegister_Should_InvokeCallback_When_UnRegisterCalled()
|
||
{
|
||
var invoked = false;
|
||
var unRegister = new DefaultUnRegister(() => { invoked = true; });
|
||
|
||
unRegister.UnRegister();
|
||
|
||
Assert.That(invoked, Is.True);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试DefaultUnRegister在注销后是否清除回调函数,防止重复执行
|
||
/// </summary>
|
||
[Test]
|
||
public void DefaultUnRegister_Should_ClearCallback_After_UnRegister()
|
||
{
|
||
var callCount = 0;
|
||
var unRegister = new DefaultUnRegister(() => { callCount++; });
|
||
|
||
unRegister.UnRegister();
|
||
unRegister.UnRegister();
|
||
|
||
Assert.That(callCount, Is.EqualTo(1));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试DefaultUnRegister在传入空回调函数时不会抛出异常
|
||
/// </summary>
|
||
[Test]
|
||
public void DefaultUnRegister_WithNullCallback_Should_NotThrow()
|
||
{
|
||
var unRegister = new DefaultUnRegister(null!);
|
||
|
||
Assert.DoesNotThrow(() => unRegister.UnRegister());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试BindablePropertyUnRegister是否能正确从属性中注销事件处理器
|
||
/// </summary>
|
||
[Test]
|
||
public void BindablePropertyUnRegister_Should_UnRegister_From_Property()
|
||
{
|
||
var property = new BindableProperty<int>();
|
||
var callCount = 0;
|
||
|
||
Action<int> handler = _ => { callCount++; };
|
||
property.Register(handler);
|
||
|
||
var unRegister = new BindablePropertyUnRegister<int>(property, handler);
|
||
unRegister.UnRegister();
|
||
|
||
property.Value = 42;
|
||
|
||
Assert.That(callCount, Is.EqualTo(0));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试BindablePropertyUnRegister在注销后是否清除内部引用
|
||
/// </summary>
|
||
[Test]
|
||
public void BindablePropertyUnRegister_Should_Clear_References()
|
||
{
|
||
var property = new BindableProperty<int>();
|
||
|
||
Action<int> handler = _ => { };
|
||
var unRegister = new BindablePropertyUnRegister<int>(property, handler);
|
||
|
||
unRegister.UnRegister();
|
||
|
||
// 验证注销后引用被清除
|
||
Assert.That(unRegister.BindableProperty, Is.Null);
|
||
Assert.That(unRegister.OnValueChanged, Is.Null);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试BindablePropertyUnRegister在传入空属性时不会抛出异常
|
||
/// </summary>
|
||
[Test]
|
||
public void BindablePropertyUnRegister_WithNull_Property_Should_NotThrow()
|
||
{
|
||
Action<int> handler = _ => { };
|
||
var unRegister = new BindablePropertyUnRegister<int>(null!, handler);
|
||
|
||
Assert.DoesNotThrow(() => unRegister.UnRegister());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试BindablePropertyUnRegister在传入空处理器时不会抛出异常
|
||
/// </summary>
|
||
[Test]
|
||
public void BindablePropertyUnRegister_WithNull_Handler_Should_NotThrow()
|
||
{
|
||
var property = new BindableProperty<int>();
|
||
var unRegister = new BindablePropertyUnRegister<int>(property, null!);
|
||
|
||
Assert.DoesNotThrow(() => unRegister.UnRegister());
|
||
}
|
||
} |