|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace Stateless.Tests |
| 6 | +{ |
| 7 | + public class DynamicAsyncTriggerBehaviourAsyncFixture |
| 8 | + { |
| 9 | + [Fact] |
| 10 | + public async Task PermitDynamic_Selects_Expected_State_Async() |
| 11 | + { |
| 12 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 13 | + sm.Configure(State.A) |
| 14 | + .PermitDynamicAsync(Trigger.X, async () => { await Task.Delay(100); return State.B; }); |
| 15 | + |
| 16 | + await sm.FireAsync(Trigger.X); |
| 17 | + |
| 18 | + Assert.Equal(State.B, sm.State); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public async Task PermitDynamic_With_TriggerParameter_Selects_Expected_State_Async() |
| 23 | + { |
| 24 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 25 | + var trigger = sm.SetTriggerParameters<int>(Trigger.X); |
| 26 | + sm.Configure(State.A) |
| 27 | + .PermitDynamicAsync(trigger, async (i) => { await Task.Delay(100); return i == 1 ? State.B : State.C; }); |
| 28 | + |
| 29 | + await sm.FireAsync(trigger, 1); |
| 30 | + |
| 31 | + Assert.Equal(State.B, sm.State); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public async Task PermitDynamic_Permits_Reentry_Async() |
| 36 | + { |
| 37 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 38 | + var onExitInvoked = false; |
| 39 | + var onEntryInvoked = false; |
| 40 | + var onEntryFromInvoked = false; |
| 41 | + sm.Configure(State.A) |
| 42 | + .PermitDynamicAsync(Trigger.X, async () => { await Task.Delay(100); return State.A; }) |
| 43 | + .OnEntry(() => onEntryInvoked = true) |
| 44 | + .OnEntryFrom(Trigger.X, () => onEntryFromInvoked = true) |
| 45 | + .OnExit(() => onExitInvoked = true); |
| 46 | + |
| 47 | + await sm.FireAsync(Trigger.X); |
| 48 | + |
| 49 | + Assert.True(onExitInvoked, "Expected OnExit to be invoked"); |
| 50 | + Assert.True(onEntryInvoked, "Expected OnEntry to be invoked"); |
| 51 | + Assert.True(onEntryFromInvoked, "Expected OnEntryFrom to be invoked"); |
| 52 | + Assert.Equal(State.A, sm.State); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task PermitDynamic_Selects_Expected_State_Based_On_DestinationStateSelector_Function_Async() |
| 57 | + { |
| 58 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 59 | + var value = 'C'; |
| 60 | + sm.Configure(State.A) |
| 61 | + .PermitDynamicAsync(Trigger.X, async () => { await Task.Delay(100); return value == 'B' ? State.B : State.C; }); |
| 62 | + |
| 63 | + await sm.FireAsync(Trigger.X); |
| 64 | + |
| 65 | + Assert.Equal(State.C, sm.State); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public async Task PermitDynamicIf_With_TriggerParameter_Permits_Transition_When_GuardCondition_Met_Async() |
| 70 | + { |
| 71 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 72 | + var trigger = sm.SetTriggerParameters<int>(Trigger.X); |
| 73 | + sm.Configure(State.A) |
| 74 | + .PermitDynamicIfAsync(trigger, async (i) =>{ await Task.Delay(100); return i == 1 ? State.C : State.B; }, (i) => i == 1); |
| 75 | + |
| 76 | + await sm.FireAsync(trigger, 1); |
| 77 | + |
| 78 | + Assert.Equal(State.C, sm.State); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public async Task PermitDynamicIf_With_2_TriggerParameters_Permits_Transition_When_GuardCondition_Met_Async() |
| 83 | + { |
| 84 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 85 | + var trigger = sm.SetTriggerParameters<int, int>(Trigger.X); |
| 86 | + sm.Configure(State.A).PermitDynamicIfAsync( |
| 87 | + trigger, |
| 88 | + async (i, j) => { await Task.Yield(); return i == 1 && j == 2 ? State.C : State.B; }, |
| 89 | + (i, j) => i == 1 && j == 2); |
| 90 | + |
| 91 | + await sm.FireAsync(trigger, 1, 2); |
| 92 | + |
| 93 | + Assert.Equal(State.C, sm.State); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public async Task PermitDynamicIf_With_3_TriggerParameters_Permits_Transition_When_GuardCondition_Met_Async() |
| 98 | + { |
| 99 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 100 | + var trigger = sm.SetTriggerParameters<int, int, int>(Trigger.X); |
| 101 | + sm.Configure(State.A).PermitDynamicIfAsync( |
| 102 | + trigger, |
| 103 | + async (i, j, k) => { await Task.Delay(100); return i == 1 && j == 2 && k == 3 ? State.C : State.B; }, |
| 104 | + (i, j, k) => i == 1 && j == 2 && k == 3); |
| 105 | + |
| 106 | + await sm.FireAsync(trigger, 1, 2, 3); |
| 107 | + |
| 108 | + Assert.Equal(State.C, sm.State); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public async Task PermitDynamicIf_With_TriggerParameter_Throws_When_GuardCondition_Not_Met_Async() |
| 113 | + { |
| 114 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 115 | + var trigger = sm.SetTriggerParameters<int>(Trigger.X); |
| 116 | + sm.Configure(State.A) |
| 117 | + .PermitDynamicIfAsync(trigger, async (i) => { await Task.Delay(100); return i > 0 ? State.C : State.B; }, guard: (i) => i == 2); |
| 118 | + |
| 119 | + await Assert.ThrowsAsync<InvalidOperationException>(async () => await sm.FireAsync(trigger, 1)); |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public async Task PermitDynamicIf_With_2_TriggerParameters_Throws_When_GuardCondition_Not_Met_Async() |
| 124 | + { |
| 125 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 126 | + var trigger = sm.SetTriggerParameters<int, int>(Trigger.X); |
| 127 | + sm.Configure(State.A).PermitDynamicIfAsync( |
| 128 | + trigger, |
| 129 | + async (i, j) => { await Task.Delay(100); return i > 0 ? State.C : State.B; }, |
| 130 | + (i, j) => i == 2 && j == 3); |
| 131 | + |
| 132 | + await Assert.ThrowsAsync<InvalidOperationException>(async () => await sm.FireAsync(trigger, 1, 2)); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public async Task PermitDynamicIf_With_3_TriggerParameters_Throws_When_GuardCondition_Not_Met_Async() |
| 137 | + { |
| 138 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 139 | + var trigger = sm.SetTriggerParameters<int, int, int>(Trigger.X); |
| 140 | + sm.Configure(State.A).PermitDynamicIfAsync(trigger, |
| 141 | + async (i, j, k) => { await Task.Delay(100); return i > 0 ? State.C : State.B; }, |
| 142 | + (i, j, k) => i == 2 && j == 3 && k == 4); |
| 143 | + |
| 144 | + await Assert.ThrowsAsync<InvalidOperationException>(async () => await sm.FireAsync(trigger, 1, 2, 3)); |
| 145 | + } |
| 146 | + |
| 147 | + [Fact] |
| 148 | + public async Task PermitDynamicIf_Permits_Reentry_When_GuardCondition_Met_Async() |
| 149 | + { |
| 150 | + var sm = new StateMachine<State, Trigger>(State.A); |
| 151 | + var onExitInvoked = false; |
| 152 | + var onEntryInvoked = false; |
| 153 | + var onEntryFromInvoked = false; |
| 154 | + sm.Configure(State.A) |
| 155 | + .PermitDynamicIfAsync(Trigger.X, async () =>{ await Task.Delay(100); return State.A; }, () => true) |
| 156 | + .OnEntry(() => onEntryInvoked = true) |
| 157 | + .OnEntryFrom(Trigger.X, () => onEntryFromInvoked = true) |
| 158 | + .OnExit(() => onExitInvoked = true); |
| 159 | + |
| 160 | + await sm.FireAsync(Trigger.X); |
| 161 | + |
| 162 | + Assert.True(onExitInvoked, "Expected OnExit to be invoked"); |
| 163 | + Assert.True(onEntryInvoked, "Expected OnEntry to be invoked"); |
| 164 | + Assert.True(onEntryFromInvoked, "Expected OnEntryFrom to be invoked"); |
| 165 | + Assert.Equal(State.A, sm.State); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments