gewuyou ff553977e3 chore(license): 补齐 Apache-2.0 文件头治理
- 新增许可证文件头检查与修复脚本

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

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

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

82 lines
2.3 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.Events;
using NUnit.Framework;
namespace GFramework.Core.Tests.Events;
/// <summary>
/// EventBus测试类用于验证事件总线的各种功能
/// </summary>
[TestFixture]
public class EventBusTests
{
/// <summary>
/// 测试设置方法在每个测试方法执行前初始化EventBus实例
/// </summary>
[SetUp]
public void SetUp()
{
_eventBus = new EventBus();
}
private EventBus _eventBus = null!;
/// <summary>
/// 测试注册事件处理器的功能
/// 验证注册的处理器能够在发送对应事件时被正确调用
/// </summary>
[Test]
public void Register_Should_Add_Handler()
{
var called = false;
_eventBus.Register<EventBusTestsEvent>(@event => { called = true; });
_eventBus.Send<EventBusTestsEvent>();
Assert.That(called, Is.True);
}
/// <summary>
/// 测试注销事件处理器的功能
/// 验证已注册的处理器在注销后不会再被调用
/// </summary>
[Test]
public void UnRegister_Should_Remove_Handler()
{
var count = 0;
Action<EventBusTestsEvent> handler = @event => { count++; };
_eventBus.Register(handler);
_eventBus.Send<EventBusTestsEvent>();
// 验证处理器被调用一次
Assert.That(count, Is.EqualTo(1));
_eventBus.UnRegister(handler);
_eventBus.Send<EventBusTestsEvent>();
// 验证处理器在注销后不再被调用
Assert.That(count, Is.EqualTo(1));
}
/// <summary>
/// 测试发送事件时调用所有处理器的功能
/// 验证同一事件类型的多个处理器都能被正确调用
/// </summary>
[Test]
public void SendEvent_Should_Invoke_All_Handlers()
{
var count1 = 0;
var count2 = 0;
_eventBus.Register<EventBusTestsEvent>(@event => { count1++; });
_eventBus.Register<EventBusTestsEvent>(@event => { count2++; });
_eventBus.Send<EventBusTestsEvent>();
// 验证所有处理器都被调用一次
Assert.That(count1, Is.EqualTo(1));
Assert.That(count2, Is.EqualTo(1));
}
}