GFramework/GFramework.Core.Tests/property/BindablePropertyTests.cs
GeWuYou dda2d8f864 test(core): 添加核心组件单元测试并优化目标框架配置
- 在AsyncTestModel和TestModel中添加override关键字修复方法重写
- 调整GFramework.Core.Tests和GFramework.SourceGenerators.Tests的目标框架顺序为net10.0;net8.0
- 优化SyncArchitectureTests中的注释格式,统一添加前导空格
- 移除TestQuery相关代码文件
- 新增BindablePropertyTests测试绑定属性功能
- 新增CommandBusTests测试命令总线功能
- 新增EasyEventsTests和EventTests测试事件系统功能
- 新增IocContainerTests测试依赖注入容器功能
- 新增ObjectExtensionsTests测试对象扩展方法功能
- 新增ObjectPoolTests测试对象池功能
- 新增OrEventTests测试或事件功能
- 新增QueryBusTests测试查询总线功能
- [skip ci]
2026-01-15 13:53:08 +08:00

133 lines
3.2 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.property;
using NUnit.Framework;
namespace GFramework.Core.Tests.property;
[TestFixture]
public class BindablePropertyTests
{
[Test]
public void Value_Get_Should_Return_Default_Value()
{
var property = new BindableProperty<int>(5);
Assert.That(property.Value, Is.EqualTo(5));
}
[Test]
public void Value_Set_Should_Trigger_Event()
{
var property = new BindableProperty<int>();
var receivedValue = 0;
property.Register(value => { receivedValue = value; });
property.Value = 42;
Assert.That(receivedValue, Is.EqualTo(42));
}
[Test]
public void Value_Set_To_Same_Value_Should_Not_Trigger_Event()
{
var property = new BindableProperty<int>(5);
var count = 0;
property.Register(_ => { count++; });
property.Value = 5;
Assert.That(count, Is.EqualTo(0));
}
[Test]
public void UnRegister_Should_Remove_Handler()
{
var property = new BindableProperty<int>();
var count = 0;
Action<int> handler = _ => { count++; };
property.Register(handler);
property.Value = 1;
Assert.That(count, Is.EqualTo(1));
property.UnRegister(handler);
property.Value = 2;
Assert.That(count, Is.EqualTo(1));
}
[Test]
public void RegisterWithInitValue_Should_Call_Handler_Immediately()
{
var property = new BindableProperty<int>(5);
var receivedValue = 0;
property.RegisterWithInitValue(value => { receivedValue = value; });
Assert.That(receivedValue, Is.EqualTo(5));
}
[Test]
public void SetValueWithoutEvent_Should_Not_Trigger_Event()
{
var property = new BindableProperty<int>();
var count = 0;
property.Register(_ => { count++; });
property.SetValueWithoutEvent(42);
Assert.That(count, Is.EqualTo(0));
Assert.That(property.Value, Is.EqualTo(42));
}
[Test]
public void WithComparer_Should_Use_Custom_Comparer()
{
var property = new BindableProperty<string>("test").WithComparer((a, b) => a.Length == b.Length);
var count = 0;
property.Register(_ => { count++; });
property.Value = "hello";
// "test" 和 "hello" 长度都是 4所以不应该触发事件
Assert.That(count, Is.EqualTo(0));
}
[Test]
public void Multiple_Handlers_Should_All_Be_Called()
{
var property = new BindableProperty<int>();
var count1 = 0;
var count2 = 0;
property.Register(_ => { count1++; });
property.Register(_ => { count2++; });
property.Value = 42;
Assert.That(count1, Is.EqualTo(1));
Assert.That(count2, Is.EqualTo(1));
}
[Test]
public void Register_Should_Return_IUnRegister()
{
var property = new BindableProperty<int>();
var unRegister = property.Register(_ => { });
Assert.That(unRegister, Is.Not.Null);
}
[Test]
public void ToString_Should_Return_Value_As_String()
{
var property = new BindableProperty<int>(42);
var result = property.ToString();
Assert.That(result, Is.EqualTo("42"));
}
}