|
| 1 | +package stateless |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestStateMachine_Fire_TriggerHandledOnSuperStateAndSubState_UsesSubstateTransition(t *testing.T) { |
| 9 | + sm := NewStateMachine(stateA) |
| 10 | + sm.Configure(stateA). |
| 11 | + Permit(triggerX, stateB) |
| 12 | + |
| 13 | + sm.Configure(stateB). |
| 14 | + SubstateOf(stateA). |
| 15 | + Permit(triggerX, stateC) |
| 16 | + |
| 17 | + sm.Fire(triggerX) |
| 18 | + if got := sm.MustState(); got != stateB { |
| 19 | + t.Errorf("sm.MustState() = %v, want %v", got, stateB) |
| 20 | + } |
| 21 | + |
| 22 | + sm.Fire(triggerX) |
| 23 | + if got := sm.MustState(); got != stateC { |
| 24 | + t.Errorf("sm.MustState() = %v, want %v", got, stateC) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestStateMachine_Fire_TriggerHandledOnSuperStateAndSubState_SubstateGuardBlocked_UsesSuperstateTransition(t *testing.T) { |
| 29 | + guardConditionValue := false |
| 30 | + sm := NewStateMachine(stateB) |
| 31 | + |
| 32 | + sm.Configure(stateA). |
| 33 | + Permit(triggerX, stateD) |
| 34 | + |
| 35 | + sm.Configure(stateB). |
| 36 | + SubstateOf(stateA). |
| 37 | + Permit(triggerX, stateC, func(_ context.Context, _ ...any) bool { |
| 38 | + return guardConditionValue |
| 39 | + }) |
| 40 | + |
| 41 | + sm.Fire(triggerX) |
| 42 | + if got := sm.MustState(); got != stateD { |
| 43 | + t.Errorf("sm.MustState() = %v, want %v", got, stateD) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestStateMachine_Fire_TriggerHandledOnSuperStateAndSubState_SubstateGuardOpen_UsesSubstateTransition(t *testing.T) { |
| 48 | + guardConditionValue := true |
| 49 | + sm := NewStateMachine(stateB) |
| 50 | + |
| 51 | + sm.Configure(stateA). |
| 52 | + Permit(triggerX, stateD) |
| 53 | + |
| 54 | + sm.Configure(stateB). |
| 55 | + SubstateOf(stateA). |
| 56 | + Permit(triggerX, stateC, func(_ context.Context, _ ...any) bool { |
| 57 | + return guardConditionValue |
| 58 | + }) |
| 59 | + |
| 60 | + sm.Fire(triggerX) |
| 61 | + if got := sm.MustState(); got != stateC { |
| 62 | + t.Errorf("sm.MustState() = %v, want %v", got, stateC) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestStateMachine_InternalTransitionIf_ExecutesOnlyFirstMatchingAction(t *testing.T) { |
| 67 | + sm := NewStateMachine(1) |
| 68 | + executed := []int{} |
| 69 | + |
| 70 | + sm.Configure(1). |
| 71 | + InternalTransition(1, func(_ context.Context, _ ...any) error { |
| 72 | + executed = append(executed, 1) |
| 73 | + return nil |
| 74 | + }, func(_ context.Context, _ ...any) bool { |
| 75 | + return true |
| 76 | + }). |
| 77 | + InternalTransition(1, func(_ context.Context, _ ...any) error { |
| 78 | + executed = append(executed, 2) |
| 79 | + return nil |
| 80 | + }, func(_ context.Context, _ ...any) bool { |
| 81 | + return false |
| 82 | + }) |
| 83 | + |
| 84 | + sm.Fire(1) |
| 85 | + |
| 86 | + if len(executed) != 1 || executed[0] != 1 { |
| 87 | + t.Errorf("expected only first action to execute, got executions: %v", executed) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestStateMachine_Fire_MultiLayerSubstates_ClosestAncestorTransitionUsed(t *testing.T) { |
| 92 | + tests := []struct { |
| 93 | + name string |
| 94 | + parentGuardConditionValue bool |
| 95 | + childGuardConditionValue bool |
| 96 | + grandchildGuardConditionValue bool |
| 97 | + expectedState string |
| 98 | + }{ |
| 99 | + {"GrandchildOpen", false, false, true, "GrandchildStateTarget"}, |
| 100 | + {"ChildOpen_GrandchildClosed", false, true, false, "ChildStateTarget"}, |
| 101 | + {"ChildOpen_GrandchildOpen", false, true, true, "GrandchildStateTarget"}, |
| 102 | + {"ParentOpen_ChildClosed_GrandchildClosed", true, false, false, "ParentStateTarget"}, |
| 103 | + {"ParentOpen_ChildClosed_GrandchildOpen", true, false, true, "GrandchildStateTarget"}, |
| 104 | + {"ParentOpen_ChildOpen_GrandchildClosed", true, true, false, "ChildStateTarget"}, |
| 105 | + {"AllOpen", true, true, true, "GrandchildStateTarget"}, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tt := range tests { |
| 109 | + t.Run(tt.name, func(t *testing.T) { |
| 110 | + sm := NewStateMachine("GrandchildState") |
| 111 | + |
| 112 | + sm.Configure("ParentState"). |
| 113 | + Permit(triggerX, "ParentStateTarget", func(_ context.Context, _ ...any) bool { |
| 114 | + return tt.parentGuardConditionValue |
| 115 | + }) |
| 116 | + |
| 117 | + sm.Configure("ChildState"). |
| 118 | + SubstateOf("ParentState"). |
| 119 | + Permit(triggerX, "ChildStateTarget", func(_ context.Context, _ ...any) bool { |
| 120 | + return tt.childGuardConditionValue |
| 121 | + }) |
| 122 | + |
| 123 | + sm.Configure("GrandchildState"). |
| 124 | + SubstateOf("ChildState"). |
| 125 | + Permit(triggerX, "GrandchildStateTarget", func(_ context.Context, _ ...any) bool { |
| 126 | + return tt.grandchildGuardConditionValue |
| 127 | + }) |
| 128 | + |
| 129 | + sm.Fire(triggerX) |
| 130 | + if got := sm.MustState(); got != tt.expectedState { |
| 131 | + t.Errorf("sm.MustState() = %v, want %v", got, tt.expectedState) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments