From e2b91caae9b729ff7cbad6a20fd4fd946b910589 Mon Sep 17 00:00:00 2001 From: Andrey Boronnikov Date: Fri, 13 Feb 2026 12:06:57 +0300 Subject: [PATCH] Add empty-queue exception for typed state machine --- .../Exceptions/QueueStateIsEmptyException.cs | 15 ++++++++++++ .../QueueStateIsEmptyException.cs.meta | 3 +++ .../StateMachines/BaseTypedStateMachine.cs | 5 +++- .../Tests/ExceptionStateMachineTests.cs | 23 ++++++++++++++++++- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 RedCatEngineUnityProject/Packages/StateMachine/Exceptions/QueueStateIsEmptyException.cs create mode 100644 RedCatEngineUnityProject/Packages/StateMachine/Exceptions/QueueStateIsEmptyException.cs.meta diff --git a/RedCatEngineUnityProject/Packages/StateMachine/Exceptions/QueueStateIsEmptyException.cs b/RedCatEngineUnityProject/Packages/StateMachine/Exceptions/QueueStateIsEmptyException.cs new file mode 100644 index 0000000..a0928dd --- /dev/null +++ b/RedCatEngineUnityProject/Packages/StateMachine/Exceptions/QueueStateIsEmptyException.cs @@ -0,0 +1,15 @@ +using System; + +namespace RedCatEngine.StateMachine.Exceptions +{ + public class QueueStateIsEmptyException : Exception + { + public string StateMachineName; + + public QueueStateIsEmptyException(string stateMachineName) + : base(string.Format("Queue is empty in state machine {0}", stateMachineName)) + { + StateMachineName = stateMachineName; + } + } +} diff --git a/RedCatEngineUnityProject/Packages/StateMachine/Exceptions/QueueStateIsEmptyException.cs.meta b/RedCatEngineUnityProject/Packages/StateMachine/Exceptions/QueueStateIsEmptyException.cs.meta new file mode 100644 index 0000000..682aac0 --- /dev/null +++ b/RedCatEngineUnityProject/Packages/StateMachine/Exceptions/QueueStateIsEmptyException.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9f91da2944d247dca73f4b3259e13d34 +timeCreated: 1760000000 diff --git a/RedCatEngineUnityProject/Packages/StateMachine/StateMachines/BaseTypedStateMachine.cs b/RedCatEngineUnityProject/Packages/StateMachine/StateMachines/BaseTypedStateMachine.cs index f076351..dd05b7d 100644 --- a/RedCatEngineUnityProject/Packages/StateMachine/StateMachines/BaseTypedStateMachine.cs +++ b/RedCatEngineUnityProject/Packages/StateMachine/StateMachines/BaseTypedStateMachine.cs @@ -78,6 +78,9 @@ public ITypedQueueStateMachine Enter(TPayload payload) public ITypedQueueStateMachine EnterNextFromQueue() { + if (_queue.Count == 0) + throw new QueueStateIsEmptyException(_name); + var data = _queue.Dequeue(); AddToHistory(data); var nextState = SelectStateAsActive(data.StateType); @@ -152,4 +155,4 @@ protected virtual TBaseState GetState(Type type) return targetState; } } -} \ No newline at end of file +} diff --git a/RedCatEngineUnityProject/Packages/StateMachine/Tests/ExceptionStateMachineTests.cs b/RedCatEngineUnityProject/Packages/StateMachine/Tests/ExceptionStateMachineTests.cs index 4f985d6..46ab3dd 100644 --- a/RedCatEngineUnityProject/Packages/StateMachine/Tests/ExceptionStateMachineTests.cs +++ b/RedCatEngineUnityProject/Packages/StateMachine/Tests/ExceptionStateMachineTests.cs @@ -48,5 +48,26 @@ public void GivenStateMachine_WhenEnterUnknownState_ThenCatchNotFoundStateExcept Assert.IsTrue(false, "Not catch exception"); } + + [Test] + public void GivenStateMachine_WhenEnterNextFromEmptyQueue_ThenCatchQueueStateIsEmptyException() + { + try + { + _stateMachine.EnterNextFromQueue(); + } + catch (Exception exception) + { + Assert.IsTrue( + exception is QueueStateIsEmptyException, + "Catch incorrect error"); + Assert.IsTrue( + exception.Message.Contains("TestedTypedStateMachine"), + "State machine name is not specified in exception message"); + return; + } + + Assert.IsTrue(false, "Not catch exception"); + } } -} \ No newline at end of file +}