mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-13 06:04:30 +08:00
test(state): 拆分 StateMachineTests 状态测试辅助类型
- 拆分 StateMachineTests 中的状态测试辅助类型到独立文件 - 补充提取后类型与扩展方法的 XML 文档说明 - 保持状态机测试行为不变并完成定向项目构建验证
This commit is contained in:
parent
7cfdd2cf21
commit
ada0d3acbf
24
GFramework.Core.Tests/State/StateMachineExtensions.cs
Normal file
24
GFramework.Core.Tests/State/StateMachineExtensions.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using GFramework.Core.Abstractions.State;
|
||||||
|
using GFramework.Core.State;
|
||||||
|
|
||||||
|
namespace GFramework.Core.Tests.State;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 为状态机测试提供仅限断言场景使用的反射辅助方法。
|
||||||
|
/// </summary>
|
||||||
|
public static class StateMachineExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 检查状态机内部缓存中是否已注册指定类型的状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">要检查的状态类型。</typeparam>
|
||||||
|
/// <param name="stateMachine">待检查的状态机实例。</param>
|
||||||
|
/// <returns>找到对应状态类型时返回 <see langword="true" />,否则返回 <see langword="false" />。</returns>
|
||||||
|
public static bool ContainsState<T>(this StateMachine stateMachine) where T : IState
|
||||||
|
{
|
||||||
|
return stateMachine.GetType().GetField("States", BindingFlags.NonPublic | BindingFlags.Instance)?
|
||||||
|
.GetValue(stateMachine) is Dictionary<Type, IState> states &&
|
||||||
|
states.ContainsKey(typeof(T));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,3 @@
|
|||||||
using System.Reflection;
|
|
||||||
using GFramework.Core.Abstractions.State;
|
using GFramework.Core.Abstractions.State;
|
||||||
using GFramework.Core.State;
|
using GFramework.Core.State;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
@ -368,237 +367,3 @@ public class StateMachineTests
|
|||||||
Assert.That(state3.ExitCalled, Is.False);
|
Assert.That(state3.ExitCalled, Is.False);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 测试状态类V2版本,实现IState接口用于测试
|
|
||||||
/// </summary>
|
|
||||||
public sealed class TestStateV2 : IState
|
|
||||||
{
|
|
||||||
public bool AllowTransition { get; set; } = true;
|
|
||||||
public bool EnterCalled { get; private set; }
|
|
||||||
public bool ExitCalled { get; private set; }
|
|
||||||
public int EnterCallCount { get; private set; }
|
|
||||||
public int ExitCallCount { get; private set; }
|
|
||||||
public IState? EnterFrom { get; private set; }
|
|
||||||
public IState? ExitTo { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 进入状态时的回调方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="from">从哪个状态进入</param>
|
|
||||||
public void OnEnter(IState? from)
|
|
||||||
{
|
|
||||||
EnterCalled = true;
|
|
||||||
EnterCallCount++;
|
|
||||||
EnterFrom = from;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 离开状态时的回调方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="to">离开到哪个状态</param>
|
|
||||||
public void OnExit(IState? to)
|
|
||||||
{
|
|
||||||
ExitCalled = true;
|
|
||||||
ExitCallCount++;
|
|
||||||
ExitTo = to;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 判断是否可以转换到目标状态
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="target">目标状态</param>
|
|
||||||
/// <returns>是否允许转换</returns>
|
|
||||||
public bool CanTransitionTo(IState target)
|
|
||||||
{
|
|
||||||
return AllowTransition;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 测试状态类V3版本,实现IState接口用于测试
|
|
||||||
/// </summary>
|
|
||||||
public sealed class TestStateV3 : IState
|
|
||||||
{
|
|
||||||
public bool EnterCalled { get; private set; }
|
|
||||||
public bool ExitCalled { get; private set; }
|
|
||||||
public int EnterCallCount { get; private set; }
|
|
||||||
public int ExitCallCount { get; private set; }
|
|
||||||
public IState? EnterFrom { get; private set; }
|
|
||||||
public IState? ExitTo { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 进入状态时的回调方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="from">从哪个状态进入</param>
|
|
||||||
public void OnEnter(IState? from)
|
|
||||||
{
|
|
||||||
EnterCalled = true;
|
|
||||||
EnterCallCount++;
|
|
||||||
EnterFrom = from;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 离开状态时的回调方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="to">离开到哪个状态</param>
|
|
||||||
public void OnExit(IState? to)
|
|
||||||
{
|
|
||||||
ExitCalled = true;
|
|
||||||
ExitCallCount++;
|
|
||||||
ExitTo = to;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 判断是否可以转换到目标状态
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="target">目标状态</param>
|
|
||||||
/// <returns>是否允许转换</returns>
|
|
||||||
public bool CanTransitionTo(IState target)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 测试状态类V4版本,实现IState接口用于测试
|
|
||||||
/// </summary>
|
|
||||||
public sealed class TestStateV4 : IState
|
|
||||||
{
|
|
||||||
public bool EnterCalled { get; private set; }
|
|
||||||
public bool ExitCalled { get; private set; }
|
|
||||||
public int EnterCallCount { get; private set; }
|
|
||||||
public int ExitCallCount { get; private set; }
|
|
||||||
public IState? EnterFrom { get; private set; }
|
|
||||||
public IState? ExitTo { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 进入状态时的回调方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="from">从哪个状态进入</param>
|
|
||||||
public void OnEnter(IState? from)
|
|
||||||
{
|
|
||||||
EnterCalled = true;
|
|
||||||
EnterCallCount++;
|
|
||||||
EnterFrom = from;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 离开状态时的回调方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="to">离开到哪个状态</param>
|
|
||||||
public void OnExit(IState? to)
|
|
||||||
{
|
|
||||||
ExitCalled = true;
|
|
||||||
ExitCallCount++;
|
|
||||||
ExitTo = to;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 判断是否可以转换到目标状态
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="target">目标状态</param>
|
|
||||||
/// <returns>是否允许转换</returns>
|
|
||||||
public bool CanTransitionTo(IState target)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 异步测试状态类,同时实现IState和IAsyncState接口用于测试异步状态功能
|
|
||||||
/// </summary>
|
|
||||||
public sealed class TestAsyncState : IState, IAsyncState
|
|
||||||
{
|
|
||||||
public bool AllowTransition { get; set; } = true;
|
|
||||||
public bool EnterCalled { get; private set; }
|
|
||||||
public bool ExitCalled { get; private set; }
|
|
||||||
public int EnterCallCount { get; private set; }
|
|
||||||
public int ExitCallCount { get; private set; }
|
|
||||||
public IState? EnterFrom { get; private set; }
|
|
||||||
public IState? ExitTo { get; private set; }
|
|
||||||
public int CanTransitionToCallCount { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 异步进入状态时的回调方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="from">从哪个状态进入</param>
|
|
||||||
public async Task OnEnterAsync(IState? from)
|
|
||||||
{
|
|
||||||
await Task.Delay(1).ConfigureAwait(false);
|
|
||||||
EnterCalled = true;
|
|
||||||
EnterCallCount++;
|
|
||||||
EnterFrom = from;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 异步离开状态时的回调方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="to">离开到哪个状态</param>
|
|
||||||
public async Task OnExitAsync(IState? to)
|
|
||||||
{
|
|
||||||
await Task.Delay(1).ConfigureAwait(false);
|
|
||||||
ExitCalled = true;
|
|
||||||
ExitCallCount++;
|
|
||||||
ExitTo = to;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 异步判断是否可以转换到目标状态
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="target">目标状态</param>
|
|
||||||
/// <returns>是否允许转换</returns>
|
|
||||||
public async Task<bool> CanTransitionToAsync(IState target)
|
|
||||||
{
|
|
||||||
await Task.Delay(1).ConfigureAwait(false);
|
|
||||||
CanTransitionToCallCount++;
|
|
||||||
return AllowTransition;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 进入状态时的回调方法(同步版本,抛出异常表示不应被调用)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="from">从哪个状态进入</param>
|
|
||||||
public void OnEnter(IState? from)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Sync OnEnter should not be called for async state");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 离开状态时的回调方法(同步版本,抛出异常表示不应被调用)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="to">离开到哪个状态</param>
|
|
||||||
public void OnExit(IState? to)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Sync OnExit should not be called for async state");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 判断是否可以转换到目标状态(同步版本,抛出异常表示不应被调用)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="target">目标状态</param>
|
|
||||||
/// <returns>是否允许转换</returns>
|
|
||||||
public bool CanTransitionTo(IState target)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Sync CanTransitionTo should not be called for async state");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 状态机扩展方法类
|
|
||||||
/// </summary>
|
|
||||||
public static class StateMachineExtensions
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 检查状态机是否包含指定类型的状态
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">要检查的状态类型</typeparam>
|
|
||||||
/// <param name="stateMachine">状态机实例</param>
|
|
||||||
/// <returns>如果包含指定类型的状态则返回true,否则返回false</returns>
|
|
||||||
public static bool ContainsState<T>(this StateMachine stateMachine) where T : IState
|
|
||||||
{
|
|
||||||
return stateMachine.GetType().GetField("States", BindingFlags.NonPublic | BindingFlags.Instance)?
|
|
||||||
.GetValue(stateMachine) is Dictionary<Type, IState> states &&
|
|
||||||
states.ContainsKey(typeof(T));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
116
GFramework.Core.Tests/State/TestAsyncState.cs
Normal file
116
GFramework.Core.Tests/State/TestAsyncState.cs
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
using GFramework.Core.Abstractions.State;
|
||||||
|
|
||||||
|
namespace GFramework.Core.Tests.State;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 为 <see cref="StateMachineTests" /> 提供异步生命周期路径的测试状态。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class TestAsyncState : IState, IAsyncState
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置是否允许向目标状态转移。
|
||||||
|
/// </summary>
|
||||||
|
public bool AllowTransition { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取异步进入状态是否已被调用。
|
||||||
|
/// </summary>
|
||||||
|
public bool EnterCalled { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取异步离开状态是否已被调用。
|
||||||
|
/// </summary>
|
||||||
|
public bool ExitCalled { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取异步进入回调被调用的次数。
|
||||||
|
/// </summary>
|
||||||
|
public int EnterCallCount { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取异步离开回调被调用的次数。
|
||||||
|
/// </summary>
|
||||||
|
public int ExitCallCount { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取最近一次异步进入时的来源状态。
|
||||||
|
/// </summary>
|
||||||
|
public IState? EnterFrom { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取最近一次异步离开时的目标状态。
|
||||||
|
/// </summary>
|
||||||
|
public IState? ExitTo { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取异步转移检查被调用的次数。
|
||||||
|
/// </summary>
|
||||||
|
public int CanTransitionToCallCount { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步记录进入状态的来源状态与调用次数。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="from">触发进入的来源状态。</param>
|
||||||
|
public async Task OnEnterAsync(IState? from)
|
||||||
|
{
|
||||||
|
await Task.Delay(1).ConfigureAwait(false);
|
||||||
|
EnterCalled = true;
|
||||||
|
EnterCallCount++;
|
||||||
|
EnterFrom = from;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步记录离开状态的目标状态与调用次数。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="to">即将切换到的目标状态。</param>
|
||||||
|
public async Task OnExitAsync(IState? to)
|
||||||
|
{
|
||||||
|
await Task.Delay(1).ConfigureAwait(false);
|
||||||
|
ExitCalled = true;
|
||||||
|
ExitCallCount++;
|
||||||
|
ExitTo = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步记录转移检查并返回当前是否允许切换。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target">目标状态。</param>
|
||||||
|
/// <returns>允许切换时返回 <see langword="true" />,否则返回 <see langword="false" />。</returns>
|
||||||
|
public async Task<bool> CanTransitionToAsync(IState target)
|
||||||
|
{
|
||||||
|
await Task.Delay(1).ConfigureAwait(false);
|
||||||
|
CanTransitionToCallCount++;
|
||||||
|
return AllowTransition;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 同步进入入口不应被异步状态机路径调用。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="from">触发进入的来源状态。</param>
|
||||||
|
/// <exception cref="InvalidOperationException">总是抛出,表示当前测试状态只允许异步入口。</exception>
|
||||||
|
public void OnEnter(IState? from)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Sync OnEnter should not be called for async state");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 同步离开入口不应被异步状态机路径调用。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="to">即将切换到的目标状态。</param>
|
||||||
|
/// <exception cref="InvalidOperationException">总是抛出,表示当前测试状态只允许异步入口。</exception>
|
||||||
|
public void OnExit(IState? to)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Sync OnExit should not be called for async state");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 同步转移检查入口不应被异步状态机路径调用。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target">目标状态。</param>
|
||||||
|
/// <returns>此方法不会正常返回。</returns>
|
||||||
|
/// <exception cref="InvalidOperationException">总是抛出,表示当前测试状态只允许异步入口。</exception>
|
||||||
|
public bool CanTransitionTo(IState target)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Sync CanTransitionTo should not be called for async state");
|
||||||
|
}
|
||||||
|
}
|
||||||
76
GFramework.Core.Tests/State/TestStateV2.cs
Normal file
76
GFramework.Core.Tests/State/TestStateV2.cs
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
using GFramework.Core.Abstractions.State;
|
||||||
|
|
||||||
|
namespace GFramework.Core.Tests.State;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 为 <see cref="StateMachineTests" /> 提供可配置转移结果的同步测试状态。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class TestStateV2 : IState
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置是否允许向目标状态转移。
|
||||||
|
/// </summary>
|
||||||
|
public bool AllowTransition { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取进入状态是否已被调用。
|
||||||
|
/// </summary>
|
||||||
|
public bool EnterCalled { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取离开状态是否已被调用。
|
||||||
|
/// </summary>
|
||||||
|
public bool ExitCalled { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取进入回调被调用的次数。
|
||||||
|
/// </summary>
|
||||||
|
public int EnterCallCount { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取离开回调被调用的次数。
|
||||||
|
/// </summary>
|
||||||
|
public int ExitCallCount { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取最近一次进入时的来源状态。
|
||||||
|
/// </summary>
|
||||||
|
public IState? EnterFrom { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取最近一次离开时的目标状态。
|
||||||
|
/// </summary>
|
||||||
|
public IState? ExitTo { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录进入状态时的来源状态与调用次数。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="from">触发进入的来源状态。</param>
|
||||||
|
public void OnEnter(IState? from)
|
||||||
|
{
|
||||||
|
EnterCalled = true;
|
||||||
|
EnterCallCount++;
|
||||||
|
EnterFrom = from;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录离开状态时的目标状态与调用次数。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="to">即将切换到的目标状态。</param>
|
||||||
|
public void OnExit(IState? to)
|
||||||
|
{
|
||||||
|
ExitCalled = true;
|
||||||
|
ExitCallCount++;
|
||||||
|
ExitTo = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据 <see cref="AllowTransition" /> 返回是否允许切换。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target">目标状态。</param>
|
||||||
|
/// <returns>允许切换时返回 <see langword="true" />,否则返回 <see langword="false" />。</returns>
|
||||||
|
public bool CanTransitionTo(IState target)
|
||||||
|
{
|
||||||
|
return AllowTransition;
|
||||||
|
}
|
||||||
|
}
|
||||||
71
GFramework.Core.Tests/State/TestStateV3.cs
Normal file
71
GFramework.Core.Tests/State/TestStateV3.cs
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
using GFramework.Core.Abstractions.State;
|
||||||
|
|
||||||
|
namespace GFramework.Core.Tests.State;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 为 <see cref="StateMachineTests" /> 提供始终允许切换的同步测试状态。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class TestStateV3 : IState
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取进入状态是否已被调用。
|
||||||
|
/// </summary>
|
||||||
|
public bool EnterCalled { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取离开状态是否已被调用。
|
||||||
|
/// </summary>
|
||||||
|
public bool ExitCalled { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取进入回调被调用的次数。
|
||||||
|
/// </summary>
|
||||||
|
public int EnterCallCount { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取离开回调被调用的次数。
|
||||||
|
/// </summary>
|
||||||
|
public int ExitCallCount { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取最近一次进入时的来源状态。
|
||||||
|
/// </summary>
|
||||||
|
public IState? EnterFrom { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取最近一次离开时的目标状态。
|
||||||
|
/// </summary>
|
||||||
|
public IState? ExitTo { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录进入状态时的来源状态与调用次数。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="from">触发进入的来源状态。</param>
|
||||||
|
public void OnEnter(IState? from)
|
||||||
|
{
|
||||||
|
EnterCalled = true;
|
||||||
|
EnterCallCount++;
|
||||||
|
EnterFrom = from;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录离开状态时的目标状态与调用次数。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="to">即将切换到的目标状态。</param>
|
||||||
|
public void OnExit(IState? to)
|
||||||
|
{
|
||||||
|
ExitCalled = true;
|
||||||
|
ExitCallCount++;
|
||||||
|
ExitTo = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 始终允许切换到目标状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target">目标状态。</param>
|
||||||
|
/// <returns>始终返回 <see langword="true" />。</returns>
|
||||||
|
public bool CanTransitionTo(IState target)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
71
GFramework.Core.Tests/State/TestStateV4.cs
Normal file
71
GFramework.Core.Tests/State/TestStateV4.cs
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
using GFramework.Core.Abstractions.State;
|
||||||
|
|
||||||
|
namespace GFramework.Core.Tests.State;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 为 <see cref="StateMachineTests" /> 保留的附加同步测试状态实现。
|
||||||
|
/// </summary>
|
||||||
|
public sealed class TestStateV4 : IState
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取进入状态是否已被调用。
|
||||||
|
/// </summary>
|
||||||
|
public bool EnterCalled { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取离开状态是否已被调用。
|
||||||
|
/// </summary>
|
||||||
|
public bool ExitCalled { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取进入回调被调用的次数。
|
||||||
|
/// </summary>
|
||||||
|
public int EnterCallCount { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取离开回调被调用的次数。
|
||||||
|
/// </summary>
|
||||||
|
public int ExitCallCount { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取最近一次进入时的来源状态。
|
||||||
|
/// </summary>
|
||||||
|
public IState? EnterFrom { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取最近一次离开时的目标状态。
|
||||||
|
/// </summary>
|
||||||
|
public IState? ExitTo { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录进入状态时的来源状态与调用次数。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="from">触发进入的来源状态。</param>
|
||||||
|
public void OnEnter(IState? from)
|
||||||
|
{
|
||||||
|
EnterCalled = true;
|
||||||
|
EnterCallCount++;
|
||||||
|
EnterFrom = from;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录离开状态时的目标状态与调用次数。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="to">即将切换到的目标状态。</param>
|
||||||
|
public void OnExit(IState? to)
|
||||||
|
{
|
||||||
|
ExitCalled = true;
|
||||||
|
ExitCallCount++;
|
||||||
|
ExitTo = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 始终允许切换到目标状态。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target">目标状态。</param>
|
||||||
|
/// <returns>始终返回 <see langword="true" />。</returns>
|
||||||
|
public bool CanTransitionTo(IState target)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user