public async Task InterruptState() { int i1Called = 0; int i2Called = 0; var sm = new StateMachineBuilder <TrigType, States>() .CreateTestState(States.S1) .End() .AsyncInterruptState(TrigType.T2, _ => { i2Called++; return(Task.CompletedTask); }, States.INTER2) .InterruptState(TrigType.T1, _ => i1Called++, States.INTER1) .Build(States.S1); sm.Next(TrigType.T1); sm.Next(TrigType.T2); await sm.NextAsync(TrigType.T1); await sm.NextAsync(TrigType.T2); i1Called.ShouldBe(2); i2Called.ShouldBe(2); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.S1).ShouldBe(0); }
public async Task TransitionPrecedenceTest() { var sm = new StateMachineBuilder <TrigType, States>() .CreateTestState(States.S1) .Transition(TrigType.T1, States.S2) .End() .InterruptTestState(TrigType.T1, States.INTER1) .CreateTestState(States.S2) .Loop(TrigType.T2) .Transition(TrigType.T3, States.S1) .End() .Build(States.S1); sm.Next(TrigType.T1); sm.Next(TrigType.T1); sm.Next(TrigType.T3); await sm.NextAsync(TrigType.T1); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.INTER1).ShouldBe(1); TestStateMachineContext <TrigType, States> .Result.ShouldBe("_T1-S2_T1-INTER1_T3-S1_T1-S2"); }
public async Task ResetInterruptState() { var sm = new StateMachineBuilder <TrigType, States>() .CreateTestState(States.S1) .Transition(TrigType.T2, States.S2) .End() .CreateTestState(States.S2) .Loop(TrigType.T2) .End() .ResetInterruptState(TrigType.T1, t => {}, States.RESETINTER1, States.S1) .AsyncResetInterruptState(TrigType.T3, _ => Task.CompletedTask, States.RESETINTER2, States.S2) .Build(States.S1); sm.Next(TrigType.T2); sm.Next(TrigType.T1); sm.CurrentState.Name.ShouldBe(States.S1); await sm.NextAsync(TrigType.T3); sm.CurrentState.Name.ShouldBe(States.S2); sm.Next(TrigType.T2); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.S1).ShouldBe(0); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.S2).ShouldBe(2); TestStateMachineContext <TrigType, States> .Result.ShouldBe("_T2-S2_T2-S2"); sm.CurrentState.Name.ShouldBe(States.S2); }
public void Transitions_test() { var sm = new StateMachineBuilder <TrigType, States>() .CreateTestState(States.S1, true) .AllTransition(States.S2) .End() .CreateTestState(States.S2) .Loop(TrigType.T2) .Transition(TrigType.T1, States.S1) .End() .Build(States.S1); sm.Next(TrigType.T1); sm.Next(TrigType.T1); sm.Next(TrigType.T1); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.S1).ShouldBe(1); TestStateMachineContext <TrigType, States> .GetStateExitTimesCalled(States.S1).ShouldBe(2); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.S2).ShouldBe(2); TestStateMachineContext <TrigType, States> .Result.ShouldBe("_ET1-ES1_T1-S2_T1-S1_ET1-ES1_T1-S2"); sm.CurrentState.Name.ShouldBe(States.S2); }
public async Task HoldingGlobState() { int i1Called = 0; int i2Called = 0; var sm = new StateMachineBuilder <TrigType, States>() .CreateTestState(States.S1) .End() .AsyncHoldingGlobState(TrigType.T2, _ => { i2Called++; return(Task.CompletedTask); }, States.INTER2, TrigType.RET) .HoldingGlobState(TrigType.T1, _ => i1Called++, States.INTER1, TrigType.RET) .Build(States.S1); sm.Next(TrigType.T1); sm.CurrentState.Name.ShouldBe(States.INTER1); await sm.NextAsync(TrigType.T1); sm.CurrentState.Name.ShouldBe(States.INTER1); sm.Next(TrigType.T2); sm.CurrentState.Name.ShouldBe(States.INTER1); sm.Next(TrigType.RET); sm.CurrentState.Name.ShouldBe(States.S1); await sm.NextAsync(TrigType.T2); sm.CurrentState.Name.ShouldBe(States.INTER2); await sm.NextAsync(TrigType.T1); sm.CurrentState.Name.ShouldBe(States.INTER2); sm.Next(TrigType.T2); sm.CurrentState.Name.ShouldBe(States.INTER2); await sm.NextAsync(TrigType.RET); sm.CurrentState.Name.ShouldBe(States.S1); i1Called.ShouldBe(1); i2Called.ShouldBe(1); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.S1).ShouldBe(2); }
public void StartState() { var sm = new StateMachineBuilder <TrigType, States>() .CreateTestState(States.S1) .Transition(TrigType.T1, States.S2) .End() .CreateTestState(States.S2) .Loop(TrigType.T2) .Transition(TrigType.T1, States.S1) .End() .Build(States.S1); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.S1).ShouldBe(0); TestStateMachineContext <TrigType, States> .Result.ShouldBe(""); }
public async Task AsynState() { var sm = new StateMachineBuilder <TrigType, States>() .CreateTestAsyncState(States.S1, true) .Transition(TrigType.T2, States.S2) .End() .CreateTestAsyncState(States.S2) .Transition(TrigType.T1, States.S1) .Transition(TrigType.T3, States.S3) .End() .CreateTestState(States.S3) .Transition(TrigType.T3, States.S1) .Loop(TrigType.T1) .End() .Build(States.S1); sm.CurrentState.Name.ShouldBe(States.S1); sm.Next(TrigType.T2); await sm.NextAsync(TrigType.T1); await sm.NextAsync(TrigType.T2); sm.Next(TrigType.T3); await sm.NextAsync(TrigType.T1); await sm.NextAsync(TrigType.T3); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.S1).ShouldBe(2); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.S2).ShouldBe(2); TestStateMachineContext <TrigType, States> .GetStateTimesCalled(States.S3).ShouldBe(2); TestStateMachineContext <TrigType, States> .Result.ShouldBe("_ET2-ES1_T2-S2_T1-S1_ET2-ES1_T2-S2_T3-S3_T1-S3_T3-S1"); }